rikardfranksson

SF1811 HW2

Dec 24th, 2019
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 8.55 KB | None | 0 0
  1. % vvvvvvvvvvvvvvv ARBITRARY ASSETS vvvvvvvvvvvvvvv
  2. clc
  3. close all
  4. clear all
  5.  
  6. % expected return of the portfolio
  7. r = (3:0.25:9)';
  8.  
  9. mu_ = zeros(size(r, 1), 4);
  10. sigma = zeros(size(r, 1), 4);
  11.  
  12. [C, mu] = getCorrMatrixAndExpectedReturn();
  13.  
  14. % minimize      x'Cx
  15. % subject to    mu'*x = r
  16. %               sum(x) = 1
  17. %               x >= 0
  18. % A summarizes the equality constraints, s.t. Ax = b
  19. A = [
  20.     mu'
  21.     ones(size(mu, 1), 1)'
  22. ];
  23.  
  24. Z = null(A);
  25.  
  26. if rank(Z) == size(A, 2)
  27.     disp("The nullspace of A spans the input space, no solution exists")
  28.     return
  29. end
  30.  
  31. if min(eig(C) >= 0) == 0
  32.     disp("C is not positive semi-definite")
  33.     return
  34. end
  35.  
  36. % for each expected return, r, of the portfolio we wish to find its
  37. % volatility (risk)
  38. for i = 1:size(r, 1)
  39.     rr = r(i);
  40.     b = [ rr; 1 ]; % since mu'x = r and sum(x) = 1
  41.    
  42.     % solve using quadprog and equality constraints
  43.     x = quadprog(2*C, [], [], [], A, b, zeros(size(A, 2), 1), []);
  44.     sigma(i, 1) = sqrt(x' * C * x);
  45.     mu_(i, 1) = mu'*x;
  46.    
  47.     % let sum(x) <= 1
  48.     x = quadprog(2*C, [], A(2, :), b(2), A(1, :), b(1), zeros(size(A, 2), 1), []);
  49.     sigma(i, 2) = sqrt(x' * C * x);
  50.     mu_(i, 2) = mu'*x;
  51.    
  52.     % keep sum(x) = 1 but let mu'x >= r, i.e. -mu'x <= -r
  53.     x = quadprog(2*C, [], -A(1, :), -b(1), A(2, :), b(2), zeros(size(A, 2), 1), []);
  54.     sigma(i, 3) = sqrt(x' * C * x);
  55.     mu_(i, 3) = mu'*x;
  56.    
  57.     % allow short selling but keep mu'x = r
  58.     x = quadprog(2*C, [], [], [], A, b, [], []);
  59.     sigma(i, 4) = sqrt(x' * C * x);
  60.     mu_(i, 4) = mu'*x;
  61. end
  62.  
  63. xmin = min(min(sigma));
  64. xmax = max(max(sigma));
  65. ymin = min(min(mu_));
  66. ymax = max(max(mu_));
  67.  
  68. figure('color', [1 1 1])
  69. plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Portfolios [1]')
  70. title('Portfolios [1], 100% of Capital Invested')
  71. xlabel('Volatility')
  72. ylabel('Expected Return')
  73. axis([xmin xmax ymin ymax]);
  74. legend
  75. set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
  76.  
  77. figure('color', [1 1 1])
  78. plot(sigma(:, 2), mu_(:, 2), '-o', 'Color', [.85 .325 .098], 'DisplayName', 'Portfolios [2]')
  79. title('Portfolios [2], No Constraint on Portion of Capital Invested')
  80. xlabel('Volatility')
  81. ylabel('Expected Return')
  82. axis([xmin xmax ymin ymax]);
  83. legend
  84. set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
  85.  
  86. figure('color', [1 1 1])
  87. plot(sigma(:, 3), mu_(:, 3), '-o', 'Color', [.929 .694 .125], 'DisplayName', 'Portfolios [3]')
  88. title('Portfolios [3], 100% of Capital Invested and ER >= r')
  89. xlabel('Volatility')
  90. ylabel('Expected Return')
  91. axis([xmin xmax ymin ymax]);
  92. set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
  93.  
  94. figure('color', [1 1 1])
  95. plot(sigma(:, 4), mu_(:, 4), '-o', 'Color', [.494 .184 .556], 'DisplayName', 'Portfolios [4]')
  96. title('Portfolios [4], 100% of Capital Invested and Short Selling Allowed')
  97. xlabel('Volatility')
  98. ylabel('Expected Return')
  99. axis([xmin xmax ymin ymax]);
  100. legend
  101. set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
  102.  
  103. figure('color', [1 1 1])
  104. plot(sigma(:, 4), mu_(:, 4), '-o', 'Color', [.494 .184 .556], 'DisplayName', 'Portfolios [4]')
  105. hold on
  106. plot(sigma(:, 2), mu_(:, 2), '-o', 'Color', [.85 .325 .098], 'DisplayName', 'Portfolios [2]')
  107. plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Portfolios [1]')
  108. plot(sigma(:, 3), mu_(:, 3), '-o', 'Color', [.929 .694 .125], 'DisplayName', 'Portfolios [3]')
  109. hold off
  110. title('Portfolios [1], [2], [3] and [4], Comparison')
  111. xlabel('Volatility')
  112. ylabel('Expected Return')
  113. axis([xmin xmax ymin ymax]);
  114. legend
  115. set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
  116.  
  117. figure('color', [1 1 1])
  118. plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Portfolios [1]')
  119. hold on
  120. plot(sigma(:, 2), mu_(:, 2), '-o', 'Color', [.85 .325 .098], 'DisplayName', 'Portfolios [2]')
  121. hold off
  122. title('Portfolios [1] and [2], Comparison')
  123. xlabel('Volatility')
  124. ylabel('Expected Return')
  125. axis([xmin xmax ymin ymax]);
  126. legend
  127. set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
  128.  
  129. figure('color', [1 1 1])
  130. plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Portfolios [1]')
  131. hold on
  132. plot(sigma(:, 3), mu_(:, 3), '-o', 'Color', [.85 .325 .098], 'DisplayName', 'Portfolios [3]')
  133. hold off
  134. title('Portfolios [1] and [3], Comparison')
  135. xlabel('Volatility')
  136. ylabel('Expected Return')
  137. axis([xmin xmax ymin ymax]);
  138. legend
  139. set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
  140.  
  141. figure('color', [1 1 1])
  142. plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Portfolios [1]')
  143. hold on
  144. plot(sigma(:, 4), mu_(:, 4), '-o', 'Color', [.85 .325 .098], 'DisplayName', 'Portfolios [4]')
  145. hold off
  146. title('Portfolios [1] and [4], Comparison')
  147. xlabel('Volatility')
  148. ylabel('Expected Return')
  149. axis([xmin xmax ymin ymax]);
  150. legend
  151. set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
  152.  
  153. function [C, mu] = getCorrMatrixAndExpectedReturn()
  154.     % generate the correlation matrix C and expected return of assets mu
  155.     n = 8;
  156.     Corr = zeros(n, n);
  157.  
  158.     for i = 1:n
  159.         for j = 1:n
  160.             Corr(i, j) = (-1)^abs(i-j)/(abs(i-j)+1);
  161.         end
  162.     end
  163.  
  164.     sigma = zeros(n, 1);
  165.     mu = zeros(n, 1);
  166.     sigma(1) = 2;
  167.     mu(1) = 3;
  168.  
  169.     for i = 1:n-1
  170.         sigma(i+1) = sigma(i) + 2*rand;
  171.         mu(i+1) = mu(i) + 1;
  172.     end
  173.  
  174.     D = diag(sigma);
  175.     C2 = D * Corr * D;
  176.     C = 0.5 * (C2 + C2');
  177. end
  178.  
  179.  
  180. % vvvvvvvvvvvvvvv REAL-WORLD ASSETS vvvvvvvvvvvvvvv
  181. close all
  182. clear all
  183. clc
  184.  
  185. % AAPL TSLA JPM AMZN WMT T(AT&T) CVX(CHEVRON) BRK.A(Berkshire Hathaway A) UNH(UnitedHealth Group)
  186. CC = [
  187.  1.00 0.10 0.26 0.34 0.18 0.09 0.17 0.20 0.20
  188.  0.10 1.00 0.11 0.05 -.06 -.18 0.00 0.16 0.18
  189.  0.26 0.11 1.00 0.28 0.09 0.15 0.51 0.57 0.32
  190.  0.34 0.05 0.28 1.00 0.08 0.12 0.19 0.29 0.22
  191.  0.18 -.06 0.09 0.08 1.00 0.21 0.09 0.30 0.16
  192.  0.09 -.18 0.15 0.12 0.21 1.00 0.43 0.25 0.00
  193.  0.17 0.00 0.51 0.19 0.09 0.43 1.00 0.42 0.28
  194.  0.20 0.16 0.57 0.29 0.30 0.25 0.42 1.00 0.30
  195.  0.20 0.18 0.32 0.22 0.16 0.00 0.28 0.30 1.00
  196. ];
  197.  
  198. ST_DEV = [
  199.  .0743
  200.  .1591
  201.  .0696
  202.  .0803
  203.  .0481
  204.  .0452
  205.  .0541
  206.  .0386
  207.  .0533
  208. ];
  209.  
  210. mu = ([
  211.  .256
  212.  .322
  213.  .1751
  214.  .3467
  215.  .1287
  216.  .1067
  217.  .1003
  218.  .1136
  219.  .2943
  220. ] + 1).^(1/12) - 1;
  221.  
  222. C = zeros(size(CC, 1));
  223.  
  224. rr = min(mu):0.0005:max(mu);
  225.  
  226. A = [
  227.  mu'
  228.  ones(size(mu, 1), 1)'
  229. ];
  230.  
  231. sigma = zeros(size(C, 2), 3);
  232. mu_ = zeros(size(C, 2), 3);
  233.  
  234. for i=1:size(CC, 1)
  235.     for j=1:size(CC, 2)
  236.         if i == j
  237.             C(i, i) = round(ST_DEV(i)^2, 5);
  238.         else
  239.             C(i, j) = round(CC(i, j) * ST_DEV(i) * ST_DEV(j), 5);
  240.         end
  241.     end
  242. end
  243.  
  244. for i=1:size(rr, 2)
  245.     r = rr(i);
  246.     b = [ r; 1 ];
  247.    
  248.     x = quadprog(2*C, [], [], [], A, b, zeros(size(A, 2), 1), []);
  249.     sigma(i, 1) = sqrt(x' * C * x);
  250.     mu_(i, 1) = mu' * x;
  251.    
  252.     x = quadprog(2*C, [], [ -A(1, :); A(2, :) ], [ -b(1); b(2) ], [], [], zeros(size(A, 2), 1), []);
  253.     sigma(i, 2) = sqrt(x' * C * x);
  254.     mu_(i, 2) = mu' * x;
  255.    
  256.     x = quadprog(2*C, [], [ -A(1, :); A(2, :) ], [ -b(1); b(2) ], [], [], [], []);
  257.     sigma(i, 3) = sqrt(x' * C * x);
  258.     mu_(i, 3) = mu' * x;
  259. end
  260.  
  261. mu_ = ((mu_ + 1).^12 - 1) * 100;
  262.  
  263. figure('color', [1 1 1])
  264. plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Standard Problem')
  265. hold on
  266. plot(sigma(:, 2), mu_(:, 2), '-o', 'Color', [.85 .325 .098], 'DisplayName', "c'x ≥ r and any portion of capital invested")
  267. hold off
  268. title('Optimal Portfolios')
  269. xlabel('Volatility')
  270. ylabel('Expected Return (CAGR)')
  271. axis([ 0 max(max(sigma)) min(min(mu_))*.9 max(max(mu_))*1.2 ])
  272. legend
  273. set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
  274.  
  275. figure('color', [1 1 1])
  276. plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Standard Problem')
  277. hold on
  278. plot(sigma(:, 3), mu_(:, 3), '-o', 'Color', [.929 .694 .125], 'DisplayName', 'Short Selling Allowed and any portion of capital invested')
  279. hold off
  280. title('Optimal Portfolios')
  281. xlabel('Volatility')
  282. ylabel('Expected Return (CAGR)')
  283. axis([ 0 max(max(sigma)) min(min(mu_))*.9 max(max(mu_))*1.2 ])
  284. legend
  285. set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
  286.  
  287. figure('color', [1 1 1])
  288. plot(sigma(:, 2), mu_(:, 2), '-o', 'Color', [.85 .325 .098], 'DisplayName', "c'x ≥ r and any portion of capital invested")
  289. hold on
  290. plot(sigma(:, 3), mu_(:, 3), '-o', 'Color', [.929 .694 .125], 'DisplayName', 'Short Selling Allowed and any portion of capital invested')
  291. hold off
  292. title('Optimal Portfolios')
  293. xlabel('Volatility')
  294. ylabel('Expected Return (CAGR)')
  295. axis([ 0 max(max(sigma)) min(min(mu_))*.9 max(max(mu_))*1.2 ])
  296. legend
  297. set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
Advertisement
Add Comment
Please, Sign In to add comment