Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

models.h

Go to the documentation of this file.
00001 /*!
00002   \file   models.h
00003   \author Klaus Holst
00004   \date   Wed Oct 13 14:21:51 2004
00005   
00006   \brief  Stochastic model classes and corresponding Prediction-Based estimating function.
00007     
00008 */
00009 
00010 #ifndef _MODELS_H_
00011 #define _MODELS_H_
00012 
00013 #include <cmath>
00014 #include <cstdlib>
00015 #include <iostream>
00016 #include <fstream>
00017 #include <iomanip>
00018 #include <ctime>
00019 #include "linalg.h"       // various (matrix)functions
00020 #include "linrdist.h"     // Various random number generators
00021 #include "qscr.h"         // Screen output
00022 #include "svd.h"          // Singular Value Decomposition (and pseudoinverse)
00023 #include "util.h"         // misc. utilities
00024 
00025 
00026 
00027 typedef safevector<double(*)(double)> functionvector;
00028 
00029 
00030 //////////////////////////
00031 //      CLASSES         //
00032 /////////////////////////
00033 
00034 
00035 //! Abstract model class
00036 /*! 
00037   
00038 */
00039 class MODEL {
00040  public:
00041   //! Constructor
00042   /*!
00043   
00044   \return 
00045   */
00046   MODEL() {}
00047   //! Destructor
00048   /*!
00049   
00050   \return 
00051   */
00052   virtual ~MODEL() {}
00053   //! Virtual simulation function
00054   /*! 
00055     
00056   \param n : number of variables simulated
00057  
00058   \return (VIRTUAL)
00059   */
00060   virtual matrix simulate(long n) =0;                    
00061   //! Virtual simulation function
00062   /*! 
00063     
00064   \param n : number of variables simulated
00065   
00066   \return vector of variables opposed to function simulate which returns matrix.
00067   */
00068   virtual unsigned getdim() =0;
00069   //! Returns dimension of parameterspace.
00070   safevector<double> simulatevector(long n);                    
00071   //! sets the seed.
00072   void setseed(long s){ seed = &seedvar; seedvar = s;  }
00073   //! sets the parameters.
00074   virtual void setpar(matrix par) {parameters=par;}
00075   //! returns the parameters.
00076   matrix getpar() {return(parameters);}
00077   //! returns the number of parameters.
00078   long noofpar() {return(parameters.row());}
00079   //! returns the data.
00080   matrix getdata() {return(data);}
00081   //! returns the datasize.
00082   long getdatasize() {return(data.row());}
00083   //! sets the data.
00084   void setdata(matrix Y) {data=Y;}
00085   //! the estimation function described in MS.
00086   /*! 
00087     
00088   \param par : the parameters of the model.
00089   
00090   \return 
00091   */
00092   virtual matrix estfunc(matrix par) =0;
00093  protected:
00094   //! matrix holding the data.
00095   matrix data;
00096 
00097   //! matrix holding the parameters.
00098   matrix parameters;
00099   //! variable helping to set the seed.
00100   long seedvar;
00101   //! the seed used in the simulations
00102   long *seed;
00103 
00104 };
00105 
00106 
00107 //! Pre-est. model
00108 /*! 
00109   inherits from MODEL
00110 */
00111 class PREESTMODEL: public MODEL {
00112  public:
00113   //! sets the data and calculates f_j(data).
00114   void setdata(matrix Y);
00115   //! the estimating function MS(2.1)
00116   matrix estfunc(matrix par);
00117   //! Returns two-norm of estimating function with parameters given by par.
00118   double twonorm_estfunc( matrix par ){ return(estfunc(par).twonorm()); }
00119   //! MS(3.11) disregarding A(theta).
00120   matrix sumH(matrix par);
00121   //! Returns two-norm of _approximated_ estimating function with parameters given by par
00122   double twonorm_sumH( matrix par ){ return(sumH(par).twonorm()); }
00123   //! calculates and sets Mbar_terms.
00124   void set_Mbar_terms(double lambda);
00125   //! Determines wether Mbar (time-consuming calculation) should be calculated.
00126   void set_iter_Mbar(bool val) { iter_Mbar = val; }
00127   //! Sets pre-estimate of sumH to x.
00128   void set_sumH_est(double x) { sumH_est = x; }
00129 
00130   //! Calculates matrix \f$\mathbf{X}\f$ with \f$j\f$th row containing observations \f$(x_{s},\ldots,x_{s+1-q}).\f$
00131   void calculate_X();
00132 
00133   matrix get_X() { return X; }
00134   //! Returns the number of inner-functions in pbef.
00135   unsigned get_N() { return N; }
00136   //! Z_{jk}^{(i)}, j=0,...,N-1, k=1,...,q, i
00137   virtual double Zval(unsigned j, long i, long k, matrix const& Y);
00138   //! Returns \f$\breve{\pi}_{j+1}^{(i-1)}(\theta) = \breve{\alpha}_{(j+1)0}(\theta) + \breve{\alpha}_{j+1}(\theta)^TZ_{j+1}^{(i-1)}\f$. Here \f$Z_j^{(i-1)}\f$ is our actual data.
00139   double prediction(unsigned j, long i, matrix const& Y);
00140   //! boolean to determine wether to print debug-info or not
00141   bool printdebug;
00142   
00143  protected:
00144   //! matrix holding f_j(data), j=1,...,N.
00145   safevector<matrix> fdata;
00146   //! The number of inner-functions, \f$f_1,...,f_N\f$, in the estimating function.
00147   unsigned N;
00148   //! inner-functions in estimating-function G.
00149   functionvector ifuncs;
00150   //! The lag-length in the prediction.
00151   long q;
00152   //! Summation start-index, \f$s\f$, of estimating function.
00153   long s;
00154   //! The number of simulations used in calculating Mbar.
00155   long MbarNOS;
00156   //! summation-start in the expresion of Mbar.
00157   long l;
00158   //! Number of simulated variables used in determination of \f$\breve{a}_j(\theta), C_j(\theta)\f$, etc.
00159   unsigned numsimul;
00160   
00161   //! For fixed \f$s\f$ the \f$numsimul\times q\f$ matrix with \f$j\f$th row given by observations \f$x_{s},\ldots, x_{s+1-q})\f$.
00162   matrix X;
00163   // A numsimul-vector with observations of \f$X_{s+1}\f$.
00164   matrix Xnext;
00165   // A numsimul-vector with observations of \f$X_{1}\f$.
00166   matrix X1;
00167   //! Boolean that determines wether to calculate the time-consuming Mbar in current iteration
00168   bool iter_Mbar;
00169   //! simulated Y for calculations.
00170   matrix simY;
00171   //! \f$A_n^*(\theta)\f$
00172   matrix Astar;
00173 
00174   //! On index j: holds simulations of \f$Z_{j1},\ldots,Z_{jq}\f$, i.e. \f$f_j\f$ mapped onto matrix X. 
00175   safevector <matrix> Z;
00176   // On index j thibxs vector holds q-vector \f$(1,EZ_{j1},...,E_{jq})\f$
00177   safevector <matrix> EtildeZ;
00178   //! On index j: Holds simulations of \f$f_j(X_{s+1})\f$.
00179   safevector <matrix> fXnext;
00180   //! On index j: Holds simulations of \f$f_j(X_{1})\f$.
00181   safevector <matrix> fX1;
00182   //!  Terms used in projection. \f$\breve{\alpha}_j\f$. MS(2.4)
00183   safevector <matrix> a;
00184   //! \f$\breve{\alpha}_{j0}(\theta)=E_\theta(f_j(X_1)) - \breve{\alpha}_j(\theta)^TE_\theta(Z_j^{(s)})\f$. MS(2.5)
00185   safevector <double> a0;
00186   //! defined (as b_j) in MS on the top of page 9.
00187   safevector <matrix> b;
00188   //! Covariance matrix of Z_j (as C_j). See MS on the top of page 9.
00189   safevector <matrix> C;
00190   //! EHH in Mbar (MS(3.14)).
00191   matrix EHH;
00192   //! columnvector containing the simulated value of var(H_i) in the i'th row.
00193   matrix varH;
00194   //! estimate of theta, based on sumH.
00195   double sumH_est;
00196   //! The number of terms in the expression of Mbar.
00197   int Mbar_terms;
00198   //! MS(3.14)
00199   matrix Mbar;
00200 
00201   virtual void calculate_Z();
00202   //! Calculates the terms \f$a_{j,0},\ldots,a_{j,q}\f$ used in calculation of projections \f$\pi_j\f$.
00203   void calculate_a();
00204   //! Calculates \f$b_j(\theta) = \left(COV_\theta(Z_{j1}^{(s)},f_j(X_{s+1})),\ldots, COV_\theta(Z_{jq}^{(s)},f_j(X_{s+1})))\right)^T \f$
00205   void calculate_b();
00206   //! Calculates covariance matrix of Z
00207   void calculate_C();
00208   //! The first term in the expression of \f$\overline{M}\f$
00209   void calculate_EHH();
00210   //! Calculation of \f$\overline{M}\f$
00211   void calculate_Mbar();
00212   //! Partial derivatives of \f$\breve{\alpha}\f$
00213   matrix calculate_Dalpha();
00214   //! \f$\overline{C} = diag(\tilde{C}_1,\ldots,\tilde{C}_N)\f$ where 
00215   // \\f$tilde{C}_j(\theta)\f$ = C_j' + E_\theta(\tilde{Z}_j^{(s)})^\otimes 2\f$
00216   // where Z_j' is the covariance matrix of Z_j^{(s)} with an extra column+row of zeros inserted at position 0.
00217   matrix calculate_Cbar();
00218   //! Calculates \f$A_n^*(\theta)\f$ derived from Mbar,Dalpha and Cbar.
00219   void calculate_Astar();
00220 
00221   //! 
00222 
00223 };
00224 
00225 
00226 //////////////////////////////////////////////////////
00227 
00228 
00229 //! Markov model class
00230 /*! 
00231   inherits from class PREESTMODEL
00232 */
00233 class MARKOVMODEL: public PREESTMODEL {
00234  public: 
00235   virtual void setinitvalue() =0;              
00236   //! updating in the simulation.        
00237   virtual double next(double oldvalue) const=0;
00238   //! simulates a process given by "next".
00239   /*! 
00240     
00241   \param n : number of variables to simulate.
00242   
00243   \return 
00244   */
00245   matrix simulate(long n);
00246   //! sets the startvalue for the simulation.
00247   void setstartvalue(double x) {startvalue = x;}
00248   //! sets delta.
00249   void setdelta(double d) {delta = d;}
00250 
00251  protected:
00252   //! variable used in the simulation.
00253   double delta;
00254   //! startvalue for the simulation.
00255   double startvalue;
00256 };
00257 
00258 //! Stochastic volatility model
00259 /*! 
00260   inherits from MARKOVMODEL
00261 */
00262 class SVMODEL: public MARKOVMODEL {
00263  protected:
00264   //! the timedistance between the observations. 
00265   double Delta;
00266   //! the number of subintervals for each Delta.
00267   long PointsPerDelta;
00268  public:
00269   //! simulates the SV-process.
00270   virtual matrix simulate(long n);
00271   //! simulates the volatility of the SV-process.
00272   virtual matrix simulateS(long n);
00273 };
00274 
00275 
00276 
00277 
00278 
00279 
00280 //////////////////////////////////////////////////////
00281 //         GLOBAL DEFINITIONS                        //
00282 //////////////////////////////////////////////////////
00283  
00284 typedef  double (PREESTMODEL::*PREpointerFn)(matrix par);
00285 #define callMemberFunction(object,ptrToMember)((object).*(ptrToMember))
00286 
00287 
00288 
00289 
00290 
00291 
00292 
00293 //********************* REAL MODELS: **********************
00294 
00295 
00296 //! Stochastic volatility model with CIR process
00297 /*! 
00298   inherits from SVMODEL
00299 */
00300 class SVCIR: public SVMODEL {
00301  public:
00302   //! Constructor of SV-CIR-process model
00303   /*! 
00304     
00305   \param FF : Inner functions
00306   \param d : The time-distance between the observations (delta)
00307   \param PPD : Points pr. delta (time-distance)
00308   \param N1 : Number of variables to simulate in Mbar-calculation
00309   \param N2 : Number of variables to simulate to estimate moments of process 
00310   \param qq : The lag-length in the prediction
00311   \param ss : Summation-start in estimating function
00312   \param ll :Summation-start in the expresion of Mbar
00313   \param sd : seed
00314   
00315   \return 
00316   */
00317   SVCIR(functionvector FF, double d = 1.0, long PPD = 200, long N1=150, long N2=150, long qq=5, long ss = 6, long ll=10, long sd = -123456) {
00318     ifuncs = FF;
00319     Delta=d; PointsPerDelta=PPD; setdelta(d/PPD); MbarNOS = N1; q = qq; 
00320     if (q>ss) 
00321       s = q;
00322     else
00323       s = ss; 
00324     l = ll; setseed(sd); 
00325     iter_Mbar = true; // Astar (and hence Mbar) is calculated
00326     numsimul = N2;
00327     N = FF.size();
00328   }
00329   //! Sets parameters of the model. Only used in simulations
00330   /*! 
00331 
00332   \param par : \f$ 3\times 1\f$-matrix containing parameters (alpha,theta,sigma).
00333   */
00334   virtual void setpar(matrix par);
00335   virtual unsigned getdim();
00336 
00337  protected:
00338 
00339   double alpha;                            // par[0]
00340   double theta;                            // par[1]
00341   double sigma;                            // par[2]
00342 
00343   virtual void setinitvalue() {MARKOVMODEL::setstartvalue(alpha);}
00344   //! updating in the simulation.
00345   virtual double next(double oldvalue) const;
00346 
00347 };
00348 
00349 
00350 
00351 //! Stochastic volatility model with Exponential Ornstein-Uhlenbeck-type process
00352 /*! 
00353   inherits from SVMODEL
00354 */
00355 class SVEOU: public SVMODEL 
00356 {
00357  public:
00358   //! Constructor of SVEOU-process model
00359   /*! 
00360 
00361   \param FF : Inner functions
00362   \param d : The time-distance between the observations (delta)
00363   \param PPD : Points pr. delta (time-distance)
00364   \param N1 : Number of variables to simulate in Mbar-calculation
00365   \param N2 : Number of variables to simulate to estimate moments of process 
00366   \param qq : The lag-length in the prediction
00367   \param ss : Summation-start in estimating function
00368   \param ll :Summation-start in the expresion of Mbar
00369   \param sd : seed  
00370   
00371   \return 
00372   */
00373   SVEOU(functionvector FF, double d = 1.0, long PPD = 200, long N1=100, long N2=100, long qq=5, long ss = 6, long ll=10, long sd = -123456) {
00374     ifuncs = FF;
00375     Delta=d; PointsPerDelta=PPD; setdelta(d/PPD); MbarNOS = N1; q = qq; 
00376     if (q>ss) 
00377       s = q;
00378     else
00379       s = ss; 
00380     l = ll; setseed(sd); 
00381     iter_Mbar = true; 
00382     numsimul = N2;
00383     N = FF.size();
00384   }
00385   virtual inline ~SVEOU() {}
00386 
00387   //! Sets parameters of the model. Only used in simulations
00388   /*! 
00389    
00390   \param par : \f$ 3\times 1\f$-matrix containing parameters (alpha,theta,sigma).
00391   */
00392   virtual void setpar(matrix par);
00393   virtual unsigned getdim();
00394 
00395  protected:
00396   double alpha;  // Parameters[0]
00397   double theta;  // Parameters[1]
00398   double sigma;  // Parameters[2]
00399   virtual double next(double oldvalue) const; 
00400   virtual void setinitvalue() {MARKOVMODEL::setstartvalue(alpha);} ///exp(theta));} 
00401 };
00402 
00403 
00404 
00405 //! Stochastic volatility model with Exponential Ornstein-Uhlenbeck-type process with NEW Z_i
00406 /*! 
00407   inherits from SVEOU. Simple example of implementation of new Z_i-fuunctions. In this case 
00408   the squareroot of each f_i is taken.
00409 */
00410 class SVEOU1: public SVEOU
00411 {
00412  public:
00413   //! Constructor of SVEOU1-process model
00414   /*! 
00415 
00416   \param FF : Inner functions
00417   \param d : The time-distance between the observations (delta)
00418   \param PPD : Points pr. delta (time-distance)
00419   \param N1 : Number of variables to simulate in Mbar-calculation
00420   \param N2 : Number of variables to simulate to estimate moments of process 
00421   \param qq : The lag-length in the prediction
00422   \param ss : Summation-start in estimating function
00423   \param ll :Summation-start in the expresion of Mbar
00424   \param sd : seed  
00425   
00426   \return 
00427   */
00428   SVEOU1(functionvector FF, double d = 1.0, long PPD = 200, long N1=100, long N2=100, long qq=5, long ss = 6, long ll=10, long sd = -123456): SVEOU(FF,d,PPD,N1,N2,qq,ss,ll,sd) {}
00429   virtual inline ~SVEOU1() {}
00430  
00431  protected:
00432   //! Z_{jk}^{(i)}, j=0,...,N-1, k=1,...,q, i
00433   virtual double Zval(unsigned j, long i, long k, matrix const& Y);
00434   virtual void calculate_Z();  
00435 };
00436 
00437 
00438 
00439 
00440 
00441 #endif // _MODELS_H_

Generated on Tue Feb 14 16:05:52 2006 for estfunc by doxygen 1.3.6