Problem with initialization for a dynamic linear model

738 views
Skip to first unread message

Philip Maybank

unread,
Apr 20, 2017, 2:34:23 AM4/20/17
to Stan users mailing list

I am having difficulty using the gaussian_dlm_obs function.

The error message I get is,

Rejecting initial value: Error evaluating the log probability at the initial value.
Initialization between (-2, 2) failed after 100 attempts. 
 Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error in eval(expr, envir, enclos) : Initialization failed." 

See attached for my code and all of the console output.  

My general aim is to infer the parameters of a linear stochastic dynamical system / differential equation.

I have got Stan working on several examples from the documentation including nile.stan from stan-dev/example-models/misc/dlm/.  

I am fairly sure that the log probability can be evaluated for the parameter values and ranges I have chosen.  I tested this by writing an R function to evaluate the likelihood (available on request).

I have found it difficult to diagnose the problem as I have been unable to print any variables for this example.  I expected that print("W=", W, " F=", F); would print some output so that I could at least see what values the function arguments have.  But this didn't produce any output for me.

My R sessionInfo() output is as follows:

> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] mvtnorm_1.0-2        rstan_2.15.1         StanHeaders_2.15.0-1 ggplot2_2.1.0       

loaded via a namespace (and not attached):
 [1] codetools_0.2-10 colorspace_1.2-6 grid_3.1.1       gridExtra_2.2.1  gtable_0.1.2     inline_0.3.14   
 [7] munsell_0.4.2    parallel_3.1.1   plyr_1.8.1       Rcpp_0.12.2      scales_0.4.0     stats4_3.1.1    
[13] tools_3.1.1    

Any help would be greatly appreciated,

Phlip

neural_dlm.stan
neural_dlm.R
console_output.txt

Ben B

unread,
Apr 20, 2017, 9:48:16 AM4/20/17
to Stan users mailing list
If you're using Rstan, check out the "init" and "init_r" parameters in the Stan function call (https://cran.r-project.org/web/packages/rstan/rstan.pdf)

