{ "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": "\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" }, "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": "\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" }, "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 }