This question is from the book An Introduction to Generalized Linear Models
The bimodal distribution is the sum of 2 normal distributions $N(2, 1)$ and $N(5, 0.5^2)$. I thought the sum of 2 normal distributions should also be normal.
I don't know how to get the density of this bimodal and sample from this bimodal distribution and calculate the moments and median of this bimodal distribution using the sample:
theta <- seq(-1,7,0.01)
d_theta = 0.5*dnorm(theta,mean=2,sd=1)+0.5*dnorm(theta,mean=5,sd=0.5)
curve(0.5dnorm(x,mean=2,sd=1)+0.5dnorm(x,mean=5,sd=0.5), -1, 7, col="sienna",
lwd=2,n=1001, ylab="PDF", xlab="theta") # plots the results
data = tibble('x'=theta, 'y'=d_theta)
data2 <- data[sample(seq_len(nrow(data)), 500, prob=data$y),]
segments(x0 = data2$x,
y0 = 0,
x1 = data2$x,
y1 = data2$y,
col= 'black',
type="l",
lwd = 0.1)
The mean of the sample can match the true mean:
#sample mean
weighted.mean(data2$x,data2$y)
weighted.mean(data$x,data$y)
#True mean
c <- integrate((x) (dnorm(x,mean=2,sd=1)+dnorm(x,mean=5,sd=0.5)), -10, 10)
density_x <- function(x){
(dnorm(x,mean=2,sd=1)+dnorm(x,mean=5,sd=0.5))*x/c$value
}
integrate(density_x, -10, 10)$value
[1] 3.63361
[1] 3.503125
[1] 3.5
But the median of the sample can't match the true median:
#sample median
quantile(data2$x, c(0.5))
#True median
for (i in 1:7) {
if(abs(integrate((x) (0.5dnorm(x,mean=2,sd=1)+0.5dnorm(x,mean=5,sd=0.5)), -10, i)$value - 0.5)<0.001){
print(i)
}
}
50%
3.035
[1] 4

