ml-finance-python

python scripts for finance machine learning

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

notebook.ipynb

(12917B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "markdown",
      5    "metadata": {
      6     "deletable": true,
      7     "editable": true
      8    },
      9    "source": [
     10     "# Exercises: Random Variables\n",
     11     "By Christopher van Hoecke, Max Margenot, and Delaney Mackenzie\n",
     12     "\n",
     13     "## Lecture Link : \n",
     14     "https://www.quantopian.com/lectures/random-variables\n",
     15     "\n",
     16     "### IMPORTANT NOTE: \n",
     17     "This lecture corresponds to the Random Variables 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": "markdown",
     29    "metadata": {
     30     "deletable": true,
     31     "editable": true
     32    },
     33    "source": [
     34     "## Key Concepts"
     35    ]
     36   },
     37   {
     38    "cell_type": "code",
     39    "execution_count": null,
     40    "metadata": {
     41     "collapsed": true,
     42     "deletable": true,
     43     "editable": true
     44    },
     45    "outputs": [],
     46    "source": [
     47     "# Useful Functions\n",
     48     "class DiscreteRandomVariable:\n",
     49     "    def __init__(self, a=0, b=1):\n",
     50     "        self.variableType = \"\"\n",
     51     "        self.low = a\n",
     52     "        self.high = b\n",
     53     "        return\n",
     54     "    def draw(self, numberOfSamples):\n",
     55     "        samples = np.random.randint(self.low, self.high, numberOfSamples)\n",
     56     "        return samples\n",
     57     "    \n",
     58     "class BinomialRandomVariable(DiscreteRandomVariable):\n",
     59     "    def __init__(self, numberOfTrials = 10, probabilityOfSuccess = 0.5):\n",
     60     "        self.variableType = \"Binomial\"\n",
     61     "        self.numberOfTrials = numberOfTrials\n",
     62     "        self.probabilityOfSuccess = probabilityOfSuccess\n",
     63     "        return\n",
     64     "    def draw(self, numberOfSamples):\n",
     65     "        samples = np.random.binomial(self.numberOfTrials, self.probabilityOfSuccess, numberOfSamples)\n",
     66     "        return samples\n",
     67     "    \n",
     68     "def factorial(n):return reduce(lambda x,y:x*y,[1]+range(1,n+1))"
     69    ]
     70   },
     71   {
     72    "cell_type": "code",
     73    "execution_count": null,
     74    "metadata": {
     75     "collapsed": false,
     76     "deletable": true,
     77     "editable": true
     78    },
     79    "outputs": [],
     80    "source": [
     81     "# Useful Libraries\n",
     82     "import pandas as pd\n",
     83     "import numpy as np\n",
     84     "import matplotlib.pyplot as plt\n",
     85     "import statsmodels.stats as stats\n",
     86     "from statsmodels.stats import stattools\n",
     87     "from __future__ import division"
     88    ]
     89   },
     90   {
     91    "cell_type": "markdown",
     92    "metadata": {
     93     "deletable": true,
     94     "editable": true
     95    },
     96    "source": [
     97     "---"
     98    ]
     99   },
    100   {
    101    "cell_type": "markdown",
    102    "metadata": {
    103     "collapsed": true,
    104     "deletable": true,
    105     "editable": true
    106    },
    107    "source": [
    108     "# Exercise 1 : Uniform Distribution\n",
    109     "- Plot the histogram of 10 tosses with a fair coin (let 1 be heads and 2 be tails).  \n",
    110     "- Plot the histogram of 1000000 tosses of a fair coin "
    111    ]
    112   },
    113   {
    114    "cell_type": "code",
    115    "execution_count": null,
    116    "metadata": {
    117     "collapsed": false,
    118     "deletable": true,
    119     "editable": true
    120    },
    121    "outputs": [],
    122    "source": [
    123     "# Histograms with 10 tosses. \n",
    124     "\n",
    125     "## Your code goes here\n",
    126     "\n",
    127     "plt.xlabel('Value')\n",
    128     "plt.ylabel('Occurences')\n",
    129     "plt.legend(['Coin Tosses']);"
    130    ]
    131   },
    132   {
    133    "cell_type": "code",
    134    "execution_count": null,
    135    "metadata": {
    136     "collapsed": false,
    137     "deletable": true,
    138     "editable": true
    139    },
    140    "outputs": [],
    141    "source": [
    142     "# Histograms with 1000000 tosses. \n",
    143     "\n",
    144     "## Your code goes here\n",
    145     "\n",
    146     "plt.xlabel('Value')\n",
    147     "plt.ylabel('Occurences')\n",
    148     "plt.legend(['Coin Tosses']);"
    149    ]
    150   },
    151   {
    152    "cell_type": "markdown",
    153    "metadata": {
    154     "deletable": true,
    155     "editable": true
    156    },
    157    "source": [
    158     "---"
    159    ]
    160   },
    161   {
    162    "cell_type": "markdown",
    163    "metadata": {
    164     "deletable": true,
    165     "editable": true
    166    },
    167    "source": [
    168     "# Exercise 2 :  Binomial Distributions.\n",
    169     "- Graph the histogram of 1000000 samples from a binomial distribution of probability 0.25 and $n = 20$\n",
    170     "- Find the value that occurs the most often\n",
    171     "- Calculate the probability of the value that occurs the most often occurring. *Use the factorial(x) function to find factorials*"
    172    ]
    173   },
    174   {
    175    "cell_type": "code",
    176    "execution_count": null,
    177    "metadata": {
    178     "collapsed": false,
    179     "deletable": true,
    180     "editable": true
    181    },
    182    "outputs": [],
    183    "source": [
    184     "# Binomial distribution with p=0.25 and n=20\n",
    185     "\n",
    186     "## Your code goes here. \n",
    187     "\n",
    188     "plt.title('Binomial Distributino with p=0.25 and n=20')\n",
    189     "plt.xlabel('Value')\n",
    190     "plt.ylabel('Occurences')\n",
    191     "plt.legend(['Die Rolls']);"
    192    ]
    193   },
    194   {
    195    "cell_type": "code",
    196    "execution_count": null,
    197    "metadata": {
    198     "collapsed": false,
    199     "deletable": true,
    200     "editable": true,
    201     "scrolled": false
    202    },
    203    "outputs": [],
    204    "source": [
    205     "# Finding x which occurs most often\n",
    206     "\n",
    207     "## Your code goes here"
    208    ]
    209   },
    210   {
    211    "cell_type": "code",
    212    "execution_count": null,
    213    "metadata": {
    214     "collapsed": false,
    215     "deletable": true,
    216     "editable": true
    217    },
    218    "outputs": [],
    219    "source": [
    220     "# Calculating the probability of finding x. \n",
    221     "\n",
    222     "## Your code goes here"
    223    ]
    224   },
    225   {
    226    "cell_type": "markdown",
    227    "metadata": {
    228     "deletable": true,
    229     "editable": true
    230    },
    231    "source": [
    232     "---"
    233    ]
    234   },
    235   {
    236    "cell_type": "markdown",
    237    "metadata": {
    238     "deletable": true,
    239     "editable": true
    240    },
    241    "source": [
    242     "# Exercise 3 : Normal Distributions\n",
    243     "## a. Graphing\n",
    244     "Graph a normal distribution using the Probability Density Function bellow, with a mean of 0 and standard deviation of 5. \n",
    245     "\n",
    246     "$$f(x) = \\frac{1}{\\sigma\\sqrt{2\\pi}}e^{-\\frac{(x - \\mu)^2}{2\\sigma^2}}$$"
    247    ]
    248   },
    249   {
    250    "cell_type": "code",
    251    "execution_count": null,
    252    "metadata": {
    253     "collapsed": false,
    254     "deletable": true,
    255     "editable": true
    256    },
    257    "outputs": [],
    258    "source": [
    259     "# Graphin a normal distribution pdf. \n",
    260     "\n",
    261     "## Your code goes here\n",
    262     "\n",
    263     "mu = \n",
    264     "sigma = \n",
    265     "x = np.linspace(-30, 30, 200)\n",
    266     "y = \n",
    267     "plt.plot(x, y)\n",
    268     "plt.title('Graph of PDF with mu = 0 and sigma = 5')\n",
    269     "plt.xlabel('Value')\n",
    270     "plt.ylabel('Probability');"
    271    ]
    272   },
    273   {
    274    "cell_type": "markdown",
    275    "metadata": {
    276     "deletable": true,
    277     "editable": true
    278    },
    279    "source": [
    280     "## b. Confidence Intervals. \n",
    281     "- Calculate the first, second, and third confidence intervals. \n",
    282     "- Plot the PDF and the first, second, and third confidence intervals. "
    283    ]
    284   },
    285   {
    286    "cell_type": "code",
    287    "execution_count": null,
    288    "metadata": {
    289     "collapsed": false,
    290     "deletable": true,
    291     "editable": true
    292    },
    293    "outputs": [],
    294    "source": [
    295     "# finding the 1st, 2nd, and third confidence intervals. \n",
    296     "\n",
    297     "## Your code goes here\n",
    298     "\n",
    299     "first_ci =\n",
    300     "second_ci =\n",
    301     "third_ci = \n",
    302     "\n",
    303     "print '1-sigma -> mu +/-', sigma\n",
    304     "print '2-sigma -> mu +/-', second_ci[1]\n",
    305     "print '3-sigma -> mu +/-', third_ci[1]"
    306    ]
    307   },
    308   {
    309    "cell_type": "code",
    310    "execution_count": null,
    311    "metadata": {
    312     "collapsed": true,
    313     "deletable": true,
    314     "editable": true
    315    },
    316    "outputs": [],
    317    "source": [
    318     "## Graphing.\n",
    319     "\n",
    320     "## Your code goes here. \n",
    321     "\n",
    322     "plt.title('Graph of PDF with 3 confidence intervals.')\n",
    323     "plt.legend();"
    324    ]
    325   },
    326   {
    327    "cell_type": "markdown",
    328    "metadata": {
    329     "deletable": true,
    330     "editable": true
    331    },
    332    "source": [
    333     "---"
    334    ]
    335   },
    336   {
    337    "cell_type": "markdown",
    338    "metadata": {
    339     "deletable": true,
    340     "editable": true
    341    },
    342    "source": [
    343     "# Exercise 4: Financial Applications: \n",
    344     "Fit the returns of SPY from 2016-01-01 to 2016-05-01 to a normal distribution. \n",
    345     "- Fit the returns to a normal distribution by clacluating the values of $\\mu$ and $\\sigma$\n",
    346     "- Plot the returns and the distribution, along with 3 confidence intervals. \n",
    347     "- Use the Jarque-Bera test to check for normality. "
    348    ]
    349   },
    350   {
    351    "cell_type": "code",
    352    "execution_count": null,
    353    "metadata": {
    354     "collapsed": false,
    355     "deletable": true,
    356     "editable": true
    357    },
    358    "outputs": [],
    359    "source": [
    360     "# Collect prices and retursn. \n",
    361     "prices = get_pricing('SPY', start_date = '2016-01-01', end_date='2016-05-01', \n",
    362     "                     fields = 'price')\n",
    363     "returns = prices.pct_change()[1:]"
    364    ]
    365   },
    366   {
    367    "cell_type": "code",
    368    "execution_count": null,
    369    "metadata": {
    370     "collapsed": false,
    371     "deletable": true,
    372     "editable": true
    373    },
    374    "outputs": [],
    375    "source": [
    376     "# Calculating the mean and standard deviation. \n",
    377     "\n",
    378     "## Your code goes here\n",
    379     "sample_mean =\n",
    380     "sample_std_dev = \n",
    381     "\n",
    382     "x = np.linspace(-(sample_mean + 4 * sample_std_dev), (sample_mean + 4 * sample_std_dev), len(returns))\n",
    383     "sample_distribution = ((1/(sample_std_dev * 2 * np.pi)) * \n",
    384     "                       np.exp(-(x - sample_mean)*(x - sample_mean) / (2 * sample_std_dev * sample_std_dev)))"
    385    ]
    386   },
    387   {
    388    "cell_type": "code",
    389    "execution_count": null,
    390    "metadata": {
    391     "collapsed": false,
    392     "deletable": true,
    393     "editable": true
    394    },
    395    "outputs": [],
    396    "source": [
    397     "# Plotting histograms and confidence intervals. \n",
    398     "\n",
    399     "## Your code goes here\n",
    400     "\n",
    401     "plt.title('Graph of returns with fitted PDF and the 3 confidence intervals. ')\n",
    402     "plt.legend();"
    403    ]
    404   },
    405   {
    406    "cell_type": "code",
    407    "execution_count": null,
    408    "metadata": {
    409     "collapsed": false,
    410     "deletable": true,
    411     "editable": true
    412    },
    413    "outputs": [],
    414    "source": [
    415     "# Run the JB test for normality. \n",
    416     "\n",
    417     "## Your code goes here\n",
    418     "\n",
    419     "\n",
    420     "print \"The JB test p-value is: \", p_value\n",
    421     "print \"We reject the hypothesis that the data are normally distributed \", p_value < cutoff\n",
    422     "print \"The skewness of the returns is: \", skewness\n",
    423     "print \"The kurtosis of the returns is: \", kurtosis"
    424    ]
    425   },
    426   {
    427    "cell_type": "markdown",
    428    "metadata": {
    429     "deletable": true,
    430     "editable": true
    431    },
    432    "source": [
    433     "---\n",
    434     "\n",
    435     "Congratulations on completing the Random Variables exercises!\n",
    436     "\n",
    437     "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",
    438     "\n",
    439     "Start by going through the [Writing a Contest Algorithm](https://www.quantopian.com/tutorials/contest) tutorial."
    440    ]
    441   },
    442   {
    443    "cell_type": "markdown",
    444    "metadata": {
    445     "collapsed": true,
    446     "deletable": true,
    447     "editable": true
    448    },
    449    "source": [
    450     "*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.*"
    451    ]
    452   }
    453  ],
    454  "metadata": {
    455   "kernelspec": {
    456    "display_name": "Python 2",
    457    "language": "python",
    458    "name": "python2"
    459   },
    460   "language_info": {
    461    "codemirror_mode": {
    462     "name": "ipython",
    463     "version": 2
    464    },
    465    "file_extension": ".py",
    466    "mimetype": "text/x-python",
    467    "name": "python",
    468    "nbconvert_exporter": "python",
    469    "pygments_lexer": "ipython2",
    470    "version": "2.7.12"
    471   }
    472  },
    473  "nbformat": 4,
    474  "nbformat_minor": 2
    475 }