0

This question is from the book An Introduction to Generalized Linear Models

enter image description here

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)

enter image description here

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
Dan Li
  • 179
  • 2
    (1) You have not sampled from a mixture of Normal distributions: you have only sampled from a mixture of two samples of Normal distributions. (2) You "calculate" moments and median the same way as you would calculate them for any dataset. It's difficult to see how your question goes beyond that in any way. Are you trying to ask something else? (3) Your code doesn't come anywhere near computing a "true median" of anything! For correct code, see the end of my post at https://stats.stackexchange.com/a/411671/919 or the middle 3 lines of code at https://stats.stackexchange.com/a/424378/919. – whuber Dec 27 '23 at 21:23
  • 1
    Re the edit: you are using the term "sum" to mean a mixture rather than a sum of random variables. See https://stats.stackexchange.com/questions/331973 (or search our site for sum convolution) for clarification. https://stats.stackexchange.com/questions/16608/ explains how to calculate moments of mixtures. Finding quantiles of mixtures, like the median, usually requires root finding, as shown in the two links I already provided. – whuber Dec 28 '23 at 15:04

0 Answers0