| | 25 | |
| | 26 | /*! Abstract common class for mergers |
| | 27 | |
| | 28 | It defines only common interface of access to sources and operation merge(); The result of merging is available via function epdf() |
| | 29 | */ |
| | 30 | class 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) |
| | 63 | template<class sq_T> |
| | 64 | class 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 | }; |