Changeset 766 for library/bdm/stat

Show
Ignore:
Timestamp:
01/11/10 22:55:57 (14 years ago)
Author:
mido
Message:

abstract methods restored wherever they are meaningful
macros NOT_IMPLEMENTED and NOT_IMPLEMENTED_VOID defined to make sources shorter
emix::set_parameters and mmix::set_parameters removed, corresponding acces methods created and the corresponding validate methods improved appropriately
some compilator warnings were avoided
and also a few other things cleaned up

Location:
library/bdm/stat
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/stat/discrete.cpp

    r760 r766  
    8181                                inds ( j ) = gridsizes ( j ) - 1; 
    8282                        else { 
    83                                 inds ( j ) = ::round ( val ( j ) - ranges ( j ) ( 0 ) / steps ( j ) ); 
     83                                inds ( j ) = (int) ::round ( val ( j ) - ranges ( j ) ( 0 ) / steps ( j ) ); 
    8484                        } 
    8585                } 
  • library/bdm/stat/emix.cpp

    r760 r766  
    33namespace bdm { 
    44 
    5 void emix::set_parameters ( const vec &w0, const Array<shared_ptr<epdf> > &Coms0 ) { 
    6         w = w0 / sum ( w0 ); 
    7          
    8         bool isnamed = Coms0 ( 0 )->isnamed(); 
    9         RV tmp_rv; 
    10         if ( isnamed ) tmp_rv = Coms0 ( 0 )->_rv(); 
    11         Coms = Coms0; 
    12  
    13         if ( isnamed ) epdf::set_rv ( tmp_rv ); //coms aer already OK, no need for set_rv 
    14 } 
    15  
    165void emix::validate (){ 
    17         dim = Coms ( 0 )->dimension(); 
    18         bool isnamed = Coms ( 0 )->isnamed(); 
    19         int i; 
    20         RV tmp_rv; 
    21         if ( isnamed ) tmp_rv = Coms ( 0 )->_rv(); 
    22         for ( i = 0; i < w.length(); i++ ) { 
     6        bdm_assert ( Coms.length() > 0, "There has to be at least one component." ); 
     7 
     8        bdm_assert ( Coms.length() == w.length(), "It is obligatory to define weights of all the components." ); 
     9 
     10        double sum_w = sum ( w ); 
     11        bdm_assert ( sum_w != 0, "There has to be a component with non-zero weight." ); 
     12        w = w / sum_w; 
     13 
     14        int dim = Coms ( 0 )->dimension(); 
     15        for ( int i = 1; i < Coms.length(); i++ ) { 
    2316                bdm_assert ( dim == ( Coms ( i )->dimension() ), "Component sizes do not match!" ); 
    24                 bdm_assert ( !isnamed || tmp_rv.equal ( Coms ( i )->_rv() ), "Component RVs do not match!" ); 
    25         } 
    26  
    27 } 
    28  
    29  
     17                bdm_assert ( Coms(i)->isnamed() , "An unnamed component is forbidden here!" ); 
     18        } 
     19 
     20        set_rv ( Coms ( 0 )->_rv() );  
     21} 
     22 
     23void emix::from_setting ( const Setting &set ) { 
     24        UI::get ( Coms, set, "pdfs", UI::compulsory ); 
     25 
     26        if ( !UI::get ( w, set, "weights", UI::optional ) ) { 
     27                int len = Coms.length(); 
     28                w.set_length ( len ); 
     29                w = 1.0 / len; 
     30        } 
     31 
     32        validate(); 
     33} 
    3034 
    3135 
     
    109113        } 
    110114 
    111         target.set_parameters ( w, Cn ); 
     115        target._w() = w; 
     116        target._Coms() = Cn; 
    112117        target.validate(); 
    113118} 
     
    190195        } 
    191196 
    192         target.set_parameters ( w, Cn ); 
     197        target._w() = w; 
     198        target._Coms() = Cn; 
    193199        target.validate(); 
    194200} 
     
    313319                dls ( i ) = new datalink_m2m; 
    314320                dls ( i )->set_connection ( pdfs ( i )->_rv(), pdfs ( i )->_rvc(), _rv(), _rvc() ); 
     321        } 
     322} 
     323 
     324void mmix::validate() 
     325{        
     326        bdm_assert ( Coms.length() > 0, "There has to be at least one component." ); 
     327 
     328        bdm_assert ( Coms.length() == w.length(), "It is obligatory to define weights of all the components." ); 
     329 
     330        double sum_w = sum ( w ); 
     331        bdm_assert ( sum_w != 0, "There has to be a component with non-zero weight." ); 
     332        w = w / sum_w; 
     333 
     334        int dim = Coms ( 0 )->dimension(); 
     335        int dimc = Coms ( 0 )->dimensionc(); 
     336        for ( int i = 1; i < Coms.length(); i++ ) { 
     337                bdm_assert ( dim == ( Coms ( i )->dimension() ), "Component sizes do not match!" ); 
     338                bdm_assert ( dimc == ( Coms ( i )->dimensionc() ), "Component sizes do not match!" ); 
     339                bdm_assert ( Coms(i)->isnamed() , "An unnamed component is forbidden here!" ); 
     340        } 
     341 
     342        set_rv ( Coms ( 0 )->_rv() ); 
     343        set_rvc ( Coms ( 0 )->_rvc() ); 
     344} 
     345 
     346void mmix::from_setting ( const Setting &set ) { 
     347        UI::get ( Coms, set, "pdfs", UI::compulsory ); 
     348 
     349        if ( !UI::get ( w, set, "weights", UI::optional ) ) { 
     350                int len = Coms.length(); 
     351                w.set_length ( len ); 
     352                w = 1.0 / len; 
    315353        } 
    316354} 
  • library/bdm/stat/emix.h

    r750 r766  
    5353                // adjust rv and rvc 
    5454 
    55                 set_rv ( rv ); // TODO co kdyby tohle samo uz nastavovalo dimension?!?! 
     55                set_rv ( rv );  
    5656                dim = rv._dsize(); 
    5757 
     
    8181                return tmp; 
    8282        } 
     83 
     84        //! Returns a sample from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 
     85        virtual vec samplecond ( const vec &cond ) NOT_IMPLEMENTED(0); 
     86 
    8387        //! Object takes ownership of nom and will destroy it 
    8488        void ownnom() { 
     
    9195                } 
    9296        } 
     97 
     98 
     99 
    93100 
    94101private: 
     
    120127        emix ( ) : epdf ( ) { } 
    121128 
    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         */ 
    128         void set_parameters ( const vec &w, const Array<shared_ptr<epdf> > &Coms ); 
    129      
    130129        virtual void validate (); 
    131130 
     
    148147        shared_ptr<pdf> condition ( const RV &rv ) const; 
    149148 
    150         //Access methods 
    151         //! returns a pointer to the internal mean value. Use with Care! 
     149        //Access methods         
     150        //! returns a reference to the internal weights. Use with Care! 
    152151        vec& _w() { 
    153152                return w; 
    154153        } 
    155154 
    156         //!access function 
     155        /*! 
     156          \brief returns a reference to the internal array of components. Use with Care! Set components \c Coms 
     157 
     158          Shared pointers in Coms are kept inside this instance and 
     159          shouldn't be modified after being passed to this method. 
     160        */ 
     161        Array<shared_ptr<epdf>>& _Coms ( ) { 
     162                return Coms; 
     163        } 
     164 
     165        //! returns a reference to the internal components specified by index i. Use with Care! 
    157166        shared_ptr<epdf> _Coms ( int i ) { 
    158167                return Coms ( i ); 
     
    174183        //! \endcode 
    175184        //!@} 
    176         void from_setting ( const Setting &set ) { 
    177  
    178                 vec w0; 
    179                 Array<shared_ptr<epdf> > Coms0; 
    180  
    181                 UI::get ( Coms0, set, "pdfs", UI::compulsory ); 
    182  
    183                 if ( !UI::get ( w0, set, "weights", UI::optional ) ) { 
    184                         int len = Coms.length(); 
    185                         w0.set_length ( len ); 
    186                         w0 = 1.0 / len; 
    187                 } 
    188  
    189                 // TODO asi lze nacitat primocare do w a coms, jen co bude hotovy validate() 
    190                 set_parameters ( w0, Coms0 ); 
    191                 validate(); 
    192         } 
     185        void from_setting ( const Setting &set ); 
    193186}; 
    194187SHAREDPTR ( emix ); 
     
    284277        //! Data link for each pdfs 
    285278        Array<shared_ptr<datalink_m2m> > dls; 
    286  
    287 protected: 
    288         //! dummy epdf used only as storage for RV and dim 
    289         epdf iepdf; 
    290279 
    291280public: 
     
    330319        //!@} 
    331320        void from_setting ( const Setting &set ) { 
    332                 Array<shared_ptr<pdf> > atmp; //temporary Array 
    333                 UI::get ( atmp, set, "pdfs", UI::compulsory ); 
    334                 set_elements ( atmp ); 
     321                Array<shared_ptr<pdf> > temp_array;  
     322                UI::get ( temp_array, set, "pdfs", UI::compulsory ); 
     323                set_elements ( temp_array ); 
    335324        } 
    336325}; 
     
    418407        mmix() : Coms ( 0 ) { } 
    419408 
    420         //! Set weights \c w and components \c R 
    421         void set_parameters ( const vec &w0, const Array<shared_ptr<pdf> > &Coms0 ) { 
    422                 //!\todo check if all components are OK 
    423                 Coms = Coms0; 
    424                 w = w0; 
    425  
    426                 if ( Coms0.length() > 0 ) { 
    427                         set_rv ( Coms ( 0 )->_rv() ); 
    428                         dim = rv._dsize(); 
    429                         set_rvc ( Coms ( 0 )->_rvc() ); 
    430                         dimc = rvc._dsize(); 
    431                 } 
    432         } 
    433409        double evallogcond ( const vec &dt, const vec &cond ) { 
    434410                double ll = 0.0; 
     
    449425        //! \endcode 
    450426        //!@} 
    451         void from_setting ( const Setting &set ) { 
    452                 UI::get ( Coms, set, "pdfs", UI::compulsory ); 
    453  
    454                 // TODO ma byt zde, ci ve validate()? 
    455                 if ( Coms.length() > 0 ) { 
    456                         set_rv ( Coms ( 0 )->_rv() ); 
    457                         dim = rv._dsize(); 
    458                         set_rvc ( Coms ( 0 )->_rvc() ); 
    459                         dimc = rvc._dsize(); 
    460                 } 
    461  
    462                 if ( !UI::get ( w, set, "weights", UI::optional ) ) { 
    463                         int len = Coms.length(); 
    464                         w.set_length ( len ); 
    465                         w = 1.0 / len; 
    466                 } 
    467         } 
     427        void from_setting ( const Setting &set ); 
     428 
     429        virtual void validate(); 
    468430}; 
    469431SHAREDPTR ( mmix ); 
  • library/bdm/stat/exp_family.h

    r763 r766  
    4444 
    4545        //!Evaluate normalized log-probability 
    46         virtual double evallog_nn ( const vec &val ) const { 
    47                 bdm_error ( "Not implemented" ); 
    48                 return 0.0; 
    49         } 
     46        virtual double evallog_nn ( const vec &val ) const NOT_IMPLEMENTED(0); 
    5047 
    5148        //!Evaluate normalized log-probability 
     
    7370 
    7471        //!Power of the density, used e.g. to flatten the density 
    75         virtual void pow ( double p ) { 
    76                 bdm_error ( "Not implemented" ); 
    77         } 
     72        virtual void pow ( double p ) NOT_IMPLEMENTED_VOID; 
    7873}; 
    7974 
     
    9287        BMEF ( const BMEF &B ) : BM ( B ), frg ( B.frg ), last_lognc ( B.last_lognc ) {} 
    9388        //!get statistics from another model 
    94         virtual void set_statistics ( const BMEF* BM0 ) { 
    95                 bdm_error ( "Not implemented" ); 
    96         } 
     89        virtual void set_statistics ( const BMEF* BM0 ) NOT_IMPLEMENTED_VOID; 
    9790 
    9891        //! Weighted update of sufficient statistics (Bayes rule) 
     
    10295 
    10396        //!Flatten the posterior according to the given BMEF (of the same type!) 
    104         virtual void flatten ( const BMEF * B ) { 
    105                 bdm_error ( "Not implemented" ); 
    106         } 
    107  
    108         BMEF* _copy_ () const { 
    109                 bdm_error ( "function _copy_ not implemented for this BM" ); 
    110                 return NULL; 
    111         } 
     97        virtual void flatten ( const BMEF * B ) NOT_IMPLEMENTED_VOID; 
     98 
     99        double logpred ( const vec &yt ) const NOT_IMPLEMENTED(0); 
     100         
     101        virtual epdf* epredictor() const NOT_IMPLEMENTED(NULL); 
     102 
     103        virtual pdf* predictor() const NOT_IMPLEMENTED(NULL); 
    112104 
    113105        void to_setting ( Setting &set ) const 
    114106        {                        
    115107                BM::to_setting( set ); 
    116                 // TODO DOPLNIT? CHYBI FROM_SETTING PRO INSPIRACI 
     108                // TODO DOPLNIT? ALE MOMENTALNE CHYBI FROM_SETTING PRO INSPIRACI 
    117109        }  
    118110}; 
     
    11121104        //! Constructor 
    11131105        migamma_ref () : migamma (), refl () {}; 
     1106         
    11141107        //! Set value of \c k 
    11151108        void set_parameters ( double k0 , vec ref0, double l0 ) { 
     
    12661259                return X*Y._Ch();// return upper triangular part of the decomposition 
    12671260        } 
     1261 
    12681262        vec sample () const { 
    12691263                return vec ( sample_mat()._data(), p*p ); 
    12701264        } 
     1265 
     1266        virtual vec mean() const NOT_IMPLEMENTED(0); 
     1267 
     1268        //! return expected variance (not covariance!) 
     1269        virtual vec variance() const NOT_IMPLEMENTED(0); 
     1270 
     1271        virtual double evallog ( const vec &val ) const NOT_IMPLEMENTED(0); 
     1272 
    12711273        //! fast access function y0 will be copied into Y.Ch. 
    12721274        void setY ( const mat &Ch0 ) { 
    12731275                copy_vector ( dim, Ch0._data(), Y._Ch()._data() ); 
    12741276        } 
     1277 
    12751278        //! fast access function y0 will be copied into Y.Ch. 
    12761279        void _setY ( const vec &ch0 ) { 
    12771280                copy_vector ( dim, ch0._data(), Y._Ch()._data() ); 
    12781281        } 
     1282 
    12791283        //! access function 
    12801284        const chmat& getY() const { 
     
    13211325                W.setY ( iCh ); 
    13221326        } 
     1327 
    13231328        virtual double evallog ( const vec &val ) const { 
    13241329                chmat X ( p ); 
     
    13451350        }; 
    13461351 
     1352        virtual vec mean() const NOT_IMPLEMENTED(0); 
     1353 
     1354        //! return expected variance (not covariance!) 
     1355        virtual vec variance() const NOT_IMPLEMENTED(0); 
    13471356}; 
    13481357 
     
    14641473 
    14651474        //! inherited operation : NOT implemented 
    1466         vec sample() const { 
    1467                 bdm_error ( "Not implemented" ); 
    1468                 return vec(); 
    1469         } 
     1475        vec sample() const NOT_IMPLEMENTED(0); 
    14701476 
    14711477        //! inherited operation : NOT implemented 
    1472         double evallog ( const vec &val ) const { 
    1473                 bdm_error ( "Not implemented" ); 
    1474                 return 0.0; 
    1475         } 
     1478        double evallog ( const vec &val ) const NOT_IMPLEMENTED(0); 
    14761479 
    14771480        vec mean() const { 
  • library/bdm/stat/merger.h

    r763 r766  
    148148 
    149149        vec variance() const; 
     150 
     151        //! Compute log-probability of argument \c val 
     152        virtual double evallog ( const vec &val ) const NOT_IMPLEMENTED(0); 
     153 
     154        //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ 
     155        virtual vec sample() const NOT_IMPLEMENTED(0); 
    150156 
    151157        //!@}