HSG-MCS-HS21_Julia/Problemsets/PS07_Optimisation.ipynb

421 lines
53 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Mean Variance Frontier with Short Sales Constraints"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Packages and Utility Functions"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"printyellow (generic function with 1 method)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Printf, LinearAlgebra, Optim\n",
"\n",
"include(\"jlFiles/printmat.jl\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"using Plots\n",
"\n",
"#pyplot(size=(600,400)) #use pyplot or gr\n",
"gr(size=(480,320))\n",
"default(fmt = :svg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Traditional MV Calculations \n",
"\n",
"(no constraints)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"μ = [11.5, 9.5, 6]/100 #expected returns\n",
"Σ = [166 34 58; #covariance matrix\n",
" 34 64 4;\n",
" 58 4 100]/100^2\n",
"\n",
"assetNames = [\"A\",\"B\",\"C\"];\n",
"n = length(μ)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A Function for Calculating the (traditional) Mean-Variance Frontier\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"MVCalc"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\"\n",
" MVCalc(μstar,μ,Σ)\n",
"\n",
"Calculate the std and weights of a portfolio (with mean return μstar) on MVF of risky assets.\n",
"\n",
"# Remark\n",
"- Only (λ,δ) and thus (w,stdRp) depend on μstar. We could therefore speed up the computations a bit\n",
"by doing the loop over different μstar values inside the function (and thus not recalculate Σ_1,a,b,c).\n",
"\"\"\"\n",
"function MVCalc(μstar,μ,Σ)\n",
" n = length(μ)\n",
" Σ_1 = inv(Σ)\n",
" a = μ'Σ_1*μ\n",
" b = μ'Σ_1*ones(n)\n",
" c = ones(n)'Σ_1*ones(n)\n",
" λ = (c*μstar - b)/(a*c-b^2)\n",
" δ = (a-b*μstar)/(a*c-b^2)\n",
" w = Σ_1 *(μ*λ.+δ)\n",
" StdRp = sqrt(w'Σ*w)\n",
" return StdRp,w\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"480\" height=\"320\" viewBox=\"0 0 1920 1280\">\n",
"<defs>\n",
" <clipPath id=\"clip240\">\n",
" <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<path clip-path=\"url(#clip240)\" d=\"\n",
"M0 1280 L1920 1280 L1920 0 L0 0 Z\n",
" \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
"<defs>\n",
" <clipPath id=\"clip241\">\n",
" <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<path clip-path=\"url(#clip240)\" d=\"\n",
"M197.427 1106.38 L1872.76 1106.38 L1872.76 123.472 L197.427 123.472 Z\n",
" \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
"<defs>\n",
" <clipPath id=\"clip242\">\n",
" <rect x=\"197\" y=\"123\" width=\"1676\" height=\"984\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<polyline clip-path=\"url(#clip242)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 197.427,1106.38 197.427,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip242)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 755.87,1106.38 755.87,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip242)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1314.31,1106.38 1314.31,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip242)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1872.76,1106.38 1872.76,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 197.427,1106.38 1872.76,1106.38 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 197.427,1106.38 197.427,1087.48 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 755.87,1106.38 755.87,1087.48 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1314.31,1106.38 1314.31,1087.48 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1872.76,1106.38 1872.76,1087.48 \n",
" \"/>\n",
"<path clip-path=\"url(#clip240)\" d=\"M197.427 1133.46 Q193.816 1133.46 191.988 1137.02 Q190.182 1140.56 190.182 1147.69 Q190.182 1154.8 191.988 1158.37 Q193.816 1161.91 197.427 1161.91 Q201.062 1161.91 202.867 1158.37 Q204.696 1154.8 204.696 1147.69 Q204.696 1140.56 202.867 1137.02 Q201.062 1133.46 197.427 1133.46 M197.427 1129.75 Q203.238 1129.75 206.293 1134.36 Q209.372 1138.94 209.372 1147.69 Q209.372 1156.42 206.293 1161.03 Q203.238 1165.61 197.427 1165.61 Q191.617 1165.61 188.539 1161.03 Q185.483 1156.42 185.483 1147.69 Q185.483 1138.94 188.539 1134.36 Q191.617 1129.75 197.427 1129.75 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M746.148 1130.38 L764.504 1130.38 L764.504 1134.32 L750.43 1134.32 L750.43 1142.79 Q751.449 1142.44 752.468 1142.28 Q753.486 1142.09 754.505 1142.09 Q760.292 1142.09 763.671 1145.26 Q767.051 1148.44 767.051 1153.85 Q767.051 1159.43 763.579 1162.53 Q760.106 1165.61 753.787 1165.61 Q751.611 1165.61 749.343 1165.24 Q747.097 1164.87 744.69 1164.13 L744.69 1159.43 Q746.773 1160.56 748.995 1161.12 Q751.218 1161.68 753.694 1161.68 Q757.699 1161.68 760.037 1159.57 Q762.375 1157.46 762.375 1153.85 Q762.375 1150.24 760.037 1148.13 Q757.699 1146.03 753.694 1146.03 Q751.819 1146.03 749.944 1146.44 Q748.093 1146.86 746.148 1147.74 L746.148 1130.38 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M1289 1161 L1296.64 1161 L1296.64 1134.64 L1288.33 1136.31 L1288.33 1132.05 L1296.59 1130.38 L1301.27 1130.38 L1301.27 1161 L1308.91 1161 L1308.91 1164.94 L1289 1164.94 L1289 1161 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M1328.35 1133.46 Q1324.74 1133.46 1322.91 1137.02 Q1321.11 1140.56 1321.11 1147.69 Q1321.11 1154.8 1322.91 1158.37 Q1324.74 1161.91 1328.35 1161.91 Q1331.99 1161.91 1333.79 1158.37 Q1335.62 1154.8 1335.62 1147.69 Q1335.62 1140.56 1333.79 1137.02 Q1331.99 1133.46 1328.35 1133.46 M1328.35 1129.75 Q1334.16 1129.75 1337.22 1134.36 Q1340.3 1138.94 1340.3 1147.69 Q1340.3 1156.42 1337.22 1161.03 Q1334.16 1165.61 1328.35 1165.61 Q1322.54 1165.61 1319.46 1161.03 Q1316.41 1156.42 1316.41 1147.69 Q1316.41 1138.94 1319.46 1134.36 Q1322.54 1129.75 1328.35 1129.75 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M1847.94 1161 L1855.58 1161 L1855.58 1134.64 L1847.27 1136.31 L1847.27 1132.05 L1855.53 1130.38 L1860.21 1130.38 L1860.21 1161 L1867.85 1161 L1867.85 1164.94 L1847.94 1164.94 L1847.94 1161 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M1877.34 1130.38 L1895.7 1130.38 L1895.7 1134.32 L1881.62 1134.32 L1881.62 1142.79 Q1882.64 1142.44 1883.66 1142.28 Q1884.68 1142.09 1885.7 1142.09 Q1891.48 1142.09 1894.86 1145.26 Q1898.24 1148.44 1898.24 1153.85 Q1898.24 1159.43 1894.77 1162.53 Q1891.3 1165.61 1884.98 1165.61 Q1882.8 1165.61 1880.53 1165.24 Q1878.29 1164.87 1875.88 1164.13 L1875.88 1159.43 Q1877.96 1160.56 1880.19 1161.12 Q1882.41 1161.68 1884.89 1161.68 Q1888.89 1161.68 1891.23 1159.57 Q1893.57 1157.46 1893.57 1153.85 Q1893.57 1150.24 1891.23 1148.13 Q1888.89 1146.03 1884.89 1146.03 Q1883.01 1146.03 1881.14 1146.44 Q1879.28 1146.86 1877.34 1147.74 L1877.34 1130.38 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M895.014 1198.08 L895.014 1204.35 Q891.354 1202.6 888.108 1201.74 Q884.861 1200.88 881.837 1200.88 Q876.586 1200.88 873.721 1202.92 Q870.888 1204.96 870.888 1208.71 Q870.888 1211.87 872.766 1213.49 Q874.676 1215.08 879.959 1216.07 L883.842 1216.86 Q891.036 1218.23 894.441 1221.7 Q897.879 1225.14 897.879 1230.93 Q897.879 1237.84 893.232 1241.4 Q888.617 1244.97 879.673 1244.97 Q876.299 1244.97 872.48 1244.2 Q868.692 1243.44 864.618 1241.94 L864.618 1235.32 Q868.533 1237.52 872.289 1238.63 Q876.045 1239.75 879.673 1239.75 Q885.179 1239.75 888.171 1237.58 Q891.163 1235.42 891.163 1231.41 Q891.163 1227.91 888.999 1225.93 Q886.866 1223.96 881.965 1222.97 L878.05 1
" 197.427,1106.38 1872.76,1106.38 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip242)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 197.427,778.744 1872.76,778.744 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip242)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 197.427,451.108 1872.76,451.108 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip242)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 197.427,123.472 1872.76,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 197.427,1106.38 197.427,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 197.427,1106.38 216.325,1106.38 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 197.427,778.744 216.325,778.744 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 197.427,451.108 216.325,451.108 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 197.427,123.472 216.325,123.472 \n",
" \"/>\n",
"<path clip-path=\"url(#clip240)\" d=\"M156.683 1092.18 Q153.072 1092.18 151.243 1095.74 Q149.438 1099.28 149.438 1106.41 Q149.438 1113.52 151.243 1117.09 Q153.072 1120.63 156.683 1120.63 Q160.317 1120.63 162.123 1117.09 Q163.952 1113.52 163.952 1106.41 Q163.952 1099.28 162.123 1095.74 Q160.317 1092.18 156.683 1092.18 M156.683 1088.47 Q162.493 1088.47 165.549 1093.08 Q168.627 1097.66 168.627 1106.41 Q168.627 1115.14 165.549 1119.75 Q162.493 1124.33 156.683 1124.33 Q150.873 1124.33 147.794 1119.75 Q144.739 1115.14 144.739 1106.41 Q144.739 1097.66 147.794 1093.08 Q150.873 1088.47 156.683 1088.47 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M147.725 761.464 L166.081 761.464 L166.081 765.399 L152.007 765.399 L152.007 773.871 Q153.026 773.524 154.044 773.362 Q155.063 773.177 156.081 773.177 Q161.868 773.177 165.248 776.348 Q168.627 779.519 168.627 784.936 Q168.627 790.515 165.155 793.617 Q161.683 796.695 155.364 796.695 Q153.188 796.695 150.919 796.325 Q148.674 795.954 146.266 795.214 L146.266 790.515 Q148.35 791.649 150.572 792.205 Q152.794 792.76 155.271 792.76 Q159.276 792.76 161.614 790.654 Q163.952 788.547 163.952 784.936 Q163.952 781.325 161.614 779.218 Q159.276 777.112 155.271 777.112 Q153.396 777.112 151.521 777.529 Q149.669 777.945 147.725 778.825 L147.725 761.464 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M117.331 464.453 L124.97 464.453 L124.97 438.087 L116.66 439.754 L116.66 435.495 L124.924 433.828 L129.6 433.828 L129.6 464.453 L137.239 464.453 L137.239 468.388 L117.331 468.388 L117.331 464.453 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M156.683 436.907 Q153.072 436.907 151.243 440.472 Q149.438 444.013 149.438 451.143 Q149.438 458.249 151.243 461.814 Q153.072 465.356 156.683 465.356 Q160.317 465.356 162.123 461.814 Q163.952 458.249 163.952 451.143 Q163.952 444.013 162.123 440.472 Q160.317 436.907 156.683 436.907 M156.683 433.203 Q162.493 433.203 165.549 437.809 Q168.627 442.393 168.627 451.143 Q168.627 459.87 165.549 464.476 Q162.493 469.059 156.683 469.059 Q150.873 469.059 147.794 464.476 Q144.739 459.87 144.739 451.143 Q144.739 442.393 147.794 437.809 Q150.873 433.203 156.683 433.203 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M118.327 136.817 L125.966 136.817 L125.966 110.451 L117.656 112.118 L117.656 107.859 L125.919 106.192 L130.595 106.192 L130.595 136.817 L138.234 136.817 L138.234 140.752 L118.327 140.752 L118.327 136.817 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M147.725 106.192 L166.081 106.192 L166.081 110.127 L152.007 110.127 L152.007 118.599 Q153.026 118.252 154.044 118.09 Q155.063 117.905 156.081 117.905 Q161.868 117.905 165.248 121.076 Q168.627 124.248 168.627 129.664 Q168.627 135.243 165.155 138.345 Q161.683 141.423 155.364 141.423 Q153.188 141.423 150.919 141.053 Q148.674 140.683 146.266 139.942 L146.266 135.243 Q148.35 136.377 150.572 136.933 Q152.794 137.488 155.271 137.488 Q159.276 137.488 161.614 135.382 Q163.952 133.275 163.952 129.664 Q163.952 126.053 161.614 123.947 Q159.276 121.84 155.271 121.84 Q153.396 121.84 151.521 122.257 Q149.669 122.674 147.725 123.553 L147.725 106.192 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M26.0842 725.546 L26.0842 695.5 L31.495 695.5 L31.495 719.117 L45.5632 719.117 L45.5632 696.487 L50.9741 696.487 L50.9741 719.117 L68.1933 719.117 L68.1933 694.927 L73.6042 694.927 L73.6042 725.546 L26.0842 725.546 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M51.3242 661.825 Q52.0244 659.756 54.3161 657.815 Q56.6077 655.842 60.6181 653.868 L73.6042 647.343 L73.6042 654.25 L61.4138 660.329 Q56.6395 662.685 55.08 664.913 Q53.5204 667.109 53.5204 670.928 L53.5204 677.931 L73.6042 677.931 L73.6042 684.36 L26.0842 684.36 L26.0842 669.846 Q26.0842 661.698 29.4898 657.688
" 1425.43,844.271 1414.49,840.667 1403.6,837.063 1392.77,833.459 1381.99,829.855 1371.26,826.251 1360.59,822.647 1349.99,819.043 1339.44,815.439 1328.95,811.835 \n",
" 1318.53,808.231 1308.17,804.627 1297.88,801.023 1287.66,797.419 1277.52,793.815 1267.44,790.211 1257.44,786.607 1247.52,783.003 1237.68,779.399 1227.92,775.795 \n",
" 1218.25,772.191 1208.66,768.587 1199.17,764.983 1189.76,761.379 1180.45,757.775 1171.24,754.171 1162.12,750.567 1153.12,746.963 1144.21,743.359 1135.42,739.755 \n",
" 1126.74,736.151 1118.17,732.547 1109.72,728.943 1101.4,725.339 1093.19,721.735 1085.12,718.131 1077.18,714.527 1069.38,710.923 1061.72,707.319 1054.2,703.715 \n",
" 1046.82,700.111 1039.6,696.507 1032.54,692.903 1025.63,689.299 1018.88,685.695 1012.31,682.091 1005.9,678.487 999.669,674.883 993.618,671.279 987.75,667.675 \n",
" 982.069,664.071 976.58,660.467 971.287,656.863 966.194,653.259 961.305,649.655 956.623,646.051 952.154,642.447 947.899,638.843 943.864,635.239 940.051,631.635 \n",
" 936.465,628.031 933.108,624.427 929.983,620.823 927.094,617.219 924.444,613.615 922.034,610.011 919.868,606.407 917.948,602.803 916.275,599.199 914.851,595.595 \n",
" 913.678,591.991 912.758,588.387 912.09,584.783 911.676,581.179 911.516,577.575 911.61,573.971 911.959,570.368 912.562,566.764 913.417,563.16 914.525,559.556 \n",
" 915.885,555.952 917.493,552.348 919.35,548.744 921.454,545.14 923.801,541.536 926.389,537.932 929.217,534.328 932.281,530.724 935.579,527.12 939.107,523.516 \n",
" 942.861,519.912 946.84,516.308 951.038,512.704 955.453,509.1 960.08,505.496 964.917,501.892 969.958,498.288 975.2,494.684 980.639,491.08 986.27,487.476 \n",
" 992.09,483.872 998.095,480.268 1004.28,476.664 1010.64,473.06 1017.18,469.456 1023.88,465.852 1030.74,462.248 1037.77,458.644 1044.95,455.04 1052.29,451.436 \n",
" 1059.77,447.832 1067.4,444.228 1075.16,440.624 1083.07,437.02 1091.11,433.416 1099.27,429.812 1107.57,426.208 1115.98,422.604 1124.52,419 1133.17,415.396 \n",
" 1141.94,411.792 1150.81,408.188 1159.8,404.584 1168.88,400.98 1178.07,397.376 1187.36,393.772 1196.74,390.168 1206.21,386.564 1215.77,382.96 1225.43,379.356 \n",
" 1235.16,375.752 1244.98,372.148 1254.88,368.544 1264.86,364.94 1274.92,361.336 1285.05,357.732 1295.25,354.128 1305.52,350.524 1315.86,346.92 1326.26,343.316 \n",
" 1336.73,339.712 1347.27,336.108 1357.86,332.504 1368.51,328.9 1379.22,325.296 1389.99,321.692 1400.81,318.088 1411.68,314.484 1422.61,310.88 1433.59,307.276 \n",
" 1444.61,303.672 1455.68,300.068 1466.8,296.464 1477.97,292.86 1489.18,289.256 1500.43,285.652 1511.72,282.048 1523.06,278.444 1534.43,274.84 1545.84,271.236 \n",
" 1557.29,267.632 1568.78,264.028 1580.31,260.424 1591.86,256.82 1603.46,253.216 1615.08,249.612 1626.74,246.008 1638.43,242.404 1650.15,238.8 1661.9,235.196 \n",
" 1673.69,231.592 1685.5,227.988 1697.33,224.384 1709.2,220.78 1721.09,217.176 1733.01,213.572 1744.95,209.968 1756.92,206.364 1768.91,202.76 1780.93,199.156 \n",
" 1792.97,195.552 1805.04,191.948 1817.12,188.344 1829.23,184.74 1841.36,181.136 1853.51,177.532 1865.68,173.928 1877.87,170.324 1890.08,166.72 1902.3,163.116 \n",
" 1914.55,159.512 1926.82,155.908 1939.1,152.304 1951.4,148.7 1963.72,145.096 1976.05,141.492 1988.4,137.888 2000.77,134.284 2013.16,130.68 2025.55,127.076 \n",
" 2037.97,123.472 \n",
" \"/>\n",
"<circle clip-path=\"url(#clip242)\" cx=\"1636.43\" cy=\"352.817\" r=\"14\" fill=\"#ff0000\" fill-rule=\"evenodd\" fill-opacity=\"1\" stroke=\"#000000\" stroke-opacity=\"1\" stroke-width=\"2.56\"/>\n",
"<circle clip-path=\"url(#clip242)\" cx=\"1090.94\" cy=\"483.872\" r=\"14\" fill=\"#ff0000\" fill-rule=\"evenodd\" fill-opacity=\"1\" stroke=\"#000000\" stroke-opacity=\"1\" stroke-width=\"2.56\"/>\n",
"<circle clip-path=\"url(#clip242)\" cx=\"1314.31\" cy=\"713.217\" r=\"14\" fill=\"#ff0000\" fill-rule=\"evenodd\" fill-opacity=\"1\" stroke=\"#000000\" stroke-opacity=\"1\" stroke-width=\"2.56\"/>\n",
"<path clip-path=\"url(#clip240)\" d=\"\n",
"M253.272 311.756 L585.159 311.756 L585.159 156.236 L253.272 156.236 Z\n",
" \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 253.272,311.756 585.159,311.756 585.159,156.236 253.272,156.236 253.272,311.756 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip240)\" style=\"stroke:#ff0000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 271.887,208.076 383.575,208.076 \n",
" \"/>\n",
"<path clip-path=\"url(#clip240)\" d=\"M402.19 190.796 L409.157 190.796 L417.977 214.314 L426.842 190.796 L433.81 190.796 L433.81 225.356 L429.25 225.356 L429.25 195.009 L420.338 218.712 L415.639 218.712 L406.727 195.009 L406.727 225.356 L402.19 225.356 L402.19 190.796 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M452.004 225.356 L438.81 190.796 L443.694 190.796 L454.643 219.893 L465.615 190.796 L470.477 190.796 L457.305 225.356 L452.004 225.356 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M475.523 190.796 L495.384 190.796 L495.384 194.731 L480.199 194.731 L480.199 204.916 L493.902 204.916 L493.902 208.851 L480.199 208.851 L480.199 225.356 L475.523 225.356 L475.523 190.796 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><circle clip-path=\"url(#clip240)\" cx=\"327.731\" cy=\"259.916\" r=\"23\" fill=\"#ff0000\" fill-rule=\"evenodd\" fill-opacity=\"1\" stroke=\"#000000\" stroke-opacity=\"1\" stroke-width=\"4.096\"/>\n",
"<path clip-path=\"url(#clip240)\" d=\"M415.593 264.163 Q410.431 264.163 408.44 265.344 Q406.449 266.524 406.449 269.372 Q406.449 271.64 407.931 272.983 Q409.435 274.302 412.005 274.302 Q415.546 274.302 417.676 271.802 Q419.829 269.279 419.829 265.112 L419.829 264.163 L415.593 264.163 M424.088 262.404 L424.088 277.196 L419.829 277.196 L419.829 273.261 Q418.37 275.622 416.194 276.756 Q414.018 277.867 410.87 277.867 Q406.889 277.867 404.528 275.645 Q402.19 273.399 402.19 269.649 Q402.19 265.274 405.106 263.052 Q408.046 260.83 413.856 260.83 L419.829 260.83 L419.829 260.413 Q419.829 257.474 417.884 255.876 Q415.963 254.256 412.468 254.256 Q410.245 254.256 408.139 254.788 Q406.032 255.321 404.088 256.386 L404.088 252.45 Q406.426 251.548 408.625 251.108 Q410.824 250.645 412.907 250.645 Q418.532 250.645 421.31 253.562 Q424.088 256.478 424.088 262.404 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M449.389 252.034 L449.389 256.062 Q447.583 255.136 445.639 254.673 Q443.694 254.21 441.611 254.21 Q438.44 254.21 436.842 255.182 Q435.268 256.154 435.268 258.099 Q435.268 259.58 436.403 260.437 Q437.537 261.27 440.963 262.034 L442.421 262.358 Q446.958 263.33 448.856 265.112 Q450.778 266.872 450.778 270.043 Q450.778 273.654 447.907 275.761 Q445.06 277.867 440.06 277.867 Q437.977 277.867 435.708 277.45 Q433.463 277.057 430.963 276.247 L430.963 271.849 Q433.324 273.075 435.616 273.7 Q437.907 274.302 440.153 274.302 Q443.162 274.302 444.782 273.284 Q446.403 272.242 446.403 270.367 Q446.403 268.631 445.222 267.705 Q444.065 266.779 440.106 265.923 L438.625 265.575 Q434.667 264.742 432.907 263.029 Q431.148 261.293 431.148 258.284 Q431.148 254.626 433.741 252.636 Q436.333 250.645 441.102 250.645 Q443.463 250.645 445.546 250.992 Q447.629 251.339 449.389 252.034 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M474.088 252.034 L474.088 256.062 Q472.282 255.136 470.338 254.673 Q468.393 254.21 466.31 254.21 Q463.139 254.21 461.541 255.182 Q459.967 256.154 459.967 258.099 Q459.967 259.58 461.102 260.437 Q462.236 261.27 465.662 262.034 L467.12 262.358 Q471.657 263.33 473.555 265.112 Q475.476 266.872 475.476 270.043 Q475.476 273.654 472.606 275.761 Q469.759 277.867 464.759 277.867 Q462.676 277.867 460.407 277.45 Q458.162 277.057 455.662 276.247 L455.662 271.849 Q458.023 273.075 460.315 273.7 Q462.606 274.302 464.852 274.302 Q467.861 274.302 469.481 273.284 Q471.102 272.242 471.102 270.367 Q471.102 268.631 469.921 267.705 Q468.764 266.779 464.805 265.923 L463.324 265.575 Q459.365 264.742 457.606 263.029 Q455.847 261.293 455.847 258.284 Q455.847 254.626 458.44 252.636 Q461.032 250.645 465.801 250.645 Q468.162 250.645 470.245 250.992 Q472.328 251.339 474.088 252.034 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M504.435 263.168 L504.435 265.251 L484.851 265.251 Q485.129 269.649 487.49 271.964 Q489.875 274.256 494.111 274.256 Q496.564 274.256 498.856 273.654 Q501.171 273.052 503.439 271.849 L503.439 275.876 Q501.148 276.848 498.74 277.358 Q496.333 277.867 493.856 277.867 Q487.652 277.867 484.018 274.256 Q480.407 270.645 480.407 264.487 Q480.407 258.122 483.833 254.395 Q487.282 250.645 493.115 250.645 Q498.347 250.645 501.379 254.025 Q504.435 257.381 504.435 263.168 M500.175 261.918 Q500.129 258.423 498.208 256.339 Q496.31 254.256 493.162 254.256 Q489.597 254.256 487.444 256.27 Q485.314 258.284 484.99 261.941 L500.175 261.918 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip240)\" d=\"M515.638 243.909 L515.638 251.27 L524.411 251.27 L524.411 254.58 L515.638 254.58 L515.638 268.654 Q515.638 271.825 516.495 272.728 Q517.374 273.631 520.036 273.631 L524.411 273.631 L524.411 277.196 L520.036 277.196 Q515.106 277.196 513.231 275.367 Q511.356 273.515 511.356 268.654 L511.356 254.58 L508.231 254.58 L508.231 251.27 L511.356 251.27 L511.356 243.909 L515.638 243.909 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#cl
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"μstar_range = range(0.04,0.15,length=201)\n",
"L = length(μstar_range)\n",
"\n",
"StdRp = fill(NaN,L)\n",
"for i = 1:L\n",
" StdRp[i] = MVCalc(μstar_range[i],μ,Σ)[1]\n",
"end\n",
"\n",
"p1 = plot( StdRp*100,μstar_range*100,\n",
" linecolor = :red,\n",
" linewidth = 2,\n",
" label = \"MVF\",\n",
" legend = :topleft,\n",
" xlim = (0,15),\n",
" ylim = (0,15),\n",
" title = \"MVF (no portfolio constraints)\",\n",
" xlabel = \"Std(Rp), %\",\n",
" ylabel = \"ERp, %\" )\n",
"scatter!(sqrt.(diag(Σ))*100,μ*100,color=:red,label=\"assets\")\n",
"display(p1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The Maximization Problem\n",
"\n",
"The investor maximizes:\n",
"\n",
"$\\text{E}R_{p}-\\frac{k}{2} \\text{Var}(R_{p})$,\n",
"\n",
"where $\\text{E}R_{p}= w'\\mu$ and $\\text{Var}(R_{p}=w'\\Sigma w$\n",
"\n",
"subject to $\\Sigma_{i=1}^n w_{i} = 1$.\n",
"\n",
"We can trace out the mean-variance frontier by solving this problem for different values of $k$.\n",
"\n",
"To impose the restriction, optimise over `v` and let `w=[v;1-sum(v)]`"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"objfun1 (generic function with 1 method)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function objfun1(v,k,μ,Σ)\n",
" w = [v;1-sum(v)]\n",
" ERp = w'μ\n",
" VarRp = w'Σ*w\n",
" StdRp = sqrt(VarRp)\n",
" U = ERp - k/2*VarRp\n",
" loss = -U #minimise this\n",
" return loss, w, ERp, StdRp\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A 0.313\n",
"B 0.695\n",
"C -0.008\n",
"\n",
"σₚ 0.079\n",
"μₚ 0.102\n",
"\n"
]
}
],
"source": [
"k = 10\n",
"Sol = optimize(v -> objfun1(v,k,μ,Σ)[1],zeros(n-1))\n",
"\n",
"v = Optim.minimizer(Sol)\n",
"(_,w,ERp,StdRp) = objfun1(v,k,μ,Σ)\n",
"\n",
"printmat(w,rowNames=assetNames)\n",
"printmat([StdRp,ERp],rowNames=[\"σₚ\",\"μₚ\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 1\n",
"\n",
"Redo the optimisation for the following range of $k$ values:\n",
"`k = [range(0.1,1.9,length=30);range(1,30,length=25)]`\n",
"\n",
"Then plot the resulting \"efficient frontier\" (similar to the MV plot above). Is is the same as the plot above?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 2\n",
"\n",
"Change the optimisation problem to `U = ERp + cost - k/2*VarRp` where `cost` is (sum of the *negative* portfolio weights)*0.05. This is a (crude/dramatic) attaempt to illustrate the impact of costs of short selling ($w_i<0$) assets. Plot the new \"effecient frontier\" together with the old one."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 3\n",
"\n",
"Compare the portfolio weights (with/without the extra `cost`)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Julia 1.6.3",
"language": "julia",
"name": "julia-1.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}