Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % vvvvvvvvvvvvvvv ARBITRARY ASSETS vvvvvvvvvvvvvvv
- clc
- close all
- clear all
- % expected return of the portfolio
- r = (3:0.25:9)';
- mu_ = zeros(size(r, 1), 4);
- sigma = zeros(size(r, 1), 4);
- [C, mu] = getCorrMatrixAndExpectedReturn();
- % minimize x'Cx
- % subject to mu'*x = r
- % sum(x) = 1
- % x >= 0
- % A summarizes the equality constraints, s.t. Ax = b
- A = [
- mu'
- ones(size(mu, 1), 1)'
- ];
- Z = null(A);
- if rank(Z) == size(A, 2)
- disp("The nullspace of A spans the input space, no solution exists")
- return
- end
- if min(eig(C) >= 0) == 0
- disp("C is not positive semi-definite")
- return
- end
- % for each expected return, r, of the portfolio we wish to find its
- % volatility (risk)
- for i = 1:size(r, 1)
- rr = r(i);
- b = [ rr; 1 ]; % since mu'x = r and sum(x) = 1
- % solve using quadprog and equality constraints
- x = quadprog(2*C, [], [], [], A, b, zeros(size(A, 2), 1), []);
- sigma(i, 1) = sqrt(x' * C * x);
- mu_(i, 1) = mu'*x;
- % let sum(x) <= 1
- x = quadprog(2*C, [], A(2, :), b(2), A(1, :), b(1), zeros(size(A, 2), 1), []);
- sigma(i, 2) = sqrt(x' * C * x);
- mu_(i, 2) = mu'*x;
- % keep sum(x) = 1 but let mu'x >= r, i.e. -mu'x <= -r
- x = quadprog(2*C, [], -A(1, :), -b(1), A(2, :), b(2), zeros(size(A, 2), 1), []);
- sigma(i, 3) = sqrt(x' * C * x);
- mu_(i, 3) = mu'*x;
- % allow short selling but keep mu'x = r
- x = quadprog(2*C, [], [], [], A, b, [], []);
- sigma(i, 4) = sqrt(x' * C * x);
- mu_(i, 4) = mu'*x;
- end
- xmin = min(min(sigma));
- xmax = max(max(sigma));
- ymin = min(min(mu_));
- ymax = max(max(mu_));
- figure('color', [1 1 1])
- plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Portfolios [1]')
- title('Portfolios [1], 100% of Capital Invested')
- xlabel('Volatility')
- ylabel('Expected Return')
- axis([xmin xmax ymin ymax]);
- legend
- set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
- figure('color', [1 1 1])
- plot(sigma(:, 2), mu_(:, 2), '-o', 'Color', [.85 .325 .098], 'DisplayName', 'Portfolios [2]')
- title('Portfolios [2], No Constraint on Portion of Capital Invested')
- xlabel('Volatility')
- ylabel('Expected Return')
- axis([xmin xmax ymin ymax]);
- legend
- set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
- figure('color', [1 1 1])
- plot(sigma(:, 3), mu_(:, 3), '-o', 'Color', [.929 .694 .125], 'DisplayName', 'Portfolios [3]')
- title('Portfolios [3], 100% of Capital Invested and ER >= r')
- xlabel('Volatility')
- ylabel('Expected Return')
- axis([xmin xmax ymin ymax]);
- set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
- figure('color', [1 1 1])
- plot(sigma(:, 4), mu_(:, 4), '-o', 'Color', [.494 .184 .556], 'DisplayName', 'Portfolios [4]')
- title('Portfolios [4], 100% of Capital Invested and Short Selling Allowed')
- xlabel('Volatility')
- ylabel('Expected Return')
- axis([xmin xmax ymin ymax]);
- legend
- set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
- figure('color', [1 1 1])
- plot(sigma(:, 4), mu_(:, 4), '-o', 'Color', [.494 .184 .556], 'DisplayName', 'Portfolios [4]')
- hold on
- plot(sigma(:, 2), mu_(:, 2), '-o', 'Color', [.85 .325 .098], 'DisplayName', 'Portfolios [2]')
- plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Portfolios [1]')
- plot(sigma(:, 3), mu_(:, 3), '-o', 'Color', [.929 .694 .125], 'DisplayName', 'Portfolios [3]')
- hold off
- title('Portfolios [1], [2], [3] and [4], Comparison')
- xlabel('Volatility')
- ylabel('Expected Return')
- axis([xmin xmax ymin ymax]);
- legend
- set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
- figure('color', [1 1 1])
- plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Portfolios [1]')
- hold on
- plot(sigma(:, 2), mu_(:, 2), '-o', 'Color', [.85 .325 .098], 'DisplayName', 'Portfolios [2]')
- hold off
- title('Portfolios [1] and [2], Comparison')
- xlabel('Volatility')
- ylabel('Expected Return')
- axis([xmin xmax ymin ymax]);
- legend
- set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
- figure('color', [1 1 1])
- plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Portfolios [1]')
- hold on
- plot(sigma(:, 3), mu_(:, 3), '-o', 'Color', [.85 .325 .098], 'DisplayName', 'Portfolios [3]')
- hold off
- title('Portfolios [1] and [3], Comparison')
- xlabel('Volatility')
- ylabel('Expected Return')
- axis([xmin xmax ymin ymax]);
- legend
- set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
- figure('color', [1 1 1])
- plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Portfolios [1]')
- hold on
- plot(sigma(:, 4), mu_(:, 4), '-o', 'Color', [.85 .325 .098], 'DisplayName', 'Portfolios [4]')
- hold off
- title('Portfolios [1] and [4], Comparison')
- xlabel('Volatility')
- ylabel('Expected Return')
- axis([xmin xmax ymin ymax]);
- legend
- set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
- function [C, mu] = getCorrMatrixAndExpectedReturn()
- % generate the correlation matrix C and expected return of assets mu
- n = 8;
- Corr = zeros(n, n);
- for i = 1:n
- for j = 1:n
- Corr(i, j) = (-1)^abs(i-j)/(abs(i-j)+1);
- end
- end
- sigma = zeros(n, 1);
- mu = zeros(n, 1);
- sigma(1) = 2;
- mu(1) = 3;
- for i = 1:n-1
- sigma(i+1) = sigma(i) + 2*rand;
- mu(i+1) = mu(i) + 1;
- end
- D = diag(sigma);
- C2 = D * Corr * D;
- C = 0.5 * (C2 + C2');
- end
- % vvvvvvvvvvvvvvv REAL-WORLD ASSETS vvvvvvvvvvvvvvv
- close all
- clear all
- clc
- % AAPL TSLA JPM AMZN WMT T(AT&T) CVX(CHEVRON) BRK.A(Berkshire Hathaway A) UNH(UnitedHealth Group)
- CC = [
- 1.00 0.10 0.26 0.34 0.18 0.09 0.17 0.20 0.20
- 0.10 1.00 0.11 0.05 -.06 -.18 0.00 0.16 0.18
- 0.26 0.11 1.00 0.28 0.09 0.15 0.51 0.57 0.32
- 0.34 0.05 0.28 1.00 0.08 0.12 0.19 0.29 0.22
- 0.18 -.06 0.09 0.08 1.00 0.21 0.09 0.30 0.16
- 0.09 -.18 0.15 0.12 0.21 1.00 0.43 0.25 0.00
- 0.17 0.00 0.51 0.19 0.09 0.43 1.00 0.42 0.28
- 0.20 0.16 0.57 0.29 0.30 0.25 0.42 1.00 0.30
- 0.20 0.18 0.32 0.22 0.16 0.00 0.28 0.30 1.00
- ];
- ST_DEV = [
- .0743
- .1591
- .0696
- .0803
- .0481
- .0452
- .0541
- .0386
- .0533
- ];
- mu = ([
- .256
- .322
- .1751
- .3467
- .1287
- .1067
- .1003
- .1136
- .2943
- ] + 1).^(1/12) - 1;
- C = zeros(size(CC, 1));
- rr = min(mu):0.0005:max(mu);
- A = [
- mu'
- ones(size(mu, 1), 1)'
- ];
- sigma = zeros(size(C, 2), 3);
- mu_ = zeros(size(C, 2), 3);
- for i=1:size(CC, 1)
- for j=1:size(CC, 2)
- if i == j
- C(i, i) = round(ST_DEV(i)^2, 5);
- else
- C(i, j) = round(CC(i, j) * ST_DEV(i) * ST_DEV(j), 5);
- end
- end
- end
- for i=1:size(rr, 2)
- r = rr(i);
- b = [ r; 1 ];
- x = quadprog(2*C, [], [], [], A, b, zeros(size(A, 2), 1), []);
- sigma(i, 1) = sqrt(x' * C * x);
- mu_(i, 1) = mu' * x;
- x = quadprog(2*C, [], [ -A(1, :); A(2, :) ], [ -b(1); b(2) ], [], [], zeros(size(A, 2), 1), []);
- sigma(i, 2) = sqrt(x' * C * x);
- mu_(i, 2) = mu' * x;
- x = quadprog(2*C, [], [ -A(1, :); A(2, :) ], [ -b(1); b(2) ], [], [], [], []);
- sigma(i, 3) = sqrt(x' * C * x);
- mu_(i, 3) = mu' * x;
- end
- mu_ = ((mu_ + 1).^12 - 1) * 100;
- figure('color', [1 1 1])
- plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Standard Problem')
- hold on
- plot(sigma(:, 2), mu_(:, 2), '-o', 'Color', [.85 .325 .098], 'DisplayName', "c'x ≥ r and any portion of capital invested")
- hold off
- title('Optimal Portfolios')
- xlabel('Volatility')
- ylabel('Expected Return (CAGR)')
- axis([ 0 max(max(sigma)) min(min(mu_))*.9 max(max(mu_))*1.2 ])
- legend
- set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
- figure('color', [1 1 1])
- plot(sigma(:, 1), mu_(:, 1), '-o', 'Color', [0 .447 .741], 'DisplayName', 'Standard Problem')
- hold on
- plot(sigma(:, 3), mu_(:, 3), '-o', 'Color', [.929 .694 .125], 'DisplayName', 'Short Selling Allowed and any portion of capital invested')
- hold off
- title('Optimal Portfolios')
- xlabel('Volatility')
- ylabel('Expected Return (CAGR)')
- axis([ 0 max(max(sigma)) min(min(mu_))*.9 max(max(mu_))*1.2 ])
- legend
- set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
- figure('color', [1 1 1])
- plot(sigma(:, 2), mu_(:, 2), '-o', 'Color', [.85 .325 .098], 'DisplayName', "c'x ≥ r and any portion of capital invested")
- hold on
- plot(sigma(:, 3), mu_(:, 3), '-o', 'Color', [.929 .694 .125], 'DisplayName', 'Short Selling Allowed and any portion of capital invested')
- hold off
- title('Optimal Portfolios')
- xlabel('Volatility')
- ylabel('Expected Return (CAGR)')
- axis([ 0 max(max(sigma)) min(min(mu_))*.9 max(max(mu_))*1.2 ])
- legend
- set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
Advertisement
Add Comment
Please, Sign In to add comment