1 | #include <itpp/itmex.h> |
---|
2 | |
---|
3 | #include "../../bdm/estim/arx.h" |
---|
4 | |
---|
5 | using namespace itpp; |
---|
6 | |
---|
7 | void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { |
---|
8 | // Check the number of inputs and output arguments |
---|
9 | if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); |
---|
10 | if ( n_input!=3 ) mexErrMsgTxt ( "Wrong number of input variables!" ); |
---|
11 | |
---|
12 | // Convert input variables to IT++ format |
---|
13 | mat Data = mxArray2mat ( input[0] ); |
---|
14 | if ( Data.rows() !=2 ) |
---|
15 | { if ( Data.cols() ==2 ) Data=Data.T(); else mexErrMsgTxt ( "Data are not 2D!" );} |
---|
16 | int ilow = mxArray2int ( input[1] )-1; |
---|
17 | int ihi = mxArray2int ( input[2] )-1; //coreection for different indeces |
---|
18 | |
---|
19 | // ------------------ Start of routine --------------------------- |
---|
20 | // ------------------ Start of routine --------------------------- |
---|
21 | int ndat=Data.cols(); |
---|
22 | int npsi=Data.rows() +1; //add a constant |
---|
23 | RV thr ( "1","{ theta_r }",vec_1 ( npsi ),vec_1 ( 0 ) ); |
---|
24 | |
---|
25 | mat V0=1e-8*eye ( npsi ); V0 ( 0,0 ) =1e-4; |
---|
26 | vec rgr ( npsi ); |
---|
27 | double nu0=0.1; |
---|
28 | // fitting a linear part => third coef is "1" |
---|
29 | rgr ( 2 ) = 1.0; |
---|
30 | |
---|
31 | // RESULTS |
---|
32 | mat Tlls=zeros ( ihi+1,ihi+1); |
---|
33 | // AR model |
---|
34 | ARX Ar ( thr,V0,nu0 ); |
---|
35 | |
---|
36 | //Initialize |
---|
37 | for ( int i=ilow+2; i<ihi-2; i++ ) { |
---|
38 | rgr.set_subvector ( 0,Data.get_col ( i ) ); |
---|
39 | Ar.bayes ( rgr ); |
---|
40 | } |
---|
41 | // FLATTEN |
---|
42 | /* Ar.get_parameters ( V0,nu0 ); |
---|
43 | V0 *=1.0/nu0/10000.; // same flattening factor as for nu0 |
---|
44 | nu0=1./10000.;*/ |
---|
45 | Ar.set_parameters ( V0,nu0 ); |
---|
46 | |
---|
47 | for ( int i=ilow; i<=ihi; i++ ) { |
---|
48 | Ar.set_parameters ( V0,nu0 ); |
---|
49 | for ( int t=i; t<=ihi; t++ ) { |
---|
50 | rgr.set_subvector ( 0,Data.get_col ( t ) ); |
---|
51 | Ar.bayes ( rgr ); |
---|
52 | Tlls ( i,t ) =Ar._tll(); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | max_index ( Tlls,ilow,ihi ); |
---|
57 | |
---|
58 | // ------------------ End of routine ----------------------------- |
---|
59 | |
---|
60 | output[0] = mxCreateDoubleMatrix ( 2,1, mxREAL ); |
---|
61 | ivec2mxArray ( vec_2 ( ilow+1,ihi+1 ) , output[0] ); |
---|
62 | |
---|
63 | if ( n_output>1 ) { |
---|
64 | // Create output vectors |
---|
65 | output[1] = mxCreateDoubleMatrix ( Tlls.rows(),Tlls.cols(), mxREAL ); |
---|
66 | // Convert the IT++ format to Matlab format for output |
---|
67 | mat2mxArray ( Tlls, output[1] ); |
---|
68 | } |
---|
69 | |
---|
70 | if ( n_output>2 ) { |
---|
71 | Ar.set_parameters ( V0,nu0 ); |
---|
72 | // Redo Ar for given points |
---|
73 | for ( int i=ilow; i<=ihi; i++ ) { |
---|
74 | rgr.set_subvector ( 0,Data.get_col ( i ) ); |
---|
75 | Ar.bayes ( rgr ); |
---|
76 | } |
---|
77 | |
---|
78 | // Create output vectors |
---|
79 | output[2] = mxCreateDoubleMatrix ( 3,1, mxREAL ); |
---|
80 | // Convert the IT++ format to Matlab format for output |
---|
81 | // mat2mxArray ( Ar._epdf().mean(), output[2] ); |
---|
82 | mat2mxArray ( Ar._epdf().mean(), output[2] ); |
---|
83 | } |
---|
84 | |
---|
85 | } |
---|