Changeset 1072 for library

Show
Ignore:
Timestamp:
06/10/10 12:29:54 (14 years ago)
Author:
smidl
Message:

new merger

Location:
library
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/datasources.h

    r1064 r1072  
    234234*/ 
    235235class PdfDS : public DS { 
    236 protected: 
     236public: 
    237237    //! internal pointer to epdf from which we samplecond 
    238238    shared_ptr<pdf> ipdf; 
     239protected: 
    239240    //! internal storage of data sample 
    240241    vec yt; 
  • library/bdm/stat/merger.h

    r1069 r1072  
    2323//!Merging methods 
    2424enum MERGER_METHOD {ARITHMETIC = 1, GEOMETRIC = 2, LOGNORMAL = 3}; 
     25 
     26/*!  Abstract common class for mergers 
     27 
     28It defines only common interface of access to sources and operation merge(); The result of merging is available via function epdf() 
     29*/ 
     30class MergerBase: public root{ 
     31        public:          
     32                //! weights of the sources -- no restrictions 
     33                vec w; 
     34                //! get ith source 
     35                Array<shared_ptr<epdf> > sources; 
     36                //! merge  
     37                void merge() NOT_IMPLEMENTED_VOID; 
     38                //!  
     39                //! check if all epdfs are on the same support 
     40                void validate() { 
     41                        int n=sources.length(); 
     42                         
     43                        bdm_assert(n>0,"merger has no sources to merge"); 
     44                        // check if weights are ok 
     45                        if (w.length()!=n) { 
     46                                w = ones(n)/n; 
     47                        } 
     48                         
     49                        // check compatibility of sources -- no types are needed 
     50                        int dim0 = sources(0)->dimension(); 
     51                        for (int i=0; i<n; i++){ 
     52                                bdm_assert(sources(i)->dimension()==dim0, "Merger: Incompatible dimensions of sources"); 
     53                                if (sources(i)->isnamed()){ 
     54                                        //check if rv match 
     55                                } 
     56                        } 
     57                } 
     58                //! return the result of merging 
     59                virtual const epdf& merger()=0; 
     60}; 
     61 
     62//! Merger into normal density, max entropy approximator for 2 moments (mean+variance) 
     63template<class sq_T> 
     64class ENormMerger: public MergerBase{ 
     65        protected: 
     66                //!internal epdf 
     67                enorm<sq_T> iepdf; 
     68        public: 
     69                ENormMerger():method(GEOMETRIC){}; 
     70                MERGER_METHOD method; 
     71                void merge(){ 
     72                        int n = sources.length(); 
     73                        int dim = sources(0)->dimension(); 
     74                        sq_T Var(zeros(dim)); 
     75                        vec mea=zeros(dim); 
     76                        // go through all sources 
     77                        for (int i=0; i<n; i++){ 
     78                                sq_T Ci = sources(i)->covariance(); 
     79                                sq_T wCi = Ci; wCi*=w(i); 
     80                                vec mi = sources(i)->mean(); 
     81                                switch (method) { 
     82                                        case ARITHMETIC:{ 
     83                                                Var += wCi; 
     84                                                Var.opupdt(mi,w(i)); // + mean * mean' 
     85                                                mea += w(i)*mi; 
     86                                                break; 
     87                                        } 
     88                                        case GEOMETRIC:{ 
     89                                                Var += wCi; 
     90                                                sq_T iCi; 
     91                                                Ci.inv(iCi); 
     92                                                mea += iCi.to_mat()*w(i)*mi; 
     93                                                break; 
     94                                        } 
     95                                        default: 
     96                                                bdm_error("Method not implemneted"); 
     97                                } 
     98                        } 
     99                        // post pocessing 
     100                        switch (method) { 
     101                                case ARITHMETIC: 
     102                                        iepdf._R()=Var; 
     103                                        iepdf._R().opupdt(mea,-1.0); // Var - mean * mean' 
     104                                        iepdf._mu()=mea; 
     105                                        break; 
     106                                case GEOMETRIC: 
     107                                        iepdf._R()=Var; 
     108                                        iepdf._mu() = Var.to_mat()*mea; // mean=sqrt(Var)*mea 
     109                                        break; 
     110                                default: 
     111                                        bdm_error("Method not implemneted"); 
     112                        } 
     113                } 
     114                //! access function 
     115                const epdf& merger(){return iepdf;} 
     116}; 
    25117 
    26118/*! 
  • library/tests/testsuite/datasource_test.cpp

    r1064 r1072  
    33#include "../mat_checks.h" 
    44#include "UnitTest++.h" 
     5#include "../bdm/stat/emix.h" 
    56 
    67using namespace bdm; 
     
    6061    OM->validate(); 
    6162 
    62     StateDS sds; 
    63     sds.set_parameters ( IM, OM ); 
    64     sds.validate(); 
     63        PdfDS pds;       
     64        Array<shared_ptr<pdf> > Arr(2); 
     65        Arr(0) = OM; 
     66        Arr(1)= IM; 
     67         
     68        shared_ptr<mprod> mp; 
     69        mp->set_elements(Arr); 
     70         
     71        pds.ipdf=mp; 
     72        pds.validate(); 
    6573 
    6674    for ( int t = 1; t < 10; t++ ) { 
    67         sds.write ( vec_1 ( double ( t ) ) ); 
     75                pds.write ( vec_1 ( double ( t ) ) ); 
    6876        // TODO ZDE TO SPADNE 
    69         sds.step(); 
     77                pds.step(); 
    7078    } 
    7179 
    7280    vec dt; 
    73     sds.getdata ( dt ); 
     81        pds.getdata ( dt ); 
    7482    CHECK_CLOSE ( vec ( "94.5, 94.5, 13.5, 9" ), dt, 1e-2 ); 
    7583} 
  • library/tests/testsuite/merger_test.cpp

    r1064 r1072  
    77 
    88using namespace bdm; 
     9 
     10TEST (ENormMerger_test){ 
     11        ENormMerger<ldmat> m1; 
     12        m1.sources.set_length(2); 
     13        m1.sources(0) = new enorm<ldmat>(vec_2(1.0, 1.0), mat_2x2(1.0, 0.0, 0.0, 2.0)); 
     14        m1.sources(1) = new enorm<ldmat>(vec_2(2.0, 2.0), mat_2x2(2.0, 1.0, 1.0, 2.0)); 
     15        m1.validate(); 
     16        m1.merge(); 
     17         
     18        cout << "mean: " << m1.merger().mean() << endl; 
     19        cout << "var: " << m1.merger().covariance() << endl; 
     20} 
    921 
    1022TEST ( merger_base_test ) {