R feature request: log prob + grad

46 views
Skip to first unread message

Bob Carpenter

unread,
Feb 13, 2013, 1:32:22 PM2/13/13
to stan...@googlegroups.com
Dave Blei just made an excellent suggestion, which I just
added to our RStan to-do list:

* (RStan) Expose to R for a model:

-- log prob and gradient functions
-- inits -> unconstrained param transform
-- results -> constrained param transform

Then users could do all sorts of things just with our models,
like run optimizers, Metropolis, etc.

- Bob

Andrew Gelman

unread,
Feb 13, 2013, 4:22:44 PM2/13/13
to stan...@googlegroups.com
I really like the idea of supplying all this to people so that Stan can become everybody's toy!
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>

Ben Goodrich

unread,
Feb 13, 2013, 4:56:21 PM2/13/13
to stan...@googlegroups.com
These are among the things that the new Rcpp syntax is intended to facilitate.

Ben

Jiqiang Guo

unread,
Feb 13, 2013, 5:09:45 PM2/13/13
to stan...@googlegroups.com
Ben, what do you mean by the new Rcpp syntax?  As far as I know, the new stuff does no really help essentially.  Is there something new I have not noticed?

Ben had mentioned this idea before.  I had some difficulty to implement it nicely as currently I have to delete the model object after I have done the sampling.  I have to investigate a bit more.  

Jiqiang 

Jiqiang Guo

unread,
Feb 23, 2013, 9:55:13 PM2/23/13
to stan...@googlegroups.com
I am working on this and I guess that I have a workaround to tackle the difficulty I had. 

Now I am having a question: how to call function log_prob and grad_log_prob of a model?  I am trying to do something like this: 

log_prob(par_r, par_i, &rstan::io::rcout);

where par_r is std::vector<double>, and par_i is std::vector<int>.

Apparently, this cannot be compiled.  So what should I do, using var?

Jiqiang 

Bob Carpenter

unread,
Feb 24, 2013, 3:13:38 AM2/24/13
to stan...@googlegroups.com
You need to call

model.log_prob(...);

