root/library/bdm/stat/emix.h @ 565

Revision 565, 12.1 kB (checked in by vbarta, 15 years ago)

using own error macros (basically copied from IT++, but never aborting)

  • Property svn:eol-style set to native
RevLine 
[107]1/*!
2  \file
3  \brief Probability distributions for Mixtures of pdfs
4  \author Vaclav Smidl.
5
6  -----------------------------------
7  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
8
9  Using IT++ for numerical operations
10  -----------------------------------
11*/
12
[394]13#ifndef EMIX_H
14#define EMIX_H
[107]15
[477]16#define LOG2  0.69314718055995
[333]17
[461]18#include "../shared_ptr.h"
[384]19#include "exp_family.h"
[107]20
[286]21namespace bdm {
[107]22
[182]23//this comes first because it is used inside emix!
24
25/*! \brief Class representing ratio of two densities
26which arise e.g. by applying the Bayes rule.
27It represents density in the form:
28\f[
29f(rv|rvc) = \frac{f(rv,rvc)}{f(rvc)}
30\f]
31where \f$ f(rvc) = \int f(rv,rvc) d\ rv \f$.
32
33In particular this type of arise by conditioning of a mixture model.
34
[211]35At present the only supported operation is evallogcond().
[182]36 */
37class mratio: public mpdf {
[192]38protected:
[182]39        //! Nominator in the form of mpdf
[192]40        const epdf* nom;
[504]41
[182]42        //!Denominator in the form of epdf
[504]43        shared_ptr<epdf> den;
44
[192]45        //!flag for destructor
46        bool destroynom;
[193]47        //!datalink between conditional and nom
48        datalink_m2e dl;
[487]49        //! dummy epdf that stores only rv and dim
50        epdf iepdf;
[192]51public:
52        //!Default constructor. By default, the given epdf is not copied!
[182]53        //! It is assumed that this function will be used only temporarily.
[487]54        mratio ( const epdf* nom0, const RV &rv, bool copy = false ) : mpdf ( ), dl ( ),iepdf() {
[286]55                // adjust rv and rvc
56                rvc = nom0->_rv().subt ( rv );
57                dimc = rvc._dsize();
[487]58                set_ep ( iepdf );
59                iepdf.set_parameters ( rv._dsize() );
60                iepdf.set_rv ( rv );
[477]61
[286]62                //prepare data structures
[477]63                if ( copy ) {
[565]64                        bdm_error ( "todo" );
65                        // destroynom = true;
[477]66                } else {
67                        nom = nom0;
68                        destroynom = false;
69                }
[565]70                bdm_assert_debug ( rvc.length() > 0, "Makes no sense to use this object!" );
[477]71
[286]72                // build denominator
[192]73                den = nom->marginal ( rvc );
[477]74                dl.set_connection ( rv, rvc, nom0->_rv() );
[192]75        };
[211]76        double evallogcond ( const vec &val, const vec &cond ) {
[214]77                double tmp;
[487]78                vec nom_val ( dimension() + dimc );
[477]79                dl.pushup_cond ( nom_val, val, cond );
[214]80                tmp = exp ( nom->evallog ( nom_val ) - den->evallog ( cond ) );
81                return tmp;
[192]82        }
[182]83        //! Object takes ownership of nom and will destroy it
[477]84        void ownnom() {
85                destroynom = true;
86        }
[182]87        //! Default destructor
[477]88        ~mratio() {
89                if ( destroynom ) {
90                        delete nom;
91                }
92        }
[550]93
94private:
95        // not implemented
96        mratio ( const mratio & );
97        mratio &operator=( const mratio & );
[182]98};
99
[107]100/*!
101* \brief Mixture of epdfs
102
103Density function:
104\f[
105f(x) = \sum_{i=1}^{n} w_{i} f_i(x), \quad \sum_{i=1}^n w_i = 1.
106\f]
107where \f$f_i(x)\f$ is any density on random variable \f$x\f$, called \a component,
108
109*/
[145]110class emix : public epdf {
[162]111protected:
112        //! weights of the components
113        vec w;
[559]114
[162]115        //! Component (epdfs)
[504]116        Array<shared_ptr<epdf> > Coms;
117
[162]118public:
[559]119        //! Default constructor
120        emix ( ) : epdf ( ) { }
121
122          /*!
123            \brief Set weights \c w and components \c Coms
124
125            Shared pointers in Coms are kept inside this instance and
126            shouldn't be modified after being passed to this method.
127          */
[504]128        void set_parameters ( const vec &w, const Array<shared_ptr<epdf> > &Coms );
[107]129
[162]130        vec sample() const;
131        vec mean() const {
[477]132                int i;
133                vec mu = zeros ( dim );
134                for ( i = 0; i < w.length(); i++ ) {
135                        mu += w ( i ) * Coms ( i )->mean();
136                }
[162]137                return mu;
138        }
[229]139        vec variance() const {
140                //non-central moment
[286]141                vec mom2 = zeros ( dim );
[477]142                for ( int i = 0; i < w.length(); i++ ) {
143                        mom2 += w ( i ) * ( Coms ( i )->variance() + pow ( Coms ( i )->mean(), 2 ) );
144                }
[229]145                //central moment
[477]146                return mom2 - pow ( mean(), 2 );
[229]147        }
[211]148        double evallog ( const vec &val ) const {
[162]149                int i;
150                double sum = 0.0;
[477]151                for ( i = 0; i < w.length(); i++ ) {
152                        sum += w ( i ) * exp ( Coms ( i )->evallog ( val ) );
153                }
154                if ( sum == 0.0 ) {
155                        sum = std::numeric_limits<double>::epsilon();
156                }
157                double tmp = log ( sum );
[565]158                bdm_assert_debug ( std::isfinite ( tmp ), "Infinite" );
[214]159                return tmp;
[162]160        };
[211]161        vec evallog_m ( const mat &Val ) const {
[477]162                vec x = zeros ( Val.cols() );
[192]163                for ( int i = 0; i < w.length(); i++ ) {
[477]164                        x += w ( i ) * exp ( Coms ( i )->evallog_m ( Val ) );
[182]165                }
[192]166                return log ( x );
[182]167        };
[214]168        //! Auxiliary function that returns pdflog for each component
[211]169        mat evallog_M ( const mat &Val ) const {
[192]170                mat X ( w.length(), Val.cols() );
171                for ( int i = 0; i < w.length(); i++ ) {
[211]172                        X.set_row ( i, w ( i ) *exp ( Coms ( i )->evallog_m ( Val ) ) );
[189]173                }
174                return X;
175        };
[107]176
[504]177        shared_ptr<epdf> marginal ( const RV &rv ) const;
[536]178        //! Update already existing marginal density  \c target
[504]179        void marginal ( const RV &rv, emix &target ) const;
180        shared_ptr<mpdf> condition ( const RV &rv ) const;
[182]181
[107]182//Access methods
[162]183        //! returns a pointer to the internal mean value. Use with Care!
[477]184        vec& _w() {
185                return w;
186        }
[204]187
[193]188        //!access function
[504]189        shared_ptr<epdf> _Coms ( int i ) {
[477]190                return Coms ( i );
[286]191        }
[504]192
[477]193        void set_rv ( const RV &rv ) {
194                epdf::set_rv ( rv );
195                for ( int i = 0; i < Coms.length(); i++ ) {
196                        Coms ( i )->set_rv ( rv );
197                }
198        }
[107]199};
[529]200SHAREDPTR( emix );
[107]201
[333]202/*!
203* \brief Mixture of egiws
204
205*/
206class egiwmix : public egiw {
207protected:
208        //! weights of the components
209        vec w;
210        //! Component (epdfs)
211        Array<egiw*> Coms;
212        //!Flag if owning Coms
213        bool destroyComs;
214public:
215        //!Default constructor
216        egiwmix ( ) : egiw ( ) {};
217
218        //! Set weights \c w and components \c Coms
219        //!By default Coms are copied inside. Parameter \c copy can be set to false if Coms live externally. Use method ownComs() if Coms should be destroyed by the destructor.
[477]220        void set_parameters ( const vec &w, const Array<egiw*> &Coms, bool copy = false );
[333]221
222        //!return expected value
223        vec mean() const;
224
225        //!return a sample from the density
226        vec sample() const;
227
228        //!return the expected variance
[477]229        vec variance() const;
[333]230
231        // TODO!!! Defined to follow ANSI and/or for future development
232        void mean_mat ( mat &M, mat&R ) const {};
[477]233        double evallog_nn ( const vec &val ) const {
234                return 0;
235        };
236        double lognc () const {
237                return 0;
[504]238        }
[333]239
[504]240        shared_ptr<epdf> marginal ( const RV &rv ) const;
241        void marginal ( const RV &rv, emix &target ) const;
242
[333]243//Access methods
244        //! returns a pointer to the internal mean value. Use with Care!
[477]245        vec& _w() {
246                return w;
247        }
248        virtual ~egiwmix() {
249                if ( destroyComs ) {
250                        for ( int i = 0; i < Coms.length(); i++ ) {
251                                delete Coms ( i );
252                        }
253                }
254        }
[333]255        //! Auxiliary function for taking ownership of the Coms()
[477]256        void ownComs() {
257                destroyComs = true;
258        }
[333]259
260        //!access function
[477]261        egiw* _Coms ( int i ) {
262                return Coms ( i );
263        }
[333]264
[477]265        void set_rv ( const RV &rv ) {
266                egiw::set_rv ( rv );
267                for ( int i = 0; i < Coms.length(); i++ ) {
268                        Coms ( i )->set_rv ( rv );
269                }
[333]270        }
271
272        //! Approximation of a GiW mix by a single GiW pdf
273        egiw* approx();
274};
275
[115]276/*! \brief Chain rule decomposition of epdf
277
[145]278Probability density in the form of Chain-rule decomposition:
279\[
280f(x_1,x_2,x_3) = f(x_1|x_2,x_3)f(x_2,x_3)f(x_3)
281\]
282Note that
[115]283*/
[507]284class mprod: public mpdf {
285private:
286        Array<shared_ptr<mpdf> > mpdfs;
287
[192]288        //! Data link for each mpdfs
[546]289        Array<shared_ptr<datalink_m2m> > dls;
[461]290
[546]291protected:
[487]292        //! dummy epdf used only as storage for RV and dim
293        epdf iepdf;
[461]294
[162]295public:
[507]296        //! \brief Default constructor
297        mprod() { }
298
299        /*!\brief Constructor from list of mFacs
[165]300        */
[507]301        mprod ( const Array<shared_ptr<mpdf> > &mFacs ) {
[477]302                set_elements ( mFacs );
[461]303        }
[536]304        //! Set internal \c mpdfs from given values
[507]305        void set_elements (const Array<shared_ptr<mpdf> > &mFacs );
[477]306
[211]307        double evallogcond ( const vec &val, const vec &cond ) {
[162]308                int i;
[270]309                double res = 0.0;
[477]310                for ( i = mpdfs.length() - 1; i >= 0; i-- ) {
[193]311                        /*                      if ( mpdfs(i)->_rvc().count() >0) {
312                                                        mpdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) );
313                                                }
314                                                // add logarithms
[270]315                                                res += epdfs ( i )->evallog ( dls ( i )->pushdown ( val ) );*/
316                        res += mpdfs ( i )->evallogcond (
317                                   dls ( i )->pushdown ( val ),
[204]318                                   dls ( i )->get_cond ( val, cond )
319                               );
[145]320                }
[193]321                return res;
[162]322        }
[477]323        vec evallogcond_m ( const mat &Dt, const vec &cond ) {
324                vec tmp ( Dt.cols() );
325                for ( int i = 0; i < Dt.cols(); i++ ) {
326                        tmp ( i ) = evallogcond ( Dt.get_col ( i ), cond );
[395]327                }
328                return tmp;
329        };
[477]330        vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) {
331                vec tmp ( Dt.length() );
332                for ( int i = 0; i < Dt.length(); i++ ) {
333                        tmp ( i ) = evallogcond ( Dt ( i ), cond );
[395]334                }
[477]335                return tmp;
[395]336        };
337
338
[270]339        //TODO smarter...
340        vec samplecond ( const vec &cond ) {
[477]341                //! Ugly hack to help to discover if mpfs are not in proper order. Correct solution = check that explicitely.
[487]342                vec smp = std::numeric_limits<double>::infinity() * ones ( dimension() );
[165]343                vec smpi;
[192]344                // Hard assumption here!!! We are going backwards, to assure that samples that are needed from smp are already generated!
[477]345                for ( int i = ( mpdfs.length() - 1 ); i >= 0; i-- ) {
[487]346                        // generate contribution of this mpdf
347                        smpi = mpdfs(i)->samplecond(dls ( i )->get_cond ( smp , cond ));                       
[162]348                        // copy contribution of this pdf into smp
[270]349                        dls ( i )->pushup ( smp, smpi );
[145]350                }
[162]351                return smp;
352        }
353
[395]354        //! Load from structure with elements:
355        //!  \code
356        //! { class='mprod';
357        //!   mpdfs = (..., ...);     // list of mpdfs in the order of chain rule
358        //! }
359        //! \endcode
360        //!@}
[477]361        void from_setting ( const Setting &set ) {
[527]362                Array<shared_ptr<mpdf> > atmp; //temporary Array
[507]363                UI::get ( atmp, set, "mpdfs", UI::compulsory );
[527]364                set_elements ( atmp );
[395]365        }
[107]366};
[477]367UIREGISTER ( mprod );
[529]368SHAREDPTR ( mprod );
[107]369
[168]370//! Product of independent epdfs. For dependent pdfs, use mprod.
371class eprod: public epdf {
372protected:
373        //! Components (epdfs)
[170]374        Array<const epdf*> epdfs;
[168]375        //! Array of indeces
[270]376        Array<datalink*> dls;
[168]377public:
[536]378        //! Default constructor
[477]379        eprod () : epdfs ( 0 ), dls ( 0 ) {};
[536]380        //! Set internal
[477]381        void set_parameters ( const Array<const epdf*> &epdfs0, bool named = true ) {
382                epdfs = epdfs0;//.set_length ( epdfs0.length() );
[286]383                dls.set_length ( epdfs.length() );
384
[477]385                bool independent = true;
[286]386                if ( named ) {
[477]387                        for ( int i = 0; i < epdfs.length(); i++ ) {
388                                independent = rv.add ( epdfs ( i )->_rv() );
[565]389                                bdm_assert_debug ( independent, "eprod:: given components are not independent." );
[286]390                        }
[477]391                        dim = rv._dsize();
392                } else {
393                        dim = 0;
394                        for ( int i = 0; i < epdfs.length(); i++ ) {
395                                dim += epdfs ( i )->dimension();
[286]396                        }
[204]397                }
[286]398                //
[477]399                int cumdim = 0;
400                int dimi = 0;
[286]401                int i;
[477]402                for ( i = 0; i < epdfs.length(); i++ ) {
[286]403                        dls ( i ) = new datalink;
[477]404                        if ( named ) {
405                                dls ( i )->set_connection ( epdfs ( i )->_rv() , rv );
406                        } else {
[286]407                                dimi = epdfs ( i )->dimension();
[477]408                                dls ( i )->set_connection ( dimi, dim, linspace ( cumdim, cumdim + dimi - 1 ) );
409                                cumdim += dimi;
[286]410                        }
411                }
[168]412        }
413
414        vec mean() const {
[270]415                vec tmp ( dim );
[477]416                for ( int i = 0; i < epdfs.length(); i++ ) {
[168]417                        vec pom = epdfs ( i )->mean();
[270]418                        dls ( i )->pushup ( tmp, pom );
[168]419                }
420                return tmp;
421        }
[229]422        vec variance() const {
[270]423                vec tmp ( dim ); //second moment
[477]424                for ( int i = 0; i < epdfs.length(); i++ ) {
[229]425                        vec pom = epdfs ( i )->mean();
[477]426                        dls ( i )->pushup ( tmp, pow ( pom, 2 ) );
[229]427                }
[477]428                return tmp - pow ( mean(), 2 );
[229]429        }
[168]430        vec sample() const {
[270]431                vec tmp ( dim );
[477]432                for ( int i = 0; i < epdfs.length(); i++ ) {
[168]433                        vec pom = epdfs ( i )->sample();
[270]434                        dls ( i )->pushup ( tmp, pom );
[168]435                }
436                return tmp;
437        }
[211]438        double evallog ( const vec &val ) const {
[477]439                double tmp = 0;
440                for ( int i = 0; i < epdfs.length(); i++ ) {
441                        tmp += epdfs ( i )->evallog ( dls ( i )->pushdown ( val ) );
[168]442                }
[565]443                bdm_assert_debug ( std::isfinite ( tmp ), "Infinite" );
[168]444                return tmp;
445        }
[170]446        //!access function
[477]447        const epdf* operator () ( int i ) const {
[565]448                bdm_assert_debug ( i < epdfs.length(), "wrong index" );
[477]449                return epdfs ( i );
450        }
[204]451
[193]452        //!Destructor
[477]453        ~eprod() {
454                for ( int i = 0; i < epdfs.length(); i++ ) {
455                        delete dls ( i );
456                }
457        }
[168]458};
459
460
[488]461/*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal RV and RVC
[124]462
463*/
[488]464class mmix : public mpdf {
465protected:
466        //! Component (mpdfs)
467        Array<shared_ptr<mpdf> > Coms;
468        //!weights of the components
469        vec w;
470        //! dummy epdfs
471        epdf dummy_epdf;
472public:
473        //!Default constructor
474        mmix() : Coms(0), dummy_epdf() { set_ep(dummy_epdf);    }
[461]475
[488]476        //! Set weights \c w and components \c R
477        void set_parameters ( const vec &w0, const Array<shared_ptr<mpdf> > &Coms0 ) {
[536]478                //!\todo check if all components are OK
[488]479                Coms = Coms0;
480                w=w0;   
[503]481
[513]482                if (Coms0.length()>0){
483                        set_rv(Coms(0)->_rv());
[515]484                        dummy_epdf.set_parameters(Coms(0)->_rv()._dsize());
[513]485                        set_rvc(Coms(0)->_rvc());
486                        dimc = rvc._dsize();
487                }
[488]488        }
489        double evallogcond (const vec &dt, const vec &cond) {
490                double ll=0.0;
491                for (int i=0;i<Coms.length();i++){
492                        ll+=Coms(i)->evallogcond(dt,cond);
493                }
494                return ll;
495        }
496
497        vec samplecond (const vec &cond);
498
499};
500
[254]501}
[107]502#endif //MX_H
Note: See TracBrowser for help on using the browser.