%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simulated data for Finite Mixture Regression Model (FMRM)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rand('state',37); % set arbitrary seed for uniform draws
randn('state',37); % set arbitrary seed for normal draws


g=3; % 3 latent classes (regimes)
cp=[0.2;0.3;0.5]; %true class membership probabilities

%set betas for each regime and build regression models

beta1true=[0.1 0.2 0.3]'; 
beta2true=[0.4 0.5 0.6]';
beta3true=[0.7 0.8 0.9]';
% these values are just for ease of comparison

% Impose ordering on variances to overcome label-switching problem
sig1true=0.5;
sig2true=0.5;
sig3true=0.5;

n=20000; %sample size - must be large for this to work well

X=[ones(n,1) -0.5+1.3*randn(n,1) 2+2*rand(n,1)];

e1=sqrt(sig1true)*randn(n,1);
e2=sqrt(sig2true)*randn(n,1);
e3=sqrt(sig3true)*randn(n,1);

y1=X*beta1true+e1;
y2=X*beta2true+e2;
y3=X*beta3true+e3;

% now assign each observation to one of the three models
Z=mnrnd(1,cp,n); %n by 3 matrix of rows with 2 zeros and 1 one

%check distribution
mean(Z) % should be similar to cp

y=sum([y1 y2 y3].*Z,2); %picks the y assigned by the Z matrix for each obs.

save c:\klaus\AAEC6564\mlab\worksp\mod10_FMRM_data beta1true beta2true beta3true ...
    sig1true sig2true sig3true X y cp;




