{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "setup" ] }, "source": [ "(c) 2016 - present. Enplus Advisors, Inc." ] }, { "cell_type": "markdown", "metadata": { "tags": [ "setup" ] }, "source": [ "This module uses:\n", "* SP500 returns" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [ "setup" ] }, "outputs": [], "source": [ "import datetime as dt\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "pd.set_option('precision', 2)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "setup" ] }, "outputs": [], "source": [ "sp5_df = pd.read_csv(\n", " 'sp500.csv', usecols=['date', 'adj_close'], \n", " parse_dates=['date'])" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "exercise" ] }, "source": [ "**Exercise:**\n", "\n", "Create a `pandas` Timestamp for January 1st, 1993 16:00 (don't worry about timezone)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('1993-01-01 16:00:00')" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp('1993-01-01 16:00') # __" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "exercise" ] }, "source": [ "**Exercise:**\n", "\n", "Generate a an Index of:\n", "* 5 calendar days starting on January 1, 2010.\n", "* All US business days (weekdays) starting on January 1, 2010\n", " and ending on January 15, 2010.\n", " \n", "__Hint:__ You can view the help for a function by running `help(function_name)`, e.g. `help(pd.Timestamp)`. Try looking at the help for `pd.date_range`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2010-01-01', '2010-01-02', '2010-01-03', '2010-01-04',\n", " '2010-01-05'],\n", " dtype='datetime64[ns]', freq='D')" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(start='2010-01-01', periods=5, freq='D') # __" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2010-01-01', '2010-01-04', '2010-01-05', '2010-01-06',\n", " '2010-01-07', '2010-01-08', '2010-01-11', '2010-01-12',\n", " '2010-01-13', '2010-01-14', '2010-01-15'],\n", " dtype='datetime64[ns]', freq='B')" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.date_range(start='2010-01-01', end='2010-01-15', freq='B') # __" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "exercise" ] }, "source": [ "**Exercise:**\n", "\n", "Create a Series named `sp5` from the `adj_close` column `sp5_df`, using `date` as the\n", "index. Make sure you call `sort_index()` to make sure the index is sorted.\n", "\n", "__Hint:__ The first two parameters of `pd.Series` are `data` and `index`. When both `data` and `index` are `Series`, the `index` of `data` is aligned against the values in `Series`. You can always force positional alignment by converting a `Series` to an `PandasArray` (`pd.Series.array`)\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "sp5 = pd.Series( # __\n", " sp5_df.adj_close.array, index=sp5_df.date, \n", " name='adj_close').sort_index()" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "exercise" ] }, "source": [ "**Exercise:**\n", "\n", "Write 2 different ways to select January 3, 1995 from the `sp5` series. \n", "\n", "_There are more than 2 ways to do this, but you only need 2!_" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "d1a = sp5['19950103'] # __\n", "d1b = sp5['1995-01-03'] # __\n", "d1c = sp5[dt.datetime(1995, 1, 3)] # __" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "exercise" ] }, "source": [ "**Exercise:**\n", "\n", "Select from `sp5` all observations for:\n", "* March 1995\n", "* Year of 1995" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "mar_95 = sp5['1995-03'] # __" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "y_95 = sp5['1995'] # __" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "exercise" ] }, "source": [ "**Exercise**\n", "\n", "For `sp5`:\n", "\n", "Calculate the day-over-day percent change in the values and to the variable `sp5_rtn`.\n", "\n", "Hint: Use `shift`" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "sp5_rtn = sp5 / sp5.shift(1) - 1 # __" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# alternative solution\n", "# sp5.pct_change()" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "exercise" ] }, "source": [ "**Exercise**\n", "\n", "Resample the data from daily to monthly to calculate average 1-day returns." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "rtn_mnth = sp5_rtn.resample('M').mean() # __" ] } ], "metadata": { "celltoolbar": "Tags", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }