> On Aug 19, 2016, at 5:33 PM, 'Ben Lambert' via Stan users mailing list <
stan-...@googlegroups.com> wrote:
>
> Hi,
>
> I am trying to implement a discrete parameter model to include in a document for my students. The model I have is the following,
>
> I have two coins in my pocket
Your model is partly designed for K coins but then hard codes K = 2.
I'd just go with a hard coded K = 2 for now and then generalize
when that's working. Our general advice is to start simple and then
extend.
> - one has a probability of heads of 0.1, and the other is 0.9.
Don't tell Andrew :-)
http://www.stat.columbia.edu/~gelman/research/published/diceRev2.pdf
> In each experiment I pull one out at random and record the result for N flips. I want to implement a model that estimates the coin type in each experiment, as well as the probabilities of heads for each coin.
The thing to do is start with the discrete indicator z[m] in { 1, 2 } indicating
which coin is drawn for study m in 1:M. I'm also going to let y[m] in 0:N
be the number of successes (er, heads) It's much more efficient than the
Bernoulli formulation. If theta[1] and theta[2] in (0,1) are the probabilities
of success (heads), then assuming the coin is selected randomly, the likelihood's just:
p(z, y | theta) = PROD_{m in 1:M} p(z[m], y[m] | theta)
Then you can marginalize inside the product
p(y[m] | theta) = 0.5 * binomial(y[m] | theta[1]) + 0.5 * binomial(y[m] | theta[2])
or
log p(y[m] | theta) = log_sum_exp(log(0.5) + log binomial(y[m] | theta[1]),
log(0.5) + log binomial(y[m] | theta[2]))
And that's just how you write the model in Stan, along with any prior
you might put on theta.
You can assume logit_theta[k] in (-inf, inf) and use binomial_logit
if you prefer that parameterization
> At the moment I have implemented this as below -- it seems to be working -- it estimates out the states of fake data very well. However I wanted to double check this implementation as I don't feel 100% comfortable with discrete stuff in Stan.
>
> Does this look ok?
Looks OK to me. The ordering you impose for identifiability can
be a problem because it can affect the posterior uncertainty (for
instance, the first coin will always have a lower probability of
heads than the second) but won't be an issue if the probabilities
are 0.1 and 0.9 and you have a reasonable amount of data.
Your sum over lp is just doing the binomial by hand.
> I know I could have used a hierarchical model where I pick coin one with probability phi, and coin two with probability 1-phi, but I wanted to keep it simple for now.
That's not what we'd call a hierarchical model --- it's just a mixture
with not necessarily symmetric mixing proportions.
The generated quantities should just be coded in expectation rather
than using a categorical_rng and you also don't need all that softmax
stuff for K = 2, which is where I'd start.
Pr[z[m] = 1 | data] = binomial(y[m] | theta[1] ) / (binomial(y[m] | theta[1]) + binomial(y[m] | theta[2]))
or on the log scale just takes the log.
- 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.