ml-finance-python

python scripts for finance machine learning

git clone https://9o.is/git/ml-finance-python.git

notebook.ipynb

(11227B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "markdown",
      5    "metadata": {
      6     "deletable": true,
      7     "editable": true
      8    },
      9    "source": [
     10     "# Exercises: Linear Correlation Analysis\n",
     11     "\n",
     12     "## Lecture Link : \n",
     13     "https://www.quantopian.com/lectures/linear-correlation-analysis\n",
     14     "\n",
     15     "###IMPORTANT NOTE: \n",
     16     "This lecture corresponds to the Linear Correlation Analysis lecture, which is part of the Quantopian lecture series. This homework expects you to rely heavily on the code presented in the corresponding lecture. Please copy and paste regularly from that lecture when starting to work on the problems, as trying to do them from scratch will likely be too difficult.\n",
     17     "\n",
     18     "Part of the Quantopian Lecture Series:\n",
     19     "\n",
     20     "* [www.quantopian.com/lectures](https://www.quantopian.com/lectures)\n",
     21     "* [github.com/quantopian/research_public](https://github.com/quantopian/research_public)\n",
     22     "\n",
     23     "----"
     24    ]
     25   },
     26   {
     27    "cell_type": "markdown",
     28    "metadata": {
     29     "deletable": true,
     30     "editable": true
     31    },
     32    "source": [
     33     "## Key Concepts"
     34    ]
     35   },
     36   {
     37    "cell_type": "code",
     38    "execution_count": null,
     39    "metadata": {
     40     "collapsed": true,
     41     "deletable": true,
     42     "editable": true
     43    },
     44    "outputs": [],
     45    "source": [
     46     "# Useful Functions\n",
     47     "def find_most_correlated(data):\n",
     48     "    n = data.shape[1]\n",
     49     "    keys = data.keys()\n",
     50     "    pair = []\n",
     51     "    max_value = 0\n",
     52     "    for i in range(n):\n",
     53     "        for j in range(i+1, n):\n",
     54     "            S1 = data[keys[i]]\n",
     55     "            S2 = data[keys[j]]\n",
     56     "            result = np.corrcoef(S1, S2)[0,1]\n",
     57     "            if result > max_value:\n",
     58     "                pair = (keys[i], keys[j])\n",
     59     "                max_value = result\n",
     60     "    return pair, max_value"
     61    ]
     62   },
     63   {
     64    "cell_type": "code",
     65    "execution_count": null,
     66    "metadata": {
     67     "collapsed": false,
     68     "deletable": true,
     69     "editable": true
     70    },
     71    "outputs": [],
     72    "source": [
     73     "# Useful Libraries\n",
     74     "import numpy as np\n",
     75     "import pandas as pd\n",
     76     "import matplotlib.pyplot as plt"
     77    ]
     78   },
     79   {
     80    "cell_type": "markdown",
     81    "metadata": {
     82     "deletable": true,
     83     "editable": true
     84    },
     85    "source": [
     86     "---"
     87    ]
     88   },
     89   {
     90    "cell_type": "markdown",
     91    "metadata": {
     92     "deletable": true,
     93     "editable": true
     94    },
     95    "source": [
     96     "# Exercise 1: Dependence of Artificial Variables\n",
     97     "\n",
     98     "## a. Finding Variance, Covariance, and Correlation I\n",
     99     "\n",
    100     "By reading the matrix output from the `np.cov()` and `np.corrcoef()` functions, find the variance of the variables $A$ and $B$ and the covariance and correlation of their relationship. "
    101    ]
    102   },
    103   {
    104    "cell_type": "code",
    105    "execution_count": null,
    106    "metadata": {
    107     "collapsed": false,
    108     "deletable": true,
    109     "editable": true
    110    },
    111    "outputs": [],
    112    "source": [
    113     "A = np.random.rand(100)\n",
    114     "B = -3 * A + np.random.exponential(0.05, 100)\n",
    115     "\n",
    116     "#Your code goes here"
    117    ]
    118   },
    119   {
    120    "cell_type": "markdown",
    121    "metadata": {
    122     "deletable": true,
    123     "editable": true
    124    },
    125    "source": [
    126     "## b. Finding Variance, Covariance, and Correlation II\n",
    127     "\n",
    128     "By reading the matrix output from the `np.cov()` and `np.corrcoef()` functions, find the variance of the variables $C$ and $D$ and the covariance and correlation of their relationship. "
    129    ]
    130   },
    131   {
    132    "cell_type": "code",
    133    "execution_count": null,
    134    "metadata": {
    135     "collapsed": false,
    136     "deletable": true,
    137     "editable": true
    138    },
    139    "outputs": [],
    140    "source": [
    141     "C = np.random.rand(100)\n",
    142     "D = np.random.normal(0, 0.5, 100)\n",
    143     "\n",
    144     "#Your code goes here"
    145    ]
    146   },
    147   {
    148    "cell_type": "markdown",
    149    "metadata": {
    150     "deletable": true,
    151     "editable": true
    152    },
    153    "source": [
    154     "----"
    155    ]
    156   },
    157   {
    158    "cell_type": "markdown",
    159    "metadata": {
    160     "deletable": true,
    161     "editable": true
    162    },
    163    "source": [
    164     "# Exercise 2: Constructing Example Relationships\n",
    165     "\n",
    166     "## a. Positive Correlation Example\n",
    167     "\n",
    168     "Construct a variable $Y$ which has a strong, but not perfect, positive correlation with $X$ $(0.9 < Corr(X,Y) < 1)$, and plot their relationship."
    169    ]
    170   },
    171   {
    172    "cell_type": "code",
    173    "execution_count": null,
    174    "metadata": {
    175     "collapsed": false,
    176     "deletable": true,
    177     "editable": true
    178    },
    179    "outputs": [],
    180    "source": [
    181     "X = np.random.rand(100)\n",
    182     "\n",
    183     "#Your code goes here"
    184    ]
    185   },
    186   {
    187    "cell_type": "markdown",
    188    "metadata": {
    189     "deletable": true,
    190     "editable": true
    191    },
    192    "source": [
    193     "## b. Negative Correlation Example\n",
    194     "\n",
    195     "Construct a variable $W$ which has a weak, negative correlation with $Z$ $(-0.3 < Corr(Z,W) < 0)$, and plot their relationship."
    196    ]
    197   },
    198   {
    199    "cell_type": "code",
    200    "execution_count": null,
    201    "metadata": {
    202     "collapsed": false,
    203     "deletable": true,
    204     "editable": true
    205    },
    206    "outputs": [],
    207    "source": [
    208     "Z = np.random.rand(100)\n",
    209     "\n",
    210     "#Your code goes here"
    211    ]
    212   },
    213   {
    214    "cell_type": "markdown",
    215    "metadata": {
    216     "deletable": true,
    217     "editable": true
    218    },
    219    "source": [
    220     "----"
    221    ]
    222   },
    223   {
    224    "cell_type": "markdown",
    225    "metadata": {
    226     "deletable": true,
    227     "editable": true
    228    },
    229    "source": [
    230     "# Exercise 3: Correlation of Real Assets\n",
    231     "\n",
    232     "## a. Finding Correlation of Real Assets\n",
    233     "\n",
    234     "Find the correlation between the stocks OKE and LAKE. Also check how they correlate with the provided benchmark."
    235    ]
    236   },
    237   {
    238    "cell_type": "code",
    239    "execution_count": null,
    240    "metadata": {
    241     "collapsed": false,
    242     "deletable": true,
    243     "editable": true
    244    },
    245    "outputs": [],
    246    "source": [
    247     "OKE = get_pricing('OKE', fields='price', start_date='2013-01-01', end_date='2015-01-01')\n",
    248     "LAKE = get_pricing('LAKE', fields='price', start_date='2013-01-01', end_date='2015-01-01')\n",
    249     "benchmark = get_pricing('SPY', fields='price', start_date='2013-01-01', end_date='2015-01-01')\n",
    250     "\n",
    251     "#Your code goes here"
    252    ]
    253   },
    254   {
    255    "cell_type": "markdown",
    256    "metadata": {
    257     "deletable": true,
    258     "editable": true
    259    },
    260    "source": [
    261     "## b. Finding Correlated Pairs\n",
    262     "\n",
    263     "Find the most correlated pair of stocks in the following portfolio using 2015 pricing data and the `find_most_correlated` function defined in the Helper Functions section above."
    264    ]
    265   },
    266   {
    267    "cell_type": "code",
    268    "execution_count": null,
    269    "metadata": {
    270     "collapsed": false,
    271     "deletable": true,
    272     "editable": true
    273    },
    274    "outputs": [],
    275    "source": [
    276     "symbol_list = ['GSK', 'SNOW', 'FB', 'AZO', 'XEC', 'AMZN']\n",
    277     "data = get_pricing(symbol_list, fields=['price']\n",
    278     "                               , start_date='2015-01-01', end_date='2016-01-01')['price']\n",
    279     "data.columns = symbol_list\n",
    280     "\n",
    281     "#Your code goes here"
    282    ]
    283   },
    284   {
    285    "cell_type": "markdown",
    286    "metadata": {
    287     "deletable": true,
    288     "editable": true
    289    },
    290    "source": [
    291     "----"
    292    ]
    293   },
    294   {
    295    "cell_type": "markdown",
    296    "metadata": {
    297     "deletable": true,
    298     "editable": true
    299    },
    300    "source": [
    301     "# Exercise 4: Limitations of Correlation\n",
    302     "\n",
    303     "## a. Out of Sample Tests\n",
    304     "\n",
    305     "Using pricing data from the first half of 2016, find the correlation coefficient between FB and AMZN and compare it to the strong positive relationship predicted from the 2015 correlation coefficient to see if that result holds."
    306    ]
    307   },
    308   {
    309    "cell_type": "code",
    310    "execution_count": null,
    311    "metadata": {
    312     "collapsed": false,
    313     "deletable": true,
    314     "editable": true
    315    },
    316    "outputs": [],
    317    "source": [
    318     "FB_15 = get_pricing('FB', fields='price', start_date='2015-01-01', end_date='2016-01-01')\n",
    319     "AMZN_15 = get_pricing('AMZN', fields='price', start_date='2015-01-01', end_date='2016-01-01')\n",
    320     "FB_16 = get_pricing('FB', fields='price', start_date='2016-01-01', end_date='2016-07-01')\n",
    321     "AMZN_16 = get_pricing('AMZN', fields='price', start_date='2016-01-01', end_date='2016-07-01')\n",
    322     "\n",
    323     "#Your code goes here"
    324    ]
    325   },
    326   {
    327    "cell_type": "markdown",
    328    "metadata": {
    329     "deletable": true,
    330     "editable": true
    331    },
    332    "source": [
    333     "## b. Rolling Correlation\n",
    334     "\n",
    335     "Plot the 60-day rolling correlation coefficient between FB and AMZN to make a conclusion about the stability of their relationship."
    336    ]
    337   },
    338   {
    339    "cell_type": "code",
    340    "execution_count": null,
    341    "metadata": {
    342     "collapsed": false,
    343     "deletable": true,
    344     "editable": true
    345    },
    346    "outputs": [],
    347    "source": [
    348     "FB = get_pricing('FB', fields='price', start_date='2015-01-01', end_date='2017-01-01')\n",
    349     "AMZN = get_pricing('AMZN', fields='price', start_date='2015-01-01', end_date='2017-01-01')\n",
    350     "\n",
    351     "#Your code goes here"
    352    ]
    353   },
    354   {
    355    "cell_type": "markdown",
    356    "metadata": {
    357     "deletable": true,
    358     "editable": true
    359    },
    360    "source": [
    361     "Congratulations on completing the Linear Correlation Analysis exercises!\n",
    362     "\n",
    363     "As you learn more about writing trading models and the Quantopian platform, enter a daily [Quantopian Contest](https://www.quantopian.com/contest). Your strategy will be evaluated for a cash prize every day.\n",
    364     "\n",
    365     "Start by going through the [Writing a Contest Algorithm](https://www.quantopian.com/tutorials/contest) tutorial."
    366    ]
    367   },
    368   {
    369    "cell_type": "markdown",
    370    "metadata": {
    371     "deletable": true,
    372     "editable": true
    373    },
    374    "source": [
    375     "*This presentation is for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation for any security; nor does it constitute an offer to provide investment advisory or other services by Quantopian, Inc. (\"Quantopian\"). Nothing contained herein constitutes investment advice or offers any opinion with respect to the suitability of any security, and any views expressed herein should not be taken as advice to buy, sell, or hold any security or as an endorsement of any security or company.  In preparing the information contained herein, Quantopian, Inc. has not taken into account the investment needs, objectives, and financial circumstances of any particular investor. Any views expressed and data illustrated herein were prepared based upon information, believed to be reliable, available to Quantopian, Inc. at the time of publication. Quantopian makes no guarantees as to their accuracy or completeness. All information is subject to change and may quickly become unreliable for various reasons, including changes in market conditions or economic circumstances.*"
    376    ]
    377   }
    378  ],
    379  "metadata": {
    380   "kernelspec": {
    381    "display_name": "Python 2",
    382    "language": "python",
    383    "name": "python2"
    384   },
    385   "language_info": {
    386    "codemirror_mode": {
    387     "name": "ipython",
    388     "version": 2
    389    },
    390    "file_extension": ".py",
    391    "mimetype": "text/x-python",
    392    "name": "python",
    393    "nbconvert_exporter": "python",
    394    "pygments_lexer": "ipython2",
    395    "version": "2.7.12"
    396   }
    397  },
    398  "nbformat": 4,
    399  "nbformat_minor": 0
    400 }