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

estfunc.cpp

Go to the documentation of this file.
00001 /*!
00002   \file   estfunc.cpp
00003   \author Klaus Holst
00004   \date   Jan 2005
00005   
00006   \brief  Demonstration of estimating function. Optimization via simulated annealing.
00007     
00008 */
00009 
00010 #include <cmath>
00011 #include <cstdlib>
00012 #include <iostream>
00013 #include <fstream>
00014 #include <iomanip>
00015 #include <ctime>
00016 #include <cstring>
00017 #include <vector>
00018 #include <map>
00019 #include "linalg.h"       // various (matrix)functions
00020 #include "models.h"
00021 #include "util.h"
00022 #include "debug.h"
00023 #include "gnuplot.h"
00024 #include "siman.h"
00025 #include "qscr.h"
00026 
00027 
00028 // Simulation and global options
00029 bool extraoutput = false;
00030 long N = 200;
00031 double d = 1.0;
00032 long q = 5;
00033 long P = 20;
00034 std::string prefix = "out";
00035 long s = 12345;
00036 long l=10;
00037 matrix par(3);
00038 
00039 // Optimization options:
00040 double start_tt = 0.05;
00041 double stop_tt = 0.0;
00042 unsigned aiter = 200;
00043 unsigned nosteps = 100;
00044 
00045 
00046 PREESTMODEL *curModel;
00047 PREpointerFn *curF;
00048 
00049 double bestvalue;
00050 
00051 double call_estfunc(matrix const& x) {
00052     double val = callMemberFunction(*curModel,*curF)(x);
00053     return val;
00054 }
00055 
00056 
00057 double cubic(double x) {
00058   return x*x*x;
00059 }
00060 
00061 double square(double x) {
00062   return x*x;
00063 }
00064 
00065 double identity(double x) {
00066   return x;
00067 }
00068 
00069 double mysqrt(double x) {
00070   return sqrt(x);
00071 }
00072 
00073 double myabs(double x) {
00074   return fabs(x);
00075 }
00076 
00077 
00078 void setsimplex(matrix& p) {
00079   p.newsize(4,3);
00080     
00081   p.set(1,0,0);
00082   p.set(1,0,1);
00083   p.set(1.5,0,2);
00084 
00085   p.set(1.5,1,0);
00086   p.set(1,1,1);
00087   p.set(1,1,2);
00088 
00089   p.set(1.5,2,0);
00090   p.set(1.5,2,1);
00091   p.set(1,2,2);
00092 
00093   p.set(1,3,0);
00094   p.set(1,3,1);
00095   p.set(1.5,3,2);
00096 }
00097 
00098 
00099 
00100 
00101 matrix anneal(PREESTMODEL *M, SimAnneal &sa, double epsilon, unsigned steps, double starttemp, matrix startpoint) {
00102 
00103 //  (*M).set_iter_Mbar(1); // Calculate A(theta) next time estfunc is evaluated.
00104 //  double estfunctrueval = call_estfunc(par);
00105 //  string st = "estfunc(" + string(par.trans().row2string(0)) + ") = " + numStr(estfunctrueval);
00106 //  qmvaddstr(15,55, st.c_str());
00107 
00108 //  (*M).set_iter_Mbar(1); // Calculate A(theta) next time estfunc is evaluated.
00109 //  matrix pt(3); pt.set(0.966078,0); pt.set(0.478924,1); pt.set(0.556969,2);
00110 //  estfunctrueval = call_estfunc(pt);
00111 //  st = "estfunc(" + string(pt.trans().row2string(0)) + ") = " + numStr(estfunctrueval);
00112 //  qmvaddstr(16,55, st.c_str());
00113 
00114 
00115   (*M).set_iter_Mbar(1); // Calculate A(theta) next time estfunc is evaluated.
00116   sa.calcyb(); // Do that now.
00117   matrix bestpoint = startpoint;
00118   //  std::cerr << "bestpoint = "; bestpoint.stdprint();
00119   //  std::cerr << "bestvalue = " << sa.get_yb() << std::endl;
00120   
00121   bestvalue = sa.funceval(bestpoint);
00122 
00123   //  std::cerr << "bestvalue = " << bestvalue << std::endl;
00124   double curtt = starttemp;
00125   for (unsigned i=0; i<nosteps; i++) {
00126     (*M).set_iter_Mbar(0); // Do NOT calculate A(theta) the next couple of times estfunc is evaluated.
00127     sa.set_tt(curtt);
00128     sa.set_simplex( newsimplex(bestpoint) );   
00129     sa.optimize();
00130     (*M).set_iter_Mbar(1);    // Set iter_Mbar = true and call calcyb to update A(theta).
00131     qmvaddstr(25,7, "Calculating yb (and hence updating A(theta))...");
00132     sa.calcyb();
00133     if (sa.get_yb()<bestvalue) {
00134       bestvalue = sa.get_yb();
00135       bestpoint = sa.get_pb();
00136     }
00137     //    std::cerr << "bestvalue = " << bestvalue << std::endl;
00138     qmove(25,1); qclrtoeol();
00139     qmove(18,1); qclrtoeol();
00140     curtt -= epsilon;
00141   }
00142   
00143   return bestpoint;
00144 }
00145 
00146 
00147 void parseArguments(int argc, char* argv[]) {
00148   char syntax[] = 
00149     "Program demonstrating 'Prediction-Based Estimating Functions'\n"
00150     "Syntax:\n"
00151     "  diffusion <options>\n"
00152     "Options:\n"
00153     " -q[i]       (The lag-length in the prediction)\n"
00154     " -N[i]       (The number of simulations used)\n"
00155     " -d[f]       (The time-distance between the observations)\n"
00156     " -P[i]       (Points per Delta (time-distance))\n"
00157     " -f[s]       (Output-file = s)\n"
00158     " -l[i]       (Summation-start in the expresion of Mbar)\n"
00159     " -s[i]       (Seed)\n"
00160     " -i[i]       (Number of optimization-iterations)\n"
00161     " -v          (Ekstra output)\n"
00162     "\n";
00163   
00164   if((argc>1)&&( strcmp(argv[1],"-h")==0)) { 
00165     std::cout << syntax;
00166     exit(0); 
00167   }
00168   
00169   for(int i=1; i<argc; ++i) { 
00170     char* arg = argv[i];
00171     std::string str(arg);
00172     std::string strval(str.substr(2,str.length()));
00173     char option = arg[1];
00174     safevector<float> paramlist = getparm(strval);     
00175     switch (option)
00176       {
00177       case 'q': 
00178         q = static_cast <long> (paramlist[0]);
00179         break;      
00180       case 'N':
00181         N = static_cast <long> (paramlist[0]);
00182         break; 
00183       case 'd': 
00184         d = static_cast <double> (paramlist[0]);
00185         break;      
00186       case 'P':
00187         P = static_cast <long> (paramlist[0]);
00188         break; 
00189       case 'l': 
00190         l = static_cast <long> (paramlist[0]);
00191         break;      
00192       case 's':
00193         s = static_cast <long> (paramlist[0]);
00194         break; 
00195       case 'f':
00196         prefix = strval;
00197         break;
00198         //     case 's':
00199         //       inwidth = static_cast <unsigned> (paramlist[0]);
00200         //       inheight = static_cast <unsigned> (paramlist[1]);
00201       case 'v':
00202         extraoutput = true;
00203         break;
00204       default:      
00205         throw std::string("Unknown option -") + option + ".\n" + syntax;
00206       }
00207   }
00208 }
00209 
00210 
00211 matrix SimulateAndEstimate() { 
00212 
00213   std::string line = "";
00214   for (unsigned i=0; i<50; i++) 
00215     line += "-";
00216   line += "\n";
00217 
00218 
00219   qmvaddstr(3,3, "Prediction-Based Estimating Function in SV-Model.");
00220 
00221   PREpointerFn f_sumH, f_estfunc;
00222 
00223   f_sumH = &PREESTMODEL::twonorm_sumH;
00224   f_estfunc = &PREESTMODEL::twonorm_estfunc;
00225 
00226 
00227   PREpointerFn A, B;
00228   A = &PREESTMODEL::twonorm_sumH;
00229   B = &PREESTMODEL::twonorm_estfunc;
00230 
00231   
00232   matrix simdata, estimate(3); 
00233 
00234   std::string st;
00235 
00236   qmvaddstr(5,3,"Options");
00237   st = "N = " + numStr(N);  qmvaddstr(6,5, st.c_str());
00238   st = "d = " + numStr(d);  qmvaddstr(7,5, st.c_str());
00239   st = "q = " + numStr(q);  qmvaddstr(8,5, st.c_str());
00240   st = "P = " + numStr(P);  qmvaddstr(9,5, st.c_str());
00241   st = "l = " + numStr(l);  qmvaddstr(10,5, st.c_str());
00242   st = "s = " + numStr(s);  qmvaddstr(12,5, st.c_str());  
00243   qmvaddstr(13,3,"Optimization options:");
00244   st = "start_tt = " + numStr(start_tt);  qmvaddstr(14,5, st.c_str());
00245   st = "stop_tt = " + numStr(stop_tt);  qmvaddstr(15,5, st.c_str());
00246   st = "aiter = " + numStr(aiter);  qmvaddstr(16,5, st.c_str());
00247   
00248 
00249   safevector <double (*)(double)> myfunctions;
00250   //  myfunctions.push_back(&identity);
00251   myfunctions.push_back(&square);
00252   myfunctions.push_back(&cubic);
00253   myfunctions.push_back(&myabs);
00254   //  myfunctions.push_back(&mysqrt);
00255 
00256 
00257 
00258     //  SVCIR testmodel(myfunctions,d,P,N,200,q,q+1,l,s);
00259   SVEOU testmodel(myfunctions,d,P,N,N,q,q+1,l,s);
00260     
00261     //  SVCIR testmodel(myfunctions,d,P,N,q,q+1,l,s);
00262   curModel = &testmodel;
00263 
00264 
00265 
00266 
00267   
00268   par.set(10.0, 0); 
00269   par.set(5.0, 1); 
00270   par.set(5.0, 2);
00271 
00272   testmodel.setpar(par);
00273   simdata = testmodel.simulate(N-1);
00274   testmodel.setdata(simdata);
00275 
00276 
00277 
00278   //  par.set(12.94339, 0); 
00279   //  par.set(5.43643, 1); 
00280   //  par.set(5.34323, 2);
00281 
00282   //  curF = &f_estfunc;
00283   //  testmodel.set_sumH_est(5); // theta-estimate
00284   //  double estfunctrueval = call_estfunc(par);
00285   //  st = "estfunc(truepar) = " + numStr(estfunctrueval);
00286   //  std::cerr << st << estfunctrueval << std::endl;
00287   //  exit(0);
00288 
00289 
00290   matrix p;
00291   setsimplex(p);
00292   
00293   qmvaddstr(24,10, "Calculating initial estimate using twonorm_sumH (simplified est.func.)"); qclrtoeol();
00294  
00295   {
00296     curF = &f_sumH;
00297     
00298     Function F(call_estfunc, 3); // Define 3d-function
00299 
00300     SimAnneal sa(&F, p, 0.0, 100);
00301 
00302     sa.optimize();
00303     estimate = sa.get_pb(); // parameter (alpha,theta,sigma)
00304     bestvalue = sa.get_yb();
00305   } // F and sa destructed
00306 
00307   //  std::cerr << "estimate="; estimate.stdprint();
00308   //  std::cerr << "value=" << bestvalue << std::endl; 
00309   
00310   testmodel.set_sumH_est(estimate.get(1)); // theta-estimate
00311     
00312   
00313   //  testmodel.set_sumH_est(1000); // theta-estimate
00314 
00315   qmvaddstr(24,10, "Calculating estimate using twonorm_estfunc."); qclrtoeol();
00316 
00317   p = newsimplex(p.getrow(0).trans()); // Setting new simplex around current parameter-estimate.
00318 
00319   {
00320     curF = &f_estfunc;
00321     Function F(call_estfunc, 3); // Define 3d-function
00322     SimAnneal sa(&F, p, start_tt, aiter); // Create annealing-object 
00323     estimate = anneal(curModel, sa, start_tt/(double)nosteps, nosteps, start_tt, p.getrow(0).trans());  // parameter (alpha,theta,sigma)
00324     //    bestvalue = sa.get_yb();
00325   } // F and sa destructed
00326   
00327   
00328   return estimate;
00329 
00330 }
00331  
00332 
00333 int main(int argc, char* argv[]) {
00334   try {
00335     parseArguments(argc,argv);
00336 
00337     qinit();
00338     matrix estimate = SimulateAndEstimate();
00339     qend();
00340 
00341     std::cerr << std::endl << "Program terminated successfully." << std::endl;
00342     std::cerr << "Minimum value: " << bestvalue << std::endl; 
00343     std::cerr << "ESTIMATE: [";
00344     unsigned n=estimate.row();
00345     for (unsigned i=0; i<n-1; i++) {
00346       std::cerr << estimate.get(i) << ", ";
00347     }
00348     std::cerr << estimate.get(n-1) << "]" << std::endl;
00349 
00350     std::cerr << std::endl;
00351      
00352     
00353     return EXIT_SUCCESS;
00354   } catch(std::string const& err) {
00355     std::cerr << "exn: " << err << std::endl;
00356     return EXIT_FAILURE;
00357   } 
00358 }
00359 
00360 
00361 
00362 

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