45 | | class merger_base : public compositepdf, public epdf |
46 | | { |
47 | | protected: |
48 | | //! Data link for each mpdf in mpdfs |
49 | | Array<datalink_m2e*> dls; |
50 | | //! Array of rvs that are not modelled by mpdfs at all, \f$ z_i \f$ |
51 | | Array<RV> rvzs; |
52 | | //! Data Links for extension \f$ f(z_i|x_i,y_i) \f$ |
53 | | Array<datalink_m2e*> zdls; |
54 | | //! number of support points |
55 | | int Npoints; |
56 | | //! number of sources |
57 | | int Nsources; |
58 | | |
59 | | //! switch of the methoh used for merging |
60 | | MERGER_METHOD METHOD; |
61 | | //! Default for METHOD |
62 | | static const MERGER_METHOD DFLT_METHOD; |
63 | | |
64 | | //!Prior on the log-normal merging model |
65 | | double beta; |
66 | | //! default for beta |
67 | | static const double DFLT_beta; |
68 | | |
69 | | //! Projection to empirical density (could also be piece-wise linear) |
70 | | eEmp eSmp; |
71 | | |
72 | | //! debug or not debug |
73 | | bool DBG; |
74 | | |
75 | | //! debugging file |
76 | | it_file* dbg_file; |
77 | | public: |
78 | | //! \name Constructors |
79 | | //! @{ |
80 | | |
81 | | //!Empty constructor |
82 | | merger_base () : compositepdf() {DBG = false;dbg_file = NULL;} |
83 | | |
84 | | //!Constructor from sources |
85 | | merger_base (const Array<mpdf*> &S, bool own=false); |
86 | | |
87 | | //! Function setting the main internal structures |
88 | | void set_sources (const Array<mpdf*> &Sources, bool own) { |
89 | | compositepdf::set_elements (Sources,own); |
90 | | Nsources=mpdfs.length(); |
91 | | //set sizes |
92 | | dls.set_size (Sources.length()); |
93 | | rvzs.set_size (Sources.length()); |
94 | | zdls.set_size (Sources.length()); |
95 | | |
96 | | rv = getrv (/* checkoverlap = */ false); |
97 | | RV rvc; setrvc (rv, rvc); // Extend rv by rvc! |
98 | | // join rv and rvc - see descriprion |
99 | | rv.add (rvc); |
100 | | // get dimension |
101 | | dim = rv._dsize(); |
102 | | |
103 | | // create links between sources and common rv |
104 | | RV xytmp; |
105 | | for (int i = 0;i < mpdfs.length();i++) { |
106 | | //Establich connection between mpdfs and merger |
107 | | dls (i) = new datalink_m2e; |
108 | | dls (i)->set_connection (mpdfs (i)->_rv(), mpdfs (i)->_rvc(), rv); |
109 | | |
110 | | // find out what is missing in each mpdf |
111 | | xytmp = mpdfs (i)->_rv(); |
112 | | xytmp.add (mpdfs (i)->_rvc()); |
113 | | // z_i = common_rv-xy |
114 | | rvzs (i) = rv.subt (xytmp); |
115 | | //establish connection between extension (z_i|x,y)s and common rv |
116 | | zdls (i) = new datalink_m2e; zdls (i)->set_connection (rvzs (i), xytmp, rv) ; |
117 | | }; |
118 | | } |
119 | | //! Rectangular support each vector of XYZ specifies (begining-end) interval for each dimension. Same number of points, \c dimsize, in each dimension. |
120 | | void set_support (const Array<vec> &XYZ, const int dimsize) { |
121 | | set_support(XYZ,dimsize*ones_i(XYZ.length())); |
122 | | } |
123 | | //! Rectangular support each vector of XYZ specifies (begining-end) interval for each dimension. \c gridsize specifies number of points is each dimension. |
124 | | void set_support (const Array<vec> &XYZ, const ivec &gridsize) { |
125 | | int dim = XYZ.length(); //check with internal dim!! |
126 | | Npoints = prod (gridsize); |
127 | | eSmp.set_parameters (Npoints, false); |
128 | | Array<vec> &samples = eSmp._samples(); |
129 | | eSmp._w() = ones (Npoints) / Npoints; //unifrom size of bins |
130 | | //set samples |
131 | | ivec ind = zeros_i (dim); //indeces of dimensions in for cycle; |
132 | | vec smpi (dim); // ith sample |
133 | | vec steps =zeros(dim); // ith sample |
134 | | // first corner |
135 | | for (int j = 0; j < dim; j++) { |
136 | | smpi (j) = XYZ (j) (0); /* beginning of the interval*/ |
137 | | it_assert(gridsize(j)!=0.0,"Zeros in gridsize!"); |
138 | | steps (j) = ( XYZ(j)(1)-smpi(j) )/gridsize(j); |
139 | | } |
140 | | // fill samples |
141 | | for (int i = 0; i < Npoints; i++) { |
142 | | // copy |
143 | | samples(i) = smpi; |
144 | | // go through all dimensions |
145 | | for (int j = 0;j < dim;j++) { |
146 | | if (ind (j) == gridsize (j) - 1) { //j-th index is full |
147 | | ind (j) = 0; //shift back |
148 | | smpi(j) = XYZ(j)(0); |
149 | | |
150 | | if (i<Npoints-1) { |
151 | | ind (j + 1) ++; //increase the next dimension; |
152 | | smpi(j+1) += steps(j+1); |
153 | | break; |
154 | | } |
155 | | |
156 | | } else { |
157 | | ind (j) ++; |
158 | | smpi(j) +=steps(j); |
| 44 | class merger_base : public compositepdf, public epdf { |
| 45 | protected: |
| 46 | //! Data link for each mpdf in mpdfs |
| 47 | Array<datalink_m2e*> dls; |
| 48 | //! Array of rvs that are not modelled by mpdfs at all, \f$ z_i \f$ |
| 49 | Array<RV> rvzs; |
| 50 | //! Data Links for extension \f$ f(z_i|x_i,y_i) \f$ |
| 51 | Array<datalink_m2e*> zdls; |
| 52 | //! number of support points |
| 53 | int Npoints; |
| 54 | //! number of sources |
| 55 | int Nsources; |
| 56 | |
| 57 | //! switch of the methoh used for merging |
| 58 | MERGER_METHOD METHOD; |
| 59 | //! Default for METHOD |
| 60 | static const MERGER_METHOD DFLT_METHOD; |
| 61 | |
| 62 | //!Prior on the log-normal merging model |
| 63 | double beta; |
| 64 | //! default for beta |
| 65 | static const double DFLT_beta; |
| 66 | |
| 67 | //! Projection to empirical density (could also be piece-wise linear) |
| 68 | eEmp eSmp; |
| 69 | |
| 70 | //! debug or not debug |
| 71 | bool DBG; |
| 72 | |
| 73 | //! debugging file |
| 74 | it_file* dbg_file; |
| 75 | public: |
| 76 | //! \name Constructors |
| 77 | //! @{ |
| 78 | |
| 79 | //!Empty constructor |
| 80 | merger_base () : compositepdf() { |
| 81 | DBG = false; |
| 82 | dbg_file = NULL; |
| 83 | } |
| 84 | |
| 85 | //!Constructor from sources |
| 86 | merger_base ( const Array<mpdf*> &S, bool own = false ); |
| 87 | |
| 88 | //! Function setting the main internal structures |
| 89 | void set_sources ( const Array<mpdf*> &Sources, bool own ) { |
| 90 | compositepdf::set_elements ( Sources, own ); |
| 91 | Nsources = mpdfs.length(); |
| 92 | //set sizes |
| 93 | dls.set_size ( Sources.length() ); |
| 94 | rvzs.set_size ( Sources.length() ); |
| 95 | zdls.set_size ( Sources.length() ); |
| 96 | |
| 97 | rv = getrv ( /* checkoverlap = */ false ); |
| 98 | RV rvc; |
| 99 | setrvc ( rv, rvc ); // Extend rv by rvc! |
| 100 | // join rv and rvc - see descriprion |
| 101 | rv.add ( rvc ); |
| 102 | // get dimension |
| 103 | dim = rv._dsize(); |
| 104 | |
| 105 | // create links between sources and common rv |
| 106 | RV xytmp; |
| 107 | for ( int i = 0; i < mpdfs.length(); i++ ) { |
| 108 | //Establich connection between mpdfs and merger |
| 109 | dls ( i ) = new datalink_m2e; |
| 110 | dls ( i )->set_connection ( mpdfs ( i )->_rv(), mpdfs ( i )->_rvc(), rv ); |
| 111 | |
| 112 | // find out what is missing in each mpdf |
| 113 | xytmp = mpdfs ( i )->_rv(); |
| 114 | xytmp.add ( mpdfs ( i )->_rvc() ); |
| 115 | // z_i = common_rv-xy |
| 116 | rvzs ( i ) = rv.subt ( xytmp ); |
| 117 | //establish connection between extension (z_i|x,y)s and common rv |
| 118 | zdls ( i ) = new datalink_m2e; |
| 119 | zdls ( i )->set_connection ( rvzs ( i ), xytmp, rv ) ; |
| 120 | }; |
| 121 | } |
| 122 | //! Rectangular support each vector of XYZ specifies (begining-end) interval for each dimension. Same number of points, \c dimsize, in each dimension. |
| 123 | void set_support ( const Array<vec> &XYZ, const int dimsize ) { |
| 124 | set_support ( XYZ, dimsize*ones_i ( XYZ.length() ) ); |
| 125 | } |
| 126 | //! Rectangular support each vector of XYZ specifies (begining-end) interval for each dimension. \c gridsize specifies number of points is each dimension. |
| 127 | void set_support ( const Array<vec> &XYZ, const ivec &gridsize ) { |
| 128 | int dim = XYZ.length(); //check with internal dim!! |
| 129 | Npoints = prod ( gridsize ); |
| 130 | eSmp.set_parameters ( Npoints, false ); |
| 131 | Array<vec> &samples = eSmp._samples(); |
| 132 | eSmp._w() = ones ( Npoints ) / Npoints; //unifrom size of bins |
| 133 | //set samples |
| 134 | ivec ind = zeros_i ( dim ); //indeces of dimensions in for cycle; |
| 135 | vec smpi ( dim ); // ith sample |
| 136 | vec steps = zeros ( dim ); // ith sample |
| 137 | // first corner |
| 138 | for ( int j = 0; j < dim; j++ ) { |
| 139 | smpi ( j ) = XYZ ( j ) ( 0 ); /* beginning of the interval*/ |
| 140 | it_assert ( gridsize ( j ) != 0.0, "Zeros in gridsize!" ); |
| 141 | steps ( j ) = ( XYZ ( j ) ( 1 ) - smpi ( j ) ) / gridsize ( j ); |
| 142 | } |
| 143 | // fill samples |
| 144 | for ( int i = 0; i < Npoints; i++ ) { |
| 145 | // copy |
| 146 | samples ( i ) = smpi; |
| 147 | // go through all dimensions |
| 148 | for ( int j = 0; j < dim; j++ ) { |
| 149 | if ( ind ( j ) == gridsize ( j ) - 1 ) { //j-th index is full |
| 150 | ind ( j ) = 0; //shift back |
| 151 | smpi ( j ) = XYZ ( j ) ( 0 ); |
| 152 | |
| 153 | if ( i < Npoints - 1 ) { |
| 154 | ind ( j + 1 ) ++; //increase the next dimension; |
| 155 | smpi ( j + 1 ) += steps ( j + 1 ); |
164 | | //! set debug file |
165 | | void set_debug_file (const string fname) { |
166 | | if (DBG) delete dbg_file; |
167 | | dbg_file = new it_file (fname); |
168 | | if (dbg_file) DBG = true; |
169 | | } |
170 | | //! Set internal parameters used in approximation |
171 | | void set_method (MERGER_METHOD MTH=DFLT_METHOD, double beta0 = DFLT_beta) { |
172 | | METHOD = MTH; |
173 | | beta = beta0; |
174 | | } |
175 | | //! Set support points from a pdf by drawing N samples |
176 | | void set_support (const epdf &overall, int N) { |
177 | | eSmp.set_statistics (&overall, N); |
178 | | Npoints = N; |
179 | | } |
180 | | |
181 | | //! Destructor |
182 | | virtual ~merger_base() { |
183 | | for (int i = 0; i < Nsources; i++) { |
184 | | delete dls (i); |
185 | | delete zdls (i); |
186 | | } |
187 | | if (DBG) delete dbg_file; |
188 | | }; |
189 | | //!@} |
190 | | |
191 | | //! \name Mathematical operations |
192 | | //!@{ |
193 | | |
194 | | //!Merge given sources in given points |
195 | | virtual void merge () { |
196 | | validate(); |
197 | | |
198 | | //check if sources overlap: |
199 | | bool OK = true; |
200 | | for (int i = 0;i < mpdfs.length(); i++) { |
201 | | OK &= (rvzs (i)._dsize() == 0); // z_i is empty |
202 | | OK &= (mpdfs (i)->_rvc()._dsize() == 0); // y_i is empty |
203 | | } |
204 | | |
205 | | if (OK) { |
206 | | mat lW = zeros (mpdfs.length(), eSmp._w().length()); |
207 | | |
208 | | vec emptyvec (0); |
209 | | for (int i = 0; i < mpdfs.length(); i++) { |
210 | | for (int j = 0; j < eSmp._w().length(); j++) { |
211 | | lW (i, j) = mpdfs (i)->evallogcond (eSmp._samples() (j), emptyvec); |
212 | | } |
213 | | } |
214 | | |
215 | | vec w_nn=merge_points (lW); |
216 | | vec wtmp = exp (w_nn-max(w_nn)); |
217 | | //renormalize |
218 | | eSmp._w() = wtmp / sum (wtmp); |
219 | | } else { |
220 | | it_error ("Sources are not compatible - use merger_mix"); |
221 | | } |
222 | | }; |
223 | | |
224 | | |
225 | | //! Merge log-likelihood values in points using method specified by parameter METHOD |
226 | | vec merge_points (mat &lW); |
227 | | |
228 | | |
229 | | //! sample from merged density |
230 | | //! weight w is a |
231 | | vec mean() const { |
232 | | const Vec<double> &w = eSmp._w(); |
233 | | const Array<vec> &S = eSmp._samples(); |
234 | | vec tmp = zeros (dim); |
235 | | for (int i = 0; i < Npoints; i++) { |
236 | | tmp += w (i) * S (i); |
237 | | } |
238 | | return tmp; |
239 | | } |
240 | | mat covariance() const { |
241 | | const vec &w = eSmp._w(); |
242 | | const Array<vec> &S = eSmp._samples(); |
243 | | |
244 | | vec mea = mean(); |
245 | | |
246 | | // cout << sum (w) << "," << w*w << endl; |
247 | | |
248 | | mat Tmp = zeros (dim, dim); |
249 | | for (int i = 0; i < Npoints; i++) { |
250 | | Tmp += w (i) * outer_product (S (i), S (i)); |
251 | | } |
252 | | return Tmp -outer_product (mea, mea); |
253 | | } |
254 | | vec variance() const { |
255 | | const vec &w = eSmp._w(); |
256 | | const Array<vec> &S = eSmp._samples(); |
257 | | |
258 | | vec tmp = zeros (dim); |
259 | | for (int i = 0; i < Nsources; i++) { |
260 | | tmp += w (i) * pow (S (i), 2); |
261 | | } |
262 | | return tmp -pow (mean(), 2); |
263 | | } |
264 | | //!@} |
265 | | |
266 | | //! \name Access to attributes |
267 | | //! @{ |
268 | | |
269 | | //! Access function |
270 | | eEmp& _Smp() {return eSmp;} |
271 | | |
272 | | //! load from setting |
273 | | void from_setting (const Setting& set) { |
274 | | // get support |
275 | | // find which method to use |
276 | | string meth_str; |
277 | | UI::get<string> (meth_str, set, "method", UI::compulsory); |
278 | | if (!strcmp (meth_str.c_str(), "arithmetic")) |
279 | | set_method (ARITHMETIC); |
280 | | else { |
281 | | if (!strcmp (meth_str.c_str(), "geometric")) |
282 | | set_method (GEOMETRIC); |
283 | | else if (!strcmp (meth_str.c_str(), "lognormal")) { |
284 | | set_method (LOGNORMAL); |
285 | | set.lookupValue( "beta",beta); |
| 166 | } |
| 167 | //! set debug file |
| 168 | void set_debug_file ( const string fname ) { |
| 169 | if ( DBG ) delete dbg_file; |
| 170 | dbg_file = new it_file ( fname ); |
| 171 | if ( dbg_file ) DBG = true; |
| 172 | } |
| 173 | //! Set internal parameters used in approximation |
| 174 | void set_method ( MERGER_METHOD MTH = DFLT_METHOD, double beta0 = DFLT_beta ) { |
| 175 | METHOD = MTH; |
| 176 | beta = beta0; |
| 177 | } |
| 178 | //! Set support points from a pdf by drawing N samples |
| 179 | void set_support ( const epdf &overall, int N ) { |
| 180 | eSmp.set_statistics ( &overall, N ); |
| 181 | Npoints = N; |
| 182 | } |
| 183 | |
| 184 | //! Destructor |
| 185 | virtual ~merger_base() { |
| 186 | for ( int i = 0; i < Nsources; i++ ) { |
| 187 | delete dls ( i ); |
| 188 | delete zdls ( i ); |
| 189 | } |
| 190 | if ( DBG ) delete dbg_file; |
| 191 | }; |
| 192 | //!@} |
| 193 | |
| 194 | //! \name Mathematical operations |
| 195 | //!@{ |
| 196 | |
| 197 | //!Merge given sources in given points |
| 198 | virtual void merge () { |
| 199 | validate(); |
| 200 | |
| 201 | //check if sources overlap: |
| 202 | bool OK = true; |
| 203 | for ( int i = 0; i < mpdfs.length(); i++ ) { |
| 204 | OK &= ( rvzs ( i )._dsize() == 0 ); // z_i is empty |
| 205 | OK &= ( mpdfs ( i )->_rvc()._dsize() == 0 ); // y_i is empty |
| 206 | } |
| 207 | |
| 208 | if ( OK ) { |
| 209 | mat lW = zeros ( mpdfs.length(), eSmp._w().length() ); |
| 210 | |
| 211 | vec emptyvec ( 0 ); |
| 212 | for ( int i = 0; i < mpdfs.length(); i++ ) { |
| 213 | for ( int j = 0; j < eSmp._w().length(); j++ ) { |
| 214 | lW ( i, j ) = mpdfs ( i )->evallogcond ( eSmp._samples() ( j ), emptyvec ); |
288 | | string dbg_file; |
289 | | if (UI::get(dbg_file, set, "dbg_file")) |
290 | | set_debug_file(dbg_file); |
291 | | //validate() - not used |
292 | | } |
293 | | |
294 | | void validate() { |
295 | | it_assert (eSmp._w().length() > 0, "Empty support, use set_support()."); |
296 | | it_assert (dim == eSmp._samples() (0).length(), "Support points and rv are not compatible!"); |
297 | | it_assert (isnamed(),"mergers must be named"); |
298 | | } |
299 | | //!@} |
| 217 | |
| 218 | vec w_nn = merge_points ( lW ); |
| 219 | vec wtmp = exp ( w_nn - max ( w_nn ) ); |
| 220 | //renormalize |
| 221 | eSmp._w() = wtmp / sum ( wtmp ); |
| 222 | } else { |
| 223 | it_error ( "Sources are not compatible - use merger_mix" ); |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | |
| 228 | //! Merge log-likelihood values in points using method specified by parameter METHOD |
| 229 | vec merge_points ( mat &lW ); |
| 230 | |
| 231 | |
| 232 | //! sample from merged density |
| 233 | //! weight w is a |
| 234 | vec mean() const { |
| 235 | const Vec<double> &w = eSmp._w(); |
| 236 | const Array<vec> &S = eSmp._samples(); |
| 237 | vec tmp = zeros ( dim ); |
| 238 | for ( int i = 0; i < Npoints; i++ ) { |
| 239 | tmp += w ( i ) * S ( i ); |
| 240 | } |
| 241 | return tmp; |
| 242 | } |
| 243 | mat covariance() const { |
| 244 | const vec &w = eSmp._w(); |
| 245 | const Array<vec> &S = eSmp._samples(); |
| 246 | |
| 247 | vec mea = mean(); |
| 248 | |
| 249 | // cout << sum (w) << "," << w*w << endl; |
| 250 | |
| 251 | mat Tmp = zeros ( dim, dim ); |
| 252 | for ( int i = 0; i < Npoints; i++ ) { |
| 253 | Tmp += w ( i ) * outer_product ( S ( i ), S ( i ) ); |
| 254 | } |
| 255 | return Tmp - outer_product ( mea, mea ); |
| 256 | } |
| 257 | vec variance() const { |
| 258 | const vec &w = eSmp._w(); |
| 259 | const Array<vec> &S = eSmp._samples(); |
| 260 | |
| 261 | vec tmp = zeros ( dim ); |
| 262 | for ( int i = 0; i < Nsources; i++ ) { |
| 263 | tmp += w ( i ) * pow ( S ( i ), 2 ); |
| 264 | } |
| 265 | return tmp - pow ( mean(), 2 ); |
| 266 | } |
| 267 | //!@} |
| 268 | |
| 269 | //! \name Access to attributes |
| 270 | //! @{ |
| 271 | |
| 272 | //! Access function |
| 273 | eEmp& _Smp() { |
| 274 | return eSmp; |
| 275 | } |
| 276 | |
| 277 | //! load from setting |
| 278 | void from_setting ( const Setting& set ) { |
| 279 | // get support |
| 280 | // find which method to use |
| 281 | string meth_str; |
| 282 | UI::get<string> ( meth_str, set, "method", UI::compulsory ); |
| 283 | if ( !strcmp ( meth_str.c_str(), "arithmetic" ) ) |
| 284 | set_method ( ARITHMETIC ); |
| 285 | else { |
| 286 | if ( !strcmp ( meth_str.c_str(), "geometric" ) ) |
| 287 | set_method ( GEOMETRIC ); |
| 288 | else if ( !strcmp ( meth_str.c_str(), "lognormal" ) ) { |
| 289 | set_method ( LOGNORMAL ); |
| 290 | set.lookupValue ( "beta", beta ); |
| 291 | } |
| 292 | } |
| 293 | string dbg_file; |
| 294 | if ( UI::get ( dbg_file, set, "dbg_file" ) ) |
| 295 | set_debug_file ( dbg_file ); |
| 296 | //validate() - not used |
| 297 | } |
| 298 | |
| 299 | void validate() { |
| 300 | it_assert ( eSmp._w().length() > 0, "Empty support, use set_support()." ); |
| 301 | it_assert ( dim == eSmp._samples() ( 0 ).length(), "Support points and rv are not compatible!" ); |
| 302 | it_assert ( isnamed(), "mergers must be named" ); |
| 303 | } |
| 304 | //!@} |
301 | | UIREGISTER(merger_base); |
302 | | |
303 | | class merger_mix : public merger_base |
304 | | { |
305 | | protected: |
306 | | //!Internal mixture of EF models |
307 | | MixEF Mix; |
308 | | //!Number of components in a mixture |
309 | | int Ncoms; |
310 | | //! coefficient of resampling [0,1] |
311 | | double effss_coef; |
312 | | //! stop after niter iterations |
313 | | int stop_niter; |
314 | | |
315 | | //! default value for Ncoms |
316 | | static const int DFLT_Ncoms; |
317 | | //! default value for efss_coef; |
318 | | static const double DFLT_effss_coef; |
319 | | |
320 | | public: |
321 | | //!\name Constructors |
322 | | //!@{ |
323 | | merger_mix () {}; |
324 | | merger_mix (const Array<mpdf*> &S,bool own=false) {set_sources (S,own);}; |
325 | | //! Set sources and prepare all internal structures |
326 | | void set_sources (const Array<mpdf*> &S, bool own) { |
327 | | merger_base::set_sources (S,own); |
328 | | Nsources = S.length(); |
329 | | } |
330 | | //! Set internal parameters used in approximation |
331 | | void set_parameters (int Ncoms0 = DFLT_Ncoms, double effss_coef0 = DFLT_effss_coef) { |
332 | | Ncoms = Ncoms0; |
333 | | effss_coef = effss_coef0; |
334 | | } |
335 | | //!@} |
336 | | |
337 | | //! \name Mathematical operations |
338 | | //!@{ |
339 | | |
340 | | //!Merge values using mixture approximation |
341 | | void merge (); |
342 | | |
343 | | //! sample from the approximating mixture |
344 | | vec sample () const { return Mix.posterior().sample();} |
345 | | //! loglikelihood computed on mixture models |
346 | | double evallog (const vec &dt) const { |
347 | | vec dtf = ones (dt.length() + 1); |
348 | | dtf.set_subvector (0, dt); |
349 | | return Mix.logpred (dtf); |
350 | | } |
351 | | //!@} |
352 | | |
353 | | //!\name Access functions |
354 | | //!@{ |
| 306 | UIREGISTER ( merger_base ); |
| 307 | |
| 308 | class merger_mix : public merger_base { |
| 309 | protected: |
| 310 | //!Internal mixture of EF models |
| 311 | MixEF Mix; |
| 312 | //!Number of components in a mixture |
| 313 | int Ncoms; |
| 314 | //! coefficient of resampling [0,1] |
| 315 | double effss_coef; |
| 316 | //! stop after niter iterations |
| 317 | int stop_niter; |
| 318 | |
| 319 | //! default value for Ncoms |
| 320 | static const int DFLT_Ncoms; |
| 321 | //! default value for efss_coef; |
| 322 | static const double DFLT_effss_coef; |
| 323 | |
| 324 | public: |
| 325 | //!\name Constructors |
| 326 | //!@{ |
| 327 | merger_mix () {}; |
| 328 | merger_mix ( const Array<mpdf*> &S, bool own = false ) { |
| 329 | set_sources ( S, own ); |
| 330 | }; |
| 331 | //! Set sources and prepare all internal structures |
| 332 | void set_sources ( const Array<mpdf*> &S, bool own ) { |
| 333 | merger_base::set_sources ( S, own ); |
| 334 | Nsources = S.length(); |
| 335 | } |
| 336 | //! Set internal parameters used in approximation |
| 337 | void set_parameters ( int Ncoms0 = DFLT_Ncoms, double effss_coef0 = DFLT_effss_coef ) { |
| 338 | Ncoms = Ncoms0; |
| 339 | effss_coef = effss_coef0; |
| 340 | } |
| 341 | //!@} |
| 342 | |
| 343 | //! \name Mathematical operations |
| 344 | //!@{ |
| 345 | |
| 346 | //!Merge values using mixture approximation |
| 347 | void merge (); |
| 348 | |
| 349 | //! sample from the approximating mixture |
| 350 | vec sample () const { |
| 351 | return Mix.posterior().sample(); |
| 352 | } |
| 353 | //! loglikelihood computed on mixture models |
| 354 | double evallog ( const vec &dt ) const { |
| 355 | vec dtf = ones ( dt.length() + 1 ); |
| 356 | dtf.set_subvector ( 0, dt ); |
| 357 | return Mix.logpred ( dtf ); |
| 358 | } |
| 359 | //!@} |
| 360 | |
| 361 | //!\name Access functions |
| 362 | //!@{ |