Count number of parameters from model in RStan

173 views
Skip to first unread message

Dustin Tran

unread,
Feb 28, 2016, 2:16:59 PM2/28/16
to Stan users mailing list

Say I'm using a model with both parameters and transformed parameters. How do I find the total number of parameters in each block separately in RStan? Using the model_pars or even things like the inits attribute, it couples the two sets of parameters so you can't distinguish the two.

Ben Goodrich

unread,
Feb 28, 2016, 4:47:05 PM2/28/16
to Stan users mailing list
On Sunday, February 28, 2016 at 2:16:59 PM UTC-5, Dustin Tran wrote:
Say I'm using a model with both parameters and transformed parameters. How do I find the total number of parameters in each block separately in RStan? Using the model_pars or even things like the inits attribute, it couples the two sets of parameters so you can't distinguish the two.

I don't think there is anything like that in RStan. Here is a function that returns the things in the parameters block.

get_stan_params <- function(object) {
  stopifnot
(is(object, "stanfit"))
 
params <- grep("context__.vals_r", fixed = TRUE, value = TRUE,
                 x
= strsplit(get_cppcode(get_stanmodel(object)), "\n")[[1]])
 
params <- sapply(strsplit(params, "\""), FUN = function(x) x[[2]])
 
params <- intersect(params, object@model_pars)
 
return(params)
}

Ben

Dustin Tran

unread,
Feb 28, 2016, 4:54:10 PM2/28/16
to stan-...@googlegroups.com
Cool, this is fine. So to get the number of pars, I would do something like

model <- stan(file, data = data, iter = 1, chains = 1)
pars <- get_stan_params(model)
length(unlist(attributes(model)$inits[[1]][pars]))

dustin
--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Goodrich

unread,
Feb 28, 2016, 5:05:43 PM2/28/16
to Stan users mailing list
On Sunday, February 28, 2016 at 4:54:10 PM UTC-5, Dustin Tran wrote:
Cool, this is fine. So to get the number of pars, I would do something like

model <- stan(file, data = data, iter = 1, chains = 1)
pars <- get_stan_params(model)
length(unlist(attributes(model)$inits[[1]][pars]))

I think you could substitute this for the last line

sum(sapply(model@par_dims[pars], prod))

and it would work even for a model with 0 chains.

Ben

Jonah Gabry

unread,
Feb 28, 2016, 5:07:33 PM2/28/16
to stan-...@googlegroups.com
Yeah that should work. You can also use get_inits(model) instead of going through the attributes.

Dustin Tran

unread,
Feb 28, 2016, 5:22:35 PM2/28/16
to stan-...@googlegroups.com
Awesome. In case this is useful when someone finds this thread in the future, I use the following code to count the number of local parameters—these are parameters that are not in the transformed block and are not hyperparameters given by another stan file.

get_stan_params <- function(object) {
  stopifnot(is(object, "stanfit"))
  params <- grep("context__.vals_r", fixed = TRUE, value = TRUE,
                 x = strsplit(get_cppcode(get_stanmodel(object)), "\n")[[1]])
  params <- sapply(strsplit(params, "\""), FUN = function(x) x[[2]])
  params <- intersect(params, object@model_pars)
  return(params)
}

count_params <- function(object, params) {
  stopifnot(is(object, "stanfit") || is(object, "stanmodel"))
  return(sum(sapply(object@par_dims[params], prod)))
}

full_model <- suppressMessages(stan(file, data = data, iter = 1, chains = 1))
local_model <- stan_model(local_file)
g_alpha <- vb(local_model, data=c(data,phi), iter=1)

pars_local <- get_stan_params(g_alpha)
pars_full <- get_stan_params(full_model)
pars <- sapply(pars_local, function(x) if (x %in% pars_full) return(x))
count_params(g_alpha, pars)

Dustin

Bob Carpenter

unread,
Feb 28, 2016, 7:55:57 PM2/28/16
to stan-...@googlegroups.com
These should be retrievable through the class that's
generated for the Stan program. See the constrained_param_names()
and unconstrained_param_names() methods, which have boolean flags
indicating whether or not to include the transformed parameters
and generated quantities. It's not straightforward, because to literally
count them you'd have to run it three times and count the size of output
each time.

- Bob
Reply all
Reply to author
Forward
0 new messages