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

Revision 1068, 7.7 kB (checked in by mido, 14 years ago)

patch of documentation - all conditional pdfs revised

  • 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 (interface) for 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*/
44class merger_base : public epdf {
45protected:
46    //! Elements of composition
47    Array<shared_ptr<pdf> > pdfs;
48
49    //! Data link for each pdf in pdfs
50    Array<datalink_m2e*> dls;
51
52    //! Array of rvs that are not modelled by pdfs at all, \f$ z_i \f$
53    Array<RV> rvzs;
54
55    //! Data Links for extension \f$ f(z_i|x_i,y_i) \f$
56    Array<datalink_m2e*> zdls;
57
58    //! number of support points
59    int Npoints;
60
61    //! number of sources
62    int Nsources;
63
64    //! switch of the methoh used for merging
65    MERGER_METHOD METHOD;
66    //! Default for METHOD
67    static const MERGER_METHOD DFLT_METHOD;
68
69    //!Prior on the log-normal merging model
70    double beta;
71    //! default for beta
72    static const double DFLT_beta;
73
74    //! Projection to empirical density (could also be piece-wise linear)
75    eEmp eSmp;
76
77    //! debug or not debug
78    bool DBG;
79
80    //! debugging file
81    it_file* dbg_file;
82public:
83    //! \name Constructors
84    //! @{
85
86    //! Default constructor
87    merger_base () : Npoints ( 0 ), Nsources ( 0 ), DBG ( false ), dbg_file ( 0 ) {
88    }
89
90    //!Constructor from sources
91    merger_base ( const Array<shared_ptr<pdf> > &S );
92
93    //! Function setting the main internal structures
94    void set_sources ( const Array<shared_ptr<pdf> > &Sources );
95
96    //! Set support points from rectangular grid
97    void set_support ( rectangular_support &Sup );
98
99    //! Set support points from dicrete grid
100    void set_support ( discrete_support &Sup ) {
101        Npoints = Sup.points();
102        eSmp.set_parameters ( Sup._Spoints() );
103        eSmp.validate();
104    }
105
106
107    //! set debug file
108    void set_debug_file ( const string fname ) {
109        if ( DBG ) delete dbg_file;
110        dbg_file = new it_file ( fname );
111        DBG = ( dbg_file != 0 );
112    }
113
114    //! Set internal parameters used in approximation
115    void set_method ( MERGER_METHOD MTH = DFLT_METHOD, double beta0 = DFLT_beta ) {
116        METHOD = MTH;
117        beta = beta0;
118    }
119    //! Set support points from a pdf by drawing N samples
120    void set_support ( const epdf &overall, int N ) {
121        eSmp.set_statistics ( overall, N );
122        Npoints = N;
123    }
124
125    //! Destructor
126    virtual ~merger_base() {
127        for ( int i = 0; i < Nsources; i++ ) {
128            delete dls ( i );
129            delete zdls ( i );
130        }
131        if ( DBG ) delete dbg_file;
132    };
133    //!@}
134
135    //! \name Mathematical operations
136    //!@{
137
138    //!Merge given sources in given points
139    virtual void merge ();
140
141    //! Merge log-likelihood values in points using method specified by parameter METHOD
142    vec merge_points ( mat &lW );
143
144
145    //! sample from merged density
146//! weight w is a
147    vec mean() const;
148
149    mat covariance() const;
150
151    vec variance() const;
152
153    //! Compute log-probability of argument \c val
154    virtual double evallog ( const vec &val ) const NOT_IMPLEMENTED(0);
155
156    //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$
157    virtual vec sample() const NOT_IMPLEMENTED(0);
158
159    //!@}
160
161    //! \name Access to attributes
162    //! @{
163
164    //! Access function
165    eEmp& _Smp() {
166        return eSmp;
167    }
168
169
170    }
171
172    /*! Create object from the following structure
173
174    \code
175    class = 'merger_base';
176    method = 'arithmetic' or 'geometric' or 'lognormal';   % name of the model used for merging
177    --- compulsory only for lognormal merging model ---
178    beta = x;                                              % scalar prior on the log-normal merging model
179    --- optional fields ---
180    dbg_file = '...';                                      % name of the file to store debug informations
181    --- inherited fields ---
182    bdm::epdf::from_setting
183    \endcode
184    */
185    void from_setting ( const Setting& set );
186
187    void to_setting  (Setting  &set) const ;
188
189    void validate() ;
190    //!@}
191};
192UIREGISTER ( merger_base );
193SHAREDPTR ( merger_base );
194
195//! \brief Merger using importance sampling with mixture proposal density
196class merger_mix : public merger_base {
197protected:
198    //!Internal mixture of EF models
199    MixEF Mix;
200    //!Number of components in a mixture
201    int Ncoms;
202    //! coefficient of resampling [0,1]
203    double effss_coef;
204    //! stop after niter iterations
205    int stop_niter;
206
207    //! default value for Ncoms
208    static const int DFLT_Ncoms;
209    //! default value for efss_coef;
210    static const double DFLT_effss_coef;
211
212public:
213    //!\name Constructors
214    //!@{
215    merger_mix () : Ncoms ( 0 ), effss_coef ( 0 ), stop_niter ( 0 ) { }
216
217    merger_mix ( const Array<shared_ptr<pdf> > &S ) :
218        Ncoms ( 0 ), effss_coef ( 0 ), stop_niter ( 0 ) {
219        set_sources ( S );
220    }
221
222    //! Set sources and prepare all internal structures
223    void set_sources ( const Array<shared_ptr<pdf> > &S ) {
224        merger_base::set_sources ( S );
225        //Nsources = S.length();
226    }
227
228    //! Set internal parameters used in approximation
229    void set_parameters ( int Ncoms0 = DFLT_Ncoms, double effss_coef0 = DFLT_effss_coef ) {
230        Ncoms = Ncoms0;
231        effss_coef = effss_coef0;
232    }
233    //!@}
234
235    //! \name Mathematical operations
236    //!@{
237
238    //!Merge values using mixture approximation
239    void merge ();
240
241    //! sample from the approximating mixture
242    vec sample () const {
243        return Mix.posterior().sample();
244    }
245    //! loglikelihood computed on mixture models
246    double evallog ( const vec &yt ) const {
247        vec dtf = ones ( yt.length() + 1 );
248        dtf.set_subvector ( 0, yt );
249        return Mix.logpred ( dtf, vec(0) );
250    }
251    //!@}
252
253    //!\name Access functions
254    //!@{
255//! Access function
256    MixEF& _Mix() {
257        return Mix;
258    }
259    //! Access function
260    emix* proposal() {
261        emix* tmp = Mix.epredictor();
262        tmp->set_rv ( rv );
263        return tmp;
264    }
265    /*! Create object from the following structure
266
267    \code
268    class = 'merger_mix';
269    --- optional fields ---
270    ncoms = [];        % number of components in a mixture
271    effss_coef = [];   % coefficient of resampling from interval [0,1]
272    stop_niter = [];   % number of interations
273    --- inherited fields ---
274    bdm::merger_base::from_setting
275    \endcode   
276    If the optional fields are not given, they will be filled as follows:
277    \code
278    ncoms = 10;       
279    effss_coef = 0.9; 
280    stop_niter = 10; 
281    \endcode
282
283    */
284    void from_setting ( const Setting& set );
285
286    void to_setting  (Setting  &set) const;
287    void validate();
288
289    //! @}
290};
291UIREGISTER ( merger_mix );
292SHAREDPTR ( merger_mix );
293
294}
295
296#endif // MER_H
Note: See TracBrowser for help on using the browser.