Repository navigation
Expand file tree
/
Copy pathsimulate_support_code.cpp
More file actions
57 lines (42 loc) · 1.71 KB
/
Copy pathsimulate_support_code.cpp
File metadata and controls
57 lines (42 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<ql/quantlib.hpp>
/*
* Multipath simulator. A multipath simulator is needed when the stochastic
* process involves more than 1 brownian. For example, Heston's model involves
* the simulation of the variance process and the simulation of the price process
*/
namespace QuantLib {
void simulateMP(const boost::shared_ptr<StochasticProcess>& process,
int nbPaths, int nbSteps, Time horizon, BigNatural seed,
bool antithetic_variates,
double *res) {
typedef PseudoRandom::rsg_type rsg_type;
typedef MultiPathGenerator<rsg_type>::sample_type sample_type;
Time length = horizon;
Size timeSteps = nbSteps;
boost::shared_ptr<StochasticProcess> sp = process;
Size assets = sp->factors();
rsg_type rsg = PseudoRandom::make_sequence_generator(timeSteps*assets,
seed);
MultiPathGenerator<rsg_type> generator(sp, TimeGrid(length, timeSteps),
rsg, false);
// res(nbPaths+1, nbSteps+1)
// time steps in first row
double dt = length/nbSteps;
for (int j=0; j<=nbSteps; ++j)
res[(0*nbSteps)+j] = dt*j;
// fill simulated paths
for (int i=0; i<nbPaths; ++i) {
const bool antithetic = (i%2)==0 ? false : true;
sample_type sample = antithetic_variates ?
(antithetic ? generator.antithetic()
: generator.next())
: generator.next();
Path p1 = sample.value[0];
int j=0;
for (Path::iterator ip = p1.begin(); ip<p1.end(); ++ip) {
res[(i+1)*(nbSteps+1)+j] = (*ip);
j++;
};
};
};
}