| 10 | } |
| 11 | |
| 12 | void merger_base::set_sources ( const Array<shared_ptr<pdf> > &Sources ) { |
| 13 | pdfs = Sources; |
| 14 | Nsources = pdfs.length(); |
| 15 | //set sizes |
| 16 | dls.set_size ( Sources.length() ); |
| 17 | rvzs.set_size ( Sources.length() ); |
| 18 | zdls.set_size ( Sources.length() ); |
| 19 | |
| 20 | rv = get_composite_rv ( pdfs, /* checkoverlap = */ false ); |
| 21 | |
| 22 | RV rvc; |
| 23 | // Extend rv by rvc! |
| 24 | for ( int i = 0; i < pdfs.length(); i++ ) { |
| 25 | RV rvx = pdfs ( i )->_rvc().subt ( rv ); |
| 26 | rvc.add ( rvx ); // add rv to common rvc |
| 27 | } |
| 28 | |
| 29 | // join rv and rvc - see descriprion |
| 30 | rv.add ( rvc ); |
| 31 | // get dimension |
| 32 | dim = rv._dsize(); |
| 33 | |
| 34 | // create links between sources and common rv |
| 35 | RV xytmp; |
| 36 | for ( int i = 0; i < pdfs.length(); i++ ) { |
| 37 | //Establich connection between pdfs and merger |
| 38 | dls ( i ) = new datalink_m2e; |
| 39 | dls ( i )->set_connection ( pdfs ( i )->_rv(), pdfs ( i )->_rvc(), rv ); |
| 40 | |
| 41 | // find out what is missing in each pdf |
| 42 | xytmp = pdfs ( i )->_rv(); |
| 43 | xytmp.add ( pdfs ( i )->_rvc() ); |
| 44 | // z_i = common_rv-xy |
| 45 | rvzs ( i ) = rv.subt ( xytmp ); |
| 46 | //establish connection between extension (z_i|x,y)s and common rv |
| 47 | zdls ( i ) = new datalink_m2e; |
| 48 | zdls ( i )->set_connection ( rvzs ( i ), xytmp, rv ) ; |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | void merger_base::set_support ( rectangular_support &Sup ) { |
| 53 | Npoints = Sup.points(); |
| 54 | eSmp.set_parameters ( Npoints, false ); |
| 55 | Array<vec> &samples = eSmp._samples(); |
| 56 | eSmp._w() = ones ( Npoints ) / Npoints; //unifrom size of bins |
| 57 | //set samples |
| 58 | samples ( 0 ) = Sup.first_vec(); |
| 59 | for ( int j = 1; j < Npoints; j++ ) { |
| 60 | samples ( j ) = Sup.next_vec(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void merger_base::merge () { |
| 65 | validate(); |
| 66 | |
| 67 | //check if sources overlap: |
| 68 | bool OK = true; |
| 69 | for ( int i = 0; i < pdfs.length(); i++ ) { |
| 70 | OK &= ( rvzs ( i )._dsize() == 0 ); // z_i is empty |
| 71 | OK &= ( pdfs ( i )->_rvc()._dsize() == 0 ); // y_i is empty |
| 72 | } |
| 73 | |
| 74 | if ( OK ) { |
| 75 | mat lW = zeros ( pdfs.length(), eSmp._w().length() ); |
| 76 | |
| 77 | vec emptyvec ( 0 ); |
| 78 | for ( int i = 0; i < pdfs.length(); i++ ) { |
| 79 | for ( int j = 0; j < eSmp._w().length(); j++ ) { |
| 80 | lW ( i, j ) = pdfs ( i )->evallogcond ( eSmp._samples() ( j ), emptyvec ); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | vec w_nn = merge_points ( lW ); |
| 85 | vec wtmp = exp ( w_nn - max ( w_nn ) ); |
| 86 | //renormalize |
| 87 | eSmp._w() = wtmp / sum ( wtmp ); |
| 88 | } else { |
| 89 | bdm_error ( "Sources are not compatible - use merger_mix" ); |
| 90 | } |
| 145 | vec merger_base::mean() const { |
| 146 | const Vec<double> &w = eSmp._w(); |
| 147 | const Array<vec> &S = eSmp._samples(); |
| 148 | vec tmp = zeros ( dim ); |
| 149 | for ( int i = 0; i < Npoints; i++ ) { |
| 150 | tmp += w ( i ) * S ( i ); |
| 151 | } |
| 152 | return tmp; |
| 153 | } |
| 154 | |
| 155 | mat merger_base::covariance() const { |
| 156 | const vec &w = eSmp._w(); |
| 157 | const Array<vec> &S = eSmp._samples(); |
| 158 | |
| 159 | vec mea = mean(); |
| 160 | |
| 161 | // cout << sum (w) << "," << w*w << endl; |
| 162 | |
| 163 | mat Tmp = zeros ( dim, dim ); |
| 164 | for ( int i = 0; i < Npoints; i++ ) { |
| 165 | Tmp += w ( i ) * outer_product ( S ( i ), S ( i ) ); |
| 166 | } |
| 167 | return Tmp - outer_product ( mea, mea ); |
| 168 | } |
| 169 | |
| 170 | vec merger_base::variance() const { |
| 171 | const vec &w = eSmp._w(); |
| 172 | const Array<vec> &S = eSmp._samples(); |
| 173 | |
| 174 | vec tmp = zeros ( dim ); |
| 175 | for ( int i = 0; i < Nsources; i++ ) { |
| 176 | tmp += w ( i ) * pow ( S ( i ), 2 ); |
| 177 | } |
| 178 | return tmp - pow ( mean(), 2 ); |
| 179 | } |
| 180 | |