root/bdm/estim/arx.cpp @ 199

Revision 199, 4.4 kB (checked in by smidl, 16 years ago)

native

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