fork(1) 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 += 5) {
  56. current[pos] = w;
  57. generatePortfolios(portfolios, current, pos + 1, totalWeight + w, maxAssets);
  58. }
  59. }
  60.  
  61. int main() {
  62. const int numAssets = 6;
  63. const int maxAssets = 3;
  64. const int step = 5;
  65.  
  66. // Asset names
  67. vector<string> assetNames = {
  68. "scv60Lcg15", "scv95", "vti", "11all", "33gld", "lcbtiltled"
  69. };
  70.  
  71. // Return data
  72. vector<vector<double>> assets = {
  73. {-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},
  74. {-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},
  75. {-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},
  76. {-0.02,0.12,0.14,-0.13,-0.19,0.22,0.26,0.04,0.06,0.19,0.07,-0.07,0.20,0.20,0.03,0.26,0.19,0.00,0.14,0.15,-0.16,0.24,0.10,0.20,-0.02,0.19,0.12,0.17,0.01,0.05,0.01,0.02,-0.06,0.32,0.14,0.07,0.17,-0.01,-0.25,0.25,0.20,0.00,0.14,0.15,0.11,-0.04,0.13,0.12,-0.10,0.20,0.07,0.12,-0.19,0.10,0.08},
  77. {-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},
  78. {-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}
  79. };
  80.  
  81. int numYears = assets[0].size();
  82.  
  83. for (int duration = 1; duration <= 31; duration += 2) {
  84. cout << "\n========== Duration: " << duration << " years ==========" << endl;
  85.  
  86. vector<Portfolio> portfolios;
  87. generatePortfolios(portfolios, Portfolio(numAssets, 0), 0, 0, maxAssets);
  88.  
  89. vector<tuple<Portfolio, double, double, double, double>> results;
  90.  
  91. for (const Portfolio& p : portfolios) {
  92. vector<double> cagrList;
  93. for (int start = 0; start <= numYears - duration; ++start) {
  94. vector<double> subReturns;
  95. for (int i = 0; i < duration; ++i) {
  96. subReturns.push_back(portfolioReturn(assets, p, start + i));
  97. }
  98. double cagr = lumpSumCAGR(subReturns);
  99. cagrList.push_back(cagr);
  100. }
  101.  
  102. double p15 = percentile15(cagrList);
  103. double pmin = *min_element(cagrList.begin(), cagrList.end());
  104. double pmax = *max_element(cagrList.begin(), cagrList.end());
  105. double pmed = median(cagrList);
  106.  
  107. results.emplace_back(p, p15, pmin, pmax, pmed);
  108. }
  109.  
  110. sort(results.begin(), results.end(), [](const auto& a, const auto& b) {
  111. return get<1>(a) > get<1>(b);
  112. });
  113.  
  114. cout << fixed << setprecision(2);
  115.  
  116. // Summary table
  117. cout << "\nTop 2 Portfolios Summary (Bottom 15th Percentile CAGR):" << endl;
  118. cout << left << setw(10) << "#" << setw(40) << "Portfolio" << setw(10) << "P15%" << setw(10) << "Min" << setw(10) << "Max" << setw(10) << "Median" << endl;
  119. cout << string(80, '-') << endl;
  120.  
  121. for (int i = 0; i < 2 && i < results.size(); ++i) {
  122. auto [weights, p15, pmin, pmax, pmed] = results[i];
  123. string portStr;
  124. for (int j = 0; j < numAssets; ++j) {
  125. if (weights[j] > 0) {
  126. portStr += assetNames[j] + ":" + to_string(weights[j]) + "% ";
  127. }
  128. }
  129. 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;
  130. }
  131.  
  132. // // Detailed breakdown
  133. // for (int i = 0; i < 2 && i < results.size(); ++i) {
  134. // auto [weights, p15, pmin, pmax, pmed] = results[i];
  135. // cout << "\nPortfolio " << (i + 1) << ":\n";
  136. // cout << left << setw(15) << "Asset" << setw(10) << "Weight\n";
  137. // cout << "-------------------------\n";
  138. // for (int j = 0; j < numAssets; ++j) {
  139. // if (weights[j] > 0) {
  140. // cout << left << setw(15) << assetNames[j] << setw(10) << weights[j] << "\n";
  141. // }
  142. // }
  143. // cout << "-------------------------\n";
  144. // cout << "Bottom 15th % CAGR : " << p15 * 100 << "%\n";
  145. // cout << "Min CAGR : " << pmin * 100 << "%\n";
  146. // cout << "Max CAGR : " << pmax * 100 << "%\n";
  147. // cout << "Median CAGR : " << pmed * 100 << "%\n";
  148. // }
  149. }
  150.  
  151. return 0;
  152. }
  153.  
