c++11 - C++ Monte Carlo portfolio Pricer -
i'm developing portfolio option pricer using monte carlo, have encountered problem. program compiles , runs. have out of range vector when program has compute portfolio price. do? have attached portfolio.cpp
, monte carlo.cpp
.
portfolio.cpp:
#include "portfolio.h" #include <iostream> #include <numeric> portfolio::portfolio(const vector<vanillaoption>& option_){ numberofoptions = option_.size(); (unsigned int = 0; < getnumberofoptions(); ++i) { theoption.push_back(option_[i].clone()); } } shared_ptr<portfolio> portfolio::clone() const { return shared_ptr<portfolio>(new portfolio(*this)); } void portfolio::setoptionprice(const double& price) { optionprice.push_back(price); } double portfolio::getoptionprice(const unsigned int& index)const { return optionprice[index]; } double portfolio::getnumberofoptions()const { return theoption.size(); } double portfolio::getpayoff(const unsigned int& index, const vector<double>& spot) const { return theoption[(int)index]->getpayoff(spot); } double portfolio::getexpiry(const unsigned int& index)const { return theoption[index]->getexpiry(); } double portfolio::getoptionvaluep()const { double sum = 0; (unsigned int = 0; <getnumberofoptions(); i++) { sum = std::accumulate(optionprice.begin(), optionprice.end(),0.0) * positionvalue; } return sum; }
this montecarlo implementation portfolio:
portfoliosimulation::portfoliosimulation(const unsigned long& numberofpaths_, const unsigned long& numberoftimesteps_) { setnumberofpaths(numberofpaths_); setnumberoftimesteps(numberoftimesteps_); } portfolio portfoliosimulation::operator()( const portfolio& theportfolio_, vector<underlyingasset>& vtheasset_, const vector<shared_ptr<process>>& vtheprocess_)const { //assign passing values local variables vector<shared_ptr<process>> vtheprocess = vtheprocess_; portfolio theportfolio = theportfolio_; vector<underlyingasset> vtheasset = vtheasset_; const unsigned int numberofoptions = (int)theportfolio.getnumberofoptions(); vector<vector<double>> simulatedspot(numberofoptions,vector<double>(getnumberoftimesteps())); vector<double> runningsum(numberofoptions,1); vector<double > mean(numberofoptions,1); vector<double> simulatedpayoff(numberofoptions,1); double time[2]; const double numberofpaths = getnumberofpaths(); const double numberoftimesteps = getnumberoftimesteps(); //path simulation (unsigned int = 0; < numberofpaths; i++) { //option (unsigned int j = 0; j < numberofoptions; j++) { //initialise values every paths const double step = theportfolio.getexpiry(j) / getnumberoftimesteps(); vtheasset[j] = vtheasset_[j]; time[0] = 0; time[1] = step; //time steps (unsigned int k = 0; k < numberoftimesteps; k++) { simulatedspot[j][k] = (*vtheprocess[j])(vtheasset[j], time[0], time[1]); vtheasset[j].setspot(simulatedspot[j][k]); time[0] = time[1]; time[1] += step; } simulatedpayoff[j] = theportfolio.getpayoff(j, simulatedspot[j]); runningsum[j] += simulatedpayoff[j]; } } (unsigned int = 0; < numberofoptions; i++) { mean[i] = runningsum[i] /getnumberofpaths(); mean[i] *= exp(-vtheasset[i].getrate()*theportfolio.getexpiry(i)); theportfolio.setoptionprice(mean[i]); } return theportfolio; }
Comments
Post a Comment