-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiStockModel.h
125 lines (109 loc) · 3.09 KB
/
MultiStockModel.h
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
#include "stdafx.h"
#include "Matrix.h"
#include "BlackScholesModel.h"
#include "MarketSimulation.h"
/**
* A model for a collection of stocks that uses
* multi-dimensional Brownian motion
*/
class MultiStockModel {
public:
/* Create a model based on a 1-d black scholes model */
explicit MultiStockModel(
const BlackScholesModel& bsm );
MultiStockModel(std::vector<std::string> stocks,
Matrix stockPrices,
Matrix drifts,
Matrix covarianceMatrix);
/* The risk free rate */
double getRiskFreeRate() const {
return riskFreeRate;
}
/* The current date in years */
double getDate() const {
return date;
}
/* Setter */
void setRiskFreeRate(double riskFreeRate) {
this->riskFreeRate = riskFreeRate;
}
/* Setter */
void setDate(double date ) {
this->date = date;
}
/* Get the names of the stocks */
std::vector<std::string> getStocks() {
return stockNames;
}
double getStockPrice(const std::string& stock) {
return stockPrices(getIndex(stock),0);
}
Matrix getCovarianceMatrix() {
return covarianceMatrix;
}
/* Extract the 1-d sub model for a given
stock code */
BlackScholesModel getBlackScholesModel(
const std::string& stockCode) const;
/* Get a sub model that uses only the given stocks */
MultiStockModel getSubmodel(
std::set<std::string> stocks) const;
/* Returns a simulation up to the given date
in the P measure */
MarketSimulation generatePricePaths(
std::mt19937& rng,
double toDate,
int nPaths,
int nSteps) const;
/* Returns a simulation up to the given date
in the Q measure */
MarketSimulation generateRiskNeutralPricePaths(
std::mt19937& rng,
double toDate,
int nPaths,
int nSteps) const;
/* How many random numbers are needed
to generate the given paths? */
long long randSize(long long nPaths,
long long nSteps) {
return stockNames.size()*nPaths*nSteps;
}
/* For testing it is useful to have a standard
dummy name for stocks */
static const std::string DEFAULT_STOCK;
/* Creates a standard 3d model for testing */
static MultiStockModel createTestModel();
private:
/* Mapping from a stock code to the index
used in our matrices */
std::unordered_map<std::string, int> stockCodeToIndex;
/* The names of the stocks */
std::vector<std::string> stockNames;
/* A column vector of drifts */
Matrix drifts;
/* A column vector of current stock prices */
Matrix stockPrices;
/* The covariance matrix */
Matrix covarianceMatrix;
/* The risk free rate */
double riskFreeRate;
/* The current date */
double date;
/* Generate price paths with the given drifts */
MarketSimulation generatePricePaths(
std::mt19937& rng,
double toDate,
int nPaths,
int nSteps,
Matrix drifts) const;
/* Gets the index of a given stock in the matrices */
int getIndex(const std::string& stockCode)
const {
auto pos = stockCodeToIndex.find(stockCode);
ASSERT(pos != stockCodeToIndex.end());
int idx = pos->second;
return idx;
}
};
void testMultiStockModel();