fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <iomanip>
  6. #include <tuple>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. using Portfolio = vector<int>; // weights in percent, size = 6
  12.  
  13. double percentile15(vector<double>& data) {
  14. sort(data.begin(), data.end());
  15. size_t index = static_cast<size_t>(floor(0.15 * data.size()));
  16. if (index >= data.size()) index = data.size() - 1;
  17. return data[index];
  18. }
  19.  
  20. double median(vector<double>& data) {
  21. sort(data.begin(), data.end());
  22. size_t n = data.size();
  23. if (n % 2 == 0)
  24. return (data[n / 2 - 1] + data[n / 2]) / 2.0;
  25. else
  26. return data[n / 2];
  27. }
  28.  
  29. double lumpSumCAGR(const vector<double>& returns) {
  30. double compound = 1.0;
  31. for (double r : returns) {
  32. compound *= (1.0 + r);
  33. }
  34. return pow(compound, 1.0 / returns.size()) - 1.0;
  35. }
  36.  
  37. double portfolioReturn(const vector<vector<double>>& assets, const Portfolio& weights, int year) {
  38. double result = 0.0;
  39. for (int i = 0; i < weights.size(); ++i) {
  40. result += (weights[i] / 100.0) * assets[i][year];
  41. }
  42. return result;
  43. }
  44.  
  45. void generatePortfolios(vector<Portfolio>& portfolios, Portfolio current, int pos, int totalWeight, int maxAssets) {
  46. if (pos == current.size()) {
  47. if (totalWeight == 100) {
  48. int nonZero = count_if(current.begin(), current.end(), [](int w) { return w > 0; });
  49. if (nonZero <= maxAssets) {
  50. portfolios.push_back(current);
  51. }
  52. }
  53. return;
  54. }
  55. for (int w = 0; w <= 100 - totalWeight; w += 1) {//const int step = 10;
  56. current[pos] = w;
  57. if(w > 68) return;
  58. // if(pos==2 && w > 40) return;
  59. if(pos == 3 && w> 6) return;
  60. // if(pos ==5 && w>40) return;
  61. if((pos != 0)&& w> 22 )return;
  62. generatePortfolios(portfolios, current, pos + 1, totalWeight + w, maxAssets);
  63. }
  64. }
  65.  
  66. int main() {
  67. const int numAssets = 6;
  68. const int maxAssets = 4;
  69.  
  70.  
  71. // Asset names
  72. vector<string> assetNames = {
  73. "scv", "lcg", "em", "reit", "gld" , "20bal"
  74. //"scv60Lcg15", "scv95", "vti", "20bal", "33gld", "lcbtiltled"
  75. };
  76.  
  77. // Return data
  78. vector<vector<double>> assets = {
  79.  
  80. {-0.03,0.13,0.06,-0.29,-0.31,0.43,0.46,0.08,0.07,0.21,0.12,0.06,0.31,0.38,0.02,0.31,0.12,-0.08,0.23,0.14,-0.22,0.37,0.24,0.19,-0.02,0.24,0.17,0.31,-0.06,0.02,0.04,0.11,-0.13,0.41,0.17,0.04,0.15,-0.09,-0.32,0.33,0.23,-0.06,0.18,0.32,0.09,-0.06,0.23,0.09,-0.15,0.20,0.03,0.19,-0.16,0.11,0.08},
  81.  
  82.  
  83. {
  84. -0.09, 0.19, 0.18, -0.28, -0.40, 0.24, 0.08, -0.16, -0.02, 0.01,
  85. 0.16, -0.16, 0.20, 0.14, -0.01, 0.30, 0.14, 0.01, 0.07, 0.32,
  86. -0.05, 0.37, 0.03, 0.00, 0.01, 0.34, 0.22, 0.32, 0.43, 0.29,
  87. -0.28, -0.13, -0.24, 0.23, 0.04, 0.02, 0.07, 0.06, -0.39, 0.33,
  88. 0.15, -0.01, 0.15, 0.31, 0.13, 0.03, 0.05, 0.25, -0.04, 0.34,
  89. 0.39, 0.18, -0.35, 0.40, 0.31
  90. },
  91.  
  92. {
  93. -0.17, 0.28, 0.34, -0.18, -0.28, 0.25, 0.04, 0.01, 0.05, 0.06,
  94. -0.04, -0.25, -0.31, 0.12, 0.12, 0.23, 0.11, 0.09, 0.34, 0.55,
  95. -0.16, 0.54, 0.09, 0.68, -0.09, -0.06, 0.03, -0.11, -0.25, 0.59,
  96. -0.31, -0.03, -0.08, 0.53, 0.22, 0.29, 0.29, 0.32, -0.52, 0.72,
  97. 0.18, -0.20, 0.17, -0.02, -0.02, -0.15, 0.10, 0.34, -0.16, 0.16,
  98. 0.16, -0.07, -0.24, 0.09, 0.05
  99. },
  100.  
  101. {
  102. -0.01, 0.10, 0.05, -0.23, -0.30, 0.13, 0.41, 0.14, 0.01, 0.20,
  103. 0.11, -0.02, 0.18, 0.26, 0.16, 0.16, 0.18, -0.08, 0.09, 0.04,
  104. -0.20, 0.32, 0.12, 0.17, 0.00, 0.13, 0.30, 0.19, -0.18, -0.07,
  105. 0.21, 0.12, 0.01, 0.35, 0.27, 0.08, 0.31, -0.19, -0.37, 0.25,
  106. 0.26, 0.05, 0.16, 0.03, 0.28, 0.02, 0.07, 0.03, -0.07, 0.23,
  107. -0.09, 0.33, -0.28, 0.10, 0.06
  108. },
  109.  
  110. {
  111. 0.00, 0.13, 0.42, 0.55, 0.44, -0.26, -0.06, 0.15, 0.25, 1.00,
  112. 0.01, -0.36, 0.13, -0.17, -0.21, 0.04, 0.17, 0.18, -0.17, -0.06,
  113. -0.09, -0.09, -0.07, 0.15, -0.05, 0.00, -0.06, -0.20, -0.03, -0.02,
  114. -0.08, 0.00, 0.21, 0.19, 0.02, 0.13, 0.20, 0.25, 0.02, 0.22,
  115. 0.27, 0.05, 0.07, -0.25, 0.00, -0.12, 0.07, 0.10, -0.03, 0.16,
  116. 0.22, -0.09, -0.06, 0.11, 0.21
  117. },
  118.  
  119. {
  120. -0.02, 0.16, 0.04, -0.04, -0.10, 0.08, -0.05, 0.16, 0.15, -0.10,
  121. -0.16, -0.16, -0.23, 0.09, -0.02, 0.04, 0.43, 0.48, 0.17, -0.02,
  122. -0.02, 0.12, 0.14, -0.02, 0.24, -0.03, 0.24, 0.18, 0.02, 0.22,
  123. -0.24, 0.01, 0.00, 0.28, 0.24, 0.19, -0.04, 0.06, 0.03, 0.04,
  124. 0.05, -0.03, -0.01, 0.17, 0.05, 0.14, -0.08, 0.02, 0.10, -0.04,
  125. 0.11, 0.20, -0.18, -0.41, 0.11, -0.08
  126. }
  127.  
  128.  
  129. // {-0.04,0.14,0.15,-0.14,-0.19,0.26,0.28,0.04,0.08,0.28,0.08,-0.06,0.24,0.22,-0.01,0.26,0.14,-0.02,0.14,0.16,-0.16,0.29,0.15,0.18,-0.03,0.20,0.13,0.20,0.02,0.08,-0.04,0.04,-0.07,0.33,0.12,0.06,0.15,0.01,-0.26,0.31,0.22,-0.03,0.15,0.19,0.08,-0.06,0.16,0.13,-0.11,0.21,0.13,0.12,-0.18,0.15,0.12},
  130. // {-0.03,0.13,0.06,-0.29,-0.31,0.43,0.46,0.08,0.07,0.21,0.12,0.06,0.31,0.38,0.02,0.31,0.12,-0.08,0.23,0.14,-0.22,0.37,0.24,0.19,-0.02,0.24,0.17,0.31,-0.06,0.02,0.04,0.11,-0.13,0.41,0.17,0.04,0.15,-0.09,-0.32,0.33,0.23,-0.06,0.18,0.32,0.09,-0.06,0.23,0.09,-0.15,0.20,0.03,0.19,-0.16,0.11,0.08},
  131. // {-0.02,0.11,0.12,-0.25,-0.36,0.30,0.21,-0.11,-0.01,0.05,0.16,-0.11,0.19,0.19,0.01,0.28,0.15,-0.02,0.12,0.24,-0.10,0.29,0.06,0.08,-0.01,0.34,0.19,0.30,0.24,0.19,-0.13,-0.09,-0.25,0.30,0.08,0.02,0.12,0.01,-0.37,0.26,0.16,-0.02,0.14,0.31,0.12,0.00,0.11,0.19,-0.07,0.28,0.19,0.18,-0.24,0.22,0.20},
  132. // {-0.03,0.15,0.18,-0.07,-0.14,0.12,0.18,0.04,0.07,0.28,0.06,-0.13,0.17,0.13,0.01,0.22,0.18,0.02,0.07,0.14,-0.13,0.23,0.06,0.17,-0.02,0.16,0.12,0.12,0.04,0.07,-0.03,0.01,-0.02,0.29,0.13,0.08,0.17,0.03,-0.23,0.27,0.21,0.02,0.13,0.07,0.12,-0.03,0.09,0.13,-0.08,0.22,0.14,0.11,-0.23,0.16,0.13}, {-0.01,0.11,0.19,0.03,-0.05,0.13,0.23,0.07,0.10,0.44,0.08,-0.13,0.20,0.14,-0.04,0.19,0.16,0.02,0.06,0.08,-0.15,0.17,0.08,0.16,-0.03,0.17,0.11,0.12,0.00,0.02,-0.01,0.02,-0.02,0.30,0.11,0.07,0.19,0.03,-0.21,0.25,0.23,0.00,0.13,0.08,0.09,-0.06,0.13,0.10,-0.08,0.20,0.10,0.11,-0.14,0.11,0.14},
  133. // {-0.03,0.13,0.12,-0.22,-0.30,0.25,0.24,-0.02,0.00,0.07,0.08,-0.09,0.14,0.20,0.07,0.26,0.15,-0.02,0.16,0.24,-0.13,0.33,0.10,0.21,-0.03,0.22,0.16,0.21,0.05,0.15,-0.05,-0.02,-0.12,0.32,0.14,0.08,0.17,0.02,-0.32,0.29,0.17,-0.01,0.14,0.17,0.13,-0.03,0.11,0.16,-0.09,0.23,0.11,0.14,-0.24,0.14,0.11}
  134. //
  135.  
  136.  
  137. };
  138.  
  139. int numYears = assets[0].size();
  140.  
  141. for (int duration = 15; duration <= 31; duration += 111) {
  142. cout << "\n========== Duration: " << duration << " years ==========" << endl;
  143.  
  144. vector<Portfolio> portfolios;
  145. generatePortfolios(portfolios, Portfolio(numAssets, 0), 0, 0, maxAssets);
  146.  
  147. vector<tuple<Portfolio, double, double, double, double>> results;
  148.  
  149. for (const Portfolio& p : portfolios) {
  150. vector<double> cagrList;
  151. for (int start = 0; start <= numYears - duration; ++start) {
  152. vector<double> subReturns;
  153. for (int i = 0; i < duration; ++i) {
  154. subReturns.push_back(portfolioReturn(assets, p, start + i));
  155. }
  156. double cagr = lumpSumCAGR(subReturns);
  157. cagrList.push_back(cagr);
  158. }
  159.  
  160. double p15 = percentile15(cagrList);
  161. double pmin = *min_element(cagrList.begin(), cagrList.end());
  162. double pmax = *max_element(cagrList.begin(), cagrList.end());
  163. double pmed = median(cagrList);
  164.  
  165. results.emplace_back(p, p15, pmin, pmax, pmed);
  166. }
  167.  
  168. sort(results.begin(), results.end(), [](const auto& a, const auto& b) {
  169. return get<1>(a) > get<1>(b);
  170. });
  171.  
  172. cout << fixed << setprecision(2);
  173.  
  174. // Summary table
  175. cout << "\nTop 2 Portfolios Summary (Bottom 15th Percentile CAGR):" << endl;
  176. cout << left << setw(10) << "#" << setw(40) << "Portfolio" << setw(10) << "P15%" << setw(10) << "Min" << setw(10) << "Max" << setw(10) << "Median" << endl;
  177. cout << string(80, '-') << endl;
  178.  
  179. for (int i = 0; i < 2 && i < results.size(); ++i) {
  180. auto [weights, p15, pmin, pmax, pmed] = results[i];
  181. string portStr;
  182. for (int j = 0; j < numAssets; ++j) {
  183. if (weights[j] > 0) {
  184. portStr += assetNames[j] + ":" + to_string(weights[j]) + "% ";
  185. }
  186. }
  187. cout << left << setw(10) << (i + 1) << setw(40) << portStr << setw(10) << p15 * 100 << setw(10) << pmin * 100 << setw(10) << pmax * 100 << setw(10) << pmed * 100 << endl;
  188. }
  189.  
  190. // // Detailed breakdown
  191. // for (int i = 0; i < 2 && i < results.size(); ++i) {
  192. // auto [weights, p15, pmin, pmax, pmed] = results[i];
  193. // cout << "\nPortfolio " << (i + 1) << ":\n";
  194. // cout << left << setw(15) << "Asset" << setw(10) << "Weight\n";
  195. // cout << "-------------------------\n";
  196. // for (int j = 0; j < numAssets; ++j) {
  197. // if (weights[j] > 0) {
  198. // cout << left << setw(15) << assetNames[j] << setw(10) << weights[j] << "\n";
  199. // }
  200. // }
  201. // cout << "-------------------------\n";
  202. // cout << "Bottom 15th % CAGR : " << p15 * 100 << "%\n";
  203. // cout << "Min CAGR : " << pmin * 100 << "%\n";
  204. // cout << "Max CAGR : " << pmax * 100 << "%\n";
  205. // cout << "Median CAGR : " << pmed * 100 << "%\n";
  206. // }
  207. }
  208.  
  209. return 0;
  210. }
  211.  
Success #stdin #stdout 4.28s 7676KB
stdin
Standard input is empty
stdout
========== Duration: 15 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv:64% lcg:14% gld:20% 20bal:2%        7.29      5.43      13.79     8.54      
2         scv:60% lcg:15% reit:6% gld:19%         7.29      5.40      13.75     8.67