{ "cells": [ { "cell_type": "markdown", "id": "3dd07b01", "metadata": {}, "source": [ "# Demo #\n", "\n", "Below we show a simple process for fitting and projecting a financial time series using `phat`. This example will utilize end-of-day daily prices of Coca-Cola, for which there is data back to 1962. The process is as follows:\n", "\n", "+ download the daily prices of Coca-Cola (ticker: KO). Find the daily returns in percentage terms (i.e. x 100).\n", "+ use the `arch` package to fit a GARCH(1,1) model to the daily returns\n", "+ use the Hill double bootstrap method to estimate the tail index of both tails of the standardized residuals of the GARCH fit.\n", "+ use `phat` custom data class, `DataSplit`, to split the data into training, testing, and validation subsets. *Be careful to scale by 1/10.*\n", "+ use `PhatNet` and `phat`'s custom loss function `PhatLoss` to fit the remaining parameters.\n", "+ use `Garchcaster` to produce 10,000 simulations of a one-year forecast via the same AR-GARCH model." ] }, { "cell_type": "markdown", "id": "0ebb55fc", "metadata": {}, "source": [ "### Download Data ###" ] }, { "cell_type": "code", "execution_count": 4, "id": "ed6f4d1b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[*********************100%***********************] 1 of 1 completed\n" ] } ], "source": [ "import yfinance as yf\n", "import arch\n", "\n", "import phat as ph\n", "\n", "ko = yf.download('KO')\n", "ko_ret = ko.Close.pct_change().dropna()*100\n", "ko_ret = ko_ret[-252*10:]" ] }, { "cell_type": "markdown", "id": "3a89eefa", "metadata": {}, "source": [ "### Fit GARCH and Estimate $\\alpha$ in Both Tails ###" ] }, { "cell_type": "code", "execution_count": 5, "id": "ac7f3a9a", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "7ef9cbee09eb4c1ca7ad18e9b63f93dc", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/10 [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "res = arch.arch_model(ko_ret, mean='Constant', vol='Garch', p=1, q=1).fit(disp='off')\n", "xi_left, xi_right = ph.two_tailed_hill_double_bootstrap(res.std_resid)" ] }, { "cell_type": "markdown", "id": "d43cfa0d", "metadata": {}, "source": [ "### Fit $\\mu$ and $\\sigma$ with Machine Learning ###" ] }, { "cell_type": "code", "execution_count": 7, "id": "d94a2fb3", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 00046: early stopping\n" ] } ], "source": [ "data = ph.DataSplit(res.std_resid[2:]/10)\n", "pnet = ph.PhatNet(neurons=1)\n", "pnet.compile(\n", " loss = ph.PhatLoss(xi_left,xi_right), \n", " optimizer = 'adam'\n", ")\n", "history = pnet.fit(data.train, validation_data=data.test, epochs=100, verbose=0)" ] }, { "cell_type": "markdown", "id": "caf39ac3", "metadata": {}, "source": [ "The training process above results in the following estimated parameters for the standardized GARCH residuals." ] }, { "cell_type": "code", "execution_count": 8, "id": "c1e3ead3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | \n", " |
---|---|
mean | \n", "0.009800 | \n", "
sig | \n", "0.034620 | \n", "
shape_l | \n", "0.346772 | \n", "
shape_r | \n", "0.285840 | \n", "