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

Revision 700, 6.9 kB (checked in by smidl, 15 years ago)

Making tutorial/userguide example work again (changes of mpdf and bayes)

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