How to generate random numbers in mixture normal distribution (considering weight)?
2 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
the cyclist
on 16 Nov 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 Comments
the cyclist
on 16 Nov 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)
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!