# Parameters with categorical and gamma distributions in posterior distribution

I'm following a very good IPython notebook (the whole list can be found here) in which some sampling techniques are explained. However, I don't understand the use of a categorical variable in a change point model.

The posterior distribution for the model is the following:

\begin{aligned} P( \lambda_1, \lambda_2, \tau | \mathbf{y} ) &\propto \left[\prod_{t=1851}^{\tau} \text{Poi}(y_t|\lambda_1) \prod_{t=\tau+1}^{1962} \text{Poi}(y_t|\lambda_2) \right] \text{Gamma}(\lambda_1|\alpha,\beta) \text{Gamma}(\lambda_2|\alpha, \beta) \\ &\propto \left[\prod_{t=1851}^{\tau} e^{-\lambda_1}\lambda_1^{y_t} \prod_{t=\tau+1}^{1962} e^{-\lambda_2} \lambda_2^{y_t} \right] \lambda_1^{\alpha-1} e^{-\beta\lambda_1} \lambda_2^{\alpha-1} e^{-\beta\lambda_2} \\ &\propto \lambda_1^{\sum_{t=1851}^{\tau} y_t +\alpha-1} e^{-(\beta+\tau)\lambda_1} \lambda_2^{\sum_{t=\tau+1}^{1962} y_i + \alpha-1} e^{-\beta\lambda_2} \end{aligned}

After this, the parameters are given by:

$$\lambda_1 \sim \text{Gamma}(\sum_{t=1851}^{\tau} y_t +\alpha, \tau+\beta)$$ $$\lambda_2 \sim \text{Gamma}(\sum_{t=\tau+1}^{1962} y_i + \alpha, 1962-\tau+\beta)$$ $$\tau \sim \text{Categorical}\left( \frac{\lambda_1^{\sum_{t=1851}^{\tau} y_t +\alpha-1} e^{-(\beta+\tau)\lambda_1} \lambda_2^{\sum_{t=\tau+1}^{1962} y_i + \alpha-1} e^{-\beta\lambda_2}}{\sum_{k=1851}^{1962} \lambda_1^{\sum_{t=1851}^{\tau} y_t +\alpha-1} e^{-(\beta+\tau)\lambda_1} \lambda_2^{\sum_{t=\tau+1}^{1962} y_i + \alpha-1} e^{-\beta\lambda_2}} \right)$$

How can we obtain the distributions for these parameters? By marginalizing the posterior distribution? In the case of $\lambda_{1}$ and $\lambda_{2}$ each parameter is described by a Gamma distribution multiplied by some constant, so maybe that is the idea. In the case of $\tau$ I can't see why we obtain a categorical distribution with that argument.

Thanks a lot

• Ones made out of paper. Small laptop-like devices. A scientific notebook for recording lab notes and such. Various pieces of software, or various kinds of files used by various kinds of software. The term 'notebook' might mean myriad things to a person unused to Python. – Glen_b Feb 10 '14 at 7:38
• Not in the least. If someone hasn't seen Python at all, why would they have guessed "notebook" means "Python notebook" by reading your question? There was no hint in the title or the tags, or in the body text. If they click the link they might eventually work it out, but a person shouldn't have to click links to figure out what you're saying. I know what a Python notebook is and I didn't realize that was what you meant for a while. – Glen_b Feb 10 '14 at 7:53

I would like to write this as a comment but probably is too big. So i am writting it as an answer. Now, for the $\lambda_1, \lambda_2$ the distributions are just the full conditional distributions for these parameters. These can be found from the full posterior for the first holding (in very simple words) only what depends on $\lambda_1$ that is

$p(\lambda_1|\ldots)\propto \lambda_1^{\sum_{1851}^{\tau}y_{i}+a-1}e^{-(\beta+\tau)\lambda_{1}}$.

i.e you find the distribution of $\lambda_1$ conditioned on all the other variables like they are known. (i cannot explain this well, i hope you get the meaning)

Same idea for $\lambda_2$.

Now for the categorical he uses as probability the normalized posterior distribution.

Taking samples from the distributions $p(\lambda_1|\ldots),p(\lambda_2|\ldots),p(\tau|\ldots)$ with a gibbs sampler it is like having sampled from the joint distribution $p(\lambda_1,\lambda_2,\tau|\ldots)$.

• Thanks. When you say that we are taking only what depends on $\lambda_{1}$, do you mean that we are ignoring the rest? Because what I usually see is that in order to obtain $p(\lambda_{1}|...)$ I would need to marginalize over the other variables. Another question. Could you elaborate on why we use a categorical distribution with the normalized posterior distribution? That seems very odd. – Robert Smith Feb 10 '14 at 6:20
• We are ignoring them in the following meaning. Suppose (for the case of $\lambda_1$ that we know a value for $\lambda_2$ and for $\tau$ then the terms of the posterior that dont include $\lambda_1$ would just be constants. So you dont have to integrate, you just drop them out. I hope this helps you a little bit. Now for the categorical i will tell you what i understand from the context, maybe this will help you. Since it is a generalization of the Bernoulli its argument must be a probability thus the normalization. The outcome of this will determine the period, if it is the early or the late. – Christos Feb 10 '14 at 6:42
• 1+ Oh, I see. Yes, that could be the reason, although I have to verify that the categorical distribution does behave the way it should. Thank you very much! – Robert Smith Feb 10 '14 at 7:04
• I already checked the categorical distribution. Yes, the idea is that we choose a likely better $tau$ according to the joint distribution, but using this nice trick to get a change point position. Then we iterate again using that $\tau$ to obtain an even better $\lambda_{1}$ and $\lambda_{2}$ and so on, until convergence. – Robert Smith Feb 10 '14 at 20:20
• exactly! you said what i was trying to say. glad you figured it out. Thank you for accepting the answer also. – Christos Feb 10 '14 at 20:56