On 2/28/13 2:16 PM, Jiqiang Guo wrote:
> I have checked in the code for exposing grad_log and grad_log_prob and the function for transforming parameters between
> its support and the unconstrained space to R. The log_prob and grad_log_prob only takes parameter values on the
> unconstrained space.
Absolutely -- that's a positive for what we want to
do with it, which is run optimization code.
More below the example.
> An example of using them:
>
> mcode <- '
> transformed data {
> vector[5] v;
> v[1] <- 1;
> v[2] <- 2;
> v[3] <- 3;
> v[4] <- 4;
> v[5] <- 5;
> }
> parameters {
> simplex[5] y;
> }
> model {
> y ~ dirichlet(v);
> }
> '
>
> library(rstan)
> K <- 5
> fit <- stan(model_code = mcode)
> lp <- log_prob(fit, rep(0, K - 1))
> gr <- grad_log_prob(fit, rep(0, K - 1))
> y <- constrain_pars(fit, rep(0, K- 1))
> uy <- transform_pars(fit, y)
This looks fantastic -- it's everything we need to
get going with it in R applications like optimization.
Some suggestions
1. I think we need to add a method to the model that
lets us get the numbers of constrained and unconstrained
parameters. That should simplify error checking in
the interface. From the R side, it'd be something like
K_unc <- num_params(fit)
We can't do this until we get in and modify the model to
return it.
2. Can we give constrain_pars and transform_pars
complementary names? I'm thinking
theta <- constrain_pars(fit, theta_unc);
theta_unc <- unconstrain_pars(fit, theta);
or
theta_trans <- transform_params(fit, theta);
theta <- inv_transform_params(fit, theta_trans);
I was rather inconsistent in our API and used "free"
in the transform code instead of "unconstrain". Daniel and
I keep meaning to set aside some time to clean up the naming
throughout.
3. I find the use of R constructs in examples confusing.
The R doc does it everywhere, but for us novices, simpler
is better. So I'd suggest an example along the lines of
theta_trans <- rep(0,K-1); # unconstrained parameters
theta <- inv_transform_pars(fit, theta_trans);
4. The doc needs to point people to the transform doc so
they can figure out how many unconstrained parameters correspond
to their model. But for the most part, if they work on the
constrained space as we have users do in Stan, the transforms will
all be under the hood.
5. It'd be really great if we could read unconstrained
variables to transform out of an environment or list as inits
are handled now. I think that'd make the R interface cleaner
from an R point of view.
Thanks! I'll add some round-trip tests to writer.
- Bob