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

Revision 1064, 6.6 kB (checked in by mido, 14 years ago)

astyle applied all over the library

  • 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
107
108    //! set debug file
109    void set_debug_file ( const string fname ) {
110        if ( DBG ) delete dbg_file;
111        dbg_file = new it_file ( fname );
112        DBG = ( dbg_file != 0 );
113    }
114
115    //! Set internal parameters used in approximation
116    void set_method ( MERGER_METHOD MTH = DFLT_METHOD, double beta0 = DFLT_beta ) {
117        METHOD = MTH;
118        beta = beta0;
119    }
120    //! Set support points from a pdf by drawing N samples
121    void set_support ( const epdf &overall, int N ) {
122        eSmp.set_statistics ( overall, N );
123        Npoints = N;
124    }
125
126    //! Destructor
127    virtual ~merger_base() {
128        for ( int i = 0; i < Nsources; i++ ) {
129            delete dls ( i );
130            delete zdls ( i );
131        }
132        if ( DBG ) delete dbg_file;
133    };
134    //!@}
135
136    //! \name Mathematical operations
137    //!@{
138
139    //!Merge given sources in given points
140    virtual void merge ();
141
142    //! Merge log-likelihood values in points using method specified by parameter METHOD
143    vec merge_points ( mat &lW );
144
145
146    //! sample from merged density
147//! weight w is a
148    vec mean() const;
149
150    mat covariance() const;
151
152    vec variance() const;
153
154    //! Compute log-probability of argument \c val
155    virtual double evallog ( const vec &val ) const NOT_IMPLEMENTED(0);
156
157    //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$
158    virtual vec sample() const NOT_IMPLEMENTED(0);
159
160    //!@}
161
162    //! \name Access to attributes
163    //! @{
164
165    //! Access function
166    eEmp& _Smp() {
167        return eSmp;
168    }
169
170    //! load from setting
171    void from_setting ( const Setting& set );
172
173    void to_setting  (Setting  &set) const ;
174
175    void validate() ;
176    //!@}
177};
178UIREGISTER ( merger_base );
179SHAREDPTR ( merger_base );
180
181//! Merger using importance sampling with mixture proposal density
182class merger_mix : public merger_base {
183protected:
184    //!Internal mixture of EF models
185    MixEF Mix;
186    //!Number of components in a mixture
187    int Ncoms;
188    //! coefficient of resampling [0,1]
189    double effss_coef;
190    //! stop after niter iterations
191    int stop_niter;
192
193    //! default value for Ncoms
194    static const int DFLT_Ncoms;
195    //! default value for efss_coef;
196    static const double DFLT_effss_coef;
197
198public:
199    //!\name Constructors
200    //!@{
201    merger_mix () : Ncoms ( 0 ), effss_coef ( 0 ), stop_niter ( 0 ) { }
202
203    merger_mix ( const Array<shared_ptr<pdf> > &S ) :
204        Ncoms ( 0 ), effss_coef ( 0 ), stop_niter ( 0 ) {
205        set_sources ( S );
206    }
207
208    //! Set sources and prepare all internal structures
209    void set_sources ( const Array<shared_ptr<pdf> > &S ) {
210        merger_base::set_sources ( S );
211        //Nsources = S.length();
212    }
213
214    //! Set internal parameters used in approximation
215    void set_parameters ( int Ncoms0 = DFLT_Ncoms, double effss_coef0 = DFLT_effss_coef ) {
216        Ncoms = Ncoms0;
217        effss_coef = effss_coef0;
218    }
219    //!@}
220
221    //! \name Mathematical operations
222    //!@{
223
224    //!Merge values using mixture approximation
225    void merge ();
226
227    //! sample from the approximating mixture
228    vec sample () const {
229        return Mix.posterior().sample();
230    }
231    //! loglikelihood computed on mixture models
232    double evallog ( const vec &yt ) const {
233        vec dtf = ones ( yt.length() + 1 );
234        dtf.set_subvector ( 0, yt );
235        return Mix.logpred ( dtf, vec(0) );
236    }
237    //!@}
238
239    //!\name Access functions
240    //!@{
241//! Access function
242    MixEF& _Mix() {
243        return Mix;
244    }
245    //! Access function
246    emix* proposal() {
247        emix* tmp = Mix.epredictor();
248        tmp->set_rv ( rv );
249        return tmp;
250    }
251    //! from_settings
252    void from_setting ( const Setting& set );
253    void        to_setting  (Setting  &set) const;
254    void validate();
255
256    //! @}
257
258};
259UIREGISTER ( merger_mix );
260SHAREDPTR ( merger_mix );
261
262}
263
264#endif // MER_H
Note: See TracBrowser for help on using the browser.