HSG-MCS-HS21_Julia/Problemsets/PS02_Stats.ipynb

341 lines
389 KiB
Plaintext
Raw Normal View History

2021-11-15 20:14:51 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Packages and Extra Functions"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"printyellow (generic function with 1 method)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Statistics, Printf, Dates, DelimitedFiles, Distributions, LinearAlgebra\n",
"\n",
"include(\"jlFiles/printmat.jl\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"using Plots\n",
"\n",
"#pyplot(size=(600,400))\n",
"gr(size=(480,320))\n",
"default(fmt = :svg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Load Data from a csv File"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"first four lines of x:\n",
"1970-01-01 -5.480 -4.875 -8.100\n",
"1970-02-01 1.465 7.546 5.130\n",
"1970-03-01 -7.232 1.061 -1.060\n",
"1970-04-01 -24.488 -8.481 -11.000\n",
"\n"
]
}
],
"source": [
"x = readdlm(\"Data/Portfolios_SGLV.csv\",',',skipstart=1) #reading the csv file\n",
" #skip 1st line\n",
"println(\"\\nfirst four lines of x:\")\n",
"printmat(x[1:4,:])\n",
"\n",
"dN = Date.(x[:,1]) #creating variables\n",
"Re = convert.(Float64,x[:,2:3]) #small growth (SG), large value (LV)\n",
"Rme = convert.(Float64,x[:,end]); #market"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task 1: Means and Standard Deviations\n",
"\n",
"Estimate and print the means and standard deviations of the three series (in `Re` and `Rme`)."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mean:\n",
" SG LV market\n",
"mean 0.218 0.677 0.593\n",
"std 8.079 5.772 4.595\n",
"\n"
]
}
],
"source": [
"println(\"Mean:\")\n",
"a = mean([Re Rme], dims=1)\n",
"b = std([Re Rme], dims=1)\n",
"\n",
"printmat([a;b],colNames=[\"SG\",\"LV\", \"market\"],rowNames=[\"mean\",\"std\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task 2: Correlations\n",
"\n",
"Estimate and print the correlation matrix."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 1.000 0.563 0.804\n",
" 0.563 1.000 0.774\n",
" 0.804 0.774 1.000\n",
"\n"
]
}
],
"source": [
"c = cor([Re Rme])\n",
"\n",
"printmat(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task 3: OLS\n",
"\n",
"Code up a function to do OLS. It should take `y` and `x` as inputs and return the slope coefficients, the standard errors and R2 as output. Notice that you can calculate the standard erors as follows\n",
"1. `cov_b = inv(x'x)*var(u)` where `u` are the residuals\n",
"2. `std_b = sqrt.(diag(cov_b))`\n",
"\n",
"Hint: cell 3 (or so) in [OLS notebook on Paul Söderlind's Github](https://github.com/PaulSoderlind/FinancialEconometrics/blob/master/Ch02_OLS1.ipynb)\n",
"\n",
"Then, regress each of return `Re[:,1]` and `Re[:,2]` on the market `Rme` and a constant. Report the coefficients and standard errors."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Small growth:\n",
"\n",
" coef stdErr\n",
"c -0.620 0.196\n",
"Rme (slope) 1.414 0.042\n",
"\n",
"Large value:\n",
"\n",
" coef stdErr\n",
"c 0.100 0.149\n",
"Rme (slope) 0.972 0.032\n",
"\n"
]
}
],
"source": [
"T = size(Re,1)\n",
"\n",
"c = ones(T)\n",
"X = [c Rme]\n",
"# b1 = X\\Re[:,1]\n",
"# b2 = X\\Re[:,2]\n",
"\n",
"function OlsGMFn(Y,X)\n",
"\n",
" T = size(Y,1)\n",
"\n",
" b = X\\Y\n",
" Yhat = X*b\n",
" u = Y - Yhat\n",
"\n",
" σ2 = var(u)\n",
" cov_b = inv(X'X)*σ2\n",
" std_b = sqrt.(diag(cov_b))\n",
" R2 = 1 - σ2/var(Y)\n",
"\n",
" return b, std_b, R2\n",
"\n",
"end\n",
"\n",
"(b1, std_b1) = OlsGMFn(Re[:,1],X)\n",
"(b2, std_b2) = OlsGMFn(Re[:,2],X)\n",
"\n",
"println(\"Small growth:\\n\")\n",
"printmat([b1 std_b1],colNames=[\"coef\",\"stdErr\"],rowNames=[\"c\", \"Rme (slope)\"])\n",
"println(\"Large value:\\n\")\n",
"printmat([b2 std_b2],colNames=[\"coef\",\"stdErr\"],rowNames=[\"c\", \"Rme (slope)\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task 4: Scatter Plot\n",
"\n",
"Make a scatter plot of `Rme` (on the horizontal axis) and `SG`. Do the same for `Rme` and `LV`. Add a 45 degree line and also the regression line from OLS."
]
},
{
"cell_type": "code",
"execution_count": 15,
"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=\"clip770\">\n <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip770)\" d=\"\nM0 1280 L1920 1280 L1920 0 L0 0 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip771\">\n <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip770)\" d=\"\nM224.375 1119.34 L1872.76 1119.34 L1872.76 106.192 L224.375 106.192 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip772\">\n <rect x=\"224\" y=\"106\" width=\"1649\" height=\"1014\"/>\n </clipPath>\n</defs>\n<polyline clip-path=\"url(#clip772)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 224.375,1119.34 224.375,106.192 \n \"/>\n<polyline clip-path=\"url(#clip772)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 554.051,1119.34 554.051,106.192 \n \"/>\n<polyline clip-path=\"url(#clip772)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 883.727,1119.34 883.727,106.192 \n \"/>\n<polyline clip-path=\"url(#clip772)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1213.4,1119.34 1213.4,106.192 \n \"/>\n<polyline clip-path=\"url(#clip772)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1543.08,1119.34 1543.08,106.192 \n \"/>\n<polyline clip-path=\"url(#clip772)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1872.76,1119.34 1872.76,106.192 \n \"/>\n<polyline clip-path=\"url(#clip770)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 224.375,1119.34 1872.76,1119.34 \n \"/>\n<polyline clip-path=\"url(#clip770)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 224.375,1119.34 224.375,1100.44 \n \"/>\n<polyline clip-path=\"url(#clip770)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 554.051,1119.34 554.051,1100.44 \n \"/>\n<polyline clip-path=\"url(#clip770)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 883.727,1119.34 883.727,1100.44 \n \"/>\n<polyline clip-path=\"url(#clip770)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1213.4,1119.34 1213.4,1100.44 \n \"/>\n<polyline clip-path=\"url(#clip770)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1543.08,1119.34 1543.08,1100.44 \n \"/>\n<polyline clip-path=\"url(#clip770)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1872.76,1119.34 1872.76,1100.44 \n \"/>\n<path clip-path=\"url(#clip770)\" d=\"M178.437 1159.34 L208.113 1159.34 L208.113 1163.28 L178.437 1163.28 L178.437 1159.34 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip770)\" d=\"M228.784 1157.03 Q225.636 1157.03 223.784 1159.18 Q221.956 1161.33 221.956 1165.08 Q221.956 1168.81 223.784 1170.99 Q225.636 1173.14 228.784 1173.14 Q231.932 1173.14 233.761 1170.99 Q235.613 1168.81 235.613 1165.08 Q235.613 1161.33 233.761 1159.18 Q231.932 1157.03 228.7
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" -0.620 1.414\n",
"\n",
" -28.895\n",
"\n",
" -40.000\n",
" -20.000\n",
" 0.000\n",
" 20.000\n",
" 40.000\n",
"\n",
" -57.169\n",
" -28.895\n",
" -0.620\n",
" 27.655\n",
" 55.930\n",
"\n"
]
}
],
"source": [
"xGrid = range(-40,40, length=5)\n",
"yLine = b1[1] .+ b1[2]*xGrid\n",
"\n",
"printmat(b1[1], b1[2])\n",
"printmat(b1[1] + b1[2]*xGrid[2])\n",
"printmat(xGrid)\n",
"printmat(yLine)\n",
"\n",
"p1 = scatter( Rme, Re[:,1],\n",
" fillcolor = :blue,\n",
" legend = false,\n",
" xlim = (-60,40),\n",
" ylim = (-60,40),\n",
" title = \"Scatter plot: two monthly return series\",\n",
" titlefont = font(10),\n",
" xlabel = \"Market excess return, %\",\n",
" ylabel = \"Excess returns on small growth stocks, %\",\n",
" guidefont = font(8))\n",
"plot!([-40;40],[-40;40],color=:black,linewidth=0.5) #easier to keep this outside plot()\n",
"plot!(xGrid, yLine ,color=:red,linestyle=:dash) #easier to keep this outside plot()\n",
"display(p1)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"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=\"clip530\">\n <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip530)\" d=\"\nM0 1280 L1920 1280 L1920 0 L0 0 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip531\">\n <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip530)\" d=\"\nM224.375 1119.34 L1872.76 1119.34 L1872.76 106.192 L224.375 106.192 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip532\">\n <rect x=\"224\" y=\"106\" width=\"1649\" height=\"1014\"/>\n </clipPath>\n</defs>\n<polyline clip-path=\"url(#clip532)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 224.375,1119.34 224.375,106.192 \n \"/>\n<polyline clip-path=\"url(#clip532)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 636.47,1119.34 636.47,106.192 \n \"/>\n<polyline clip-path=\"url(#clip532)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1048.57,1119.34 1048.57,106.192 \n \"/>\n<polyline clip-path=\"url(#clip532)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1460.66,1119.34 1460.66,106.192 \n \"/>\n<polyline clip-path=\"url(#clip532)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n 1872.76,1119.34 1872.76,106.192 \n \"/>\n<polyline clip-path=\"url(#clip530)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 224.375,1119.34 1872.76,1119.34 \n \"/>\n<polyline clip-path=\"url(#clip530)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 224.375,1119.34 224.375,1100.44 \n \"/>\n<polyline clip-path=\"url(#clip530)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 636.47,1119.34 636.47,1100.44 \n \"/>\n<polyline clip-path=\"url(#clip530)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1048.57,1119.34 1048.57,1100.44 \n \"/>\n<polyline clip-path=\"url(#clip530)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1460.66,1119.34 1460.66,1100.44 \n \"/>\n<polyline clip-path=\"url(#clip530)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1872.76,1119.34 1872.76,1100.44 \n \"/>\n<path clip-path=\"url(#clip530)\" d=\"M178.437 1159.34 L208.113 1159.34 L208.113 1163.28 L178.437 1163.28 L178.437 1159.34 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip530)\" d=\"M231.053 1145.69 L219.247 1164.13 L231.053 1164.13 L231.053 1145.69 M229.826 1141.61 L235.706 1141.61 L235.706 1164.13 L240.636 1164.13 L240.636 1168.02 L235.706 1168.02 L235.706 1176.17 L231.053 1176.17 L231.053 1168.02 L215.451 1168.02 L215.451 1163.51 L229.826 1141.61 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip530)\" d=\"M258.368 1144.69 Q254.756 1144.69 252.928 1148.26 Q251.122 1151.8 251.122 1158.93 Q251.122 1166.03 252.928 1169.6 Q254.756 1173.14 258.368 1173.14 Q262.002 1173.14 263.807 1169.6 Q265.636 1166.03 265.636 1158.93 Q265.636 1151.8 263.807 1148.26 Q262.002 1144.69 258.368 1144.69 M258.368 1140.99 Q264.178 1140.99 2
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"yLine = b2[1] .+ b2[2]*xGrid\n",
"\n",
"p1 = scatter( Rme,Re[:,2],\n",
" fillcolor = :blue,\n",
" legend = false,\n",
" xlim = (-40,40),\n",
" ylim = (-40,60),\n",
" title = \"Scatter plot: two monthly return series\",\n",
" titlefont = font(10),\n",
" xlabel = \"Market excess return, %\",\n",
" ylabel = \"Excess returns on large value stocks, %\",\n",
" guidefont = font(8) )\n",
"plot!([-40;60],[-40;60],color=:black,linewidth=0.5) #easier to keep this outside plot()\n",
"plot!(xGrid, yLine ,color=:red,linestyle=:dash) #easier to keep this outside plot()\n",
"display(p1)"
]
}
],
"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",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}