Success #stdin #stdout 1.66s 5292KB
stdin
Standard input is empty
stdout
========== Duration: 1 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         11all:35% 33gld:65%                     -2.65     -22.40    35.25     10.45     
2         vti:5% 11all:25% 33gld:70%              -2.65     -22.80    35.80     10.55     

========== Duration: 3 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv95:20% vti:35% 33gld:45%             2.71      -7.91     20.42     8.07      
2         scv95:15% vti:35% 33gld:50%             2.67      -6.67     19.89     8.05      

========== Duration: 5 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv60Lcg15:55% 33gld:45%                4.39      0.35      18.57     8.28      
2         scv60Lcg15:40% 33gld:50% lcbtiltled:10% 4.37      0.29      17.82     8.06      

========== Duration: 7 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv60Lcg15:15% scv95:35% 33gld:50%      6.15      3.69      15.15     8.05      
2         scv60Lcg15:25% scv95:30% 33gld:45%      6.15      3.70      15.12     8.19      

========== Duration: 9 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv60Lcg15:10% scv95:30% 33gld:60%      7.01      3.70      16.35     8.21      
2         scv95:35% 33gld:65%                     7.00      3.78      16.64     8.15      

========== Duration: 11 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv60Lcg15:20% scv95:65% 33gld:15%      6.75      2.20      18.65     9.56      
2         scv60Lcg15:25% scv95:60% 33gld:15%      6.74      2.28      18.31     9.50      

========== Duration: 13 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv60Lcg15:80% vti:15% 33gld:5%         6.59      4.70      12.48     8.64      
2         scv60Lcg15:15% scv95:70% 33gld:15%      6.58      4.81      16.42     9.69      

========== Duration: 15 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv60Lcg15:90% scv95:10%                7.21      5.39      13.86     9.00      
2         scv60Lcg15:85% scv95:5% 33gld:10%       7.20      5.36      13.38     8.75      

========== Duration: 17 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv60Lcg15:85% scv95:5% 33gld:10%       7.03      5.97      12.19     8.88      
2         scv60Lcg15:80% scv95:5% 33gld:15%       7.02      5.95      12.06     8.76      

========== Duration: 19 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv60Lcg15:15% scv95:85%                6.90      5.79      16.20     9.59      
2         scv60Lcg15:10% scv95:90%                6.90      5.74      16.39     9.62      

========== Duration: 21 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv60Lcg15:20% scv95:75% lcbtiltled:5%  7.70      5.93      15.14     9.26      
2         scv60Lcg15:20% scv95:70% lcbtiltled:10% 7.70      5.94      14.89     9.29      

========== Duration: 23 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv95:90% lcbtiltled:10%                7.28      6.36      16.29     9.75      
2         scv95:95% lcbtiltled:5%                 7.27      6.33      16.54     9.92      

========== Duration: 25 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv95:45% vti:20% lcbtiltled:35%        7.74      5.74      13.37     8.87      
2         scv95:50% vti:25% lcbtiltled:25%        7.74      5.74      13.60     8.81      

========== Duration: 27 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv95:100%                              8.36      6.04      14.57     9.72      
2         scv95:95% lcbtiltled:5%                 8.36      6.06      14.37     9.72      

========== Duration: 29 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv95:80% vti:5% lcbtiltled:15%         8.56      7.20      13.49     9.62      
2         scv95:75% vti:5% lcbtiltled:20%         8.56      7.18      13.29     9.59      

========== Duration: 31 years ==========

Top 2 Portfolios Summary (Bottom 15th Percentile CAGR):
#         Portfolio                               P15%      Min       Max       Median    
--------------------------------------------------------------------------------
1         scv95:70% vti:20% lcbtiltled:10%        8.37      7.56      12.78     9.46      
2         scv95:70% vti:15% lcbtiltled:15%        8.37      7.51      12.82     9.51