- Main Page
- Related Pages
- Modules
- Classes
- Files

# merger.h

Go to the documentation of this file.

    00001
    00013 #ifndef MER_H
    00014 #define MER_H
    00015
    00016
    00017 #include "mixef.h"
    00018
    00019 namespace bdm{
    00020 using std::string;
    00021
    00028 class merger : public compositepdf, public epdf {
    00029 protected:
    00031         MixEF Mix;
    00033         Array<datalink_m2e*> dls;
    00035         Array<RV> rvzs;
    00037         Array<datalink_m2e*> zdls;
    00038
    00040         int Ns;
    00042         int Nc;
    00044         double beta;
    00046         eEmp eSmp;
    00047
    00048 public:
    00050         merger ( const Array<mpdf*> &S ) :
    00051                         compositepdf ( S ), epdf ( ),
    00052                         Mix ( Array<BMEF*> ( 0 ),vec ( 0 ) ), dls ( n ), rvzs ( n ), zdls ( n ), eSmp() {
    00053                 RV ztmp;
    00054                 // Extend rv by rvc!
    00055                 RV rvc; setrvc ( rv,rvc );
    00056                 rv.add ( rvc );
    00057                 for ( int i=0;i<n;i++ ) {
    00058                         //Establich connection between mpdfs and merger
    00059                         dls ( i ) = new datalink_m2e ( mpdfs ( i )->_rv(), mpdfs ( i )->_rvc(), rv );
    00060                         // find out what is missing in each mpdf
    00061                         ztmp= mpdfs ( i )->_rv();
    00062                         ztmp.add ( mpdfs ( i )->_rvc() );
    00063                         rvzs ( i ) =rv.subt ( ztmp );
    00064                         zdls ( i ) = new datalink_m2e ( rvzs ( i ), ztmp, rv ) ;
    00065                 };
    00066                 //Set Default values of parameters
    00067                 beta=2.0;
    00068                 Ns=100;
    00069                 Nc=10;
    00070                 Mix.set_method ( EM );
    00071         }
    00073         void set_parameters ( double beta0, int Ns0, int Nc0 ) {beta=beta0;Ns=Ns0;Nc=Nc0;eSmp.set_parameters(Ns0,false);}
    00075         void init() {
    00076                 Array<vec> Smps ( n );
    00077                 //Gibbs sampling
    00078                 for ( int i=0;i<n;i++ ) {Smps ( i ) =zeros ( 0 );}
    00079         }
    00081         void merge ( const epdf* g0 );
    00083         void merge () {merge ( & ( Mix.posterior() ) );};
    00084
    00086         vec lognorm_merge ( mat &lW );
    00089         vec sample ( ) const { return Mix.posterior().sample();}
    00090         double evallog ( const vec &dt ) const {
    00091                 vec dtf=ones ( dt.length() +1 );
    00092                 dtf.set_subvector ( 0,dt );
    00093                 return Mix.logpred ( dtf );
    00094         }
    00095         vec mean() const {
    00096                 const Vec<double> &w = eSmp._w();
    00097                 const Array<vec> &S = eSmp._samples();
    00098                 vec tmp=zeros ( dim);
    00099                 for ( int i=0; i<Ns; i++ ) {
    00100                         tmp+=w ( i ) *S ( i );
    00101                 }
    00102                 return tmp;
    00103         }
    00104         mat covariance() const {
    00105                 const vec &w = eSmp._w();
    00106                 const Array<vec> &S = eSmp._samples();
    00107
    00108                 vec mea = mean();
    00109
    00110                 cout << sum(w) << "," << w*w <<endl;
    00111
    00112                 mat Tmp=zeros(dim, dim);
    00113                 for ( int i=0; i<Ns; i++ ) {
    00114                         Tmp+=w ( i ) *outer_product(S ( i ), S(i));
    00115                 }
    00116                 return Tmp-outer_product(mea,mea);
    00117         }
    00118         vec variance() const {
    00119                 const vec &w = eSmp._w();
    00120                 const Array<vec> &S = eSmp._samples();
    00121
    00122                 vec tmp=zeros(dim);
    00123                 for ( int i=0; i<Ns; i++ ) {
    00124                         tmp+=w ( i ) *pow(S ( i ),2);
    00125                 }
    00126                 return tmp-pow(mean(),2);
    00127         }
    00129         virtual ~merger() {
    00130                 for ( int i=0; i<n; i++ ) {
    00131                         delete dls ( i );
    00132                         delete zdls ( i );
    00133                 }
    00134         };
    00135
    00137         MixEF& _Mix() {return Mix;}
    00139         eEmp& _Smp() {return eSmp;}
    00140 };
    00141
    00142 }
    00143
    00144 #endif // MER_H