and pass non-constant vectors (they're not declared const,
which I've been meaning to fix, but it's all tangled up
with the io::reader class's mappings).

The R output stream looks right, but you might want to try it
with 0 just in case that's causing some kind of compilation problem
then add in the R stream later.

What's the error message?

- Bob

On 2/23/13 9:55 PM, Jiqiang Guo wrote:
> I am working on this and I guess that I have a workaround to tackle the difficulty I had.
>
> Now I am having a question: how to call function log_prob and grad_log_prob of a model? I am trying to do something
> like this:
>
> log_prob(par_r, par_i, &rstan::io::rcout);
>
> where par_r is std::vector<double>, and par_i is std::vector<int>.
>
> Apparently, this cannot be compiled. So what should I do, using var?
>
> Jiqiang
>
> On Wed, Feb 13, 2013 at 5:09 PM, Jiqiang Guo <guo...@gmail.com <mailto:guo...@gmail.com>> wrote:
>
> Ben, what do you mean by the new Rcpp syntax? As far as I know, the new stuff does no really help essentially. Is
> there something new I have not noticed?
>
> Ben had mentioned this idea before. I had some difficulty to implement it nicely as currently I have to delete the
> model object after I have done the sampling. I have to investigate a bit more.
>
> Jiqiang
>
>
> On Wed, Feb 13, 2013 at 4:56 PM, Ben Goodrich <goodri...@gmail.com <mailto:goodri...@gmail.com>> wrote:
>
> These are among the things that the new Rcpp syntax is intended to facilitate.
>
> Ben
>
>
> On Wednesday, February 13, 2013 1:32:22 PM UTC-5, Bob Carpenter wrote:
>
> Dave Blei just made an excellent suggestion, which I just
> added to our RStan to-do list:
>
> * (RStan) Expose to R for a model:
>
> -- log prob and gradient functions
> -- inits -> unconstrained param transform
> -- results -> constrained param transform
>
> Then users could do all sorts of things just with our models,
> like run optimizers, Metropolis, etc.
>
> - Bob
>
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> stan-dev+u...@googlegroups.com <mailto:stan-dev%2Bunsu...@googlegroups.com>.

Jiqiang Guo

unread,
Feb 24, 2013, 2:21:50 PM2/24/13
to stan...@googlegroups.com
The error message is:

error: non-const lvalue reference to type
      'vector<stan::agrad::var>' cannot bind to a value of unrelated type 'std::vector<double>'
      double lp = model_.log_prob(par_r, par_i, &rstan::io::rcout);
                                  ^~~~~
normal_mean_model.cpp:245:35: note: in instantiation of member function
      'rstan::stan_fit<normal_mean_model_namespace::normal_mean_model,
      boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0,
      2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >::log_prob'
      requested here
  double lp = Rcpp::as<double>(sf.log_prob(Rcpp::wrap(mu1)));
                                  ^
normal_mean_model.cpp:85:31: note: passing argument to parameter 'params_r__' here
    var log_prob(vector<var>& params_r__,

And this is the C++ code inside my function to get the log_prob: 

      std::vector<double> par_r = Rcpp::as<std::vector<double> >(params_r);
      std::vector<int> par_i(model_.num_params_i(), 0);
      double lp = model_.log_prob(par_r, par_i, &rstan::io::rcout);


Additionally, the following similar code for the grad_log_prob works:

      std::vector<double> par_r = Rcpp::as<std::vector<double> >(params_r);
      std::vector<int> par_i(model_.num_params_i(), 0);
      std::vector<double> gradient;
      model_.grad_log_prob(par_r, par_i, gradient, &rstan::io::rcout);

--
Jiqiang 




        For more options, visit https://groups.google.com/groups/opt_out.




--
You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+unsubscribe@googlegroups.com.

Jiqiang Guo

unread,
Feb 24, 2013, 2:40:40 PM2/24/13
to stan...@googlegroups.com
Suggested from the error, I am using std::agrad::var and it at least compiles.  So now I have

      vector<stan::agrad::var> par_r2(par_r.size());
      for (size_t i = 0; i < par_r.size(); i++)
        par_r2[i] = par_r[i];
      vector<int> par_i(model_.num_params_i(), 0);
      double lp = model_.log_prob(par_r2, par_i, &rstan::io::rcout).val();

--
Jiqiang

Bob Carpenter

unread,
Feb 24, 2013, 4:56:22 PM2/24/13
to stan...@googlegroups.com
You should be able to call log_prob with double arrays, but
it looks like that's broken. It should be defined to
do exactly what you had to code by hand.

I have to recode the log_prob function so that we can use
it for RM-HMC, and at that point, the calls will change
slightly and doubles will definitely be supported. I'll
let you know when that happens.

- Bob


On 2/24/13 2:40 PM, Jiqiang Guo wrote:
> Suggested from the error, I am using std::agrad::var and it at least compiles. So now I have
>
> vector<stan::agrad::var> par_r2(par_r.size());
> for (size_t i = 0; i < par_r.size(); i++)
> par_r2[i] = par_r[i];
> vector<int> par_i(model_.num_params_i(), 0);
> double lp = model_.log_prob(par_r2, par_i, &rstan::io::rcout).val();
>
> --
> Jiqiang
>
>
> On Sun, Feb 24, 2013 at 2:21 PM, Jiqiang Guo <guo...@gmail.com <mailto:guo...@gmail.com>> wrote:
>
> The error message is:
>
> error: non-const lvalue reference to type
> 'vector<stan::agrad::var>' cannot bind to a value of unrelated type 'std::vector<double>'
> double lp = model_.log_prob(par_r, par_i, &rstan::io::rcout);
> ^~~~~
> normal_mean_model.cpp:245:35: note: in instantiation of member function
> 'rstan::stan_fit<normal_mean_model_namespace::normal_mean_model,
> boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0,
> 2147483563 <tel:2147483563>>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399
> <tel:2147483399>> > >::log_prob'
> stan-dev+unsubscribe@__googlegroups.com <mailto:stan-dev%2Bunsu...@googlegroups.com>
> <mailto:stan-dev%__2Buns...@googlegroups.com <mailto:stan-dev%252Buns...@googlegroups.com>__>.
>
> For more options, visit https://groups.google.com/__groups/opt_out
> <https://groups.google.com/groups/opt_out>.
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> stan-dev+unsubscribe@__googlegroups.com <mailto:stan-dev%2Bunsu...@googlegroups.com>.
> For more options, visit https://groups.google.com/__groups/opt_out <https://groups.google.com/groups/opt_out>.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> stan-dev+unsubscribe@__googlegroups.com <mailto:stan-dev%2Bunsu...@googlegroups.com>.
> For more options, visit https://groups.google.com/__groups/opt_out <https://groups.google.com/groups/opt_out>.
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.

Jiqiang Guo

unread,
Feb 26, 2013, 2:28:42 PM2/26/13
to stan...@googlegroups.com
A suggestion on grad_prob_log: I noticed that log_prob is also a byproduct of grad_prob_log. So would it be a good idea to return log_prob for this function as well say using a parameter?  I am thinking if I need prob_log and then grad_prob_log in R, it would be more efficient that if only grad_prob_log is called. 

--
Jiqiang

Ben Goodrich

unread,
Feb 28, 2013, 8:07:34 PM2/28/13
to stan...@googlegroups.com
On Tuesday, February 26, 2013 2:28:42 PM UTC-5, Jiqiang Guo wrote:
A suggestion on grad_prob_log: I noticed that log_prob is also a byproduct of grad_prob_log. So would it be a good idea to return log_prob for this function as well say using a parameter?  I am thinking if I need prob_log and then grad_prob_log in R, it would be more efficient that if only grad_prob_log is called. 

This would be a good idea. It wouldn't help much when calling optim() but it would help for nlm(), which allows returning the gradient as an attribute of the function value.

Ben

Reply all
Reply to author
Forward
0 new messages