You're right that you can't code the direct model in Stan because
it involves discrete parameters. If the counts aren't too high, it'd
be possible in theory to marginalize out the possible x values by
enumerating them.
Or you could build a more direct model where you model the counts
with some correlation. That is, you let lambda[1,...,K] be drawn
from a multivariate distribution directly. I am not familiar with
any of these models, so don't really know what the goal is.
We often use overdispersed models where you have a bit more latitutde
than in the Poisson (which has only a single parameter, which is both
mean and variance).
I'm a little confused by your model because you seem to have swapped
x and y from the description, with X in the Stan program being y in
the description. But otherwise it looks OK.
You can code the model a lot more efficiently.
This
> for (k in 1:K) {
> lambda[k] ~ gamma(0.1, 0.1);
> }
>
can just be
lambda ~ gamma(0.1, 0.1);
though I'd recommend more informative priors than this.
and
> for (n in 1:N) {
> for (m in 1:M) {
> lmbd <- 0;
> for (k in 1:K) lmbd <- lmbd + A[m, k] * lambda[k];
> X[n, m] ~ poisson(lmbd);
> }
> }
should be (with A being coded as a matrix and lambda as a vector,
as
for (m in 1:M) {
lmbd <- 0;
for (k in 1:K) if (A[m, k]) lmbd <- lmbd + lambda[k];
X[ , m] ~ poisson(lmbd);
}
Even better if you can transpose X and just use X[m] ~ ...
Less copying that way. This isn't the most efficient way to do this,
but it should be a whole lot faster than what you have above.
And if the X[, m] isn't working in this context (2.10 fixes some
of the edge cases), then X[1:N, m] will.
And if you want to move this all to a GLM setting, you can use poisson_log,
which takes the log of the mean as a parameter.
- Bob
> On Apr 19, 2016, at 5:55 PM, Aleksandar Dimitriev <
dimitriev....@gmail.com> wrote:
>
>
> Let's say that I have a random data set of N observations of a k-dimensional vector that is drawn from a MVP distribution. I will try to briefly describe the approach, but it's probably better outlined here around slide 30:
http://www.stat-athens.aueb.gr/~karlis/multivariate%20Poisson%20models.pdf
> So, the model is the following:
> Let Yn = (y1, ..., ym)' be one observation. The assumption is that Y = AX where X=(x1,...xk)' is a vector of univariate uncorrelated Poissons, and A is an indicator matrix such that each element of Y is a sum of particular elements of X. For example, for a trivariate case we may have:
> Y = (y1, y2, y3)', and if we want to model covariances between each y we can create a vector X = (x1, x2, x3, x12, x23, x13). Thus, the latter 3 terms would model the (positive) covariance. My goal is given a set of draws from this distribution, to infer the rates of each of the independent Poissons in X.
> The problem is that I can't seem to implement this in Stan because the Y are not observed and therefore should probably be parameters, but can't be since they are discrete, so the following code does not compile, of course.
> One thing that managed to work in Stan is another model: since each Y is a sum of certain X Poissons it is therefore distributed Poisson with a rate that is the sum of the rates of its Poissons, but I'm pretty sure this model is not equivalent to the previous one. This model in Stan is:
>
> data {
> int N; // number of observations
> int M; // M-variate Poisson
> int K; // M + M(M - 1) / 2, i.e. number of mean and covariance latent Poissons
> int A[M, K]; // indicator matrix
> int X[N, M]; // observed data
> }
>
> parameters {
> real<lower=0> lambda[K]; // rates of the latent Poissons
> }
>
> model {
> real lmbd;
> for (k in 1:K) {
> lambda[k] ~ gamma(0.1, 0.1);
> }
>
> for (n in 1:N) {
> for (m in 1:M) {
> lmbd <- 0;
> for (k in 1:K) lmbd <- lmbd + A[m, k] * lambda[k];
> X[n, m] ~ poisson(lmbd);
> }
> }
> }
>
> Also, the results from 2000 samples of this model had very low effective sizes and inference took a long time (~15min for a dataset of 1000 points of a trivariate Poisson). For reference, I generated a toy data with mean rates (lambda 1,2,3) of 10, 40 and 2, and covariance rates (lambda 12 13 and 23, or 4 5 and 6 in Stan output) of 50, 5 and 8, and I am a bit puzzled with the close-to-zero values of 1 3 and 5.
>
>
> Inference for Stan model: karlis.
> 4 chains, each with iter=1000; warmup=500; thin=1;
> post-warmup draws per chain=500, total post-warmup draws=2000.
> mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
> lambda[1] 0.31 0.10 0.88 0.00 0.00 0.00 0.13 2.98 71 1.06
> lambda[2] 19.83 1.47 4.41 17.54 18.19 18.57 19.15 36.13 9 1.16
> lambda[3] 0.34 0.30 1.47 0.00 0.00 0.00 0.05 3.45 24 1.08
> lambda[4] 64.14 0.63 2.12 56.86 64.40 64.76 64.98 65.34 11 1.12
> lambda[5] 0.50 0.42 1.93 0.00 0.00 0.00 0.08 6.87 21 1.10
> lambda[6] 14.26 0.85 2.58 3.87 14.71 14.99 15.11 15.29 9 1.15
> lp__ 584414.88 0.20 2.02 584409.92 584413.77 584415.24 584416.32 584417.82 102 1.04
>
> Lastly, I saw that the bivariate case has been handled in Stan (
https://groups.google.com/forum/#!newtopic/stan-users/stan-users/9mMsp1oB69g), but this is just hand-coding the bivariate PMF.
> I've also attached a traceplot of the 4 chains, does it indicate some problem with identifiability or is the likelihood just multi-modal?
>
> --
> 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.
> <traceplot.png>