root/bdm/estim/arx.cpp @ 204

Revision 204, 4.6 kB (checked in by smidl, 16 years ago)

merger is now in logarithms + new merge_test

  • Property svn:eol-style set to native
Line 
1#include "arx.h"
2
3using namespace itpp;
4
5void ARX::bayes ( const vec &dt, const double w ) {
6        double lnc;
7
8        if ( frg<1.0 ) {
9                est.pow ( frg );
10                if ( evalll ) {
11                        last_lognc = est.lognc();
12                }
13        }
14        V.opupdt ( dt,w );
15        nu+=w;
16
17        // log(sqrt(2*pi)) = 0.91893853320467
18        if ( evalll ) {
19                lnc = est.lognc();
20                ll = lnc - last_lognc - 0.91893853320467;
21                last_lognc = lnc;
22        }
23}
24
25double ARX::logpred ( const vec &dt ) const {
26        egiw pred ( est );
27        ldmat &V=pred._V();
28        double &nu=pred._nu();
29
30        double lll;
31
32        if ( frg<1.0 ) {
33                pred.pow ( frg );
34                lll = pred.lognc();
35        }
36        else//should be save: last_lognc is changed only by bayes;
37                if ( evalll ) {lll=last_lognc;}
38                else{lll=pred.lognc();}
39
40        V.opupdt ( dt,1.0 );
41        nu+=1.0;
42        // log(sqrt(2*pi)) = 0.91893853320467
43        return pred.lognc()-lll - 0.91893853320467;
44}
45
46ARX* ARX::_copy_ ( bool changerv ) {
47        ARX* Tmp=new ARX ( *this );
48        if ( changerv ) {Tmp->rv.newids(); Tmp->est._renewrv ( Tmp->rv );}
49        return Tmp;
50}
51
52void ARX::set_statistics ( const BMEF* B0 ) {
53        const ARX* A0=dynamic_cast<const ARX*> ( B0 );
54
55        it_assert_debug ( V.rows() ==A0->V.rows(),"ARX::set_statistics Statistics  differ" );
56        set_parameters ( A0->V,A0->nu );
57}
58
59enorm<ldmat>* ARX::predictor ( const RV &rv, const vec &rgr ) const {
60        mat mu ( rv.count(), V.rows()-rv.count() );
61        mat R ( rv.count(),rv.count() );
62        enorm<ldmat>* tmp;
63        tmp=new enorm<ldmat> ( rv );
64
65        est.mean_mat ( mu,R ); //mu =
66        //correction for student-t  -- TODO check if correct!!
67        //R*=nu/(nu-2);
68        mat p_mu=mu.T() *rgr;   //the result is one column
69        tmp->set_parameters ( p_mu.get_col ( 0 ),ldmat ( R ) );
70        return tmp;
71}
72
73mlnorm<ldmat>* ARX::predictor ( const RV &rv, const RV &rvc ) const {
74        int dif=V.rows() - rv.count() - rvc.count();
75        it_assert_debug ( ( dif==0 ) || ( dif==1 ), "Give RVs do not match" );
76
77        mat mu ( rv.count(), V.rows()-rv.count() );
78        mat R ( rv.count(),rv.count() );
79        mlnorm<ldmat>* tmp;
80        tmp=new mlnorm<ldmat> ( rv,rvc );
81
82        est.mean_mat ( mu,R ); //mu =
83        mu = mu.T();
84        //correction for student-t  -- TODO check if correct!!
85
86        if ( dif==0 ) { // no constant term
87                tmp->set_parameters ( mu, zeros ( rv.count() ), ldmat ( R ) );
88        }
89        else {
90                //Assume the constant term is the last one:
91                tmp->set_parameters ( mu.get_cols (0,mu.cols()-2 ), mu.get_col ( mu.cols()-1 ), ldmat ( R ) );
92        }
93        return tmp;
94}
95
96mlstudent* ARX::predictor_student ( const RV &rv, const RV &rvc ) const {
97        int dif=V.rows() - rv.count() - rvc.count();
98        it_assert_debug ( ( dif==0 ) || ( dif==1 ), "Give RVs do not match" );
99
100        mat mu ( rv.count(), V.rows()-rv.count() );
101        mat R ( rv.count(),rv.count() );
102        mlstudent* tmp;
103        tmp=new mlstudent ( rv,rvc );
104
105        est.mean_mat ( mu,R ); //
106        mu = mu.T();
107       
108        int xdim = rv.count();
109        int end = V._L().rows()-1;
110        ldmat Lam ( V._L() ( xdim,end,xdim,end ), V._D() ( xdim,end ) );  //exp val of R
111
112
113        if ( dif==0 ) { // no constant term
114                tmp->set_parameters ( mu, zeros ( rv.count() ), ldmat ( R ), Lam);
115        }
116        else {
117                //Assume the constant term is the last one:
118                tmp->set_parameters ( mu.get_cols (0,mu.cols()-2 ), mu.get_col ( mu.cols()-1 ), ldmat ( R ), Lam);
119        }
120        return tmp;
121}
122
123/*! \brief Return the best structure
124@param Eg a copy of GiW density that is being examined
125@param Eg0 a copy of prior GiW density before estimation
126@param Egll likelihood of the current Eg
127@param indeces current indeces
128\return best likelihood in the structure below the given one
129*/
130double egiw_bestbelow ( egiw Eg, egiw Eg0, double Egll, ivec &indeces ) { //parameter Eg is a copy!
131        ldmat Vo = Eg._V(); //copy
132        ldmat Vo0 = Eg._V(); //copy
133        ldmat& Vp = Eg._V(); // pointer into Eg
134        ldmat& Vp0 = Eg._V(); // pointer into Eg
135        int end = Vp.rows()-1;
136        int i;
137        mat Li;
138        mat Li0;
139        double maxll=Egll;
140        double tmpll=Egll;
141        double belll=Egll;
142
143        ivec tmpindeces;
144        ivec maxindeces=indeces;
145
146
147        cout << "bb:(" << indeces <<") ll=" << Egll <<endl;
148
149        //try to remove only one rv
150        for ( i=0;i<end;i++ ) {
151                //copy original
152                Li = Vo._L();
153                Li0 = Vo0._L();
154                //remove stuff
155                Li.del_col ( i+1 );
156                Li0.del_col ( i+1 );
157                Vp.ldform ( Li,Vo._D() );
158                Vp0.ldform ( Li0,Vo0._D() );
159                tmpll = Eg.lognc()-Eg0.lognc(); // likelihood is difference of norm. coefs.
160
161                cout << "i=(" << i <<") ll=" << tmpll <<endl;
162
163                //
164                if ( tmpll > Egll ) { //increase of the likelihood
165                        tmpindeces = indeces;
166                        tmpindeces.del ( i );
167                        //search for a better match in this substructure
168                        belll=egiw_bestbelow ( Eg, Eg0, tmpll, tmpindeces );
169                        if ( belll>maxll ) { //better match found
170                                maxll = belll;
171                                maxindeces = tmpindeces;
172                        }
173                }
174        }
175        indeces = maxindeces;
176        return maxll;
177}
178
179ivec ARX::structure_est ( egiw est0 ) {
180        ivec ind=linspace ( 1,rv.count()-1 );
181        egiw_bestbelow ( est, est0, est.lognc()- est0.lognc(), ind );
182        return ind;
183}
Note: See TracBrowser for help on using the browser.