{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline\n", "from datetime import date\n", "from l1 import l1\n", "import os\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "from scipy import signal as sp_signal\n", "from sklearn import ensemble\n", "import statsmodels.api as sm\n", "\n", "plt.style.use('fivethirtyeight')\n", "\n", "_DATA_DIR = 'data'\n", "_FIG_DIR = 'figures'\n", "\n", "if not os.path.exists(_FIG_DIR):\n", " os.makedirs(_FIG_DIR)\n", "\n", "_SNP_500_PATH = os.path.join(_DATA_DIR, 'snp500.csv')\n", "_BEGINNING_DATE = date(2014, 1, 1)\n", "\n", "## Matplotlib Variables\n", "_FIG_SIZE = (16, 12)\n", "_FIG_FORMAT = 'png'\n", "_FIG_DPI = 200\n", "_FIG_LEGEND_LOCATION = 4\n", "\n", "_ORIGINAL_SIGNAL_LABEL = 'Original Signal'\n", "_MEAN_AVERAGE_SIGNAL_LABEL = 'Mean Averaged Signal'\n", "_MEDIAN_FILTERED_SIGNAL_LABEL = 'Median Filtered Signal'\n", "_EXPONENTIAL_SMOOTHED_AVERAGE_SIGNAL_LABEL = 'Exponential Moving Averaged Signal'\n", "_SNP_CYCLE_SIGNAL_LABEL = 'Cycle Signal'\n", "_SNP_TREND_SIGNAL_LABEL = 'Trend'\n", "_L1_TREND_SIGNAL_LABEL = '$l_1$ Trend'\n", "_BAND_PASS_FILTER_LABEL = 'Band-pas filtered'\n", "\n", "\n", "_BUTTERWORTH_FILTER_TITLE = 'Butterworth filter frequency response'\n", "_BUTTERWORTH_FILTER_XLABEL = 'Frequency [radians / second]'\n", "_BUTTERWORTH_FILTER_YLABEL = 'Amplitude [dB]'\n", "\n", "_MEAN_AVERAGE_SIGNAL_TITLE_TEMPLATE = 'Mean Average signal with window = {}'.format\n", "_MEDIAN_FILTER_SIGNAL_TITLE_TEMPLATE = 'Median Filtered signal with window = {}'.format\n", "_EXPONENTIAL_SMOOTHING_TITLE_TEMPLATE = 'Exponential Smoothed signal with span = {}'.format\n", "_HODRICK_PRESSCOTT_TITLE_TEMPLATE = 'Trend and Cycle Signal with smoothing parameter: {}'.format\n", "_L1_TITLE_TEMPLATE = '$l_1$ Trend Signal with regularizer: {}'.format\n", "_BAND_PASS_TITLE_TEMPLATE = 'Band-pass order: {}, low - high cutoff frequency: {} - {}'.format" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Data Explanation\n", "- The S&P 500, or the Standard & Poor's 500, is an American stock market index based on the market capitalizations of 500 large companies having common stock listed on the NYSE or NASDAQ. \n", "- The S&P 500 index components and their weightings are determined by S&P Dow Jones Indices. \n", "- It differs from other U.S. stock market indices, such as the Dow Jones Industrial Average or the Nasdaq Composite index, because of its diverse constituency and weighting methodology. \n", "- It is one of the most commonly followed equity indices, and many consider it one of the best representations of the U.S. stock market, and a bellwether for the U.S. economy. \n", "- The National Bureau of Economic Research has classified common stocks as a leading indicator of __business cycles__.\n", "\n", "Reference: http://www.albany.edu/cer/bc/bc_essays.html" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def _file_format(string_):\n", " string_ = string_.replace('-', '_').replace(' ', '_').replace('$', '')\n", " string_ += '.' + _FIG_FORMAT\n", " return string_" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df = pd.read_csv(_SNP_500_PATH, parse_dates=['Date'])\n", "df = df.sort(['Date'])\n", "df = df[df.Date > _BEGINNING_DATE]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | Date | \n", "Open | \n", "High | \n", "Low | \n", "Close | \n", "Volume | \n", "Adj Close | \n", "
---|---|---|---|---|---|---|---|
382 | \n", "2014-01-02 | \n", "1845.859985 | \n", "1845.859985 | \n", "1827.739990 | \n", "1831.979980 | \n", "3080600000 | \n", "1831.979980 | \n", "
381 | \n", "2014-01-03 | \n", "1833.209961 | \n", "1838.239990 | \n", "1829.130005 | \n", "1831.369995 | \n", "2774270000 | \n", "1831.369995 | \n", "
380 | \n", "2014-01-06 | \n", "1832.310059 | \n", "1837.160034 | \n", "1823.729980 | \n", "1826.770020 | \n", "3294850000 | \n", "1826.770020 | \n", "
379 | \n", "2014-01-07 | \n", "1828.709961 | \n", "1840.099976 | \n", "1828.709961 | \n", "1837.880005 | \n", "3511750000 | \n", "1837.880005 | \n", "
378 | \n", "2014-01-08 | \n", "1837.900024 | \n", "1840.020020 | \n", "1831.400024 | \n", "1837.489990 | \n", "3652140000 | \n", "1837.489990 | \n", "