Changeset 1068
- Timestamp:
- 06/09/10 18:35:14 (15 years ago)
- Location:
- library/bdm
- Files:
-
- 8 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.h
r1066 r1068 694 694 dimc = d; 695 695 } 696 //! Load from structure with elements: 697 //! \code 698 //! { class = "pdf_offspring", 699 //! rv = {class="RV", names=(...),}; // RV describing meaning of random variable 700 //! rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition 701 //! // elements of offsprings 702 //! } 703 //! \endcode 704 //!@} 696 697 /*! Create object from the following structure 698 \code 699 class = 'pdf'; 700 --- optional fields --- 701 rv = RV({'names',...},[sizes,...],[times,...]); % description of the random variable - typically delayed values, time=-1, etc.! 702 rvc= RV({'names',...},[sizes,...],[times,...]); % description of the random variable in condition 703 --- inherited fields --- 704 bdm::root::from_setting 705 \endcode 706 */ 707 //! @} 705 708 void from_setting ( const Setting &set ); 706 709 -
library/bdm/estim/particles.h
r1066 r1068 28 28 shared_ptr<BM> bm; 29 29 30 // custom posterior - product of empirical and exact part30 //! \brief Internal class for custom posterior - product of empirical and exact part 31 31 class eprod_2:public eprod_base { 32 32 protected: -
library/bdm/stat/emix.cpp
r1064 r1068 133 133 134 134 135 void 135 void emix::validate () { 136 136 emix_base::validate(); 137 137 dim = Coms ( 0 )->dimension(); … … 143 143 double res = 0.0; 144 144 for ( i = pdfs.length() - 1; i >= 0; i-- ) { 145 /* 146 147 148 149 145 /* if ( pdfs(i)->_rvc().count() >0) { 146 pdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) ); 147 } 148 // add logarithms 149 res += epdfs ( i )->evallog ( dls ( i )->pushdown ( val ) );*/ 150 150 res += pdfs ( i )->evallogcond ( 151 151 dls ( i )->pushdown ( val ), … … 198 198 set_elements ( temp_array ); 199 199 } 200 void 200 void mprod::to_setting (Setting &set) const { 201 201 pdf::to_setting(set); 202 202 UI::save( pdfs, set, "pdfs"); … … 243 243 } 244 244 245 void 245 void mmix::to_setting (Setting &set) const { 246 246 pdf::to_setting(set); 247 247 UI::save( Coms, set, "pdfs"); -
library/bdm/stat/emix.h
r1066 r1068 24 24 25 25 /*! \brief Class representing ratio of two densities 26 26 27 which arise e.g. by applying the Bayes rule. 27 28 It represents density in the form: … … 266 267 } 267 268 268 //! Load from structure with elements: 269 //! \code 270 //! { class='mprod'; 271 //! pdfs = (..., ...); // list of pdfs in the order of chain rule 272 //! } 269 //! Create object from the following structure 270 //! \code 271 //! class='mprod'; 272 //! pdfs = { list of bdm::pdf }; % list of bdm::pdf offsprings stored in the order of chain rule, bdm::pdf::from_setting 273 //! --- inherited fields --- 274 //! bdm::pdf::from_setting 273 275 //! \endcode 274 276 //!@} 275 277 void from_setting ( const Setting &set ) ; 276 void to_setting (Setting &set) const; 277 278 278 void to_setting (Setting &set) const; 279 279 }; 280 280 UIREGISTER ( mprod ); … … 370 370 UIREGISTER(eprod); 371 371 372 //! \brief Internal class similar to eprod - factors are external pointers. To be used onlyinternally!372 //! \brief Internal class similar to eprod - factors are external pointers. To be used internally! 373 373 class eprod_internal: public eprod_base { 374 374 protected: … … 409 409 vec samplecond ( const vec &cond ); 410 410 411 //! Load from structure with elements:412 //! \code413 //! { class='mmix';414 //! pdfs = (..., ...); // list of pdfs in the mixture415 //! weights = ( 0.5, 0.5 ); // weights of pdfs in the mixture416 411 //! } 417 412 //! \endcode 418 413 //!@} 414 415 /*! Create object from the following structure 416 \code 417 class = 'mmix'; 418 pdfs = { list of components bdm::pdf }; % list of pdf offsprings, bdm::pdf::from_setting 419 --- optional fields --- 420 weights = [...]; % weights of pdfs in the mixture 421 --- inherited fields --- 422 bdm::pdf::from_setting 423 \endcode 424 \endcode 425 If the optional fields are not given, they will be filled as follows: 426 \code 427 weights = 1/n * [1,1,1,...]; 428 \endcode 429 */ 419 430 void from_setting ( const Setting &set ); 420 431 void to_setting (Setting &set) const; … … 430 441 Array<vec_from_vec> bm_yt; 431 442 Array<vec_from_2vec> bm_cond; 443 444 //! \brief Internal class 432 445 class eprod_bm : public eprod_base { 433 446 ProdBMBase & pb; -
library/bdm/stat/exp_family.cpp
r1064 r1068 134 134 135 135 double m = nu - nPsi - dimx - 1; 136 #define 137 #define 136 #define log2 0.693147180559945286226763983 137 #define logpi 1.144729885849400163877476189 138 138 #define log2pi 1.83787706640935 139 139 #define Inf std::numeric_limits<double>::infinity() … … 149 149 - 0.5 * dimx * ( m * log2 + 0.5 * ( dimx - 1 ) * log2pi ) - lg; 150 150 151 // 151 // bdm_assert_debug ( ( ( -nkG - nkW ) > -Inf ) && ( ( -nkG - nkW ) < Inf ), "ARX improper" ); 152 152 if ( -nkG - nkW == Inf ) { 153 153 cout << "??" << endl; … … 186 186 M = iLsub * Lpsi; 187 187 } 188 /* 189 190 191 192 193 194 195 188 /* if ( 0 ) { // test with Peterka 189 mat VF = V.to_mat(); 190 mat Vf = VF ( 0, dimx - 1, 0, dimx - 1 ); 191 mat Vzf = VF ( dimx, end, 0, dimx - 1 ); 192 mat VZ = VF ( dimx, end, dimx, end ); 193 194 mat Lam2 = Vf - Vzf.T() * inv ( VZ ) * Vzf; 195 }*/ 196 196 } 197 197 … … 203 203 204 204 mat Lsub = L ( 1, end, 1, end ); 205 // 205 // mat Dsub = diag ( D ( 1, end ) ); 206 206 207 207 ldmat LD ( inv ( Lsub ).T(), 1.0 / D ( 1, end ) ); … … 715 715 } 716 716 717 void 717 void euni::to_setting (Setting &set) const { 718 718 epdf::to_setting ( set ); 719 719 UI::save ( high, set, "high" ); … … 731 731 pdf::from_setting(set); 732 732 g=UI::build<fnc>(set,"g",UI::compulsory); 733 validate(); 734 } 733 } 734 735 735 void mgdirac::to_setting(Setting &set) const { 736 736 pdf::to_setting(set); 737 737 UI::save(g.get(), set, "g"); 738 738 } 739 739 740 void mgdirac::validate() { 740 741 pdf::validate(); -
library/bdm/stat/exp_family.h
r1066 r1068 124 124 }; 125 125 126 /*! Dirac delta density with predefined transformation126 /*! \brief Dirac delta density with predefined transformation 127 127 128 128 Density of the type:\f[ f(x_t | y_t) = \delta (x_t - g(y_t)) \f] … … 141 141 return std::numeric_limits< double >::max(); 142 142 } 143 144 /*! Create object from the following structure 145 146 \code 147 class = 'mgdirac'; 148 g = function bdm::fnc; % any offspring of fnc, bdm::fnc::from_setting 149 --- inherited fields --- 150 bdm::pdf::from_setting 151 \endcode 152 */ 143 153 void from_setting(const Setting& set); 144 154 void to_setting(Setting &set) const; … … 666 676 UIREGISTER ( eBeta ); 667 677 668 /*! Random Walk on Dirichlet 678 /*! \brief Random Walk on Dirichlet 679 669 680 Using simple assignment 670 681 \f[ \beta = rvc / k + \beta_c \f] … … 676 687 By default is it set to 0.1; 677 688 */ 678 679 689 class mDirich: public pdf_internal<eDirich> { 680 690 protected: … … 690 700 _beta = val / k + betac; 691 701 }; 692 /*! Create Dirichlet random walk 693 \f[ f(rv|rvc) = Di(rvc*k) \f] 694 from structure 702 703 /*! Create object from the following structure 695 704 \code 696 705 class = 'mDirich'; 697 k = 1; //multiplicative constant k706 k = 1; % multiplicative constant k 698 707 --- optional --- 699 rv = RV({'name'},size) // description of RV 700 beta0 = []; // initial value of beta 701 betac = []; // initial value of beta 708 beta0 = [...]; % initial values of beta 709 betac = [...]; % initial values of beta stabilizing coefficients 710 --- inherited fields --- 711 bdm::pdf::from_setting 712 \endcode 713 fulfilling form \f[ f(rv|rvc) = Di(rvc*k) \f] 714 715 If the optional fields are not given, they will be filled as follows: 716 \code 717 beta0 = [1,1,1,...]; 718 betac = 0.1 * [1,1,1,...]; 702 719 \endcode 703 720 */ … … 709 726 710 727 /*! \brief Random Walk with vector Beta distribution 728 711 729 Using simple assignment 712 730 \f{eqnarray*} … … 733 751 }; 734 752 735 /*! Create Beta random walk 736 \f[ f(rv|rvc) = \prod Beta(rvc,k) \f] 737 from structure 753 /*! Create object from the following structure 738 754 \code 739 755 class = 'mBeta'; 740 k = 1; // multiplicative constant k 741 --- optional --- 742 rv = RV({'name'},size) // description of RV 743 beta = []; // initial value of beta 744 betac = []; // initial value of beta stabilizing constant 756 k = [...]; % vector of multiplicative constants k 757 --- optional fields --- 758 beta = [...]; % initial values of beta 759 betac = [...]; % initial values of beta stabilizing constants 760 --- inherited fields --- 761 bdm::pdf::from_setting 745 762 \endcode 763 fulfilling form \f[ f(rv|rvc) = \prod Beta(rvc,k) \f] 764 765 If the optional fields are not given, they will be filled as follows: 766 \code 767 beta = [1,1,1,...]; 768 betac = 0.1 * [1,1,1,...]; 769 \endcode 770 746 771 */ 747 772 void from_setting ( const Setting &set ); … … 928 953 */ 929 954 930 //! Uniform distributed density on a rectangular support 931 955 //! \brief Uniform distributed density on a rectangular support 932 956 class euni: public epdf { 933 957 protected: … … 976 1000 return ( pow ( high, 2 ) + pow ( low, 2 ) + elem_mult ( high, low ) ) / 3.0; 977 1001 } 978 /*! Create Uniform density 979 \f[ f(rv) = U(low,high) \f] 980 fromstructure1002 1003 1004 /*! Create object from the following structure 981 1005 \code 1006 982 1007 class = 'euni' 983 high = [...]; // vector of upper bounds 984 low = [...]; // vector of lower bounds 985 rv = RV({'name'}); // description of RV 1008 high = [...]; % vector of upper bounds 1009 low = [...]; % vector of lower bounds 1010 rv = RV({'names',...},[sizes,...],[times,...]); % description of RV 1011 --- inherited fields --- 1012 bdm::epdf::from_setting 986 1013 \endcode 1014 1015 fulfilling form \f[ f(rv) = U(low,high) \f] 987 1016 */ 988 1017 void from_setting ( const Setting &set ); … … 1003 1032 iepdf.set_parameters ( mea - delta, mea + delta ); 1004 1033 } 1005 //! load from 1034 1035 /*! Create object from the following structure 1036 \code 1037 class = 'mguni'; 1038 mean = function bdm::fnc; % any offspring of fnc, bdm::fnc::from_setting 1039 delta = [...]; % distance from mean to both sides 1040 --- inherited fields --- 1041 bdm::pdf::from_setting 1042 \endcode 1043 */ 1006 1044 void from_setting ( const Setting &set ) { 1007 1045 pdf::from_setting ( set ); //reads rv and rvc … … 1204 1242 1205 1243 1206 /*! (Approximate) Student t density with linear function of mean value1244 /*! \brief (Approximate) Student t density with linear function of mean value 1207 1245 1208 1246 The internal epdf of this class is of the type of a Gaussian (enorm). … … 1271 1309 _beta = k / val; 1272 1310 }; 1273 /*! Create Gamma density with conditional mean value 1274 \f[ f(rv|rvc) = \Gamma(k, k/rvc) \f] 1275 from structure 1311 1312 /*! Create object from the following structure 1276 1313 \code 1277 class = 'mgamma'; 1278 beta = [...]; // vector of initial alpha 1279 k = 1.1; // multiplicative constant k 1280 rv = RV({'name'}) // description of RV 1281 rvc = RV({'name'}) // description of RV in condition 1282 \endcode 1314 class = 'mgamma'; 1315 beta = [...]; % vector of initial beta 1316 k = x; % multiplicative scalar constant k 1317 --- inherited fields --- 1318 bdm::pdf::from_setting 1319 \endcode 1320 fulfilling form \f[ f(rv|rvc) = \Gamma(k, k/rvc) \f] 1283 1321 */ 1284 1322 void from_setting ( const Setting &set ); … … 1340 1378 }; 1341 1379 }; 1342 1343 1380 1344 1381 /*! … … 1369 1406 }; 1370 1407 1371 void 1408 void validate () { 1372 1409 mgamma::validate(); 1373 1410 dimc = dimension(); … … 1420 1457 }; 1421 1458 1422 1423 /*! Create inverse-Gamma density with conditional mean value 1424 \f[ f(rv|rvc) = i\Gamma(k, k/(rvc^l \circ ref^{(1-l)}) \f] 1425 from structure 1459 /*! Create object from the following structure 1426 1460 \code 1427 1461 class = 'migamma_ref'; 1428 ref = [ 1e-5; 1e-5; 1e-2 1e-3]; //reference vector1429 l = 0.999; // constantl1430 k = 0.1; // constant k1431 rv = RV({'name'}) // description of RV1432 rvc = RV({'name'}) // description of RV in condition1462 ref = [...]; % reference vector 1463 l = []; % constant scalar l 1464 k = []; % constant scalar k 1465 --- inherited fields --- 1466 bdm::migamma::from_setting 1433 1467 \endcode 1468 fulfilling form \f[ f(rv|rvc) = i\Gamma(k, k/(rvc^l \circ ref^{(1-l)}) \f] 1434 1469 */ 1435 1470 void from_setting ( const Setting &set ); … … 1442 1477 SHAREDPTR ( migamma_ref ); 1443 1478 1444 /*! Log-Normal probability density 1445 only allow diagonal covariances! 1479 /*! \brief Log-Normal probability density - it allows only diagonal covariances! 1446 1480 1447 1481 Density of the form \f$ \log(x)\sim \mathcal{N}(\mu,\sigma^2) \f$ , i.e. … … 1499 1533 }; 1500 1534 1501 /*! Create logNormal random Walk 1502 \f[ f(rv|rvc) = log\mathcal{N}( \log(rvc)-0.5\log(k^2+1), k I) \f] 1503 from structure 1535 /*! Create object from the following structure 1504 1536 \code 1505 1537 class = 'mlognorm'; 1506 k = 0.1; //"variance" k1507 mu0 = 0.1; // Initial value of mean1508 rv = RV({'name'}) // description of RV1509 rvc = RV({'name'}) // description of RV in condition1538 k = []; % "variance" k 1539 mu0 = []; % initial value of mean 1540 --- inherited fields --- 1541 bdm::pdf_internal<elognorm>::from_setting 1510 1542 \endcode 1543 fulfilling form \f[ f(rv|rvc) = log\mathcal{N}( \log(rvc)-0.5\log(k^2+1), k I) \f] 1511 1544 */ 1512 1545 void from_setting ( const Setting &set ); … … 1518 1551 SHAREDPTR ( mlognorm ); 1519 1552 1520 /*! inverse Wishart density defined on Choleski decomposition 1521 1553 /*! \brief Inverse Wishart density defined on Choleski decomposition 1522 1554 */ 1523 1555 class eWishartCh : public epdf { -
library/bdm/stat/merger.cpp
r1064 r1068 160 160 vec mea = mean(); 161 161 162 // 162 // cout << sum (w) << "," << w*w << endl; 163 163 164 164 mat Tmp = zeros ( dim, dim ); … … 174 174 } 175 175 176 177 void merger_base::from_setting ( const Setting& set ) { 178 // get support 179 // find which method to use 180 epdf::from_setting (set); 181 string meth_str; 182 UI::get( meth_str, set, "method", UI::compulsory ); 183 if ( meth_str == "arithmetic" ) 184 set_method ( ARITHMETIC ); 185 else if ( meth_str == "geometric" ) 186 set_method ( GEOMETRIC ); 187 else if ( meth_str == "lognormal" ) { 188 set_method ( LOGNORMAL ); 189 UI::get(beta, set, "beta", UI::compulsory ); 190 } 191 192 193 string dbg_filename; 194 if ( UI::get ( dbg_filename, set, "dbg_file" ) ) 195 set_debug_file( dbg_filename ); 196 197 } 198 199 void merger_base::to_setting (Setting &set) const { 200 epdf::to_setting(set); 201 202 UI::save( METHOD, set, "method"); 203 204 if( METHOD == LOGNORMAL ) 205 UI::save (beta, set, "beta" ); 206 207 if( DBG ) 208 UI::save ( dbg_file->get_fname(), set, "dbg_file" ); 209 } 210 211 void merger_base::validate() { 212 // bdm_assert ( eSmp._w().length() > 0, "Empty support, use set_support()." ); 213 // bdm_assert ( dim == eSmp._samples() ( 0 ).length(), "Support points and rv are not compatible!" ); 214 epdf::validate(); 215 bdm_assert ( isnamed(), "mergers must be named" ); 216 } 217 218 // DEFAULTS FOR MERGER_BASE 219 const MERGER_METHOD merger_base::DFLT_METHOD = LOGNORMAL; 220 const double merger_base::DFLT_beta = 1.2; 221 176 222 void merger_mix::merge ( ) { 177 223 if(Npoints<1) { … … 190 236 } 191 237 192 if ( DBG ) 238 if ( DBG ) *dbg_file << Name ( "Smp_0" ) << Smp_ex; 193 239 194 240 // Stuff for merging 195 vec lw_src ( Npoints ); 196 vec lw_mix ( Npoints ); 197 vec lw ( Npoints ); 241 vec lw_src ( Npoints ); // weights of the ith source 242 vec lw_mix ( Npoints ); // weights of the approximating mixture 243 vec lw ( Npoints ); // tmp 198 244 mat lW = zeros ( Nsources, Npoints ); // array of weights of all sources 199 245 vec vec0 ( 0 ); … … 285 331 } 286 332 287 // 288 // 333 // sprintf ( str,"marg%d",niter ); 334 // *dbg << Name ( str ) << lw_src; 289 335 290 336 } … … 345 391 } 346 392 delete Mpred; 347 // 393 // cout << endl; 348 394 349 395 } … … 359 405 } 360 406 361 void 407 void merger_mix::to_setting (Setting &set) const { 362 408 merger_base::to_setting(set); 363 409 UI::save( Ncoms, set, "ncoms"); … … 371 417 } 372 418 373 void merger_base::from_setting ( const Setting& set ) {374 // get support375 // find which method to use376 epdf::from_setting (set);377 string meth_str;378 UI::get( meth_str, set, "method", UI::compulsory );379 if ( meth_str == "arithmetic" )380 set_method ( ARITHMETIC );381 else if ( meth_str == "geometric" )382 set_method ( GEOMETRIC );383 else if ( meth_str == "lognormal" ) {384 set_method ( LOGNORMAL );385 UI::get(beta, set, "beta", UI::compulsory );386 }387 388 389 string dbg_filename;390 if ( UI::get ( dbg_filename, set, "dbg_file" ) )391 set_debug_file( dbg_filename );392 393 }394 395 void merger_base::to_setting (Setting &set) const {396 epdf::to_setting(set);397 398 UI::save( METHOD, set, "method");399 400 if( METHOD == LOGNORMAL )401 UI::save (beta, set, "beta" );402 403 if( DBG )404 UI::save ( dbg_file->get_fname(), set, "dbg_file" );405 }406 407 void merger_base::validate() {408 // bdm_assert ( eSmp._w().length() > 0, "Empty support, use set_support()." );409 // bdm_assert ( dim == eSmp._samples() ( 0 ).length(), "Support points and rv are not compatible!" );410 epdf::validate();411 bdm_assert ( isnamed(), "mergers must be named" );412 }413 414 // DEFAULTS FOR MERGER_BASE415 const MERGER_METHOD merger_base::DFLT_METHOD = LOGNORMAL;416 const double merger_base::DFLT_beta = 1.2;417 419 // DEFAULTS FOR MERGER_MIX 418 420 const int merger_mix::DFLT_Ncoms = 10; -
library/bdm/stat/merger.h
r1064 r1068 25 25 26 26 /*! 27 @brief Base class for general combination of pdfs on discrete support27 @brief Base class (interface) for for general combination of pdfs on discrete support 28 28 29 29 Mixtures of Gaussian densities are used internally. Switching to other densities should be trivial. … … 42 42 For merging of more general cases, use offsprings merger_mix and merger_grid. 43 43 */ 44 45 44 class merger_base : public epdf { 46 45 protected: … … 168 167 } 169 168 170 //! load from setting 169 170 } 171 172 /*! Create object from the following structure 173 174 \code 175 class = 'merger_base'; 176 method = 'arithmetic' or 'geometric' or 'lognormal'; % name of the model used for merging 177 --- compulsory only for lognormal merging model --- 178 beta = x; % scalar prior on the log-normal merging model 179 --- optional fields --- 180 dbg_file = '...'; % name of the file to store debug informations 181 --- inherited fields --- 182 bdm::epdf::from_setting 183 \endcode 184 */ 171 185 void from_setting ( const Setting& set ); 172 186 … … 179 193 SHAREDPTR ( merger_base ); 180 194 181 //! Merger using importance sampling with mixture proposal density195 //! \brief Merger using importance sampling with mixture proposal density 182 196 class merger_mix : public merger_base { 183 197 protected: … … 249 263 return tmp; 250 264 } 251 //! from_settings 265 /*! Create object from the following structure 266 267 \code 268 class = 'merger_mix'; 269 --- optional fields --- 270 ncoms = []; % number of components in a mixture 271 effss_coef = []; % coefficient of resampling from interval [0,1] 272 stop_niter = []; % number of interations 273 --- inherited fields --- 274 bdm::merger_base::from_setting 275 \endcode 276 If the optional fields are not given, they will be filled as follows: 277 \code 278 ncoms = 10; 279 effss_coef = 0.9; 280 stop_niter = 10; 281 \endcode 282 283 */ 252 284 void from_setting ( const Setting& set ); 253 void to_setting (Setting &set) const; 285 286 void to_setting (Setting &set) const; 254 287 void validate(); 255 288 256 289 //! @} 257 258 290 }; 259 291 UIREGISTER ( merger_mix );