lundi 24 juillet 2017

Transforming draws in Matlab from Gaussian mixture to uniform

Consider the following draws for a 2x1 vector in Matlab with a probability distribution that is a mixture of two Gaussian components.

P=10^3; %number draws
v=0.038462;

%First component
mu_a = [0,0.2806];
sigma_a = [v,0;0,v];

%Second component
mu_b = [0,-1.6806];
sigma_b = [v,0;0,v];


%Combine    
MU = [mu_a;mu_b];
SIGMA = cat(3,sigma_a,sigma_b);
w = ones(1,2)/2; %equal weight 0.5
obj = gmdistribution(MU,SIGMA,w);

%Draws
RV_temp = random(obj,P);%Px2

% Transform each component of RV_temp into a uniform in [0,1] by estimating the cdf.
RV1=ksdensity(RV_temp(:,1), RV_temp(:,1),'function', 'cdf');
RV2=ksdensity(RV_temp(:,2), RV_temp(:,2),'function', 'cdf'); 

Now, if we check whether RV1 and RV2 are uniformly distributed on [0,1] by doing

ecdf(RV1)
ecdf(RV2)

we can see that RV1 is uniformly distributed on [0,1] (the empirical cdf is close to the 45 degree line) while RV2 is not.

Could you help me to understand why?




Aucun commentaire:

Enregistrer un commentaire