-
Notifications
You must be signed in to change notification settings - Fork 0
/
Portfolio.cpp
90 lines (81 loc) · 2.69 KB
/
Portfolio.cpp
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
#include "Portfolio.h"
#include <time.h>
namespace MeanVarianceFrontier
{
Portfolio::Portfolio(unsigned int numberOfAssets, boost::numeric::ublas::matrix<double> covarianceMatrix, std::vector<double> expReturns, std::vector<double> variances) :
m_portfolioMeanVariance{ numberOfAssets },
m_covarianceMatrix{ covarianceMatrix },
m_expReturnsVector{ numberOfAssets },
m_returnsVarianceVector{ numberOfAssets }
{
srand(static_cast<unsigned int>(time(NULL)));
auto index = 0;
for (auto expReturn : expReturns)
{
m_expReturnsVector(index) = expReturn;
++index;
}
index = 0;
for (auto var : variances)
{
m_returnsVarianceVector(index) = var;
++index;
}
}
std::vector<double> Portfolio::generateRandomWeights()
{
std::vector<double> portfolioWeights;
double sumOfWeights = 0.0l;
const double portfolioMax = 1.0l;
for (unsigned int i = 0; i < m_portfolioMeanVariance.numberOfAssets; i++)
{
if (i != m_portfolioMeanVariance.numberOfAssets - 1)
{
double weight = fRand(sumOfWeights, portfolioMax) - sumOfWeights;
portfolioWeights.push_back(weight);
sumOfWeights += weight;
}
else
{
double weight = portfolioMax - sumOfWeights;
portfolioWeights.push_back(weight);
}
}
return portfolioWeights;
}
double Portfolio::computeMeanWith(std::vector<double> portfolioWeights)
{
unsigned int index = 0;
double meanReturn = 0.0l;
for (auto weight : portfolioWeights)
{
meanReturn += (m_expReturnsVector[index] * weight);
++index;
}
return meanReturn;
}
double Portfolio::computeVarianceWith(std::vector<double> portfolioWeights)
{
boost::numeric::ublas::vector<double> weights{ portfolioWeights .size() };
unsigned int index = 0;
for (auto weight : portfolioWeights)
{
weights(index) = weight;
++index;
}
return inner_prod(prod(weights, m_covarianceMatrix), weights);
}
const boost::numeric::ublas::matrix<double> Portfolio::getCovarianceMatrix()
{
return m_covarianceMatrix;
}
const boost::numeric::ublas::vector<double> Portfolio::getExpReturnsVector()
{
return m_expReturnsVector;
}
double MeanVarianceFrontier::Portfolio::fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
}