How to manually provide the gradient of log probability?

262 views
Skip to first unread message

Michael Prange

unread,
Jan 5, 2016, 4:08:40 PM1/5/16
to Stan users mailing list
I'm computing the log pdf in an external C++ routine and including it in Stan using increment_log_prob(). In order for the sampler to properly function, I understand that it also needs the gradient of this function. Is there an interface for me to provide this gradient?

I realize that Stan can automatically compute this gradient if I code everything in Stan, but my external libraries are too complicated to translate into Stan. These external libraries already provide gradients, so I only need an interface for including them in Stan.

Michael

Bob Carpenter

unread,
Jan 5, 2016, 4:46:11 PM1/5/16
to stan-...@googlegroups.com
For all the gory details on the C++ and derivatives, see:

http://arxiv.org/abs/1509.07164

The in-between solution for external libraries without gradients
is to template out the scalar type. But if you have gradients,
then you want the above. Sorry it's so long --- you only need
the first bit on how to write simple functions.

For the interfacing with Stan itself, see:

https://github.com/stan-dev/stan/wiki/Contributing-New-Functions-to-Stan

It explains where things have to go in the symbol table to be
picked up by the Stan parser.

One of our to-do items for next year is making Stan extensible at
run time, which should be possible given that it's header only.

- Bob
> --
> 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.

Michael Prange

unread,
Jan 5, 2016, 5:18:40 PM1/5/16
to stan-...@googlegroups.com
The links you provide seem to be related to automatic differentiation. It would be easier for me if there were a set_gradient() function in Stan. Then I could call my external libraries for function values and gradient, and then within the Stan code call increment_log_prob() and set_gradient() with these values. 

To call these external libraries, I created some dummy functions within my Stan program and then edited the hpp file that stanc creates. This works well for the log probability, but I can't figure out how to input the gradient. Maybe this isn't possible, since I can't locate any gradient calculation within the stanc-generated hpp file.

You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/B-W6PKnzrFk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.

Bob Carpenter

unread,
Jan 5, 2016, 5:56:58 PM1/5/16
to stan-...@googlegroups.com

> On Jan 5, 2016, at 5:18 PM, Michael Prange <pra...@alum.mit.edu> wrote:
>
> The links you provide seem to be related to automatic differentiation. It would be easier for me if there were a set_gradient() function in Stan.

There's not a direct way to do that. You need to
implement a function that integrates into our autodiff.

We provide a base class that makes it pretty easy --- you
really just need to set the gradients (on each component of
the value if it's a multivariate return function).

> Then I could call my external libraries for function values and gradient, and then within the Stan code call increment_log_prob() and set_gradient() with these values.
>
> To call these external libraries, I created some dummy functions within my Stan program and then edited the hpp file that stanc creates. This works well for the log probability, but I can't figure out how to input the gradient. Maybe this isn't possible, since I can't locate any gradient calculation within the stanc-generated hpp file.

It's all done using autodiff on the templated log_prob
function.

- Bob

Michael Prange

unread,
Jan 5, 2016, 6:18:57 PM1/5/16
to stan-...@googlegroups.com
Thanks. I'll look into the links you provided me to learn how to do that.

Bob Carpenter

unread,
Jan 5, 2016, 6:35:58 PM1/5/16
to stan-...@googlegroups.com
The key is the base class in stan-dev/math:

stan/math/rev/core/precomputed_gradients.hpp

That lets you just compute the gradients and store them
and compute the value and return it; then you just need
to wrap the whole thing up in a Stan function and expose it.

- Bob
Reply all
Reply to author
Forward
0 new messages