Session Ready
Exercise

Sampling from a mixture of distributions (I)

A mixture distribution is a distribution whose density is a linear combination of normal distribution densities (components). Each component has a weight (its probability of being chosen), and a mean and standard deviation (just like any other normal distribution).

You'll build up the algorithm over two exercises. Here you'll choose the component to sample from by completing the definition of choose_component().

Instructions
100 XP
  • Generate a uniform random number from 0 to total_weight using the runif() function in the R namespace.
  • Inside the while loop, decrease the value of x by the jth element of weights.