On 10/14/12 12:39 PM, Ben Goodrich wrote:
> On Sunday, October 14, 2012 4:29:09 AM UTC-4, Sebastian Weber wrote:
>
> thank you very much. When I tried your code, it first did not compile for me since stan complained about the 0-1
> limitation for p in the model block. Are limitations not allowed there? Removing the upper and lower statements
> solved the problem and the model seems to work fine.
>
>
> Sorry about that. Restrictions on the support of a local variable are not allowed (and are not checked) in the model {}
> block. But since the variable is on the (0,1) interval by construction, it is fine to remove the lower and upper bounds.
> I believe that transformations in the transformed parameters block are checked, so it is good to provide the bounds in
> that case.
Yes, anywhere you can declare constraints, the
constraints are enforced at the end of the block
in which the variable is declared.
> When I do it with a large number of chains and a large number of iterations, sometimes it goes perfectly and sometimes
> one chain experiences a nan and never recovers. Many functions in Stan are not numerically stable, so things like this
> happen with some probability.
There are also basic functions, like 0.0 / 0.0 that
produce NaN. In some cases, we can get around that.
If there are known places where you think we're doing
something at boundaries that we could make better
behaved than returning NaN or throwing an exception, let
us know and we can fix it.
> Some more questions in this context:
>
> - Concerning the inv_logit transform: To me it is simply a transform of a given parameter, so why shouldn't I put it
> in the transformed parameter block? Should I always put inverse links into the model block? And if yes, which
> transformations should I then included in transformed parameters and which in the model?
>
>
> The question is really: Do you want to analyze its posterior distribution? If yes, put it into the transformed
> parameters {} block. If no, put it in the model {} block. I assumed no since your R code did not reference those
> probabilities, but whichever choice you make "should" have no effect on the sampling, in theory.
We can work out the behavior based on the definitions
of the error checking in the manual.
Transformed parameters: constraints enforced at the end
of the model block
Parameters to probability functions: domain constraints
enforced when called
Given this behavior, if you have a variable that has to
be between 0 and 1, say because it's the parameter for a Bernoulli,
then declaring a constraint on the transformed parameter
catches the error before any of the model is executed. Defining
it as a local variable in the model block will lead to the error
being caught the first time it's used.
All else being equal, trapping errors early is a good thing.
> But in practice, maybe
> numeric instability issues are handled more gracefully when the probability is in the transformed parameters {} block
> because if it violates its bounds, then a non-fatal exception is thrown before the log-posterior is evaluated.
Please let us know if you run into reproducible numerical
stability issues. We could definitely use better limiting
behavior in many cases.
In some cases, it's just plain old IEEE floating-point
arithmetic, like if you divde 0.0 / 0.0 somewhere.
> - should I always limit my scale parameters by 0 and some upper bound, lets say 100 in the declarations or is a
> cauchy prior preferable?
>
>
> In principle, it is a question of what you believe about the data-generating process. Andrew's paper recommended either
> of those, and maybe bounding on both sides is more numerically stable. What Andrew found to be objectionable in theory
> and problematic in practice were (nearly) improper priors over the entire positive half of the real line.
You can try both and see what effect they have on the posterior.
They will also have different posterior geometries (that is, the
covariance structure across the parameter values), which can make
one or the other more efficient in terms of drawing effective
samples.
- Bob