want gradient from rstan

201 views
Skip to first unread message

Andrew Gelman

unread,
Dec 7, 2016, 9:49:23 PM12/7/16
to Stan users mailing list
Hi, I fit a Stan model and now I want the gradient as a function of the parameters in the original space (I think that's what's called the constrained parameters in the rstan documentation).

Suppose I have a stanfit object called "fit" and 3000 simulation draws (which I can get using extract(fit)). I want the gradient for each of these simulation draws. I'm confused trying to figure out how to do this using grad_log_prob. It seems like I have to extract the simulations, pull them out one at a time, then put them back into lists. There must be an easier way, no?

Also it seems that grad_log_prob gives me the gradient on the unconstrained space. How to I get the gradients on the original (constrained) space?

Thx
A

Ben Goodrich

unread,
Dec 7, 2016, 10:27:57 PM12/7/16
to Stan users mailing list, gel...@stat.columbia.edu
On Wednesday, December 7, 2016 at 9:49:23 PM UTC-5, Andrew Gelman wrote:
Suppose I have a stanfit object called "fit" and 3000 simulation draws (which I can get using extract(fit)).  I want the gradient for each of these simulation draws.  I'm confused trying to figure out how to do this using grad_log_prob.  It seems like I have to extract the simulations, pull them out one at a time, then put them back into lists.  There must be an easier way, no?

No, due to the fact that the compiled Stan function can only input a vector of (unconstrained) parameters.
 
Also it seems that grad_log_prob gives me the gradient on the unconstrained space.  How to I get the gradients on the original (constrained) space?

There is an adjust_transform argument that is TRUE by default which indicates that the gradient calculation should be with respect to the constrained parameters. So, all together it would be

pars <- as.matrix(posterior)
skel <- get_inits(posterior)[[1]]
grad <- t(apply(pars, MARGIN = 1, FUN = function(theta) {
  grad_log_prob(posterior, upars = unconstrain_pars(posterior, relist(theta, skel)))
}))
colnames(grad) <- colnames(pars)

Ben

Andrew Gelman

unread,
Dec 7, 2016, 10:39:54 PM12/7/16
to stan-...@googlegroups.com
Thanks.
--
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.

Jiqiang Guo

unread,
Dec 7, 2016, 10:44:28 PM12/7/16
to stan-...@googlegroups.com
No, I don't think you can get the derivative with respect to the parameter on the constrained space.  adjust_transform is used only for whether the Jacobian adjustment is done or not for the log-posterior.  (Ben, right?)

Jiqiang

Thanks.

To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.

Daniel Lee

unread,
Dec 7, 2016, 11:28:41 PM12/7/16
to stan-...@googlegroups.com
If you needed it, we can get that in C++ without much effort, I think. We can devise some way to get that out. grad_log_prob() wasn't designed for that purpose, so it wouldn't be appropriate to retrofit it for that functionality. It's really a matter of specifying what you want out from some input. 
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

Ben Goodrich

unread,
Dec 8, 2016, 12:17:32 AM12/8/16
to Stan users mailing list
On Wednesday, December 7, 2016 at 10:44:28 PM UTC-5, Jiqiang Guo wrote:
No, I don't think you can get the derivative with respect to the parameter on the constrained space.  adjust_transform is used only for whether the Jacobian adjustment is done or not for the log-posterior.  (Ben, right?)

Yeah, that sounds right. It was designed to do MLEs.

Ben

Bob Carpenter

unread,
Dec 8, 2016, 1:44:14 AM12/8/16
to stan-...@googlegroups.com
We've just had this discussion on the dev list:

http://discourse.mc-stan.org/t/feature-request-jacobians-of-unconstraining-transform/154/1

and I believe Ryan was suggesting what Ben was suggesting
and I said the same thing as Jiqiang.

I don't think this can be fished out of a current compiled
model. It would be possible to generate just the constrained
function call. In fact, we could factor the code generation
that way, which we've also been talking about. But we need to
get rid of the derivative w.r.t. the transform as well as
the Jacobian calcs.

- Bob

> On Dec 7, 2016, at 11:28 PM, Daniel Lee <bea...@alum.mit.edu> wrote:
>
> If you needed it, we can get that in C++ without much effort, I think. We can devise some way to get that out. grad_log_prob() wasn't designed for that purpose, so it wouldn't be appropriate to retrofit it for that functionality. It's really a matter of specifying what you want out from some input.
>
> On Dec 7, 2016, at 10:44 PM, Jiqiang Guo <guo...@gmail.com> wrote:
>
>> No, I don't think you can get the derivative with respect to the parameter on the constrained space. adjust_transform is used only for whether the Jacobian adjustment is done or not for the log-posterior. (Ben, right?)
>>
>> Jiqiang
>>
>> On Wed, Dec 7, 2016 at 10:39 PM, Andrew Gelman <gel...@stat.columbia.edu> wrote:
>> Thanks.
>>
>>> On Dec 7, 2016, at 10:27 PM, Ben Goodrich <goodri...@gmail.com> wrote:
>>>
>>> On Wednesday, December 7, 2016 at 9:49:23 PM UTC-5, Andrew Gelman wrote:
>>> Suppose I have a stanfit object called "fit" and 3000 simulation draws (which I can get using extract(fit)). I want the gradient for each of these simulation draws. I'm confused trying to figure out how to do this using grad_log_prob. It seems like I have to extract the simulations, pull them out one at a time, then put them back into lists. There must be an easier way, no?
>>>
>>> No, due to the fact that the compiled Stan function can only input a vector of (unconstrained) parameters.
>>>
>>> Also it seems that grad_log_prob gives me the gradient on the unconstrained space. How to I get the gradients on the original (constrained) space?
>>>
>>> There is an adjust_transform argument that is TRUE by default which indicates that the gradient calculation should be with respect to the constrained parameters. So, all together it would be
>>>
>>> pars <- as.matrix(posterior)
>>> skel <- get_inits(posterior)[[1]]
>>> grad <- t(apply(pars, MARGIN = 1, FUN = function(theta) {
>>> grad_log_prob(posterior, upars = unconstrain_pars(posterior, relist(theta, skel)))
>>> }))
>>> colnames(grad) <- colnames(pars)
>>>
>>> Ben
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
Reply all
Reply to author
Forward
0 new messages