# In a 2 class problem how do I compute the normalization constant for finding the posterior distributions?

Cross Validated Asked by anonymous on October 2, 2020

I have a two-class problem. Let’s call the first class W1 and the second W2. I am trying to find the posterior distributions of both W1 and W2. I know I can use the following formula to do so…

$$P(W_j|X) = frac{P(X|W_j)P(W_j)}{P(X)}$$
where
$$P(X) = sum_{i=1}^{2}P(X|W_i)P(W_i)$$
So if I was trying to find the posterior distribution of W1, I would do the following
$$P(W_1|X) = frac{P(X|W_1)P(W_1)}{P(X|W_1)P(W_1)+P(X|W_2)P(W_2)}$$
I know the denominator is supposed to be a constant… but looking at this formula I don’t understand how the denominator would become a constant. Aren’t $$P(X|W_1)P(W_1)$$ and $$P(X|W_2)P(W_2)$$ both distributions. Would I have to sum all the values of both distributions together? Thanks for any help and I apologize if this is a silly question.

Code attempt is below:

P_w1_likelihood <- rnorm(n = 1000,mean = 4,sd = 2)
P_w1_prior <- 0.5

P_w2_likelihood <- rnorm(n = 1000,mean = 6,sd = 3)
P_w2_prior <- 0.5

P_w1_posterior <- P_w1_likelihood*P_w1_prior/(sum(P_w1_likelihood*P_w1_prior)+sum(P_w2_likelihood*P_w2_prior))
P_w2_posterior <- P_w2_likelihood*P_w2_prior/(sum(P_w1_likelihood*P_w1_prior)+sum(P_w2_likelihood*P_w2_prior))

Posteriors <- data.frame(
"Posterior" = c(P_w1_posterior,P_w2_posterior),
"Distribution" = c(rep("W1|X",times = 1000),rep("W2|X",times = 1000))
)

ggplot(Posteriors,aes(x = Posterior,group = Distribution,fill = Distribution))+
geom_histogram(alpha = 0.7)+
scale_fill_manual(values = c("orangered","darkorchid2"))