//
//Cholesky factarize command
//This command is used to Genaration normal distribution which has original covariance.
//


//Syntax
chole [variable];

//Syntax used options
prin [variables]/
[options]
;
//Option
input:corr   //calculate principal from correlation data(default).
input:cov    //calculate principal from covariance data.
input:direct //calculate principal from original data.

//
//Example 1 
//Multiply cholesky matorix and this tranpose one shows same with orignal correlation.
//
//Read bankers data.
get bankR.csv@;

//Get correlation of property of bankers. corr salnow salbeg time work age;

get freq@ana; //Execute cholesky factarizetion. chole salnow salbeg time work age;

get freq@ana; put chole; //Transpose cholesky matrix. transpose; select col_1-5; put trans;

get chole; //Multiply cholesky matrix and transpose matrix. mxmult salnow salbeg time work age by trans;

Multiplied matrix shows same with original correlation

// //Example 2 //Generate normal distribution which has same covariance with original data. // //Read data as original covariance. get school.csv@;

//Get correlation for input of cholesky factarization. corr x1 x2 y;

//Get output corration and save tempolary file to check comparing with generated data. get freq@ana; put f_corr; //Execution cholesky factarization by input of correlation. chole x1 x2 y;

//Get output cholesky matrix and save tempolary file to use outlook function as below. get freq@ana; put f_chole; //Get average and standard devietaion by statis command to use outlook function as below. get school.csv@; statis x1 x2 y;

//Save average and std. to tempolary file. get freq@ana; put f_stat; // //Generate normal distibution which has same average and std,covariance of school.csv. // //Declare data clear and vector to use as below. clear; vector x[3]; vector ch[3][3]; vector sum[3]; //Generate 500 records which is normal distribution by loop. for(i=1;i<=500;i++) { //Get average and std from file by outlook function. x1m=outlook(f_stat,"_items_","AVERAGE","x1"); x1v=outlook(f_stat,"_items_","STD","x1"); x2m=outlook(f_stat,"_items_","AVERAGE","x2"); x2v=outlook(f_stat,"_items_","STD","x2"); ym=outlook(f_stat,"_items_","AVERAGE","y"); yv=outlook(f_stat,"_items_","STD","y"); //Generate 3 normal distoribution. x[1]=ranstd(x1m,x1v); x[2]=ranstd(x2m,x2v); x[3]=ranstd(ym,yv); //Make cholesky matrix from file by outlook function. ch[1][1]=outlook(f_chole,"#",1,"x1"); ch[1][2]=outlook(f_chole,"#",1,"x2"); ch[1][3]=outlook(f_chole,"#",1,"y"); ch[2][1]=outlook(f_chole,"#",2,"x1"); ch[2][2]=outlook(f_chole,"#",2,"x2"); ch[2][3]=outlook(f_chole,"#",2,"y"); ch[3][1]=outlook(f_chole,"#",3,"x1"); ch[3][2]=outlook(f_chole,"#",3,"x2"); ch[3][3]=outlook(f_chole,"#",3,"y"); //Set covariance to normal distribution. for(j=1;j<=3;j++) { sum[j]=0; for(k=1;k<=3;k++) { sum[j]=sum[j]+ch[j][k]*x[k]; } } x1r=sum[1]; x2r=sum[2]; yr =sum[3]; //Output earch reacord. outrec; }

//View Genaration of normal distribution which may be same distoribution of school.csv. plot scat x1r x2r yr;

//Check same correlation comparing generated data with original school.csv data corr x1r x2r yr;