MATLAB: How can I generate data mixture of 2 Normal distributions and has the form f(x) = 0.4X1 + 0.6X2 where X1 ~ N(0.4 ,0.15^2) and X2 ~ N(0.7 , 0.09^2)

//Statistics and Machine Learning Toolbox

i need help to generate data from two mixture normal distributions of the denstiy function f(x)=0.4*x1+0.6*x2 where x1~N(0.4,0.15^2) & x2~N(0.7,0.09^2)

Best Answer

Here is one way:
% The number of random samples to generate
N = 10000;
% First, generate a binomial variable, which 40% of the time (on average) will be equal to 1,
% and 60% of the time (on average) will be equal to zero.
frac = rand(N,1) < 0.4;
% Generate the two normal distributions to sample from
norm1 = 0.4 + 0.15*randn(N,1);
norm2 = 0.7 + 0.09*randn(N,1);
% If "frac" is equal to 1, then the choice will be from the first normal.
% If "frac" is equal to 0, then the choice will be from the second normal.
d = frac.*norm1 + (1-frac).*norm2;
% Plot the resulting distribution
figure
histogram(d)
Related Question