How to generate random numbers in mixture normal distribution (considering weight)?

2 visualizaciones (últimos 30 días)
For a mixture normal distribution the code below is used (for 1000 sample):
x1= mu(1) + std(1)*randn(1000,1)
x2= mu(2) + std(2)*randn(1000,1)
But, I wan to take the respected weight into account, Wi=[0.8 0.2], for instance.
I tried this code, but when I plot the PDFs of them, it gives me wrong curves.
x1= Wi(1)*(mu(1) + std(1)*randn(1000,1));
x2= Wi(2)*(mu(2) + std(2)*randn(1000,1));
I was wondering how I can solve this problem?

Respuesta aceptada

the cyclist
the cyclist el 16 de Nov. de 2015
Does this do what you intend?
mu = [0 5];
std = [1 1];
N = 1000;
r = rand(N,1);
x1= (r >0.8).*(mu(1) + std(1)*randn(N,1));
x2= (r<=0.8).*(mu(2) + std(2)*randn(N,1));
x = x1+x2;
figure
histogram(x,99)
  3 comentarios
the cyclist
the cyclist el 16 de Nov. de 2015
mu = [0 3 6 9];
std = [1 1 1 1];
W = [0.1 0.2 0.3 0.4]; % Make sure to normalize this, if it isn't already
cumW = [0 cumsum(W)];
N = 100000;
r = rand(N,1);
xsum = zeros(N,1);
for k = 1:numel(mu)
x{k} = (r > cumW(k) & r <= cumW(k+1)).*(mu(k) + std(k)*randn(N,1));
xsum = xsum + x{k};
end
figure
histogram(xsum,99)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by