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 | |
---|
13 | #ifndef MX_H |
---|
14 | #define MX_H |
---|
15 | |
---|
16 | #include "libBM.h" |
---|
17 | #include "libEF.h" |
---|
18 | //#include <std> |
---|
19 | |
---|
20 | using namespace itpp; |
---|
21 | |
---|
22 | /*! |
---|
23 | * \brief Mixture of epdfs |
---|
24 | |
---|
25 | Density function: |
---|
26 | \f[ |
---|
27 | f(x) = \sum_{i=1}^{n} w_{i} f_i(x), \quad \sum_{i=1}^n w_i = 1. |
---|
28 | \f] |
---|
29 | where \f$f_i(x)\f$ is any density on random variable \f$x\f$, called \a component, |
---|
30 | |
---|
31 | */ |
---|
32 | class emix : public epdf { |
---|
33 | protected: |
---|
34 | //! weights of the components |
---|
35 | vec w; |
---|
36 | //! Component (epdfs) |
---|
37 | Array<epdf*> Coms; |
---|
38 | public: |
---|
39 | //!Default constructor |
---|
40 | emix ( RV &rv ) : epdf ( rv ) {}; |
---|
41 | //! Set weights \c w and components \c R |
---|
42 | void set_parameters ( const vec &w, const Array<epdf*> &Coms ); |
---|
43 | |
---|
44 | vec sample() const; |
---|
45 | vec mean() const { |
---|
46 | int i; vec mu = zeros ( rv.count() ); |
---|
47 | for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); } |
---|
48 | return mu; |
---|
49 | } |
---|
50 | double evalpdflog ( const vec &val ) const { |
---|
51 | int i; |
---|
52 | double sum = 0.0; |
---|
53 | for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * Coms ( i )->evalpdflog ( val );} |
---|
54 | return log ( sum ); |
---|
55 | }; |
---|
56 | |
---|
57 | //Access methods |
---|
58 | //! returns a pointer to the internal mean value. Use with Care! |
---|
59 | vec& _w() {return w;} |
---|
60 | }; |
---|
61 | |
---|
62 | /*! \brief Chain rule decomposition of epdf |
---|
63 | |
---|
64 | Probability density in the form of Chain-rule decomposition: |
---|
65 | \[ |
---|
66 | f(x_1,x_2,x_3) = f(x_1|x_2,x_3)f(x_2,x_3)f(x_3) |
---|
67 | \] |
---|
68 | Note that |
---|
69 | */ |
---|
70 | class mprod: public mpdf { |
---|
71 | protected: |
---|
72 | // |
---|
73 | int n; |
---|
74 | // pointers to epdfs |
---|
75 | Array<epdf*> epdfs; |
---|
76 | Array<mpdf*> mpdfs; |
---|
77 | // |
---|
78 | //! Indeces of rvs in common rv |
---|
79 | Array<ivec> rvinds; |
---|
80 | //! Indeces of rvc in common rv |
---|
81 | Array<ivec> rvcinrv; |
---|
82 | //! Indeces of rvc in common rvc |
---|
83 | Array<ivec> rvcinds; |
---|
84 | // //! Indicate independence of its factors |
---|
85 | // bool independent; |
---|
86 | // //! Indicate internal creation of mpdfs which must be destroyed |
---|
87 | // bool intermpdfs; |
---|
88 | public: |
---|
89 | /*!\brief Constructor from list of mFacs, |
---|
90 | Additional parameter overlap is left for future use. Do not set to true for mprod. |
---|
91 | */ |
---|
92 | mprod ( Array<mpdf*> mFacs, bool overlap=false ); |
---|
93 | |
---|
94 | double evalpdflog ( const vec &val ) const { |
---|
95 | int i; |
---|
96 | double res = 0.0; |
---|
97 | for ( i = n - 1;i > 0;i++ ) { |
---|
98 | if ( rvcinds ( i ).length() > 0 ) |
---|
99 | {mpdfs ( i )->condition ( val ( rvcinds ( i ) ) );} |
---|
100 | // add logarithms |
---|
101 | res += epdfs ( i )->evalpdflog ( val ( rvinds ( i ) ) ); |
---|
102 | } |
---|
103 | return res; |
---|
104 | } |
---|
105 | vec samplecond ( const vec &cond, double &ll ) { |
---|
106 | vec smp=zeros ( rv.count() ); |
---|
107 | vec condi; |
---|
108 | vec smpi; |
---|
109 | ll = 0; |
---|
110 | for ( int i = ( n - 1 );i >= 0;i-- ) { |
---|
111 | if ( rvcinds ( i ).length() > 0 ) { |
---|
112 | condi = zeros ( rvcinrv.length() + rvcinds.length() ); |
---|
113 | // copy data in condition |
---|
114 | set_subvector ( condi,rvcinds ( i ), cond ); |
---|
115 | // copy data in already generated sample |
---|
116 | set_subvector ( condi,rvcinrv ( i ), smp ); |
---|
117 | |
---|
118 | mpdfs ( i )->condition ( condi ); |
---|
119 | } |
---|
120 | smpi = epdfs ( i )->sample(); |
---|
121 | // copy contribution of this pdf into smp |
---|
122 | set_subvector ( smp,rvinds ( i ), smpi ); |
---|
123 | // add ith likelihood contribution |
---|
124 | ll+=epdfs ( i )->evalpdflog ( smpi ); |
---|
125 | } |
---|
126 | return smp; |
---|
127 | } |
---|
128 | mat samplecond ( const vec &cond, vec &ll, int N ) { |
---|
129 | mat Smp ( rv.count(),N ); |
---|
130 | for ( int i=0;i<N;i++ ) {Smp.set_col ( i,samplecond ( cond,ll ( i ) ) );} |
---|
131 | return Smp; |
---|
132 | } |
---|
133 | |
---|
134 | ~mprod() {}; |
---|
135 | }; |
---|
136 | |
---|
137 | //! Product of independent epdfs. For dependent pdfs, use mprod. |
---|
138 | class eprod: public epdf { |
---|
139 | protected: |
---|
140 | //! Components (epdfs) |
---|
141 | Array<epdf*> epdfs; |
---|
142 | //! Array of indeces |
---|
143 | Array<ivec> rvinds; |
---|
144 | public: |
---|
145 | eprod ( const Array<epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ),rvinds ( epdfs.length() ) { |
---|
146 | bool independent=true; |
---|
147 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
148 | independent=rv.add ( epdfs ( i )->_rv() ); |
---|
149 | it_assert_debug ( independent==true, "eprod:: given components are not independent ." ); |
---|
150 | rvinds ( i ) = ( epdfs ( i )->_rv() ).dataind ( rv ); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | vec mean() const { |
---|
155 | vec tmp ( rv.count() ); |
---|
156 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
157 | vec pom = epdfs ( i )->mean(); |
---|
158 | set_subvector ( tmp,rvinds ( i ), pom ); |
---|
159 | } |
---|
160 | return tmp; |
---|
161 | } |
---|
162 | vec sample() const { |
---|
163 | vec tmp ( rv.count() ); |
---|
164 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
165 | vec pom = epdfs ( i )->sample(); |
---|
166 | set_subvector ( tmp,rvinds ( i ), pom ); |
---|
167 | } |
---|
168 | return tmp; |
---|
169 | } |
---|
170 | double evalpdflog ( const vec &val ) const { |
---|
171 | double tmp=0; |
---|
172 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
173 | tmp+=epdfs(i)->evalpdflog(val(rvinds(i))); |
---|
174 | } |
---|
175 | return tmp; |
---|
176 | } |
---|
177 | }; |
---|
178 | |
---|
179 | |
---|
180 | /*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type |
---|
181 | |
---|
182 | */ |
---|
183 | class mmix : public mpdf { |
---|
184 | protected: |
---|
185 | //! Component (epdfs) |
---|
186 | Array<mpdf*> Coms; |
---|
187 | //!Internal epdf |
---|
188 | emix Epdf; |
---|
189 | public: |
---|
190 | //!Default constructor |
---|
191 | mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep = &Epdf;}; |
---|
192 | //! Set weights \c w and components \c R |
---|
193 | void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) { |
---|
194 | Array<epdf*> Eps ( Coms.length() ); |
---|
195 | |
---|
196 | for ( int i = 0;i < Coms.length();i++ ) { |
---|
197 | Eps ( i ) = & ( Coms ( i )->_epdf() ); |
---|
198 | } |
---|
199 | Epdf.set_parameters ( w, Eps ); |
---|
200 | }; |
---|
201 | |
---|
202 | void condition ( const vec &cond ) { |
---|
203 | for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );} |
---|
204 | }; |
---|
205 | }; |
---|
206 | #endif //MX_H |
---|