Question about gradients

50 views
Skip to first unread message

Alp Kucukelbir

unread,
Jul 30, 2014, 7:16:56 PM7/30/14
to stan...@googlegroups.com
Hi folks,

I'm making progress with blackbox VB! But I'm stuck with models that have transformations.

Specifically, I'm confused about the gradients I'm getting for models with constraints/transformations, even simple ones like <lower=0>.

I suspect there is a significant disconnect between what I *think* is going on, and what is *actually* going on.

I wrote the essence of my question up in LaTeX and am attaching it to this email.

Could someone help me out when they get the chance?

Thanks in advance.

Cheers,
Alp

question_to_stan-dev.pdf

Bob Carpenter

unread,
Jul 30, 2014, 10:57:01 PM7/30/14
to stan...@googlegroups.com
1. To keep on Andrew's terminological kick, what you're calling a
latent variable, we just call a parameter. We reserve the term
"latent" for some particular kinds of things, like latent predictors
in an IRT model or latent continuous values in a classification problem.

To be precise, we drop constant terms (those that only depend on data, x)
where we can figure it out automatically. So if you do

z ~ normal(0,10);

in the modeling language you don't get the -log(10) term that you'd get
if 10 were a parameter.

And it doesn't have to be a log joint, just something proportional
to the posterior of interest.

2. Correct.

3. I find it helps to not use the same variable z for f(z) and f^{-1}(z).
But that's not an issue.

For reference, the transforms are all outlined in the penultimate chapter
of the manual on transformations of variables.

You should be using Eigen::Matrix<double, Eigen::Dynamic, 1> for
some_z in the call to gradient(), not Eigen::blablabla for any other
value of blablabla.

In that case, you should be getting \nabla_z \psi(z), where \psi(z) is
defined up to an additive constant.

- 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.
> For more options, visit https://groups.google.com/d/optout.
> <question_to_stan-dev.pdf>

Alp Kucukelbir

unread,
Jul 31, 2014, 6:53:58 AM7/31/14
to stan...@googlegroups.com
Thanks Bob.

That confirms my understanding. Thus my code must be wrong.

I'll continue hacking at it today. If I still haven't solved by noon, I'll try to create a minimum working example of my confusion and share that during the meeting.

Cheers,
Alp

Bob Carpenter

unread,
Jul 31, 2014, 10:55:49 AM7/31/14
to stan...@googlegroups.com
Could you send the example to the list before the meeting?

- Bob

Alp Kucukelbir

unread,
Jul 31, 2014, 10:57:35 AM7/31/14
to stan...@googlegroups.com
Hi Bob,

Now I'm worried that something is wrong with my maths.

Let me untangle that first; then I'll post to the list.

Cheers,
Alp
> You received this message because you are subscribed to a topic in the Google Groups "stan development mailing list" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-dev/5QtDGQeCdgY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to stan-dev+u...@googlegroups.com.

Bob Carpenter

unread,
Jul 31, 2014, 10:59:29 AM7/31/14
to stan...@googlegroups.com
Did the simple example you sent in LaTeX work as
you expected?

- Bob

Alp Kucukelbir

unread,
Jul 31, 2014, 11:05:15 AM7/31/14
to stan...@googlegroups.com
I think so.

Basically, the "symptom" I'm suffering from right now is this.

I have the simplest model that I can think of:

transformed data {
real y[2];
y[1] <- 20.0;
y[2] <- 10.0;
}
parameters {
real mu;
// real<lower=0> mu;
}
model {
for (n in 1:2)
y[n] ~ normal(mu,1.0);
}

sample, optimize, and vb all estimate mu = 15.

but when I introduce the <lower=0>, vb estimates something different. (sample and optimize still work just fine.)

Also, I found that I had to really shrink the step size. I suppose this is due to the exp(mu) transform "squeezing" the parameter space.

I'm now thinking this has something to do with the jacobian, but other thoughts/suggestions are welcome!

Cheers,
Alp

Andrew Gelman

unread,
Jul 31, 2014, 11:06:53 AM7/31/14
to stan...@googlegroups.com
Testing on a simple example is a great move. It does sound like there must be a bug in your code.

Michael Betancourt

unread,
Jul 31, 2014, 11:11:50 AM7/31/14
to stan...@googlegroups.com
Be careful — in log space the normal will be skewed, so the mean of the variational
distribution is not guaranteed to lie on log(mu) = log(15). The best way to test
variational methods is visually — plot the target distribution and the variational
approximation in log space and see how much they overlap.

Alp Kucukelbir

unread,
Jul 31, 2014, 11:35:21 AM7/31/14
to stan...@googlegroups.com
Amazing. Exactly the comment I needed to read. Thank you, Michael.

In this preliminary version of the algorithm I was using a Gaussian with fixed variance for my variational family q.

If I reduce the variance of the Gaussian to make it look like a delta function, vb indeed converges to log(15) in the transformed model.

(this is good news: my maths *and* [i'm so proud] my code was OK!)

OK. Now moving onto having the algorithm adapt the variance of the variational family (as intended)!




On Jul 31, 2014, at 11:11 AM, Michael Betancourt <betan...@gmail.com> wrote:

> Be careful -- in log space the normal will be skewed, so the mean of the variational
> distribution is not guaranteed to lie on log(mu) = log(15). The best way to test
> variational methods is visually -- plot the target distribution and the variational
Reply all
Reply to author
Forward
0 new messages