{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Packages and Extra 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, Dates, Statistics, Distributions, XLSX\n", "\n", "include(\"jlFiles/printmat.jl\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "using Plots\n", "\n", "gr(size=(480,320))\n", "default(fmt = :svg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Loading Daily S&P 500 Data\n", "\n", "The data file contains some missing values indicated by the value -999.99. Do not use those observations when the S&P level is missing. \n", "\n", "1. Calculate daily returns (treat missing days as if they do not exist).\n", "\n", "2. Create a vector of dates (from the first column in the data set)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of days in the sample: 17864\n" ] } ], "source": [ "xx = XLSX.readxlsx(\"Data/PS08_Data.xlsx\") #reads a sheet from the xlsx file\n", "x = xx[\"SP500RfPs!A2:B17870\"] #pick out some cells\n", "\n", "...do the rest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 1\n", "\n", "1. Show a normalised histogram of the returns. Use the bins `-5:0.25:5`\n", "2. Add a curve for the density of the best fitting $N(\\mu,\\sigma)$ distribution." ] }, { "cell_type": "code", "execution_count": 4, "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" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(μ,σ) = (mean(R),std(R))\n", "\n", "xGrid = -5:0.25:5\n", "pdfX = pdf.(Normal(μ,σ),xGrid) #\"Distributions\" wants σ, not σ^2\n", "\n", "do the rest..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 2\n", "\n", "1. Calculate the quantile 0.01 in the data and according to the fitted normal distribution. Change the sign and call it \"99% Value at Risk\". Hint: `quantile(R,0.01)`and `quantile(Normal(μ,σ),0.01)`\n", "\n", "2. Calculate the expected return conditional on the return being less than the 5th quantile, both in data and according to the normal distribution. Change sign and call it \"expected shortfall\". Hint: `mean(TruncatedNormal(μ,σ,-Inf,quantile001))`\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 3\n", "\n", "Construct an simple estimate of $\\sigma_t^2$ as a backward looking exponential moving average\n", "\n", "$\\sigma_t^2 = \\lambda \\sigma_{t-1}^2 + (1-\\lambda) (R_{t-1} -\\mu_{t-1})^2$,\n", "where $\\mu_{t}=\\lambda \\mu_{t-1} + (1-\\lambda) R_{t-1}$ \n", "\n", "Use $\\lambda=0.94$\n", "\n", "Plot the standard deviation in a time series plot." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 4\n", "\n", "Calculate 99% VaR and ES according to time-varying normal distributions `Normal(μ[t],sqrt(σ²[t]))` and plot the results (as time series plots)." ] }, { "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 }