MATLAB: Generate random numbers from a mixture distribution

mixture modelsrandom numbers

Hi, I would like to generate random numbers from a mixture distribution defined as:
pdf_mix = p*burrpdf(x,alpha,c,k) + (1-p)*wblpdf(x,a,b);
The aim is to compute a kstest2 between the observed data and the mixture distribution.
Is this possible? Can I get some help on this?
T PLOCOSTE

Best Answer

Read David's answer. But let me explain it like this, because I think many people do not understand what a mixture distribution means or what it is.
A mixture distribution means you have two (well, or more) different distributions. I'll call them A and B. With probability p, a sample arises from distribution A, then with probability 1-p, the sample arises from distribution B.
The solution is simple. For each sample, first, choose a uniform random number. If the number is less than or equal to p, then you will sample from distribution A. If the number exceeds p, then you choose from distribution B. Easy, peasy.
Lets see how it might work. I'll pick two distributions that will be clearly different.
Distribution A: Uniform between limits [2,3]
Distribution B: Normally distributed, with mean 0, standard deviation 1.
Lets say 1000000 samples overall, enough to get a nice clean looking histogram. I'll set p as 0.4, arbitrarily. So roughly 40% of the samples will be uniform, 60% normal.
N = 1000000;
p = 0.4;
% U is a binary indicator variable, that tells how to
% sample for each sample. Here, 1 --> A, 0 --> B.
U = rand(N,1) <= p;
nA = sum(U);
nB = N - nA;
% preallocate X
X = zeros(N,1);
X(U) = rand(nA,1)*1 + 2;
X(~U) = randn(nB,1);
% finally, a histogram, just to show we did what was expected.
histogram(X,1000)
So we have some overlap between the distributions, but clearly a mixture of a Gaussian and a uniform, as we expected.
sum(U)
ans =
399632
We would have expected roughly 400000 of the events to be from the uniform case. Pretty close.
Related Question