I'm answering both e-mails inline below.
On Aug 18, 2014, at 7:25 AM, Tamas Papp <
tkp...@gmail.com> wrote:
> On Mon, Aug 18 2014, Bob Carpenter <
ca...@alias-i.com> wrote:
>
>> If you set the log probability to -infinity, it will do rejection.
>
> Just out of curiosity (not that I indend to do it, the reparametrization
> below is better), how would I do that? I thought of
>
> lp__ <- negative_infinity()
>
> but lp__ is deprecated, so I am not sure about this. Is there a function
> that sets lp__ to -infinity, _and_ possibly exits early from the model
> posterior calculation? If not, will just open an issue.
increment_log_prob(negative_infinity());
will do it. We're also going to add a raise_exception(...) [perhaps to be
renamed] statement in 2.5 that'll short-circuit to the same effect.
> Some clarifying questions on the approach you suggested, below:
>
>> It's better to actually implement the constraints. The problem is
>> that Stan doesn't let you separately implement constraints for
>> each variable.
>>
>> So you have to enforce this constraint yourself:
>>
>>> z[i]/w[i] > m
>>
>> Rewriting, that's
>>
>> z[i] > m / w[i]
>
> For the archives, that is z[i] > m * w[i], but I get the point.
Thanks for fixing my algebra!
>
>> So you need a lower-bound on z[i] equal to m / w[i].
>>
>> The way to do this is just do what Stan does internally for
>> transformed parameters. Start with a raw, unconstrained parameter,
>> then transform it and apply the Jacobian adjustment:
>>
>> parameters {
>> vector[N] z_raw;
>> }
>
> Just to clarify: I should use an unconstrained variable (or vector)
> because Stan will automatically assign a flat (\propto 1) prior to it,
> so it will not mess up my likelihood.
Correct. Stan assigns flat priors over the valid values
of each variable.
> And if I wanted to use something
> like
>
> parameters {
> real<lower=0> z_raw[N];
> }
> transformed parameters {
> vector[N] z;
> for (i in 1:N)
> z[i] <- z_raw + m / w[i];
> // no need to increment lp__ with Jacobian correction, transformation linear
> }
> model {
> z ~ ...; // whatever prior you want here
> }
>
> then (maybe some future version of) Stan may use a prior that would add
> transformed terms to my posterior? Or would this be innocuous?
After fixing the algebra, then you can use whatever prior you want on z.
Or you could put a prior on z_raw, and then you don't need the Jacobian
adjustment. See below.
Is w[i] constrained to be positive? It's not necessary
for the model here. If it is, then you can just
define the parameter with a constraint lower=0 and then you
won't need another term. Same for m if that's constrained
to be positive.
> model {
> increment_log_prob(m_aux);
> for (i in 1:N) {
> z[i] ~ lognormal(0.0, 1.0);
> w[i] ~ lognormal(0.0, 1.0);
> z_hat[i]/z[i] ~ lognormal(0, 0.1);
> w_hat[i]/w[i] ~ lognormal(0, 0.1);
> increment_log_prob(z_aux[i]);
> increment_log_prob(w_aux[i]);
> }
> }
You can't use
z_hat[i] / z[i] ~ ...
without applying another Jacobian, this time for the u -> const/u
transform.
Every time you transform a variable on the left side of a sampling
statement, you need to add the log Jacobian determinant.
> --8<---------------cut here---------------end--------------->8---
>
> I run and check it with
>
> --8<---------------cut here---------------start------------->8---
> model <- stan_model(file = "bivariate_censoring1.stan")
> fit <- sampling(model,
> data =
> list(N = nrow(d),
> z_hat = d$z_hat,
> w_hat = d$w_hat),
> iter=1000,
> chains=1)
> posterior <- extract(fit, permuted = TRUE)
> plot(d$z, colMeans(posterior$z))
> abline(a=0, b=1, col="gray50")
> plot(d$w, colMeans(posterior$w))
> abline(a=0, b=1, col="gray50")
> hist(posterior$m)
> abline(v=1, col="gray50")
> --8<---------------cut here---------------end--------------->8---
>
> and everything checks out except that the posterior mean of m is well
> below 1, and also, it depends on the sample size. I know that censoring
> boundaries are tricky to identify well, but I am still wondering if I
> miscoded something.
I'm not sure what you were trying to code, but I think you missed
a Jacobian.
>
> Also, I get a couple of
> --8<---------------cut here---------------start------------->8---
> Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
> Error in function stan::prob::lognormal_log(N4stan5agrad3varE): Random variable is -nan:0, but must not be nan!
> If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
> but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
> --8<---------------cut here---------------end--------------->8---
> messages but not more than 1-2 per chain, so I think these can be
> ignored safely.
Correct --- this is usually just an early adaptation problem until
the variable scales and step sizes are adjusted properly. Even then,
you can run over boundaries in some cases, but the rejection will do
the appropriate Metropolis adjustment.
- Bob