root/library/bdm/estim/arx.cpp @ 679

Revision 679, 6.7 kB (checked in by smidl, 15 years ago)

Major changes in BM -- OK is only test suite and tests/tutorial -- the rest is broken!!!

  • Property svn:eol-style set to native
Line 
1#include "arx.h"
2namespace bdm {
3
4        void ARX::bayes_weighted ( const vec &yt, const vec &cond, const double w ) {
5        double lnc;
6        //cache
7        ldmat &V=est._V(); 
8        double &nu=est._nu();
9
10        dyad.set_subvector(0,yt);
11        dyad.set_subvector(dimy,cond);
12        // possible "1" is there from the beginning
13       
14        if ( frg < 1.0 ) {
15                est.pow ( frg ); // multiply V and nu
16
17               
18                //stabilize
19                ldmat V0=alter_est._V(); //$ copy
20                double &nu0=alter_est._nu();
21               
22                V0*=(1-frg);
23                V += V0; //stabilization
24                nu +=(1-frg)*nu0;
25               
26                // recompute loglikelihood of new "prior"
27                if ( evalll ) {
28                        last_lognc = est.lognc();
29                }
30        }
31        V.opupdt ( dyad, w );
32        nu += w;
33
34        // log(sqrt(2*pi)) = 0.91893853320467
35        if ( evalll ) {
36                lnc = est.lognc();
37                ll = lnc - last_lognc - 0.91893853320467;
38                last_lognc = lnc;
39        }
40}
41
42double ARX::logpred ( const vec &yt ) const {
43        egiw pred ( est );
44        ldmat &V = pred._V();
45        double &nu = pred._nu();
46
47        double lll;
48        vec dyad_p = dyad;
49        dyad_p.set_subvector(0,yt);
50
51        if ( frg < 1.0 ) {
52                pred.pow ( frg );
53                lll = pred.lognc();
54        } else//should be save: last_lognc is changed only by bayes;
55                if ( evalll ) {
56                        lll = last_lognc;
57                } else {
58                        lll = pred.lognc();
59                }
60
61        V.opupdt ( dyad_p, 1.0 );
62        nu += 1.0;
63        // log(sqrt(2*pi)) = 0.91893853320467
64        return pred.lognc() - lll - 0.91893853320467;
65}
66
67ARX* ARX::_copy_ ( ) const {
68        ARX* Tmp = new ARX ( *this );
69        return Tmp;
70}
71
72void ARX::set_statistics ( const BMEF* B0 ) {
73        const ARX* A0 = dynamic_cast<const ARX*> ( B0 );
74
75        bdm_assert_debug ( dimension() == A0->dimension(), "Statistics of different dimensions" );
76        set_statistics ( A0->dimensiony(), A0->posterior()._V(), A0->posterior()._nu() );
77}
78
79enorm<ldmat>* ARX::epredictor ( const vec &rgr ) const {
80        mat mu ( dimy, posterior()._V().rows() - dimy );
81        mat R ( dimy, dimy );
82
83        enorm<ldmat>* tmp;
84        tmp = new enorm<ldmat> ( );
85        //TODO: too hackish
86        if ( yrv._dsize() > 0 ) {
87        }
88
89        est.mean_mat ( mu, R ); //mu =
90        //correction for student-t  -- TODO check if correct!!
91        //R*=nu/(nu-2);
92        mat p_mu = mu.T() * rgr;        //the result is one column
93        tmp->set_parameters ( p_mu.get_col ( 0 ), ldmat ( R ) );
94        return tmp;
95}
96
97mlnorm<ldmat>* ARX::predictor ( ) const {
98        mat mu ( dimy, posterior()._V().rows() - dimy );
99        mat R ( dimy, dimy );
100        mlnorm<ldmat>* tmp;
101        tmp = new mlnorm<ldmat> ( );
102
103        est.mean_mat ( mu, R ); //mu =
104        mu = mu.T();
105        //correction for student-t  -- TODO check if correct!!
106
107        if ( have_constant) { // constant term
108                //Assume the constant term is the last one:
109                tmp->set_parameters ( mu.get_cols ( 0, mu.cols() - 2 ), mu.get_col ( mu.cols() - 1 ), ldmat ( R ) );
110        } else {
111                tmp->set_parameters ( mu, zeros ( dimy ), ldmat ( R ) );
112        }
113        return tmp;
114}
115
116mlstudent* ARX::predictor_student ( ) const {
117        const ldmat &V = posterior()._V();
118       
119        mat mu ( dimy, V.rows() - dimy );
120        mat R ( dimy, dimy );
121        mlstudent* tmp;
122        tmp = new mlstudent ( );
123
124        est.mean_mat ( mu, R ); //
125        mu = mu.T();
126
127        int end = V._L().rows() - 1;
128        ldmat Lam ( V._L() ( dimy, end, dimy, end ), V._D() ( dimy, end ) );  //exp val of R
129
130
131        if ( have_constant) { // no constant term
132                //Assume the constant term is the last one:
133                if ( mu.cols() > 1 ) {
134                        tmp->set_parameters ( mu.get_cols ( 0, mu.cols() - 2 ), mu.get_col ( mu.cols() - 1 ), ldmat ( R ), Lam );
135                } else {
136                        tmp->set_parameters ( mat ( dimy, dimc ), mu.get_col ( mu.cols() - 1 ), ldmat ( R ), Lam );
137                }
138        } else {
139                // no constant term
140                tmp->set_parameters ( mu, zeros ( dimy ), ldmat ( R ), Lam );
141        }
142        return tmp;
143}
144
145
146
147/*! \brief Return the best structure
148@param Eg a copy of GiW density that is being examined
149@param Eg0 a copy of prior GiW density before estimation
150@param Egll likelihood of the current Eg
151@param indeces current indeces
152\return best likelihood in the structure below the given one
153*/
154double egiw_bestbelow ( egiw Eg, egiw Eg0, double Egll, ivec &indeces ) { //parameter Eg is a copy!
155        ldmat Vo = Eg._V(); //copy
156        ldmat Vo0 = Eg._V(); //copy
157        ldmat& Vp = Eg._V(); // pointer into Eg
158        ldmat& Vp0 = Eg._V(); // pointer into Eg
159        int end = Vp.rows() - 1;
160        int i;
161        mat Li;
162        mat Li0;
163        double maxll = Egll;
164        double tmpll = Egll;
165        double belll = Egll;
166
167        ivec tmpindeces;
168        ivec maxindeces = indeces;
169
170
171        cout << "bb:(" << indeces << ") ll=" << Egll << endl;
172
173        //try to remove only one rv
174        for ( i = 0; i < end; i++ ) {
175                //copy original
176                Li = Vo._L();
177                Li0 = Vo0._L();
178                //remove stuff
179                Li.del_col ( i + 1 );
180                Li0.del_col ( i + 1 );
181                Vp.ldform ( Li, Vo._D() );
182                Vp0.ldform ( Li0, Vo0._D() );
183                tmpll = Eg.lognc() - Eg0.lognc(); // likelihood is difference of norm. coefs.
184
185                cout << "i=(" << i << ") ll=" << tmpll << endl;
186
187                //
188                if ( tmpll > Egll ) { //increase of the likelihood
189                        tmpindeces = indeces;
190                        tmpindeces.del ( i );
191                        //search for a better match in this substructure
192                        belll = egiw_bestbelow ( Eg, Eg0, tmpll, tmpindeces );
193                        if ( belll > maxll ) { //better match found
194                                maxll = belll;
195                                maxindeces = tmpindeces;
196                        }
197                }
198        }
199        indeces = maxindeces;
200        return maxll;
201}
202
203ivec ARX::structure_est ( egiw est0 ) {
204        ivec ind = linspace ( 1, est.dimension() - 1 );
205        egiw_bestbelow ( est, est0, est.lognc() - est0.lognc(), ind );
206        return ind;
207}
208
209
210
211ivec ARX::structure_est_LT ( egiw est0 ) {
212        //some stuff with beliefs etc.
213//      ivec ind = bdm::straux1(V,nu, est0._V(), est0._nu());
214        return ivec();//ind;
215}
216
217void ARX::from_setting ( const Setting &set ) {
218        shared_ptr<RV> yrv_ = UI::build<RV> ( set, "rv", UI::compulsory );
219        shared_ptr<RV> rrv = UI::build<RV> ( set, "rgr", UI::compulsory );
220        dimy = yrv_->_dsize();
221        // rgrlen - including constant!!!
222        dimc = rrv->_dsize();
223       
224        yrv = *yrv_;
225        rvc = *rrv;
226       
227        string opt;
228        if ( UI::get(opt, set,  "options", UI::optional) ) {
229                BM::set_options(opt);
230        }
231        int constant;
232        if (!UI::get(constant, set, "constant", UI::optional)){
233                have_constant=true;
234        } else {
235                have_constant=constant>0;
236        }
237        int rgrlen = dimc+int(have_constant==true);
238
239        //init
240        shared_ptr<egiw> pri=UI::build<egiw>(set, "prior", UI::optional);
241        if (pri) {
242                bdm_assert(pri->_dimx()==dimy,"prior is not compatible");
243                bdm_assert(pri->_V().rows()==dimy+rgrlen,"prior is not compatible");
244                est.set_parameters( pri->_dimx(),pri->_V(), pri->_nu());
245        }else{
246                est.set_parameters( dimy, zeros(dimy+rgrlen));
247                set_prior_default(est);
248        }
249               
250        shared_ptr<egiw> alt=UI::build<egiw>(set, "alternative", UI::optional);
251        if (alt) {
252                bdm_assert(alt->_dimx()==dimy,"alternative is not compatible");
253                bdm_assert(alt->_V().rows()==dimy+rgrlen,"alternative is not compatible");
254                alter_est.set_parameters( alt->_dimx(),alt->_V(), alt->_nu());
255        } else {
256                alter_est = est;
257        }
258
259        double frg;
260        if ( !UI::get ( frg, set, "frg" ) )
261                frg = 1.0;
262
263        set_parameters ( frg );
264       
265        //name results (for logging)
266        shared_ptr<RV> rv_par=UI::build<RV>(set, "rv_param",UI::optional );
267        if (!rv_par){
268                est.set_rv ( RV ( "{theta r }", vec_2 ( dimy*rgrlen, dimy*dimy ) ) );
269        } else {
270                est.set_rv ( *rv_par );
271        }
272        validate();
273}
274}
Note: See TracBrowser for help on using the browser.