0

How do I implement the following: Create a Gaussian mixture model sampler. In this sampler, a datum has a 40% chance of being sampled from a N(-1,1) distribution, and a 60% chance of being sampled from a N(2,1/9) distribution. Sample 100,000 data and create a density histogram of your result. In R.

I'm supposed to use sample(), but I am unsure of which vector to use in prob = "". Any help/guidance is much appreciated, thank you!

1 Answers1

1
n <- 100000L
sims <- numeric(n)
for(i in 1L:n){
  choice <- sample(c(1L, 2L), size = 1L, prob = c(0.4, 0.6))
  if(choice == 1L){
    sims[i] <- rnorm(1L, mean = -1, sd = 1)
  }else{
    sims[i] <- rnorm(1L, mean = 2, sd = 1/9)
  }
}

hist(sims)

Or, more efficient:

n <- 100000L
sims <- numeric(n)
choices <- sample(c(1L,2L), size = n, prob = c(0.4, 0.6), replace = TRUE)
ones <- choices == 1L
n_ones <- sum(ones)
sims[ones] <- rnorm(n_ones, mean = -1, sd = 1)
sims[!ones] <- rnorm(n - n_ones, mean = 2, sd = 1/9)

hist(sims)

Be careful with the normal distribution. Perhaps 1/9 is the variance (sd²).

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225