Posted by
thusitha liyanage on
URL: http://quantlib.414.s1.nabble.com/Question-on-Path-Generation-for-Heston-Processes-tp7043p7045.html
Hi Kevin,
Thanks for your answer. I have used your code to generate paths for the Heston Process. Here is my C++ code incase if anyone is interested. Please not that the process is one dimensional. I need to modify this to get it to work in Multi Dimensional Heston Process Scenario.
vector<Real> generateOneDimPathUsingMultiPathGen(const boost::shared_ptr<StochasticProcess>& process,
const std::string& tag, Size timeSteps,
Time length, Size numOfPaths) {
time_t seconds;
cout << "Begin generateOneDimPathUsingMultiPathGen()\n";
typedef PseudoRandom::rsg_type rsg_type;
typedef MultiPathGenerator<rsg_type>::sample_type sample_type;
//Since we put the initial value at the first element we need (timeStep + 1) * numOfPaths
vector<Real> values(numOfPaths*(timeSteps + 1));
//The seed to the simulation engine
seconds = time (NULL);
cout << "Seed: " << seconds << endl;
//Here we multiply the timeSteps by two. This is because for heston process
//There should be one random number for the price process and another for the volatility process.
rsg_type rsg = PseudoRandom::make_sequence_generator(2*(timeSteps), seconds);
TimeGrid timeGrid(length, timeSteps);
cout << " generator dim= " << rsg.dimension() << std::endl;
cout << " process factors * (timeGrid.size()-1)= " << (timeGrid.size()-1)*(process->factors()) << std::endl;
// The generator dimension should match the (timeGrid.size()-1)*(process->factors())
// Otherwise the multipath generator would throw an exception.
//
MultiPathGenerator<rsg_type> generator(process, timeGrid, rsg, false);
//const Path& path;
Real assetPrice, assetVol;
for (int k=0; k< numOfPaths; k++){
sample_type sample = generator.next();
MultiPath multipath = sample.value;
const Path& pricePath = multipath[0];
const Path& volPath = multipath[1];
for (int i=0; i <= timeSteps; ++i) {
assetPrice = pricePath[i];
assetVol = volPath[i];
cout << " i= " << i << " assetPrice= " << assetPrice << std::endl;
values[k*(timeSteps + 1) + i] = assetPrice;
}
cout << "\n\n\n";
} // end i loop
cout << ("End generateOneDimPathUsingMultiPathGen()...\n");
return values;
}
Thusitha
-----------------------------------------------------------
kmanley wrote
You need to use a multi path generator. This is an example of how I do it from Python, this can be translated directly into C++
from lib.QuantLib import *
def run():
today = Date(15, October, 2008)
riskFreeRate = FlatForward(today, 0.0, ActualActual())
dividendRate = FlatForward(today, 0.0, ActualActual())
discretization = Reflection # PartialTrunction, FullTruncation, Reflection, ExactVariance
hp = HestonProcess(YieldTermStructureHandle(riskFreeRate),
YieldTermStructureHandle(dividendRate),
QuoteHandle(SimpleQuote(570.89)), # s0
.48, # v0
1.2, # kappa
0.25, # theta
0.80, # sigma
-0.64, # rho
discretization)
times = [n/365.0 for n in range(20)]
rsg = GaussianRandomSequenceGenerator(UniformRandomSequenceGenerator(2*(len(times)-1), UniformRandomGenerator()))
mpg = GaussianMultiPathGenerator(hp, TimeGrid(times), rsg)
while True:
sample = mpg.next()
multipath = sample.value()
assetprices = multipath[0]
vols = multipath[1]
for i in range(len(multipath)):
print "time %f: asset=%.2f, vol=%.2f" % (assetprices.time(i), assetprices.value(i), vols.value(i))
raw_input("press any key to generate another multipath")
if __name__ == "__main__":
run()
thusi wrote
Would the following code work, if the process is Heston Process?
rsg_type rsg = PseudoRandom::make_sequence_generator(timeSteps, seconds);
PathGenerator<rsg_type> generator(process, length, timeSteps,
rsg, false);
for (int k=0; k< numOfPaths; k++){
sample_type sample = generator.next();
When I try to use this it generate an exception. I believe the reason is that the Process for PathGenerator is assumed to be StochasticProcess1D