Changeset 204
- Timestamp:
- 11/10/08 15:40:29 (16 years ago)
- Files:
-
- 1 added
- 6 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/arx.cpp
r201 r204 15 15 nu+=w; 16 16 17 // log(sqrt(2*pi)) = 0.91893853320467 17 18 if ( evalll ) { 18 19 lnc = est.lognc(); 19 ll = lnc - last_lognc ;20 ll = lnc - last_lognc - 0.91893853320467; 20 21 last_lognc = lnc; 21 22 } -
bdm/estim/merger.cpp
r198 r204 11 11 switch ( nu ) { 12 12 case 2: 13 coef= ( 1-0.5*sqrt ( ( 4 *beta-3) /beta ) );14 return exp ( coef*sq2bl + mu );13 coef= ( 1-0.5*sqrt ( ( 4.0*beta-3.0 ) /beta ) ); 14 return coef*sq2bl + mu ; 15 15 break; 16 16 case 3://Ratio of Bessel 17 17 coef = sqrt ( ( 3*beta-2 ) /3*beta ); 18 return elem_mult ( 19 elem_div ( besselk ( 0,sq2bl*coef ), besselk ( 0,sq2bl ) ), 20 exp ( mu ) ); 18 return log( besselk ( 0,sq2bl*coef )) - log( besselk ( 0,sq2bl ) ) + mu; 21 19 break; 22 20 case 4: … … 46 44 vec lw_src ( Ns ); 47 45 vec lw_mix ( Ns ); 46 vec lw ( Ns ); 48 47 mat lW=zeros ( n,Ns ); 49 48 vec vec0 ( 0 ); … … 75 74 // dbg << Name ( str ) << Mpred->mean(); 76 75 77 if ( niter<10) {76 if ( 1 ) { 78 77 // Generate new samples 79 78 eSmp.set_samples ( Mpred ); 80 for ( int i=0;i<Ns;i++ ) { set_col_part ( Smp_ex,i,Smp ( i ) );} 81 82 } 79 for ( int i=0;i<Ns;i++ ) { 80 //////////// !!!!!!!!!!!!! 81 if ( Smp ( i ) ( 1 ) <0 ) {Smp ( i ) ( 1 ) = GamRNG(); } 82 set_col_part ( Smp_ex,i,Smp ( i ) ); 83 } 84 {cout<<"Eff. sample size=" << 1./sum_sqr ( w ) << endl;} 85 cout << sum ( Smp_ex,2 ) /Ns <<endl; 86 } 87 else 88 {cout<<"Eff. sample size=" << 1./sum_sqr ( w ) << endl;} 89 83 90 // sprintf ( str,"Mpdf%d",niter ); 84 91 // for ( int i=0;i<Ns;i++ ) {Mix_pdf ( i ) = Mix.logpred ( Smp_ex.get_col ( i ) );} … … 127 134 zdls ( i )->get_val ( Smp ( k ) ), 128 135 zdls ( i )->get_cond ( Smp ( k ) ) ) ); 136 if ( !std::isfinite ( lw_src ( k ) ) ) { 137 cout << endl; 138 } 129 139 } 130 140 delete tmp_cond; … … 147 157 // dbg << Name ( str ) << lW; 148 158 149 w = lognorm_merge ( lW ); //merge159 lw = lognorm_merge ( lW ); //merge 150 160 151 161 // sprintf ( str,"w%d",niter ); … … 155 165 156 166 //Importance weighting 157 w /=exp ( lw_mix ); // hoping that it is not numerically sensitive... 167 lw -= lw_mix; // hoping that it is not numerically sensitive... 168 w = exp(lw-max(lw)); 158 169 //renormalize 159 w /=sum ( w ); 160 170 double sumw =sum ( w ); 171 if ( std::isfinite ( sumw ) ) { 172 w = w/sumw; 173 } 174 else { 175 176 } 177 { 178 double eff = 1./sum_sqr ( w ); 179 if ( eff<2 ) { 180 int mi= max_index ( w ); 181 cout << w(mi) <<endl; 182 cout << lW.get_col ( mi ) <<endl; 183 mat mm(2,1);mm.set_col(0,lW.get_col(mi)); 184 cout << lognorm_merge(mm) <<endl; 185 cout << lw_mix ( mi ) <<endl; 186 cout << lw ( mi ) <<endl; 187 cout << Smp ( mi ) <<endl; 188 cout << Mix._Coms(0)->_e()->mean() <<endl; 189 } 190 cout << "Eff: " << eff <<endl; 191 } 161 192 // sprintf ( str,"w_is_%d",niter ); 162 193 // dbg << Name ( str ) << w; … … 170 201 // ==== stopping rule === 171 202 niter++; 172 converged = ( niter> 5);203 converged = ( niter>20 ); 173 204 } 205 cout << endl; 174 206 175 207 } -
bdm/stat/emix.h
r193 r204 47 47 //!Default constructor. By default, the given epdf is not copied! 48 48 //! It is assumed that this function will be used only temporarily. 49 mratio ( const epdf* nom0, const RV &rv, bool copy=false ) :mpdf ( rv,nom0->_rv().subt (rv) ), dl(rv,rvc,nom0->_rv()) {49 mratio ( const epdf* nom0, const RV &rv, bool copy=false ) :mpdf ( rv,nom0->_rv().subt ( rv ) ), dl ( rv,rvc,nom0->_rv() ) { 50 50 if ( copy ) { 51 51 // nom = nom0->_copy_(); 52 it_error ("todo");52 it_error ( "todo" ); 53 53 destroynom=true; 54 54 } … … 61 61 }; 62 62 double evalcond ( const vec &val, const vec &cond ) { 63 vec nom_val (rv.count()+rvc.count());64 dl.fill_val_cond (nom_val,val,cond);63 vec nom_val ( rv.count() +rvc.count() ); 64 dl.fill_val_cond ( nom_val,val,cond ); 65 65 return exp ( nom->evalpdflog ( nom_val ) - den->evalpdflog ( cond ) ); 66 66 } … … 132 132 //! Auxiliary function for taking ownership of the Coms() 133 133 void ownComs() {destroyComs=true;} 134 134 135 135 //!access function 136 epdf* _Coms (int i){return Coms(i);}136 epdf* _Coms ( int i ) {return Coms ( i );} 137 137 }; 138 138 … … 175 175 // add logarithms 176 176 res += epdfs ( i )->evalpdflog ( dls ( i )->get_val ( val ) );*/ 177 res *= mpdfs ( i )->evalcond ( 178 179 dls ( i )->get_cond ( val, cond )180 177 res *= mpdfs ( i )->evalcond ( 178 dls ( i )->get_val ( val ), 179 dls ( i )->get_cond ( val, cond ) 180 ); 181 181 } 182 182 return res; … … 222 222 independent=rv.add ( epdfs ( i )->_rv() ); 223 223 it_assert_debug ( independent==true, "eprod:: given components are not independent ." ); 224 dls ( i ) = new datalink_e2e( epdfs ( i )->_rv() , rv ); 224 } 225 for ( int i=0;i<epdfs.length();i++ ) { 226 dls ( i ) = new datalink_e2e ( epdfs ( i )->_rv() , rv ); 225 227 } 226 228 } … … 230 232 for ( int i=0;i<epdfs.length();i++ ) { 231 233 vec pom = epdfs ( i )->mean(); 232 dls (i)->fill_val(tmp, pom );234 dls ( i )->fill_val ( tmp, pom ); 233 235 } 234 236 return tmp; … … 238 240 for ( int i=0;i<epdfs.length();i++ ) { 239 241 vec pom = epdfs ( i )->sample(); 240 dls (i)->fill_val(tmp, pom );242 dls ( i )->fill_val ( tmp, pom ); 241 243 } 242 244 return tmp; … … 245 247 double tmp=0; 246 248 for ( int i=0;i<epdfs.length();i++ ) { 247 tmp+=epdfs ( i )->evalpdflog ( dls (i)->get_val ( val ) );249 tmp+=epdfs ( i )->evalpdflog ( dls ( i )->get_val ( val ) ); 248 250 } 249 251 return tmp; … … 251 253 //!access function 252 254 const epdf* operator () ( int i ) const {it_assert_debug ( i<epdfs.length(),"wrong index" );return epdfs ( i );} 253 255 254 256 //!Destructor 255 ~eprod() {for(int i=0;i<epdfs.length();i++){delete dls(i);}}257 ~eprod() {for ( int i=0;i<epdfs.length();i++ ) {delete dls ( i );}} 256 258 }; 257 259 -
bdm/stat/libEF.h
r203 r204 48 48 virtual double evalpdflog_nn ( const vec &val ) const{it_error ( "Not implemented" );return 0.0;}; 49 49 //!Evaluate normalized log-probability 50 virtual double evalpdflog ( const vec &val ) const { return evalpdflog_nn ( val )-lognc();}50 virtual double evalpdflog ( const vec &val ) const {double tmp;tmp= evalpdflog_nn ( val )-lognc();it_assert_debug(std::isfinite(tmp),"why?"); return tmp;} 51 51 //!Evaluate normalized log-probability for many samples 52 52 virtual vec evalpdflog ( const mat &Val ) const { -
mpdm/CMakeLists.txt
r198 r204 20 20 target_link_libraries (merg_pred ${BdmLibs}) 21 21 22 add_executable (merg_giw merg_giw.cpp) 23 target_link_libraries (merg_giw ${BdmLibs}) 24 22 25 add_executable (merger_iter_cond merger_iter_cond.cpp) 23 26 target_link_libraries (merger_iter_cond ${BdmLibs}) -
mpdm/merg_pred.cpp
r198 r204 34 34 mat V0g = 0.001*eye ( thrg.count() ); V0g ( 0,0 ) *= 10; // 35 35 double nu0 = ord+6.0; 36 double frg = 1.0;//0.99;36 double frg = 0.95; 37 37 38 38 ARX P1 ( thri, V0, nu0, frg ); … … 40 40 ARX PG ( thrg, V0g, nu0, frg ); 41 41 //Test estimation 42 int ndat = 500;42 int ndat = 200; 43 43 int t; 44 44 … … 51 51 int Li_Data = L.add ( RV ( "{Y U1 U2 }" ), "" ); 52 52 int Li_LL = L.add ( RV ( "{1 2 G }" ), "LL" ); 53 int Li_Pred = L.add ( RV ( "{1 2 G ar ge ln}" ), "Pred" );53 int Li_Pred = L.add ( RV ( "{1 2 G ar ge }" ), "Pred" ); 54 54 55 55 L.init(); … … 66 66 vec PostLLs(3); 67 67 vec PredLLs_m=zeros(5); 68 vec PostLLs_m=zeros(3);69 68 ivec ind_r1 = "0 1 3"; 70 69 ivec ind_r2 = "0 2 3"; … … 84 83 // Test predictors 85 84 if (t>2){ 86 mlnorm<ldmat>* P1p = P1.predictor _student(y,concat(ym,u1));87 mlnorm<ldmat>* P2p = P2.predictor _student(y,concat(ym,u2));88 mlnorm<ldmat>* Pgp = PG.predictor _student(y,concat(ym,uu));85 mlnorm<ldmat>* P1p = P1.predictor(y,concat(ym,u1)); 86 mlnorm<ldmat>* P2p = P2.predictor(y,concat(ym,u2)); 87 mlnorm<ldmat>* Pgp = PG.predictor(y,concat(ym,uu)); 89 88 90 89 Array<mpdf*> A(2); A(0)=P1p;A(1)=P2p; 91 90 merger M(A); 92 91 enorm<ldmat> g0(concat(yy,uu)); g0.set_parameters("0 0 0 0 ",3*eye(4)); 93 M.set_parameters(1 .2, 100,1);92 M.set_parameters(10000000.0, 100,1); 94 93 M.merge(&g0); 95 94 … … 98 97 double P2pl = P2p->evalcond(yt,rgr2); 99 98 double PGpl = Pgp->evalcond(yt,rgrg); 100 PredLLs(0) = log(P1pl); PredLLs(1) = log(P2pl); PredLLs(2) = log(PGpl); 99 { 100 cout << "yt: " << yt << endl; 101 cout << "yt_1: " << P1p->_epdf().mean() << endl; 102 cout << "yt_2: " << P2p->_epdf().mean() << endl; 103 cout << "yt_G: " << P2p->_epdf().mean() << endl; 104 } 105 double cP1pl; 106 double cP2pl; 101 107 { 102 108 ARX* Apred = (ARX*)M._Mix()._Coms(0); … … 107 113 mlnorm<ldmat>* cP2p = (mlnorm<ldmat>*)mP2p->condition(y); 108 114 109 double cP1pl = cP1p->evalcond(yt,rgr1); 110 double cP2pl = cP2p->evalcond(yt,rgr2); 111 PredLLs(3) = log(cP1pl); 112 PredLLs(4) = log(cP2pl); 115 cP1pl = cP1p->evalcond(yt,rgr1); 116 cP2pl = cP2p->evalcond(yt,rgr2); 117 118 cout << "ytm1: " << cP1p->_epdf().mean() << endl; 119 cout << "ytm2: " << cP2p->_epdf().mean() << endl; 113 120 } 114 121 115 L.logit(Li_Pred, PredLLs_m*frg+ PredLLs); //log-normal116 PredLLs _m *=frg;117 PredLLs_m += PredLLs;122 PredLLs *=frg; 123 PredLLs += log(concat(vec_3(P1pl, P2pl, PGpl), vec_2(cP1pl,cP2pl))); 124 L.logit(Li_Pred, PredLLs); //log-normal 118 125 119 126 delete P1p;