This repository was archived by the owner on Jan 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPortfolioOptimizer.h
More file actions
142 lines (132 loc) · 4.88 KB
/
Copy pathPortfolioOptimizer.h
File metadata and controls
142 lines (132 loc) · 4.88 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef PORTFOLIO_OPTIMIZER_H
#define PORTFOLIO_OPTIMIZER_H
#include <vector>
#include <string>
#include <cmath>
#include <random>
#include <algorithm>
#include <stdexcept>
/**
* Structure representing a single simulated portfolio
*/
struct PortfolioResult {
double portfolioReturn; // Annualized expected return
double portfolioRisk; // Annualized volatility (standard deviation)
double sharpeRatio; // Risk-adjusted return metric
std::vector<double> weights; // Asset allocation weights (sum to 1.0)
};
/**
* Structure representing the efficient frontier analysis results
*/
struct EfficientFrontierResult {
PortfolioResult optimalSharpePortfolio; // Portfolio with highest Sharpe Ratio
std::vector<PortfolioResult> allSimulations; // All simulated portfolios
std::vector<std::string> assetNames; // Names of assets in portfolio
};
/**
* PortfolioOptimizer - Implements Modern Portfolio Theory optimization
*
* Uses Monte Carlo simulation to find the optimal asset allocation that
* maximizes risk-adjusted returns (Sharpe Ratio).
*
* WARNING: This uses HISTORICAL data. Past performance does NOT guarantee
* future results. Optimal allocations change as market conditions change.
*
* Based on Modern Portfolio Theory by Harry Markowitz (Nobel Prize, 1990)
*/
class PortfolioOptimizer {
public:
/**
* Calculate the efficient frontier using Monte Carlo simulation
*
* This function simulates thousands of random portfolio allocations,
* calculates their expected return and risk, and finds the optimal
* portfolio with the highest Sharpe Ratio.
*
* Algorithm:
* 1. Generate random weights for each asset (sum to 1.0)
* 2. Calculate portfolio return: weighted average of asset returns
* 3. Calculate portfolio risk: √(w^T × Σ × w) where Σ is covariance matrix
* 4. Calculate Sharpe Ratio: (return - risk_free) / risk
* 5. Repeat for numPortfolios iterations
* 6. Find portfolio with maximum Sharpe Ratio
*
* @param assetReturns Vector of return series for each asset
* Example: {goldReturns, sp500Returns, btcReturns}
* @param assetNames Names of assets for labeling
* @param numPortfolios Number of random portfolios to simulate (e.g., 10,000)
* @param riskFreeRate Annual risk-free rate (e.g., 0.03 for 3%)
* @param randomSeed Random seed for reproducibility (0 = random)
* @return EfficientFrontierResult containing optimal portfolio and all simulations
*/
static EfficientFrontierResult CalculateEfficientFrontier(
const std::vector<std::vector<double>>& assetReturns,
const std::vector<std::string>& assetNames,
int numPortfolios,
double riskFreeRate,
unsigned int randomSeed = 0
);
/**
* Calculate portfolio expected return
*
* Portfolio Return = Σ(weight_i × mean_return_i)
*
* @param weights Asset allocation weights
* @param meanReturns Mean return for each asset
* @return Expected portfolio return
*/
static double CalculatePortfolioReturn(
const std::vector<double>& weights,
const std::vector<double>& meanReturns
);
/**
* Calculate portfolio volatility (risk)
*
* Portfolio Risk = √(w^T × Σ × w)
* where w is weights vector and Σ is covariance matrix
*
* This accounts for diversification effects based on asset correlations.
*
* @param weights Asset allocation weights
* @param covMatrix Covariance matrix of asset returns
* @return Portfolio volatility (standard deviation)
*/
static double CalculatePortfolioRisk(
const std::vector<double>& weights,
const std::vector<std::vector<double>>& covMatrix
);
/**
* Calculate covariance matrix for multiple assets
*
* The covariance matrix Σ has:
* - Variances on the diagonal (σ_i²)
* - Covariances on off-diagonal (σ_i × σ_j × ρ_ij)
*
* @param assetReturns Vector of return series for each asset
* @return Covariance matrix (n×n where n = number of assets)
*/
static std::vector<std::vector<double>> CalculateCovarianceMatrix(
const std::vector<std::vector<double>>& assetReturns
);
/**
* Generate random portfolio weights that sum to 1.0
*
* Uses Dirichlet distribution for uniform sampling of simplex.
*
* @param numAssets Number of assets
* @param rng Random number generator
* @return Vector of weights summing to 1.0
*/
static std::vector<double> GenerateRandomWeights(
size_t numAssets,
std::mt19937& rng
);
private:
/**
* Validate that all asset return series have the same length
*/
static void ValidateAssetReturns(
const std::vector<std::vector<double>>& assetReturns
);
};
#endif // PORTFOLIO_OPTIMIZER_H