root/library/bdm/stat/merger.h @ 750

Revision 750, 6.7 kB (checked in by sarka, 15 years ago)

dimc ze set_parameters do validate

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief Mergers for combination 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
13#ifndef MERGER_H
14#define MERGER_H
15
16
17#include "../estim/mixtures.h"
18#include "discrete.h"
19
20namespace bdm {
21using std::string;
22
23//!Merging methods
24enum MERGER_METHOD {ARITHMETIC = 1, GEOMETRIC = 2, LOGNORMAL = 3};
25
26/*!
27@brief Base class for general combination of pdfs on discrete support
28
29Mixtures of Gaussian densities are used internally. Switching to other densities should be trivial.
30
31The merged pdfs are expected to be of the form:
32 \f[ f(x_i|y_i),  i=1..n \f]
33where the resulting merger is a density on \f$ \cup [x_i,y_i] \f$ .
34Note that all variables will be joined.
35
36As a result of this feature, each source must be extended to common support
37\f[ f(z_i|y_i,x_i) f(x_i|y_i) f(y_i)  i=1..n \f]
38where \f$ z_i \f$ accumulate variables that were not in the original source.
39These extensions are calculated on-the-fly.
40
41However, these operations can not be performed in general. Hence, this class merges only sources on common support, \f$ y_i={}, z_i={}, \forall i \f$.
42For merging of more general cases, use offsprings merger_mix and merger_grid.
43*/
44
45class merger_base : public epdf {
46protected:
47        //! Elements of composition
48        Array<shared_ptr<pdf> > pdfs;
49
50        //! Data link for each pdf in pdfs
51        Array<datalink_m2e*> dls;
52
53        //! Array of rvs that are not modelled by pdfs at all, \f$ z_i \f$
54        Array<RV> rvzs;
55
56        //! Data Links for extension \f$ f(z_i|x_i,y_i) \f$
57        Array<datalink_m2e*> zdls;
58
59        //! number of support points
60        int Npoints;
61
62        //! number of sources
63        int Nsources;
64
65        //! switch of the methoh used for merging
66        MERGER_METHOD METHOD;
67        //! Default for METHOD
68        static const MERGER_METHOD DFLT_METHOD;
69
70        //!Prior on the log-normal merging model
71        double beta;
72        //! default for beta
73        static const double DFLT_beta;
74
75        //! Projection to empirical density (could also be piece-wise linear)
76        eEmp eSmp;
77
78        //! debug or not debug
79        bool DBG;
80
81        //! debugging file
82        it_file* dbg_file;
83public:
84        //! \name Constructors
85        //! @{
86
87        //! Default constructor
88        merger_base () : Npoints ( 0 ), Nsources ( 0 ), DBG ( false ), dbg_file ( 0 ) {
89        }
90
91        //!Constructor from sources
92        merger_base ( const Array<shared_ptr<pdf> > &S );
93
94        //! Function setting the main internal structures
95        void set_sources ( const Array<shared_ptr<pdf> > &Sources );
96
97        //! Set support points from rectangular grid
98        void set_support ( rectangular_support &Sup );
99
100        //! Set support points from dicrete grid
101        void set_support ( discrete_support &Sup ) {
102                Npoints = Sup.points();
103                eSmp.set_parameters ( Sup._Spoints() );
104                eSmp.validate();
105        }
106        //! set debug file
107        void set_debug_file ( const string fname ) {
108                if ( DBG ) delete dbg_file;
109                dbg_file = new it_file ( fname );
110                if ( dbg_file ) DBG = true;
111        }
112        //! Set internal parameters used in approximation
113        void set_method ( MERGER_METHOD MTH = DFLT_METHOD, double beta0 = DFLT_beta ) {
114                METHOD = MTH;
115                beta = beta0;
116        }
117        //! Set support points from a pdf by drawing N samples
118        void set_support ( const epdf &overall, int N ) {
119                eSmp.set_statistics ( overall, N );
120                Npoints = N;
121        }
122
123        //! Destructor
124        virtual ~merger_base() {
125                for ( int i = 0; i < Nsources; i++ ) {
126                        delete dls ( i );
127                        delete zdls ( i );
128                }
129                if ( DBG ) delete dbg_file;
130        };
131        //!@}
132
133        //! \name Mathematical operations
134        //!@{
135
136        //!Merge given sources in given points
137        virtual void merge ();
138
139        //! Merge log-likelihood values in points using method specified by parameter METHOD
140        vec merge_points ( mat &lW );
141
142
143        //! sample from merged density
144//! weight w is a
145        vec mean() const;
146
147        mat covariance() const;
148
149        vec variance() const;
150
151        //!@}
152
153        //! \name Access to attributes
154        //! @{
155
156        //! Access function
157        eEmp& _Smp() {
158                return eSmp;
159        }
160
161        //! load from setting
162        void from_setting ( const Setting& set ) {
163                // get support
164                // find which method to use
165                string meth_str;
166                UI::get<string> ( meth_str, set, "method", UI::compulsory );
167                if ( !strcmp ( meth_str.c_str(), "arithmetic" ) )
168                        set_method ( ARITHMETIC );
169                else {
170                        if ( !strcmp ( meth_str.c_str(), "geometric" ) )
171                                set_method ( GEOMETRIC );
172                        else if ( !strcmp ( meth_str.c_str(), "lognormal" ) ) {
173                                set_method ( LOGNORMAL );
174                                set.lookupValue ( "beta", beta );
175                        }
176                }
177                string dbg_file;
178                if ( UI::get ( dbg_file, set, "dbg_file" ) )
179                        set_debug_file ( dbg_file );
180                //validate() - not used
181        }
182
183        void validate() {
184                bdm_assert ( eSmp._w().length() > 0, "Empty support, use set_support()." );
185                bdm_assert ( dim == eSmp._samples() ( 0 ).length(), "Support points and rv are not compatible!" );
186                bdm_assert ( isnamed(), "mergers must be named" );
187        }
188        //!@}
189};
190UIREGISTER ( merger_base );
191SHAREDPTR ( merger_base );
192
193//! Merger using importance sampling with mixture proposal density
194class merger_mix : public merger_base {
195protected:
196        //!Internal mixture of EF models
197        MixEF Mix;
198        //!Number of components in a mixture
199        int Ncoms;
200        //! coefficient of resampling [0,1]
201        double effss_coef;
202        //! stop after niter iterations
203        int stop_niter;
204
205        //! default value for Ncoms
206        static const int DFLT_Ncoms;
207        //! default value for efss_coef;
208        static const double DFLT_effss_coef;
209
210public:
211        //!\name Constructors
212        //!@{
213        merger_mix () : Ncoms ( 0 ), effss_coef ( 0 ), stop_niter ( 0 ) { }
214
215        merger_mix ( const Array<shared_ptr<pdf> > &S ) :
216                        Ncoms ( 0 ), effss_coef ( 0 ), stop_niter ( 0 ) {
217                set_sources ( S );
218        }
219
220        //! Set sources and prepare all internal structures
221        void set_sources ( const Array<shared_ptr<pdf> > &S ) {
222                merger_base::set_sources ( S );
223                Nsources = S.length();
224        }
225
226        //! Set internal parameters used in approximation
227        void set_parameters ( int Ncoms0 = DFLT_Ncoms, double effss_coef0 = DFLT_effss_coef ) {
228                Ncoms = Ncoms0;
229                effss_coef = effss_coef0;
230        }
231        //!@}
232
233        //! \name Mathematical operations
234        //!@{
235
236        //!Merge values using mixture approximation
237        void merge ();
238
239        //! sample from the approximating mixture
240        vec sample () const {
241                return Mix.posterior().sample();
242        }
243        //! loglikelihood computed on mixture models
244        double evallog ( const vec &yt ) const {
245                vec dtf = ones ( yt.length() + 1 );
246                dtf.set_subvector ( 0, yt );
247                return Mix.logpred ( dtf );
248        }
249        //!@}
250
251        //!\name Access functions
252        //!@{
253//! Access function
254        MixEF& _Mix() {
255                return Mix;
256        }
257        //! Access function
258        emix* proposal() {
259                emix* tmp = Mix.epredictor();
260                tmp->set_rv ( rv );
261                return tmp;
262        }
263        //! from_settings
264        void from_setting ( const Setting& set ) {
265                merger_base::from_setting ( set );
266                set.lookupValue ( "ncoms", Ncoms );
267                set.lookupValue ( "effss_coef", effss_coef );
268                set.lookupValue ( "stop_niter", stop_niter );
269        }
270
271        //! @}
272
273};
274UIREGISTER ( merger_mix );
275SHAREDPTR ( merger_mix );
276
277}
278
279#endif // MER_H
Note: See TracBrowser for help on using the browser.