95 | | void set_sources ( const Array<shared_ptr<pdf> > &Sources ) { |
96 | | pdfs = Sources; |
97 | | Nsources = pdfs.length(); |
98 | | //set sizes |
99 | | dls.set_size ( Sources.length() ); |
100 | | rvzs.set_size ( Sources.length() ); |
101 | | zdls.set_size ( Sources.length() ); |
102 | | |
103 | | rv = get_composite_rv ( pdfs, /* checkoverlap = */ false ); |
104 | | |
105 | | RV rvc; |
106 | | // Extend rv by rvc! |
107 | | for ( int i = 0; i < pdfs.length(); i++ ) { |
108 | | RV rvx = pdfs ( i )->_rvc().subt ( rv ); |
109 | | rvc.add ( rvx ); // add rv to common rvc |
110 | | } |
111 | | |
112 | | // join rv and rvc - see descriprion |
113 | | rv.add ( rvc ); |
114 | | // get dimension |
115 | | dim = rv._dsize(); |
116 | | |
117 | | // create links between sources and common rv |
118 | | RV xytmp; |
119 | | for ( int i = 0; i < pdfs.length(); i++ ) { |
120 | | //Establich connection between pdfs and merger |
121 | | dls ( i ) = new datalink_m2e; |
122 | | dls ( i )->set_connection ( pdfs ( i )->_rv(), pdfs ( i )->_rvc(), rv ); |
123 | | |
124 | | // find out what is missing in each pdf |
125 | | xytmp = pdfs ( i )->_rv(); |
126 | | xytmp.add ( pdfs ( i )->_rvc() ); |
127 | | // z_i = common_rv-xy |
128 | | rvzs ( i ) = rv.subt ( xytmp ); |
129 | | //establish connection between extension (z_i|x,y)s and common rv |
130 | | zdls ( i ) = new datalink_m2e; |
131 | | zdls ( i )->set_connection ( rvzs ( i ), xytmp, rv ) ; |
132 | | }; |
133 | | } |
| 95 | void set_sources ( const Array<shared_ptr<pdf> > &Sources ); |
| 96 | |
135 | | void set_support ( rectangular_support &Sup ) { |
136 | | Npoints = Sup.points(); |
137 | | eSmp.set_parameters ( Npoints, false ); |
138 | | Array<vec> &samples = eSmp._samples(); |
139 | | eSmp._w() = ones ( Npoints ) / Npoints; //unifrom size of bins |
140 | | //set samples |
141 | | samples ( 0 ) = Sup.first_vec(); |
142 | | for ( int j = 1; j < Npoints; j++ ) { |
143 | | samples ( j ) = Sup.next_vec(); |
144 | | } |
145 | | } |
| 98 | void set_support ( rectangular_support &Sup ); |
| 99 | |
182 | | virtual void merge () { |
183 | | validate(); |
184 | | |
185 | | //check if sources overlap: |
186 | | bool OK = true; |
187 | | for ( int i = 0; i < pdfs.length(); i++ ) { |
188 | | OK &= ( rvzs ( i )._dsize() == 0 ); // z_i is empty |
189 | | OK &= ( pdfs ( i )->_rvc()._dsize() == 0 ); // y_i is empty |
190 | | } |
191 | | |
192 | | if ( OK ) { |
193 | | mat lW = zeros ( pdfs.length(), eSmp._w().length() ); |
194 | | |
195 | | vec emptyvec ( 0 ); |
196 | | for ( int i = 0; i < pdfs.length(); i++ ) { |
197 | | for ( int j = 0; j < eSmp._w().length(); j++ ) { |
198 | | lW ( i, j ) = pdfs ( i )->evallogcond ( eSmp._samples() ( j ), emptyvec ); |
199 | | } |
200 | | } |
201 | | |
202 | | vec w_nn = merge_points ( lW ); |
203 | | vec wtmp = exp ( w_nn - max ( w_nn ) ); |
204 | | //renormalize |
205 | | eSmp._w() = wtmp / sum ( wtmp ); |
206 | | } else { |
207 | | bdm_error ( "Sources are not compatible - use merger_mix" ); |
208 | | } |
209 | | }; |
210 | | |
| 136 | virtual void merge (); |
218 | | vec mean() const { |
219 | | const Vec<double> &w = eSmp._w(); |
220 | | const Array<vec> &S = eSmp._samples(); |
221 | | vec tmp = zeros ( dim ); |
222 | | for ( int i = 0; i < Npoints; i++ ) { |
223 | | tmp += w ( i ) * S ( i ); |
224 | | } |
225 | | return tmp; |
226 | | } |
227 | | mat covariance() const { |
228 | | const vec &w = eSmp._w(); |
229 | | const Array<vec> &S = eSmp._samples(); |
230 | | |
231 | | vec mea = mean(); |
232 | | |
233 | | // cout << sum (w) << "," << w*w << endl; |
234 | | |
235 | | mat Tmp = zeros ( dim, dim ); |
236 | | for ( int i = 0; i < Npoints; i++ ) { |
237 | | Tmp += w ( i ) * outer_product ( S ( i ), S ( i ) ); |
238 | | } |
239 | | return Tmp - outer_product ( mea, mea ); |
240 | | } |
241 | | vec variance() const { |
242 | | const vec &w = eSmp._w(); |
243 | | const Array<vec> &S = eSmp._samples(); |
244 | | |
245 | | vec tmp = zeros ( dim ); |
246 | | for ( int i = 0; i < Nsources; i++ ) { |
247 | | tmp += w ( i ) * pow ( S ( i ), 2 ); |
248 | | } |
249 | | return tmp - pow ( mean(), 2 ); |
250 | | } |
| 144 | vec mean() const; |
| 145 | |
| 146 | mat covariance() const; |
| 147 | |
| 148 | vec variance() const; |
| 149 | |