# Finding the marginal using Bayes Theorem

I am trying to find the marginal distribution f(x) when given the prior distribution $\pi(\theta)$ (Gamma $\alpha, \beta$) and conditional distribution $f(x|\theta)$ (Poisson, $\theta$).

I know the formula to use is either:

${f(x)}=\sum_i{\pi(\theta_i)f(x|\theta_i)}$

or

${f(x)}=\int_\Theta{\pi(\theta)f(x|\theta)} d\theta$

But since the prior is continuous, and the conditional is discrete, I'm not sure which to proceed with (or how to combine them?)

I have assumed to used an integration but not sure about how to proceed after what I have done so far:

f(x) = $\int\frac{\beta^\alpha\theta^{\alpha-1}}{\Gamma(\alpha)}exp(-\beta\theta)\frac{e^{-\theta}\theta^x}{x!}d\theta$

= $\frac{\beta^\alpha}{\Gamma(\alpha)x!}\int\theta^{\alpha-1}e^{-\beta\theta}e^{-\theta}\theta^xd\theta$

Note: The solution is $f(x) = \frac{\beta^\alpha (x+\alpha-1)! }{(\beta+1)^{x+\alpha} \Gamma(\alpha)x!}$ (discrete)

In this case, $X$ is discrete and $\Theta$ is continuous. So whether you regard $f(x|\theta)$ as a function of a discrete random variable depends on whether you are using it as a conditional probability of $X=x$ given the parameter $\Theta=\theta$ or as a likelihood of $\Theta=\theta$ given the observation $X=x$.
Your aggregation of ${\pi(\theta)f(x|\theta)}$ is over $\theta$ so you want the integral.
If you were aggregating over $x$, then you would want the sum, and you should find that $\sum_X f(x)=1$.
$$\int_0^\infty \theta^{\alpha-1}e^{-\beta\theta}e^{-\theta}\theta^xd\theta=\int_0^\infty \theta^{\alpha+x-1}e^{-\theta(\beta+1)}d\theta$$ is the integral of the kernel of a Gamma distribution (kernel meaning the part of the density that's left when you take away the constants). More specifically, it's the kernel of a Gamma($\alpha+x, \beta+1)$ distribution (can you see this?). Now, put the constant term back in front of this $$\frac{\beta^\alpha}{\Gamma(\alpha)x!} \int_0^\infty \theta^{\alpha+x-1}e^{-\theta(\beta+1)}d\theta$$ and multiply by 1 to get the right normalizing constant: $$\frac{\beta^\alpha}{\Gamma(\alpha)x!} \frac{\frac{(\beta+1)^{\alpha+x}}{\Gamma(\alpha+x)}}{\frac{(\beta+1)^{\alpha+x}}{\Gamma(\alpha+x)}}\int_0^\infty \theta^{\alpha+x-1}e^{-\theta(\beta+1)}d\theta=\frac{\beta^\alpha}{\Gamma(\alpha)x!} \frac{{\Gamma(\alpha+x)}}{(\beta+1)^{\alpha+x}}\int_0^\infty \frac{(\beta+1)^{\alpha+x}}{\Gamma(\alpha+x)}\theta^{\alpha+x-1}e^{-\theta(\beta+1)}d\theta$$ The integral is equal to 1, since it's the integral of a complete density on its entire domain. So the result is:
$$\frac{\beta^\alpha}{\Gamma(\alpha)x!} \frac{{\Gamma(\alpha+x)}}{(\beta+1)^{\alpha+x}}=\frac{\beta^\alpha}{\Gamma(\alpha)x!} \frac{{(\alpha+x-1)!}}{(\beta+1)^{\alpha+x}}$$ as desired.