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

Revision 665, 6.6 kB (checked in by smidl, 15 years ago)

Compilation and minor extensions

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