stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.03k stars 264 forks source link

API for getting model parameters information #156

Open alyst opened 9 years ago

alyst commented 9 years ago

Is it possible to get the information about the stanfit object parameters in Rstan? E.g. get_pars_info(stanfit) that would return something like:

list( a = c( type = 'vector', dim = 2, lower = -1, upper = 1 ),
      b = c( type = 'matrix', dim = c( 2, 5 ), lower = 0, upper = NA ) )

One potential use-case. I've ended up using DEoptim R package to optimize the function defined as a Stan model (log_prob(stanfit, upars)). DEoptim allows specifying element-wise constraints on the parameter vector, and it appears that differential evolution in the constrained space is much more efficient than in the unconstrained one. ATM I have to "manually" take care that constraints specification for DEoptim is in sync with the Stan model, with get_pars_info() it would be possible to generate this specification automatically.

alyst commented 9 years ago

A related issue: is it possible to add include_trans and include_gq parameters to constrain_pars() to enable/disable the output of transformed parameters and generated quantities?

bob-carpenter commented 9 years ago

I'm reposting this in the hope that someone reading the RStan issue tracker will help. But this isn't the place to ask questions --- it's for bugs or feature requests. Discussions should start on the users list, so I'm cc-ing that.

You can use Stan's own L-BFGS optimizer. And we've found that in some cases it's better to not transform a constrained parameter, but just define it as:

real sigma;

rather than

real sigma;

There's no programmatic way to turn the constraints off. You can turn the Jacobian off for the adjustment, which you want to do if you're trying to fit an MLE. It's an argument in the log_prob method on a fit object.

On Apr 20, 2015, at 7:04 PM, Alexey Stukalov notifications@github.com wrote:

Is it possible to get the information about the stanfit object parameters in Rstan? E.g. get_pars_info(stanfit) that would return something like:

list( a = c( type = 'vector', dim = 2, lower = -1, upper = 1 ), b = c( type = 'matrix', dim = c( 2, 5 ), lower = 0, upper = NA ) )

One potential use-case. I've ended up using DEoptim R package to optimize the function defined as a Stan model (log_prob(stanfit, upars)). DEoptim allows specifying element-wise constraints on the parameter vector, and it appears that differential evolution in the constrained space is much more efficient than in the unconstrained one. ATM I have to "manually" take care that constraints specification for DEoptim is in sync with the Stan model, with get_pars_info() it would be possible to generate this specification automatically.

— Reply to this email directly or view it on GitHub.

bob-carpenter commented 9 years ago

On Apr 20, 2015, at 7:15 PM, Alexey Stukalov notifications@github.com wrote:

A related issue: is it possible to add include_trans and include_gq parameters to constrain_pars() to enable/disable the output of transformed parameters and generated quantities?

I don't know if you can do this from RStan.

We're reworking a lot of this behavior for Stan 3 (coming some year) to try to make everything more flexible from the outside. We hadn't really anticipated all the things people would try to do with Stan!

alyst commented 9 years ago

@bob-carpenter Thank you for the feedback! I intended this issue to be a "feature request" for retrieving parameters description using RStan + one motivating example. My initial estimation was that it should not be too hard to add it to current RStan. Actually, I can try implementing this if RStan developer would consider it worthy and give some rough idea how they would do it.

bgoodri commented 9 years ago

I guess it could be added. But rstan doesn't really know what is a transformed parameter and a generated quantity, so you would currently have to parse the C++ file to figure out which is which.

alyst commented 9 years ago

@bgoodri There's SEXP unconstrained_param_names(SEXP include_tparams, SEXP include_gqs) in include/rstan/stan_fit.hpp that is used in stanmodel-class.R. Once there's a stanfit object, one can also use it outside of Rstan (in an ugly way, via @.MISC). So parameters/quantities discrimination should already be possible in the current rstan, although there's no public API for it. Dimension information is already there. So the parameter constraints/domain is the only thing missing.

bgoodri commented 9 years ago

If you only need to distinguish parameters vs. functions of parameters, that is not too difficult along the lines of what you said. If sometimes you need transformed parameters included and other times you need generated quantities included, that is harder. But the parameter constraints are not exposed to rstan at all. There has been some talk of exposing the transformation functions, at which point you could input the unconstrained parameters to the transformation functions without knowing the codomain.

On Sun, Apr 26, 2015 at 3:11 PM, Alexey Stukalov notifications@github.com wrote:

@bgoodri https://github.com/bgoodri There's SEXP unconstrained_param_names(SEXP include_tparams, SEXP include_gqs) in include/rstan/stan_fit.hpp that is used in stanmodel-class.R. Once there's a stanfit object, one can also use it outside of Rstan (in an ugly way, via @.MISC). So parameters/quantities discrimination should already be possible in the current rstan, although there's no public API for it. Dimension information is already there. So the parameter constraints/domain is the only thing missing.

— Reply to this email directly or view it on GitHub https://github.com/stan-dev/rstan/issues/156#issuecomment-96421625.

bob-carpenter commented 9 years ago

That's right. You could do it this way. But it only works for all parameters at once --- you can't do them one at a time. It's a side-effect of the too-coarse implementation of the model on the C++ side. This is what we're hoping to make more fine-grained during the current model refactor-athon.

On Apr 27, 2015, at 5:11 AM, Alexey Stukalov notifications@github.com wrote:

@bgoodri There's SEXP unconstrained_param_names(SEXP include_tparams, SEXP include_gqs) in include/rstan/stan_fit.hpp that is used in stanmodel-class.R. Once there's a stanfit object, one can also use it outside of Rstan (in an ugly way, via @.MISC). So parameters/quantities discrimination should already be possible in the current rstan, although there's no public API for it. Dimension information is already there. So the parameter constraints/domain is the only thing missing.

— Reply to this email directly or view it on GitHub.