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

estfuncR.cpp

Go to the documentation of this file.
00001 /*!
00002   \file   estfuncR.cpp
00003   \author Klaus Holst
00004   \date   Jan 2005
00005   
00006   \brief  The interface to R.
00007     
00008 */
00009 
00010 #include <map>
00011 #include <string>
00012 #include <cstdlib>
00013 #include <cmath>
00014 #include "linalg.h"
00015 #include "linrdist.h"
00016 #include "qscr.h"
00017 #include "models.h"
00018 #include "svd.h"
00019 #include "debug.h"
00020 #include "util.h"
00021 // #include "siman.h"
00022 
00023 
00024 // Simulation and global options
00025 bool extraoutput = false;
00026 long N = 100;
00027 double d = 1.0;
00028 long q = 5;
00029 long P = 100;
00030 std::string prefix = "out";
00031 long s = 12345;
00032 long l=10;
00033 //matrix par(3);
00034 
00035 // Optimization options:
00036 double start_tt = 0.05;
00037 double stop_tt = 0.0;
00038 unsigned aiter = 200;
00039 unsigned nosteps = 3;
00040 
00041 PREESTMODEL *curModel;
00042 PREpointerFn *curF;
00043 
00044 double bestvalue;
00045   
00046 double call_estfunc(matrix const& x) {
00047   double val = callMemberFunction(*curModel,*curF)(x);
00048   return val;
00049 }
00050   
00051 
00052 double cubic(double x) {
00053   return x*x*x;
00054 }
00055 
00056 double cubicabs(double x) {
00057   return x*x*x;
00058 }
00059 
00060 double square(double x) {
00061   return x*x;
00062 }
00063  
00064 double identity(double x) {
00065   return x;
00066 }
00067   
00068 double mysqrt(double x) {
00069   return sqrt(x);
00070 }
00071   
00072 double mysqrtabs(double x) {
00073   return sqrt(fabs(x));
00074 }
00075 
00076 double myabs(double x) {
00077   return fabs(x);
00078 }
00079 
00080 double myexp(double x) {
00081   return exp(x);
00082 }
00083 
00084 double mylogabs(double x) {
00085   return log(fabs(x));
00086 }
00087 
00088 double myx4(double x) {
00089   double x2 = x*x;
00090   return x2*x2;
00091 }
00092 
00093 
00094 double estfunc(double *par) {
00095   unsigned n = (*curModel).getdim();
00096     matrix x(n);
00097     for (unsigned i=0; i<n; i++)
00098       x.set(par[i],i);
00099     // Evaluating Estimating Function in x
00100     qinit();    
00101     double val = call_estfunc(x);
00102     qend();    
00103     return(val);
00104 }
00105 
00106 
00107 
00108 // ************************* Export to R ****************************
00109 
00110 
00111 
00112 
00113 std::map<std::string, int> modelslist; 
00114 
00115 extern "C" {
00116 
00117 
00118   void estfunceval(char **model, double *indata, int *dimdata, double *par, int *dimpar,
00119                int *NN, double *dd, int *PP,
00120                    int *qq, int *ll, int *ss, int *innerfunc, int *diminnerfunc, int *calcAstar, 
00121                    int *debug,
00122                    double *res) {
00123 
00124     PREpointerFn f_sumH, f_estfunc;
00125 
00126     f_sumH = &PREESTMODEL::twonorm_sumH;
00127     f_estfunc = &PREESTMODEL::twonorm_estfunc;
00128 
00129 
00130     N = *NN;
00131     d = *dd;
00132     q = *qq;
00133     P = *PP;
00134     s = *ss;
00135     l = *ll;
00136 
00137     // inner functions:
00138     // 0: identity x
00139     // 1: |x|
00140     // 2: x^2    
00141     // 3: x^3
00142     // 4: x^4
00143     // 5: sqrt(abs())
00144     // 6: exp
00145     // 7: log(abs())
00146     // 8: |x|^3
00147     safevector <double (*)(double)> myfunctions;    
00148     for (int i=0; i<*diminnerfunc; i++) {
00149       switch(innerfunc[i]) {
00150       case 0:
00151         if (*debug) {
00152           std::cerr << "f" << i+1 << "(x) = x" << std::endl;
00153         }
00154         myfunctions.push_back(&identity);
00155         break;
00156       case 1:
00157         if (*debug) {
00158           std::cerr << "f" << i+1 << "(x) = |x|" << std::endl;
00159         }
00160         myfunctions.push_back(&myabs);
00161         break;
00162       case 2:
00163         if (*debug) {
00164           std::cerr << "f" << i+1 << "(x) = x^2" << std::endl;
00165         }
00166         myfunctions.push_back(&square);
00167         break;
00168       case 3:
00169         if (*debug) {
00170           std::cerr << "f" << i+1 << "(x) = x^3" << std::endl;
00171         }
00172         myfunctions.push_back(&cubic);
00173         break;
00174       case 4:
00175         if (*debug) {
00176           std::cerr << "f" << i+1 << "(x) = x^4" << std::endl;
00177         }
00178         myfunctions.push_back(&myx4);
00179         break;
00180       case 5:
00181         if (*debug) {
00182           std::cerr << "f" << i+1 << "(x) = sqrt(|x|)" << std::endl;
00183         }
00184         myfunctions.push_back(&mysqrtabs);
00185         break;
00186       case 6:
00187         if (*debug) {
00188           std::cerr << "f" << i+1 << "(x) = exp(x)" << std::endl;
00189         }
00190 
00191         myfunctions.push_back(&myexp);
00192         break;
00193       case 7:
00194         if (*debug) {
00195           std::cerr << "f" << i+1 << "(x) = log(|x|)" << std::endl;
00196         }
00197 
00198         myfunctions.push_back(&mylogabs);
00199         break;
00200       case 8:
00201         if (*debug) {
00202           std::cerr << "f" << i+1 << "(x) = |x|^3" << std::endl;
00203         }
00204         myfunctions.push_back(&cubicabs);
00205         break;
00206       default:
00207         if (*debug) {
00208           std::cerr << "f" << i+1 << "(x) = |x|" << std::endl;
00209         }
00210         myfunctions.push_back(&myabs);
00211         break;
00212       }
00213     }
00214     
00215 
00216     enum Models { Error = 0, SVCIR_MODEL, SVEOU_MODEL };
00217     modelslist["svcir"] = SVCIR_MODEL; 
00218     modelslist["sveou"] = SVEOU_MODEL;     
00219 
00220     SVCIR mSVCIR(myfunctions,d,P,N,N,q,q+1,l,s);      
00221     SVEOU mSVEOU(myfunctions,d,P,N,N,q,q+1,l,s);  
00222 
00223     switch (modelslist[std::string(*model)]) { 
00224     case SVCIR_MODEL: 
00225       if (*debug) {
00226         std::cerr << "Using SVCIR-model" << std::endl;
00227       }
00228       curModel = &mSVCIR;
00229       break; 
00230     case SVEOU_MODEL:     
00231       if (*debug) {
00232         std::cerr << "Using SVEOU-model" << std::endl;
00233       }
00234       curModel = &mSVEOU;
00235       break;      
00236     default:
00237       std::cerr << std::endl << "You specified an unknown model" << std::endl;
00238       break;
00239     }   
00240 
00241     curF = &f_estfunc;
00242 
00243     if (modelslist[std::string(*model)]!=Error) {
00244 
00245       matrix datamatrix(*dimdata);
00246       for (unsigned i=0; i<*dimdata; i++) 
00247         datamatrix.set(indata[i],i);
00248       (*curModel).setdata(datamatrix);
00249 
00250       matrix p(*dimpar);
00251       for (unsigned i=0; i<*dimpar; i++)
00252         p.set(par[i],i);     
00253 
00254       if ((*debug)!=0) {
00255         (*curModel).printdebug = 1;
00256       }
00257 
00258 
00259       if (*calcAstar) {
00260         curF = &f_estfunc;
00261         (*curModel).set_sumH_est(p.get(1)); // theta-estimate
00262         (*curModel).set_iter_Mbar(1); // Make sure Astar is calculated
00263       }
00264       else {
00265         curF = &f_sumH; 
00266       }
00267       double val = call_estfunc(p);
00268 
00269       *res = val;
00270     }
00271     
00272   }
00273 
00274 
00275   void simdata(char **model, double *par, int *dimpar, int *NN, double *dd, int*PP, int *ss,  double *data) {
00276 
00277     N = *NN;   
00278     d = *dd;
00279     P = *PP;
00280     s = *ss;
00281 
00282     matrix p(*dimpar);
00283     for (unsigned i=0; i<*dimpar; i++)
00284       p.set(par[i],i);     
00285 
00286     safevector <double (*)(double)> myfunctions;
00287     myfunctions.push_back(&square);
00288 
00289 
00290     enum Models { Error = 0, SVCIR_MODEL, SVEOU_MODEL };
00291     modelslist["svcir"] = SVCIR_MODEL; 
00292     modelslist["sveou"] = SVEOU_MODEL;     
00293 
00294     SVCIR mSVCIR(myfunctions,d,P,N,N,q,q+1,l,s);      
00295     SVEOU mSVEOU(myfunctions,d,P,N,N,q,q+1,l,s);
00296 
00297     switch (modelslist[std::string(*model)]) { 
00298     case SVCIR_MODEL: 
00299       curModel = &mSVCIR;
00300       break; 
00301     case SVEOU_MODEL:     
00302       curModel = &mSVEOU;
00303       break;      
00304     default:
00305       std::cerr << std::endl << "You specified an unknown model";
00306       break;
00307     }   
00308 
00309     if (modelslist[std::string(*model)]!=Error) {      
00310       (*curModel).setpar(p);
00311       matrix d = (*curModel).simulate(N);
00312       for (int i=0; i<N; i++)
00313         data[i] = d.get(i);
00314     }
00315   
00316   }
00317 
00318 
00319 }

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