I'm sure this is dreadfully simple to everyone who sees it, but I'm completely lost. From what I've read, I think this concept must be so fundamental to Bayesian statistics that no one bothers to explain it. Anyway, here's a concept I'm trying to understand from a class:
I want to find the marginal distribution $f(x)=\int f(x|p)f(p)dp$ given $x|p\sim Binom(n,p)$ and $p\sim Beta(\alpha,\beta)$.
Then I want to get some summary statistics by drawing a few (hundreds) $x$ from $f(x)$ using an MCMC method. However, I must first draw a $p$ from $f(p)$ to draw an $x$ from $f(x)$.
My problem is these two points:
- $f(x)$ is not a distribution. It is the beta function and evaluates to $\frac{\Gamma(x+\alpha)\Gamma(n+\beta-y)}{\Gamma(\alpha+\beta+n)}$.
- There is no $p$ in the above $f(x)$ because I specifically integrated it out.
Even if I were to draw some $p$ from $f(p)$ there is no place for it in the beta function for it. In my understanding when doing this kind of sampling, my marginal distribution should be some kind of function (in this case since $P\sim Beta$, then $f(p)=p^{\alpha-1}(1-p)^{\beta-1}$) and then I draw the needed parameter ($p$) from its distribution (assuming $\alpha$ and $\beta$ are known) and plug it in to obtain my $x$.
Every resource I've seen up till now seems to only be dealing with posterior distributions, not this kind of integrated marginal distribution and none seem to deal with a sampling problem that does not contain the parameter I need to first sample for.
So why then do I need to draw the parameter for a marginal distribution that does not contain the parameter?
Major edit for clarification (My previous understanding of the concept was wrong):
I am trying to get the marginal distribution $f(x)=\int f(x|p)f(p)dp$, so I can calculate the statistics of $f(x)$ (e.g., mean). There are two ways to do so:
- Multiply and integrate the two distributions and then find the expectation. The result of this is and is supposed to be some $f(x)=$ equation with $x$ as an independent variable.
- Use sampling to generate a set of $x$ drawn from $f(x)$ by drawing $x$ from $f(x|p)$. To draw from $f(x|p)$ I first need a parameter $p$.
Example to draw 1000 samples:
list of samples $X=[x^{(1)},x^{(2)},...,x^{(1000)}]$.
$x^{(1)}\sim Binom(n,p)$ where $p\sim Beta(\alpha,\beta)$
$x^{(2)}\sim Binom(n,p)$ where $p\sim Beta(\alpha,\beta)$
...
To show it in simple R code:
a=1;b=1;n=50 #My arbitrary distribution parameters
X=double(1000) #An empty vector for X
for (i in seq(1,1000)){
p = rbeta(1,a,b)
X[i] = rbinom(1,n,p)
}
mean(X)
This is how to draw parameters to draw from a marginal distribution. I'll leave this here if I can, since I could not find anything online to explain it.