[262] | 1 | |
---|
[384] | 2 | #include "stat/exp_family.h" |
---|
| 3 | #include "stat/mixtures.h" |
---|
[13] | 4 | |
---|
[254] | 5 | using namespace bdm; |
---|
[13] | 6 | |
---|
| 7 | //These lines are needed for use of cout and endl |
---|
| 8 | using std::cout; |
---|
| 9 | using std::endl; |
---|
| 10 | |
---|
[32] | 11 | void disp(const vec &tmu, const mat &tR,const mat &Smp){ |
---|
| 12 | int N = Smp.cols(); |
---|
| 13 | vec Emu = Smp*ones(N) /N ; |
---|
| 14 | mat Er = (Smp*Smp.transpose())/N - outer_product(Emu,Emu); |
---|
| 15 | cout << "True mu:" << tmu <<endl; |
---|
| 16 | cout << "Emp mu:" << Emu <<endl; |
---|
| 17 | |
---|
| 18 | cout << "True R:" << tR <<endl; |
---|
| 19 | cout << "Emp R:" << Er <<endl; |
---|
| 20 | } |
---|
| 21 | |
---|
[13] | 22 | int main() { |
---|
| 23 | |
---|
[32] | 24 | RNG_randomize(); |
---|
[13] | 25 | |
---|
[162] | 26 | RV x("{x }","2"); |
---|
| 27 | RV y("{y }","2"); |
---|
[13] | 28 | int N = 10000; //number of samples |
---|
| 29 | vec mu0 = "1.5 1.7"; |
---|
[22] | 30 | mat V0("1.2 0.3; 0.3 5"); |
---|
| 31 | ldmat R = ldmat(V0); |
---|
[32] | 32 | |
---|
| 33 | cout << "====== ENorm ====== " <<endl; |
---|
[270] | 34 | enorm<ldmat> eN; |
---|
[28] | 35 | eN.set_parameters(mu0,R); |
---|
[13] | 36 | mat Smp = eN.sample(N); |
---|
[32] | 37 | |
---|
| 38 | disp(mu0,R.to_mat(),Smp); |
---|
| 39 | |
---|
| 40 | cout << "====== MlNorm ====== " <<endl; |
---|
| 41 | mat I = eye(2); |
---|
[270] | 42 | mlnorm<ldmat> ML; |
---|
[179] | 43 | ML.set_parameters(I,zeros(2),R); |
---|
[270] | 44 | Smp = ML.samplecond_m(mu0,N); |
---|
[13] | 45 | |
---|
[32] | 46 | disp(mu0,R.to_mat(),Smp); |
---|
[13] | 47 | |
---|
[32] | 48 | cout << "====== EGamma ====== " <<endl; |
---|
| 49 | vec a = "100000,10000"; |
---|
| 50 | vec b = a/10.0; |
---|
[270] | 51 | egamma eG; |
---|
[32] | 52 | eG.set_parameters(a,b); |
---|
| 53 | |
---|
[211] | 54 | cout << eG.evallog(a)<<endl; |
---|
[201] | 55 | Smp = eG.sample_m(N); |
---|
[32] | 56 | |
---|
| 57 | vec g_mu = elem_div(a,b); |
---|
| 58 | vec g_var = elem_div(a,pow(b,2.0)); |
---|
| 59 | disp(g_mu,diag(g_var),Smp); |
---|
| 60 | |
---|
| 61 | cout << "====== MGamma ====== " <<endl; |
---|
[270] | 62 | mgamma mG; |
---|
[32] | 63 | double k = 10.0; |
---|
[270] | 64 | mG.set_parameters(k,mu0); |
---|
[32] | 65 | |
---|
[270] | 66 | Smp=mG.samplecond_m(mu0,N); |
---|
[32] | 67 | disp(mu0,pow(mu0,2.0)/k,Smp); |
---|
[104] | 68 | |
---|
| 69 | cout << "======= EMix ======== " << endl; |
---|
[270] | 70 | emix eMix; |
---|
[104] | 71 | Array<epdf*> Coms(2); |
---|
| 72 | Coms(0) = &eG; |
---|
| 73 | Coms(1) = &eN; |
---|
| 74 | |
---|
| 75 | eMix.set_parameters(vec_2(0.5,0.5), Coms); |
---|
| 76 | vec smp = eMix.sample(); |
---|
[201] | 77 | Smp = eMix.sample_m(N); |
---|
[104] | 78 | disp(eMix.mean(),zeros(2),Smp); |
---|
[32] | 79 | |
---|
[125] | 80 | cout << "======= MEpdf ======== " << endl; |
---|
[203] | 81 | mepdf meMix(&eMix); |
---|
[104] | 82 | |
---|
[270] | 83 | Smp = meMix.samplecond_m(mu0,N); |
---|
[104] | 84 | disp(eMix.mean(),zeros(2),Smp); |
---|
| 85 | |
---|
[125] | 86 | cout << "======= MMix ======== " << endl; |
---|
[270] | 87 | mmix mMix; |
---|
[125] | 88 | Array<mpdf*> mComs(2); |
---|
| 89 | mComs(0) = &mG; |
---|
| 90 | eN.set_mu(vec_2(0.0,0.0)); |
---|
[203] | 91 | mepdf mEnorm(&eN); |
---|
[125] | 92 | mComs(1) = &mEnorm; |
---|
| 93 | mMix.set_parameters(vec_2(0.5,0.5),mComs); |
---|
| 94 | |
---|
[270] | 95 | Smp = mMix.samplecond_m(mu0,N); |
---|
[125] | 96 | disp(mMix._epdf().mean(),zeros(2),Smp); |
---|
| 97 | |
---|
[145] | 98 | cout << "======= EProd ======== " << endl; |
---|
| 99 | // we have to change eG.rv to y |
---|
[270] | 100 | eN.set_rv(x); |
---|
| 101 | eG.set_rv(y); |
---|
[145] | 102 | //create array |
---|
[162] | 103 | Array<mpdf*> A(2); |
---|
[203] | 104 | mepdf meN(&eN); |
---|
[270] | 105 | mepdf meG(&eG); |
---|
[162] | 106 | A(0) = &meN; |
---|
| 107 | A(1) = &meG; |
---|
[145] | 108 | |
---|
[162] | 109 | mprod eP(A); |
---|
[145] | 110 | mat epV=zeros(4,4); |
---|
| 111 | epV.set_submatrix(0,0,V0); |
---|
| 112 | epV.set_submatrix(2,2,diag(g_var)); |
---|
| 113 | |
---|
[162] | 114 | vec v0=vec(0); |
---|
[270] | 115 | Smp = eP.samplecond(v0,N); |
---|
| 116 | disp(concat(eN.mean(),eG.mean()), epV,Smp); |
---|
[162] | 117 | |
---|
[294] | 118 | cout << "======= eWishart ======== " << endl; |
---|
| 119 | mat wM="1.0 0.9; 0.9 1.0"; |
---|
| 120 | eWishartCh eW; eW.set_parameters(wM/100,100); |
---|
| 121 | mat mea=zeros(2,2); |
---|
| 122 | mat Ch(2,2); |
---|
| 123 | for (int i=0;i<100;i++){Ch=eW.sample_mat(); mea+=Ch.T()*Ch;} |
---|
| 124 | cout << mea /100 <<endl; |
---|
| 125 | |
---|
| 126 | cout << "======= rwiWishart ======== " << endl; |
---|
| 127 | rwiWishartCh rwW; rwW.set_parameters(2,0.1,"1 1",0.9); |
---|
| 128 | mea=zeros(2,2); |
---|
| 129 | mat wMch=chol(wM); |
---|
| 130 | for (int i=0;i<100;i++){ |
---|
| 131 | vec tmp=rwW.samplecond(vec(wMch._data(),4)); |
---|
| 132 | copy_vector(4,tmp._data(), Ch._data()); |
---|
| 133 | mea+=Ch.T()*Ch; |
---|
| 134 | } |
---|
| 135 | cout << mea /100 <<endl; |
---|
[13] | 136 | //Exit program: |
---|
| 137 | return 0; |
---|
| 138 | |
---|
| 139 | } |
---|