| 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] ); |
|---|
| 17 | int ihi = mxArray2int ( input[2] ); |
|---|
| 18 | |
|---|
| 19 | // ------------------ Start of routine --------------------------- |
|---|
| 20 | int ndat=Data.cols(); |
|---|
| 21 | int npsi=Data.rows()+1; //add a constant |
|---|
| 22 | RV thr ( "1","{ theta_r }",vec_1 ( npsi ),vec_1 ( 0 ) ); |
|---|
| 23 | |
|---|
| 24 | mat V0=1e-8*eye ( npsi ); V0 ( 0,0 ) =10.0; |
|---|
| 25 | vec rgr(npsi); |
|---|
| 26 | double nu0=npsi+2; |
|---|
| 27 | // fitting a linear part => third coef is "1" |
|---|
| 28 | rgr(2) = 1.0; |
|---|
| 29 | |
|---|
| 30 | // AR model |
|---|
| 31 | ARX Ar ( thr,V0,nu0 ); |
|---|
| 32 | // start at i0 |
|---|
| 33 | bool gohi=true; bool golow=true; |
|---|
| 34 | // certain period |
|---|
| 35 | for (int i=ilow;i<ihi;i++){ |
|---|
| 36 | rgr.set_subvector(0,Data.get_col ( i )); |
|---|
| 37 | Ar.bayes ( rgr ); |
|---|
| 38 | } |
|---|
| 39 | double tll=Ar._tll(); |
|---|
| 40 | // go hi |
|---|
| 41 | do { |
|---|
| 42 | rgr.set_subvector(0,Data.get_col ( ++ihi )); |
|---|
| 43 | Ar.bayes (rgr); |
|---|
| 44 | if (Ar._tll()>tll) tll=Ar._tll(); |
|---|
| 45 | else gohi=false; |
|---|
| 46 | } while (gohi); |
|---|
| 47 | // go low |
|---|
| 48 | do { |
|---|
| 49 | rgr.set_subvector(0,Data.get_col ( --ilow )); |
|---|
| 50 | Ar.bayes (rgr); |
|---|
| 51 | if (Ar._tll()>tll) tll=Ar._tll(); |
|---|
| 52 | else golow=false; |
|---|
| 53 | } while (golow); |
|---|
| 54 | |
|---|
| 55 | vec th = Ar._epdf().mean(); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | // ------------------ End of routine ----------------------------- |
|---|
| 59 | |
|---|
| 60 | // Create output vectors |
|---|
| 61 | output[0] = mxCreateDoubleMatrix ( 1,th.length(), mxREAL ); |
|---|
| 62 | // Convert the IT++ format to Matlab format for output |
|---|
| 63 | vec2mxArray ( th, output[0] ); |
|---|
| 64 | |
|---|
| 65 | if ( n_output>1 ) { |
|---|
| 66 | output[1] = mxCreateDoubleMatrix ( 1,1, mxREAL ); |
|---|
| 67 | double2mxArray ( ilow, output[1] ); |
|---|
| 68 | } |
|---|
| 69 | if ( n_output>1 ) { |
|---|
| 70 | output[2] = mxCreateDoubleMatrix ( 1,1, mxREAL ); |
|---|
| 71 | double2mxArray ( ihi, output[2] ); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | } |
|---|