ml-finance-python

python scripts for finance machine learning

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

notebook.ipynb

(8917B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "markdown",
      5    "metadata": {
      6     "deletable": true,
      7     "editable": true
      8    },
      9    "source": [
     10     "# Exercises: Variance\n",
     11     "By Christopher van Hoecke, Maxwell Margenot, and Delaney Mackenzie\n",
     12     "\n",
     13     "## Lecture Link :\n",
     14     "https://www.quantopian.com/lectures/variance\n",
     15     "\n",
     16     "### IMPORTANT NOTE: \n",
     17     "This lecture corresponds to the Variance 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",
     18     "\n",
     19     "Part of the Quantopian Lecture Series:\n",
     20     "\n",
     21     "* [www.quantopian.com/lectures](https://www.quantopian.com/lectures)\n",
     22     "* [github.com/quantopian/research_public](https://github.com/quantopian/research_public)\n",
     23     "\n",
     24     "-----"
     25    ]
     26   },
     27   {
     28    "cell_type": "code",
     29    "execution_count": null,
     30    "metadata": {
     31     "collapsed": true,
     32     "deletable": true,
     33     "editable": true
     34    },
     35    "outputs": [],
     36    "source": [
     37     "# Useful Libraries\n",
     38     "import pandas as pd\n",
     39     "import numpy as np\n",
     40     "import matplotlib.pyplot as plt"
     41    ]
     42   },
     43   {
     44    "cell_type": "markdown",
     45    "metadata": {
     46     "deletable": true,
     47     "editable": true
     48    },
     49    "source": [
     50     "#### Data:"
     51    ]
     52   },
     53   {
     54    "cell_type": "code",
     55    "execution_count": null,
     56    "metadata": {
     57     "collapsed": true,
     58     "deletable": true,
     59     "editable": true
     60    },
     61    "outputs": [],
     62    "source": [
     63     "X = np.random.randint(100, size = 100)"
     64    ]
     65   },
     66   {
     67    "cell_type": "markdown",
     68    "metadata": {
     69     "deletable": true,
     70     "editable": true
     71    },
     72    "source": [
     73     "---"
     74    ]
     75   },
     76   {
     77    "cell_type": "markdown",
     78    "metadata": {
     79     "deletable": true,
     80     "editable": true
     81    },
     82    "source": [
     83     "# Exercise 1: \n",
     84     "Using the skills aquired in the lecture series, find the following parameters of the list X above:\n",
     85     "- Range\n",
     86     "- Mean Absolute Deviation\n",
     87     "- Variance and Standard Deviation\n",
     88     "- Semivariance and Semideviation\n",
     89     "- Target variance (with B = 60)"
     90    ]
     91   },
     92   {
     93    "cell_type": "code",
     94    "execution_count": null,
     95    "metadata": {
     96     "collapsed": false,
     97     "deletable": true,
     98     "editable": true
     99    },
    100    "outputs": [],
    101    "source": [
    102     "# Range of X\n",
    103     "range_X =\n",
    104     "\n",
    105     "## Your code goes here\n",
    106     "print 'Range of X: %s' %(range_X)"
    107    ]
    108   },
    109   {
    110    "cell_type": "code",
    111    "execution_count": null,
    112    "metadata": {
    113     "collapsed": false,
    114     "deletable": true,
    115     "editable": true
    116    },
    117    "outputs": [],
    118    "source": [
    119     "# Mean Absolute Deviation\n",
    120     "# First calculate the value of mu (the mean)\n",
    121     "\n",
    122     "mu = np.mean(X)\n",
    123     "\n",
    124     "## Your code goes here\n",
    125     "print 'Mean absolute deviation of X:', MAD"
    126    ]
    127   },
    128   {
    129    "cell_type": "code",
    130    "execution_count": null,
    131    "metadata": {
    132     "collapsed": false,
    133     "deletable": true,
    134     "editable": true
    135    },
    136    "outputs": [],
    137    "source": [
    138     "# Variance and standard deviation\n",
    139     "\n",
    140     "## Your code goes here\n",
    141     "\n",
    142     "print 'Variance of X:',\n",
    143     "print 'Standard deviation of X:',"
    144    ]
    145   },
    146   {
    147    "cell_type": "code",
    148    "execution_count": null,
    149    "metadata": {
    150     "collapsed": false,
    151     "deletable": true,
    152     "editable": true
    153    },
    154    "outputs": [],
    155    "source": [
    156     "# Semivariance and semideviation\n",
    157     "\n",
    158     "## Your code goes here\n",
    159     "\n",
    160     "print 'Semivariance of X:', \n",
    161     "print 'Semideviation of X:', "
    162    ]
    163   },
    164   {
    165    "cell_type": "code",
    166    "execution_count": null,
    167    "metadata": {
    168     "collapsed": false,
    169     "deletable": true,
    170     "editable": true
    171    },
    172    "outputs": [],
    173    "source": [
    174     "# Target variance\n",
    175     "\n",
    176     "## Your code goes here\n",
    177     "\n",
    178     "print 'Target semivariance of X:',\n",
    179     "print 'Target semideviation of X:', "
    180    ]
    181   },
    182   {
    183    "cell_type": "markdown",
    184    "metadata": {
    185     "deletable": true,
    186     "editable": true
    187    },
    188    "source": [
    189     "---"
    190    ]
    191   },
    192   {
    193    "cell_type": "markdown",
    194    "metadata": {
    195     "deletable": true,
    196     "editable": true
    197    },
    198    "source": [
    199     "# Exercise 2:\n",
    200     "Using the skills aquired in the lecture series, find the following parameters of prices for AT&T stock over a year:\n",
    201     "- 30 days rolling variance \n",
    202     "- 15 days rolling Standard Deviation"
    203    ]
    204   },
    205   {
    206    "cell_type": "code",
    207    "execution_count": null,
    208    "metadata": {
    209     "collapsed": false,
    210     "deletable": true,
    211     "editable": true
    212    },
    213    "outputs": [],
    214    "source": [
    215     "att = get_pricing('T', fields='open_price', start_date='2016-01-01', end_date='2017-01-01')"
    216    ]
    217   },
    218   {
    219    "cell_type": "code",
    220    "execution_count": null,
    221    "metadata": {
    222     "collapsed": false,
    223     "deletable": true,
    224     "editable": true
    225    },
    226    "outputs": [],
    227    "source": [
    228     "# Rolling mean\n",
    229     "\n",
    230     "## Your code goes here"
    231    ]
    232   },
    233   {
    234    "cell_type": "code",
    235    "execution_count": null,
    236    "metadata": {
    237     "collapsed": true,
    238     "deletable": true,
    239     "editable": true
    240    },
    241    "outputs": [],
    242    "source": [
    243     "# Rolling standard deviation\n",
    244     "\n",
    245     "## Your code goes here"
    246    ]
    247   },
    248   {
    249    "cell_type": "markdown",
    250    "metadata": {
    251     "deletable": true,
    252     "editable": true
    253    },
    254    "source": [
    255     "---"
    256    ]
    257   },
    258   {
    259    "cell_type": "markdown",
    260    "metadata": {
    261     "deletable": true,
    262     "editable": true
    263    },
    264    "source": [
    265     "# Exercise 3 : \n",
    266     "The portfolio variance is calculated as\n",
    267     "\n",
    268     "$$\\text{VAR}_p = \\text{VAR}_{s1} (w_1^2) + \\text{VAR}_{s2}(w_2^2) + \\text{COV}_{S_1, S_2} (2 w_1 w_2)$$\n",
    269     "\n",
    270     "Where $w_1$ and $w_2$ are the weights of $S_1$ and $S_2$.\n",
    271     "\n",
    272     "Find values of $w_1$ and $w_2$ to have a portfolio variance of 50. "
    273    ]
    274   },
    275   {
    276    "cell_type": "code",
    277    "execution_count": null,
    278    "metadata": {
    279     "collapsed": false,
    280     "deletable": true,
    281     "editable": true
    282    },
    283    "outputs": [],
    284    "source": [
    285     "asset1 = get_pricing('AAPL', fields='open_price', start_date='2016-01-01', end_date='2017-01-01')\n",
    286     "asset2 = get_pricing('XLF', fields='open_price', start_date='2016-01-01', end_date='2017-01-01')\n",
    287     "\n",
    288     "cov = np.cov(asset1, asset2)[0,1]\n",
    289     "\n",
    290     "w1 = ## Your code goes here.\n",
    291     "w2 = 1 - w1\n",
    292     "\n",
    293     "v1 = np.var(asset1)\n",
    294     "v2 = np.var(asset2)\n",
    295     "\n",
    296     "pvariance = (w1**2)*v1+(w2**2)*v2+(2*w1*w2)*cov\n",
    297     "\n",
    298     "print 'Portfolio variance: ', pvariance"
    299    ]
    300   },
    301   {
    302    "cell_type": "markdown",
    303    "metadata": {
    304     "deletable": true,
    305     "editable": true
    306    },
    307    "source": [
    308     "---\n",
    309     "\n",
    310     "Congratulations on completing the Variance exercises!\n",
    311     "\n",
    312     "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",
    313     "\n",
    314     "Start by going through the [Writing a Contest Algorithm](https://www.quantopian.com/tutorials/contest) tutorial."
    315    ]
    316   },
    317   {
    318    "cell_type": "markdown",
    319    "metadata": {
    320     "deletable": true,
    321     "editable": true
    322    },
    323    "source": [
    324     "*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.*"
    325    ]
    326   }
    327  ],
    328  "metadata": {
    329   "kernelspec": {
    330    "display_name": "Python 2",
    331    "language": "python",
    332    "name": "python2"
    333   },
    334   "language_info": {
    335    "codemirror_mode": {
    336     "name": "ipython",
    337     "version": 2
    338    },
    339    "file_extension": ".py",
    340    "mimetype": "text/x-python",
    341    "name": "python",
    342    "nbconvert_exporter": "python",
    343    "pygments_lexer": "ipython2",
    344    "version": "2.7.12"
    345   }
    346  },
    347  "nbformat": 4,
    348  "nbformat_minor": 2
    349 }