我正在使用蒙特卡罗开发一个投资组合选项价格,但我遇到了一个问题。我的程序编译并运行。然后,当程序必须计算投资组合价格时,我有超出范围向量。该怎么办?我已附上我的Portfolio.cpp
和Monte Carlo.cpp
。
PortFolio.cpp:
#include "Portfolio.h"
#include <iostream>
#include <numeric>
Portfolio::Portfolio(const vector<VanillaOption>& Option_){
numberOfOptions = Option_.size();
for (unsigned int i = 0; i < 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;
for (unsigned int i = 0; i <getNumberOfOptions(); i++) {
sum = std::accumulate(optionPrice.begin(), optionPrice.end(),0.0) * positionValue;
}
return sum;
}
这是我对产品组合的MonteCarlo实施:
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 to 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
for (unsigned int i = 0; i < numberOfPaths; i++) {
//Option
for (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
for (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];
}
}
for (unsigned int i = 0; i < numberOfOptions; i++) {
mean[i] = runningSum[i] /getNumberOfPaths();
mean[i] *= exp(-vTheAsset[i].getRate()*thePortfolio.getExpiry(i)); thePortfolio.setOptionPrice(mean[i]);
}
return thePortfolio;
}