Show
Ignore:
Timestamp:
06/19/09 11:43:48 (15 years ago)
Author:
smidl
Message:

mergers

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/stat/merger.h

    r384 r388  
    7575 
    7676                //!Empty constructor 
    77                 merger_base () : compositepdf() {DBG=false;dbg_file=NULL;}; 
     77                merger_base () : compositepdf() {DBG = false;dbg_file = NULL;}; 
    7878                //!Constructor from sources 
    79                 merger_base (const Array<mpdf*> &S) {set_sources (S);}; 
    80                 //! Function setting the main internal structures  
    81                 void set_sources (const Array<mpdf*> &Sources) { 
    82                         compositepdf::set_elements (Sources); 
     79                merger_base (const Array<mpdf*> &S, bool own=false) {set_sources (S,own);}; 
     80                //! Function setting the main internal structures 
     81                void set_sources (const Array<mpdf*> &Sources, bool own) { 
     82                        compositepdf::set_elements (Sources,own); 
    8383                        //set sizes 
    8484                        dls.set_size (Sources.length()); 
     
    109109                        }; 
    110110                } 
     111                //! Rectangular support  each vector of XYZ specifies (begining-end) interval for each dimension. Same number of points, \c dimsize, in each dimension.  
     112                void set_support (const Array<vec> &XYZ, const int dimsize) { 
     113                        set_support(XYZ,dimsize*ones_i(XYZ.length())); 
     114                } 
     115                //! Rectangular support  each vector of XYZ specifies (begining-end) interval for each dimension. \c gridsize specifies number of points is each dimension. 
     116                void set_support (const Array<vec> &XYZ, const ivec &gridsize) { 
     117                        int dim = XYZ.length();  //check with internal dim!! 
     118                        Npoints = prod (gridsize);  
     119                        eSmp.set_parameters (Npoints, false); 
     120                        Array<vec> &samples = eSmp._samples(); 
     121                        eSmp._w() = ones (Npoints) / Npoints; //unifrom size of bins 
     122                        //set samples 
     123                        ivec ind = zeros_i (dim);      //indeces of dimensions in for cycle; 
     124                        vec smpi (dim);            // ith sample 
     125                        vec steps =zeros(dim);            // ith sample 
     126                        // first corner 
     127                        for (int j = 0; j < dim; j++) { 
     128                                smpi (j) = XYZ (j) (0); /* beginning of the interval*/  
     129                                it_assert(gridsize(j)!=0.0,"Zeros in gridsize!"); 
     130                                steps (j) = (smpi(j) - XYZ(j)(1))/gridsize(j); 
     131                        } 
     132                        // fill samples 
     133                        int act_dim=0; //active dimension 
     134                        for (int i = 0; i < Npoints; i++) { 
     135                                // copy  
     136                                samples(i) = smpi;  
     137                                // go through all dimensions 
     138                                for (int j = 0;j < dim;j++) { 
     139                                        if (ind (j) == gridsize (j) - 1) { //j-th index is full 
     140                                                ind (j) = 0; //shift back 
     141                                                smpi(j) = XYZ(j)(0); 
     142                                                 
     143                                                ind (j + 1) ++; //increase the next dimension; 
     144                                                smpi(j+1) += steps(j+1); 
     145                                                 
     146                                                if (ind (j + 1) < gridsize (j + 1) - 1) break; 
     147                                        } else { 
     148                                                ind (j) ++;  
     149                                                smpi(j) +=steps(j); 
     150                                                break; 
     151                                        } 
     152                                } 
     153                        } 
     154                } 
    111155                //! set debug file 
    112                 void set_debug_file (const string fname) {  
    113                         if (DBG) delete dbg_file;  
    114                         dbg_file = new it_file (fname);  
     156                void set_debug_file (const string fname) { 
     157                        if (DBG) delete dbg_file; 
     158                        dbg_file = new it_file (fname); 
    115159                        if (dbg_file) DBG = true; 
    116160                } 
    117161                //! Set internal parameters used in approximation 
    118                 void set_method (MERGER_METHOD MTH, double beta0=0.0) { 
    119                         METHOD = MTH;  
     162                void set_method (MERGER_METHOD MTH, double beta0 = 0.0) { 
     163                        METHOD = MTH; 
    120164                        beta = beta0; 
    121165                } 
    122166                //! Set support points from a pdf by drawing N samples 
    123                 void set_support(const epdf &overall, int N){ 
    124                         it_assert_debug ( rv.equal ( overall._rv() ),"Incompatible parameter overall!" ); 
    125                         eSmp.set_statistics(&overall,N); 
    126                         Npoints=N; 
    127                 } 
    128                  
     167                void set_support (const epdf &overall, int N) { 
     168                        eSmp.set_statistics (&overall, N); 
     169                        Npoints = N; 
     170                } 
     171 
    129172                //! Destructor 
    130173                virtual ~merger_base() { 
     
    136179                }; 
    137180                //!@} 
    138                  
     181 
    139182                //! \name Mathematical operations 
    140183                //!@{ 
    141                  
     184 
    142185                //!Merge given sources in given points 
    143186                void merge () { 
    144                         if (eSmp._w().length() ==0) {it_error("Empty support points use set_support" );} 
     187                        validate(); 
     188 
    145189                        //check if sources overlap: 
    146190                        bool OK = true; 
     
    164208                                eSmp._w() = wtmp / sum (wtmp); 
    165209                        } else { 
    166                                 it_error("Sources are not compatible - use merger_mix"); 
    167                         } 
    168                         ; 
     210                                it_error ("Sources are not compatible - use merger_mix"); 
     211                        } 
    169212                }; 
    170213 
     
    172215                //! Merge log-likelihood values in points using method specified by parameter METHOD 
    173216                vec merge_points (mat &lW); 
    174                  
    175                  
     217 
     218 
    176219                //! sample from merged density 
    177220//! weight w is a 
     
    216259                //! Access function 
    217260                eEmp& _Smp() {return eSmp;} 
    218                  
     261 
     262                //! load from setting 
     263                void from_setting (const Setting& set) { 
     264                        // get support 
     265                        // find which method to use 
     266                        string meth_str; 
     267                        UI::get<string> (meth_str, set, "method"); 
     268                        if (!strcmp (meth_str.c_str(), "arithmetic")) 
     269                                set_method (ARITHMETIC); 
     270                        else { 
     271                                if (!strcmp (meth_str.c_str(), "geometric")) 
     272                                        set_method (GEOMETRIC); 
     273                                else if (!strcmp (meth_str.c_str(), "lognormal")) { 
     274                                        set_method (GEOMETRIC); 
     275                                        set.lookupValue( "beta",beta); 
     276                                } 
     277                        } 
     278                        validate(); 
     279                } 
     280 
     281                void validate() { 
     282                        it_assert (eSmp._w().length() > 0, "Empty support, use set_support()."); 
     283                        it_assert (dim == eSmp._samples() (0).length(), "Support points and rv are not compatible!"); 
     284                        it_assert (isnamed(),"mergers must be named"); 
     285                } 
    219286                //!@} 
    220287}; 
     288UIREGISTER(merger_base); 
    221289 
    222290class merger_mix : public merger_base 
     
    234302                //!@{ 
    235303                merger_mix () {}; 
    236                 merger_mix (const Array<mpdf*> &S) {set_sources(S);}; 
     304                merger_mix (const Array<mpdf*> &S,bool own=false) {set_sources (S,own);}; 
    237305                //! Set sources and prepare all internal structures 
    238                 void set_sources (const Array<mpdf*> &S) { 
    239                         merger_base::set_sources(S); 
     306                void set_sources (const Array<mpdf*> &S, bool own) { 
     307                        merger_base::set_sources (S,own); 
    240308                        Nsources = S.length(); 
    241309                } 
    242310                //! Set internal parameters used in approximation 
    243                 void set_parameters (int Ncoms0=10, double effss_coef0 = 0.5) { 
     311                void set_parameters (int Ncoms0 = 10, double effss_coef0 = 0.5) { 
    244312                        Ncoms = Ncoms0; 
    245313                        effss_coef = effss_coef0; 
    246314                } 
    247315                //!@} 
    248                  
     316 
    249317                //! \name Mathematical operations 
    250318                //!@{ 
    251                  
     319 
    252320                //!Merge values using mixture approximation 
    253321                void merge (); 
     
    269337                //! Access function 
    270338                emix* proposal() {emix* tmp = Mix.epredictor(); tmp->set_rv (rv); return tmp;} 
    271         //! @} 
     339                //! from_settings 
     340                void from_settings(const Setting& set){ 
     341                        merger_base::from_setting(set); 
     342                        set.lookupValue("ncoms",Ncoms); 
     343                } 
    272344                 
     345                //! @} 
     346 
    273347}; 
     348UIREGISTER(merger_mix); 
    274349 
    275350}