1 | |
---|
2 | #include "stat/exp_family.h" |
---|
3 | #include "stat/emix.h" |
---|
4 | |
---|
5 | using namespace bdm; |
---|
6 | |
---|
7 | //These lines are needed for use of cout and endl |
---|
8 | using std::cout; |
---|
9 | using std::endl; |
---|
10 | |
---|
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 | |
---|
22 | int main() { |
---|
23 | |
---|
24 | RNG_randomize(); |
---|
25 | |
---|
26 | RV x ( "{x }", "2" ); |
---|
27 | RV y ( "{y }", "2" ); |
---|
28 | int N = 10000; //number of samples |
---|
29 | vec mu0 = "1.5 1.7"; |
---|
30 | mat V0 ( "1.2 0.3; 0.3 5" ); |
---|
31 | ldmat R = ldmat ( V0 ); |
---|
32 | |
---|
33 | cout << "====== ENorm ====== " << endl; |
---|
34 | shared_ptr<enorm<ldmat> > eN = new enorm<ldmat>(); |
---|
35 | eN->set_parameters ( mu0, R ); |
---|
36 | mat Smp = eN->sample_m ( N ); |
---|
37 | |
---|
38 | disp ( mu0, R.to_mat(), Smp ); |
---|
39 | |
---|
40 | cout << "====== MlNorm ====== " << endl; |
---|
41 | mat I = eye ( 2 ); |
---|
42 | mlnorm<ldmat> ML; |
---|
43 | ML.set_parameters ( I, zeros ( 2 ), R ); |
---|
44 | Smp = ML.samplecond_m ( mu0, N ); |
---|
45 | |
---|
46 | disp ( mu0, R.to_mat(), Smp ); |
---|
47 | |
---|
48 | cout << "====== EGamma ====== " << endl; |
---|
49 | vec a = "100000,10000"; |
---|
50 | vec b = a / 10.0; |
---|
51 | shared_ptr<egamma> eG = new egamma(); |
---|
52 | eG->set_parameters ( a, b ); |
---|
53 | |
---|
54 | cout << eG->evallog ( a ) << endl; |
---|
55 | Smp = eG->sample_m ( N ); |
---|
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; |
---|
62 | shared_ptr<mgamma> mG = new mgamma(); |
---|
63 | double k = 10.0; |
---|
64 | mG->set_parameters ( k, mu0 ); |
---|
65 | |
---|
66 | Smp = mG->samplecond_m ( mu0, N ); |
---|
67 | disp ( mu0, pow ( mu0, 2.0 ) / k, Smp ); |
---|
68 | |
---|
69 | cout << "======= EMix ======== " << endl; |
---|
70 | shared_ptr<emix> eMix = new emix(); |
---|
71 | Array<shared_ptr<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(); |
---|
77 | Smp = eMix->sample_m ( N ); |
---|
78 | disp ( eMix->mean(), zeros ( 2 ), Smp ); |
---|
79 | |
---|
80 | cout << "======= MEpdf ======== " << endl; |
---|
81 | mepdf meMix ( eMix ); |
---|
82 | |
---|
83 | Smp = meMix.samplecond_m ( mu0, N ); |
---|
84 | disp ( eMix->mean(), zeros ( 2 ), Smp ); |
---|
85 | |
---|
86 | cout << "======= MMix ======== " << endl; |
---|
87 | mmix mMix; |
---|
88 | Array<shared_ptr<mpdf> > mComs ( 2 ); |
---|
89 | |
---|
90 | // emix::set_parameters requires the first mpdf to be named |
---|
91 | mG->set_rv(x); |
---|
92 | mG->set_rvc(y); |
---|
93 | mComs ( 0 ) = mG; |
---|
94 | |
---|
95 | eN->set_mu ( vec_2 ( 0.0, 0.0 ) ); |
---|
96 | shared_ptr<mepdf> mEnorm = new mepdf ( eN ); |
---|
97 | mComs ( 1 ) = mEnorm; |
---|
98 | mMix.set_parameters ( vec_2 ( 0.5, 0.5 ), mComs ); |
---|
99 | |
---|
100 | Smp = mMix.samplecond_m ( mu0, N ); |
---|
101 | disp ( 0.5 * eN->mean() + 0.4 * eG->mean(), zeros ( 2 ), Smp ); |
---|
102 | |
---|
103 | cout << "======= EProd ======== " << endl; |
---|
104 | // we have to change eG->rv to y |
---|
105 | eN->set_rv ( x ); |
---|
106 | eG->set_rv ( y ); |
---|
107 | //create array |
---|
108 | Array<shared_ptr<mpdf> > A ( 2 ); |
---|
109 | A ( 0 ) = new mepdf ( eN ); |
---|
110 | A ( 1 ) = new mepdf ( eG ); |
---|
111 | |
---|
112 | mprod eP ( A ); |
---|
113 | mat epV = zeros ( 4, 4 ); |
---|
114 | epV.set_submatrix ( 0, 0, V0 ); |
---|
115 | epV.set_submatrix ( 2, 2, diag ( g_var ) ); |
---|
116 | |
---|
117 | vec v0 = vec ( 0 ); |
---|
118 | Smp = eP.samplecond ( v0, N ); |
---|
119 | disp ( concat ( eN->mean(), eG->mean() ), epV, Smp ); |
---|
120 | |
---|
121 | cout << "======= eWishart ======== " << endl; |
---|
122 | mat wM = "1.0 0.9; 0.9 1.0"; |
---|
123 | eWishartCh eW; |
---|
124 | eW.set_parameters ( wM / 100, 100 ); |
---|
125 | mat mea = zeros ( 2, 2 ); |
---|
126 | mat Ch ( 2, 2 ); |
---|
127 | for ( int i = 0; i < 100; i++ ) { |
---|
128 | Ch = eW.sample_mat(); |
---|
129 | mea += Ch.T() * Ch; |
---|
130 | } |
---|
131 | cout << mea / 100 << endl; |
---|
132 | |
---|
133 | cout << "======= rwiWishart ======== " << endl; |
---|
134 | rwiWishartCh rwW; |
---|
135 | rwW.set_parameters ( 2, 0.1, "1 1", 0.9 ); |
---|
136 | mea = zeros ( 2, 2 ); |
---|
137 | mat wMch = chol ( wM ); |
---|
138 | for ( int i = 0; i < 100; i++ ) { |
---|
139 | vec tmp = rwW.samplecond ( vec ( wMch._data(), 4 ) ); |
---|
140 | copy_vector ( 4, tmp._data(), Ch._data() ); |
---|
141 | mea += Ch.T() * Ch; |
---|
142 | } |
---|
143 | cout << mea / 100 << endl; |
---|
144 | //Exit program: |
---|
145 | return 0; |
---|
146 | |
---|
147 | } |
---|