test4.cpp

Go to the documentation of this file.
00001 #include "indii/ml/aux/WienerProcess.hpp"
00002 #include "indii/ml/aux/GaussianPdf.hpp"
00003 #include "indii/ml/aux/DiracMixturePdf.hpp"
00004 #include "indii/ml/aux/vector.hpp"
00005 #include "indii/ml/aux/matrix.hpp"
00006 #include "indii/ml/aux/Random.hpp"
00007 
00008 #include <iostream>
00009 #include <fstream>
00010 
00011 using namespace std;
00012 
00013 namespace aux = indii::ml::aux;
00014 
00015 /**
00016  * @file test4.cpp
00017  *
00018  * Test of WienerProcess.
00019  *
00020  * This test creates a univariate WienerProcess and simulates several,
00021  * plotting:
00022  *
00023  * @li the actual mean and variance at each point
00024  * @li the expected mean and variance at each point.
00025  * @li the mean and variance at each point calculated using importance
00026  * sampling, so as to test the densityAt() function.
00027  *
00028  * Results are as follows:
00029  *
00030  * \image html test4.png "Results"
00031  * \image latex test4.eps "Results"
00032  */
00033 
00034 /**
00035  * Dimensionality of the process.
00036  */
00037 const unsigned int M = 1;
00038 
00039 /**
00040  * Number of sample trajectories.
00041  */
00042 const unsigned int N = 1000;
00043 
00044 /**
00045  * Length of each trajectory.
00046  */
00047 const unsigned int LENGTH = 500;
00048 
00049 /**
00050  * Length of time step.
00051  */
00052 const unsigned int DELTA = 3;
00053 
00054 /**
00055  * Number of importance samples.
00056  */
00057 const unsigned int P = 10000;
00058 
00059 /**
00060  * Run tests.
00061  */
00062 int main(int argc, const char* argv[]) {
00063   /* set up distributions */
00064   unsigned int i, j;
00065 
00066   std::ofstream fact("results/test4_actual.out"); // actual statistics
00067   std::ofstream fexp("results/test4_expected.out"); // expected statistics
00068   std::ofstream fimp("results/test4_sample.out"); // importance sampling
00069 
00070   aux::WienerProcess<unsigned int> wiener(1);
00071   std::vector<aux::DiracMixturePdf> ts; // for calculating mean and variance
00072   aux::DiracPdf trajectory(M);
00073 
00074   for (i = 0; i < LENGTH; i++) {
00075     ts.push_back(aux::DiracMixturePdf(M));
00076   }
00077 
00078   /* calculate trajectories */
00079   for (i = 0; i < N; i++) {
00080     trajectory(0) = 0.0;
00081     ts[0].add(trajectory);
00082 
00083     for (j = 1; j < LENGTH; j+=DELTA) {
00084       trajectory += wiener.sample(DELTA);
00085 
00086       ts[j/DELTA].add(trajectory);
00087     }
00088   }
00089 
00090   /* output mean and std. dev. of trajectories at each time point,
00091      importance sample to achieve same result */
00092   for (j = 0; j < LENGTH; j+=DELTA) {
00093     fact << j << '\t';
00094     fact << ts[j/DELTA].getExpectation()(0) << '\t';
00095     fact << ts[j/DELTA].getCovariance()(0,0) << endl;
00096   }
00097 
00098   /* output expected mean and covariance at each time point */
00099   for (j = 0; j < LENGTH; j+=DELTA) {
00100     fexp << j << '\t';
00101     fexp << 0.0 << '\t';
00102     fexp << j << endl;
00103   }
00104 
00105   /* importance sample mean and std.dev of trajectories at each time
00106      point */
00107   aux::GaussianPdf gaussian(M);
00108   aux::GaussianPdf q(M); // proposal distribution
00109   aux::DiracMixturePdf sampled(M);
00110   aux::vector s(M);
00111   double w;
00112 
00113   q.setExpectation(aux::zero_vector(M));
00114   q.setCovariance(5000.0*aux::identity_matrix(M));
00115 
00116   for (j = 0; j < LENGTH; j+=DELTA) {
00117     gaussian.setExpectation(ts[j/DELTA].getExpectation());
00118     gaussian.setCovariance(ts[j/DELTA].getCovariance());
00119 
00120     sampled.clear();
00121     for (i = 0; i < P; i++) {
00122       s = q.sample();
00123       w = gaussian.densityAt(s) / q.densityAt(s);
00124       sampled.add(s, w);
00125     }
00126 
00127     fimp << j << '\t';
00128     fimp << sampled.getExpectation()(0) << '\t';
00129     fimp << sampled.getCovariance()(0,0) << endl;
00130   }
00131 
00132 }

Generated on Wed Dec 17 15:01:30 2008 for dysii Probability Distributions Test Suite by  doxygen 1.5.3