# A complicated posterior

I have asked this question before about estimating a H matrix filled with 1 and 0 and it seems like I have to integrate H to have a posterior. So attached please is what my posterior looks like: P(H_ij=1| all the other parameters)= the picture. So my question 1 is: how to implement such a complicated posterior using stan?

My question 2 is if I relaxed my assumption, I use a continuous prior that would mainly generate 1 and 0, for example beta(0.0001,0.0001), would that be Okay? If it Okay, then I will still feel uneasy in using stan. Because as stan only requires the data and likelihood function, it would search through the posterior parameter space for you automatically, but as the picture demonstrates, the posterior is pretty complicated, and it is high-level formula includes integration. Can I trust the results provided by stan without knowing the intermediate steps? I do not doubt stan, I just feel I do not have a good knowledge of using any Bayesian-related tools. And thanks for reading my question.

It is a bit unclear what your equation means. Does \pi_j have a prior? How does it depend on X? Are \alpha and Z hyperparameters?

In Stan, you do not in fact need to implement the posterior per se. Mostly because calculating the posterior implies calculating the normalization constant in Bayes’ rule, and that is not required for MCMC. Instead, you just implement your entire generative model, which internally has the same effect as writing out the distribution of the complete joint distribution of H, X, and \Phi.

it would look something like this for a trivial 1x1 matrix:

model{
phi ~ normal(0,alpha);
x ~ Binomial(phi, z);
h ~ Bernoulli(pi);
}


but requires some looping in the general case.

1 Like

You can use LaTeX on the site now with $e^x$ for example.

It depends wha you’re trying to do. Usually, you want to stick to the actual data, not try to approximate discrete things with continuous variables. The problem for sampling is that the sampler will get stuck on one side of that beta distribution.

Don’t trust Stan, test Stan. But to do that, you need to have a well-defined model you can simulate from. I’m still not clear on what exactly you’re trying to do.

Stan’s not the best tool to be using for complicated discrete parameter problems.

It requires a definition of the data, the parameters, and a log density proportional equal to the posterior up to a constant that doesn’t depend on the parameters.

What you want to do is write your likelihood and prior down precisely. You can use discrete parameters. Then we can think about how to marginalize them out. If the marginalization is intractable due to combinatorics, it will mean that sampling is going to be hard if not impossible no matter what you try.