If Stan isn't successfully guessing a good initial value, then it'd just be worth changing the initialization. I believe Stan just randomly picks initial parameters between -2 and 2 on the unconstrained parameter space. You could make this bigger. I'm not sure what (-2, 2) means exactly for your alpha, but check out chapter 33 in the manual and you could figure it out. What your tests seem to mean is that -2 to 2 on the uncontrained space doesn't contain alphas that do anything other than blow up (so Stan can't start sampling).

Worst case, if you know a value of alpha that *will* work, you can start your chains there (using "init").

Ben

Philip Maybank

unread,
Apr 20, 2017, 11:09:17 AM4/20/17
to Stan users mailing list
Thanks for the reply.  I modified my .R file as follows,

f <- sampling(m, data = dlm_dat,
              iter = 2000, chains = 1,
              init = list(dlm_par_true))

I assume this initialises 'alpha' at 'dlm_par_true$alpha', which is the value that I used to simulate the data?

I get a similar error message as before,

Rejecting initial value:
  Error evaluating the log probability at the initial value.
[1] "Error in eval(expr, envir, enclos) : Initialization failed."
error occurred during calling the sampler; sampling not done

In case it is helpful, I have attached an R function that calculates the log probability I am interested in.  When I run this I get something reasanable:

> eval_dlm_log_prob(dlm_par_true, dlm_dat)
[1] -159.4588

Philip
dlm_r_functions.R

Ben B

unread,
Apr 20, 2017, 11:22:26 AM4/20/17
to Stan users mailing list
Ah! Dang. I'm off to work now, I probably won't be able to get back to this for awhile.

This sounds like there's a difference in the R implementation and the Stan implementation.

Check out the "expose_stan_functions" functionality in the Rstan docs. This should allow you to test your user-defined functions outside of the Stan samplers (so you can directly mimic the function call you're making in R now). You can throw some print statements in your functions and that'd probly make this easy to debug.

Ben

Ben B

unread,
Apr 20, 2017, 11:30:39 AM4/20/17
to Stan users mailing list
(testing this way would implicitly require you to code your stuff up in Stan as a user defined function, which should be pretty straight forward)

Philip Maybank

unread,
Apr 20, 2017, 12:15:34 PM4/20/17
to Stan users mailing list
Thanks for the suggestion about writing a user-defined function and using print statements.  I will have a think about this.

Note that in my original .stan file there were some print statements. E.g.,

model {
  [...]
  print("hello (model)");
  [...]
  y ~ gaussian_dlm_obs(F, G, V, W, m0, C0);
}

These print statements did not produce any output.

I have got print statements working on other problems.  But for this problem I have been unable to get the print function to produce any output after trying it in several different places.

Philip

Ben B

unread,
Apr 20, 2017, 3:16:13 PM4/20/17
to Stan users mailing list
Yeah, hopefully if you could make your function externally callable then you'd be able to debug that (and get prints to work) without having to go through the samplers.

If it helps, this appears to be the file that has the actual implementation in it:


You might be able to thumb through that and make sure it's doing the thing you think it's doing.

Ben

Bob Carpenter

unread,
Apr 21, 2017, 12:42:39 PM4/21/17
to stan-...@googlegroups.com

> On Apr 20, 2017, at 12:15 PM, Philip Maybank <philip....@gmail.com> wrote:
>
> Thanks for the suggestion about writing a user-defined function and using print statements. I will have a think about this.
>
> Note that in my original .stan file there were some print statements. E.g.,
>
> model {
> [...]
> print("hello (model)");
> [...]
> y ~ gaussian_dlm_obs(F, G, V, W, m0, C0);
> }
>
> These print statements did not produce any output.
>
> I have got print statements working on other problems. But for this problem I have been unable to get the print function to produce any output after trying it in several different places.

There was a bug around the print statements being supressed in RStan
(and maybe CmdStan). That should be cleared up in 2.15, but let us know
if it's still an issue.

- Bob

Bob Carpenter

unread,
Apr 21, 2017, 12:43:37 PM4/21/17
to stan-...@googlegroups.com
This is almost always because you are passing illegal variables
to fuctions. This is often due to not having the appropriate constraints
on parameters.

You should not try to fix this by initializing in a good space, but
by making the model coherent. Every parameter value that satisfies
the constraints should have a finite log density.

- 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.
> <neural_dlm.stan><neural_dlm.R><console_output.txt>

Andrew Gelman

unread,
Apr 21, 2017, 10:37:23 PM4/21/17
to stan-...@googlegroups.com
I recommend scaling your parameters so they are of order 1.  For example, if you have some parameter that you expect takes on a value such as 200, change the units so it will be closer to unit scale.  This should make Stan run faster and should also help the model make more sense.
A

Philip Maybank

unread,
Apr 24, 2017, 7:26:54 AM4/24/17
to stan-...@googlegroups.com
Thanks all for taking the time to respond to my post.

Andrew's comment about the scaling of the parameters relates to
sampling efficiency (from what I understand). I am still stuck with
the more basic issue of getting the sampler to run.

Following Ben B's suggestion, I have now written and tested a
user-defined function for evaluating the dlm log density. I copied
the source code for gaussian_dlm_obs_lpdf using the link that Ben B
sent. Then I edited it so that it was consistent with the
higher-level Stan syntax.

So I now have an R function called 'model_test' that was generated by
the command,

expose_stan_functions('./neural_dlm.stan')

I think that 'model_test' evaluates the log density of the dlm.
However in contrast to the corresponding Stan function
'gaussian_dlm_obs', it returns finite log densities:

--------------------------------
> model_test(y, N, h, sigma_obs, ..., alpha)
[1] 771.6542
--------------------------------

And as before, when I run a Stan program that includes

'y ~ gaussian_dlm_obs(F, G, V, W, m0, C0);'

I get the following error message,

------------------------------
Rejecting initial value:
Error evaluating the log probability at the initial value.
[1] "Error in eval(expr, envir, enclos) : Initialization failed."
[1] "error occurred during calling the sampler; sampling not done"
------------------------------

I have attached my updated .stan and .R files.

As an aside, I think 'expose_stan_functions' is great feature, and am
slowly becoming a fan of Stan as a whole.

Philip
> 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/x3OOBRQH7T4/unsubscribe.
> To unsubscribe from this group and all its topics, 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.



--


------------------------------------
Philip Maybank
+44 (0)7407 219 422
neural_dlm.stan
neural_dlm.R

Philip Maybank

unread,
Apr 24, 2017, 8:04:13 AM4/24/17
to stan-...@googlegroups.com
Problem solved now. :)

I stored my simulated data in an Nx1 matrix, and it should have been
stored it in a 1xN matrix:

y = matrix(0, 1, N)

in my R code.

And now that the sampling is working, the print statements are also working.

Thanks again for your help.

Philip
Reply all
Reply to author
Forward
0 new messages