HSG-MCS-HS21_Julia/Problemsets/MV_by_MC.ipynb

206 lines
766 KiB
Plaintext
Raw Normal View History

2021-11-15 20:14:51 +00:00
{
"cells": [
{
"cell_type": "markdown",
"source": [
"## Load Packages"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 1,
"source": [
"using Printf, Distributions\n",
"include(\"jlFiles/printmat.jl\")"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"printyellow (generic function with 1 method)"
]
},
"metadata": {},
"execution_count": 1
}
],
"metadata": {
"tags": []
}
},
{
"cell_type": "code",
"execution_count": 2,
"source": [
"using Plots\n",
"gr(size=(480,320))\n",
"default(fmt = :svg)"
],
"outputs": [],
"metadata": {
"tags": []
}
},
{
"cell_type": "markdown",
"source": [
"# MV Frontier by Simulations\n",
"\n",
"We illustrate the mean-variance frontier (really, the $\\sigma \\times \\mu$ frontier) by calculating the portfolio mean and standard deviation for a large number of randomly drawn portfolios (in each case the portfolio weights should sum to one).\n",
"\n",
"As always, we have \n",
"$\\text{E}R_p = w'\\mu$ and \n",
"\n",
"$\\text{Var}(R_p) = w'\\Sigma w$.\n",
"\n",
"The information about $\\mu$ and $\\Sigma$ is found below."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 3,
"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",
"n = length(μ)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"3"
]
},
"metadata": {},
"execution_count": 3
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 4,
"source": [
"using Random\n",
"Random.seed!(123)\n",
"\n",
"NSim = 2000\n",
"(ERp,StdRp) = (fill(NaN,NSim),fill(NaN,NSim))\n",
"\n",
"for i = 1:NSim\n",
" w = randn(n-1) #draw n-1 random portfolio weights\n",
" w = [w;1-sum(w)] #make the n weights sum to 1\n",
" ERp[i] = w'μ\n",
" StdRp[i] = sqrt(w'Σ*w)\n",
"end"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 6,
"source": [
"p1 = scatter(StdRp,ERp,\n",
" title = \"MV frontier by bootstrap\",\n",
" xlabel = \"std\",\n",
" ylabel = \"mean\",\n",
" xlims = [0,0.2],\n",
" ylims = [0.0,0.15],\n",
" legend = false )\n",
"display(p1)"
],
"outputs": [
{
"output_type": "display_data",
"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=\"clip780\">\n <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip780)\" d=\"\nM0 1280 L1920 1280 L1920 0 L0 0 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip781\">\n <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip780)\" d=\"\nM244.742 1106.38 L1872.76 1106.38 L1872.76 123.472 L244.742 123.472 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip782\">\n <rect x=\"244\" y=\"123\" width=\"1629\" height=\"984\"/>\n </clipPath>\n</defs>\n<polyline clip-path=\"url(#clip782)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 290.818,1106.38 290.818,123.472 \n \"/>\n<polyline clip-path=\"url(#clip782)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 674.783,1106.38 674.783,123.472 \n \"/>\n<polyline clip-path=\"url(#clip782)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1058.75,1106.38 1058.75,123.472 \n \"/>\n<polyline clip-path=\"url(#clip782)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1442.71,1106.38 1442.71,123.472 \n \"/>\n<polyline clip-path=\"url(#clip782)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1826.68,1106.38 1826.68,123.472 \n \"/>\n<polyline clip-path=\"url(#clip780)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 244.742,1106.38 1872.76,1106.38 \n \"/>\n<polyline clip-path=\"url(#clip780)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 290.818,1106.38 290.818,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip780)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 674.783,1106.38 674.783,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip780)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1058.75,1106.38 1058.75,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip780)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1442.71,1106.38 1442.71,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip780)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1826.68,1106.38 1826.68,1087.48 \n \"/>\n<path clip-path=\"url(#clip780)\" d=\"M253.121 1131.73 Q249.51 1131.73 247.682 1135.3 Q245.876 1138.84 245.876 1145.97 Q245.876 1153.07 247.682 1156.64 Q249.51 1160.18 253.121 1160.18 Q256.756 1160.18 258.561 1156.64 Q260.39 1153.07 260.39 1145.97 Q260.39 1138.84 258.561 1135.3 Q256.756 1131.73 253.121 1131.73 M253.121 1128.03 Q258.931 1128.03 261.987 1132.63 Q265.066 1137.22 265.066 1145.97 Q265.066 1154.69 261.987 1159.3 Q258.931 1163.88 253.121 1163.88 Q247.311 1163.88 244.232 1159.3 Q241.177 1154.69 241.177 1145.97 Q241.177 1137.22 244.232 1132.63 Q247.311 1128.03 253.121 1128.03 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip780)\" d=\"M273.283 1157.33 L278.168 1157.33 L278.168 1163.21 L273.283 1163.21 L273.283 1157.33 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip780)\" d=\"M298.353 1131.73 Q294.741 11
},
"metadata": {}
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 7,
"source": [
"NSim = 2000\n",
"(ERp,StdRp) = (fill(NaN,NSim),fill(NaN,NSim))\n",
"\n",
"for i = 1:NSim\n",
" w = rand(n-1) #draw n-1 random portfolio weights on [0,1]\n",
" w = [w;1-sum(w)] #make the n weights sum to 1\n",
" if all(w.>=0) #don't allow short sales\n",
" ERp[i] = w'μ\n",
" StdRp[i] = sqrt(w'Σ*w)\n",
" end \n",
"end"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 8,
"source": [
"scatter!(p1,StdRp,ERp,\n",
" title = \"MV frontier by bootstrap\",\n",
" xlabel = \"std\",\n",
" ylabel = \"mean\",\n",
" xlims = [0,0.2],\n",
" ylims = [0,0.15],\n",
" legend = false,\n",
" color= :red )"
],
"outputs": [
{
"output_type": "execute_result",
"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=\"clip820\">\n <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip820)\" d=\"\nM0 1280 L1920 1280 L1920 0 L0 0 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip821\">\n <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip820)\" d=\"\nM244.742 1106.38 L1872.76 1106.38 L1872.76 123.472 L244.742 123.472 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip822\">\n <rect x=\"244\" y=\"123\" width=\"1629\" height=\"984\"/>\n </clipPath>\n</defs>\n<polyline clip-path=\"url(#clip822)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 290.818,1106.38 290.818,123.472 \n \"/>\n<polyline clip-path=\"url(#clip822)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 674.783,1106.38 674.783,123.472 \n \"/>\n<polyline clip-path=\"url(#clip822)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1058.75,1106.38 1058.75,123.472 \n \"/>\n<polyline clip-path=\"url(#clip822)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1442.71,1106.38 1442.71,123.472 \n \"/>\n<polyline clip-path=\"url(#clip822)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1826.68,1106.38 1826.68,123.472 \n \"/>\n<polyline clip-path=\"url(#clip820)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 244.742,1106.38 1872.76,1106.38 \n \"/>\n<polyline clip-path=\"url(#clip820)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 290.818,1106.38 290.818,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip820)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 674.783,1106.38 674.783,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip820)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1058.75,1106.38 1058.75,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip820)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1442.71,1106.38 1442.71,1087.48 \n \"/>\n<polyline clip-path=\"url(#clip820)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1826.68,1106.38 1826.68,1087.48 \n \"/>\n<path clip-path=\"url(#clip820)\" d=\"M253.121 1131.73 Q249.51 1131.73 247.682 1135.3 Q245.876 1138.84 245.876 1145.97 Q245.876 1153.07 247.682 1156.64 Q249.51 1160.18 253.121 1160.18 Q256.756 1160.18 258.561 1156.64 Q260.39 1153.07 260.39 1145.97 Q260.39 1138.84 258.561 1135.3 Q256.756 1131.73 253.121 1131.73 M253.121 1128.03 Q258.931 1128.03 261.987 1132.63 Q265.066 1137.22 265.066 1145.97 Q265.066 1154.69 261.987 1159.3 Q258.931 1163.88 253.121 1163.88 Q247.311 1163.88 244.232 1159.3 Q241.177 1154.69 241.177 1145.97 Q241.177 1137.22 244.232 1132.63 Q247.311 1128.03 253.121 1128.03 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip820)\" d=\"M273.283 1157.33 L278.168 1157.33 L278.168 1163.21 L273.283 1163.21 L273.283 1157.33 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip820)\" d=\"M298.353 1131.73 Q294.741 11
},
"metadata": {},
"execution_count": 8
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [],
"outputs": [],
"metadata": {}
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Julia 1.7.0-rc1",
"language": "julia",
"name": "julia-1.7"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.7.0"
},
"nteract": {
"version": "0.25.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}