ml-finance-python

python scripts for finance machine learning

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

03_dirichlet_distribution.ipynb

(4797B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "markdown",
      5    "metadata": {},
      6    "source": [
      7     "# Dirichlet Distribution"
      8    ]
      9   },
     10   {
     11    "cell_type": "markdown",
     12    "metadata": {
     13     "slideshow": {
     14      "slide_type": "slide"
     15     }
     16    },
     17    "source": [
     18     "## Imports"
     19    ]
     20   },
     21   {
     22    "cell_type": "code",
     23    "execution_count": 1,
     24    "metadata": {
     25     "ExecuteTime": {
     26      "end_time": "2018-11-30T18:32:41.753600Z",
     27      "start_time": "2018-11-30T18:32:41.741408Z"
     28     },
     29     "slideshow": {
     30      "slide_type": "fragment"
     31     }
     32    },
     33    "outputs": [],
     34    "source": [
     35     "import warnings\n",
     36     "from collections import OrderedDict\n",
     37     "from pathlib import Path\n",
     38     "\n",
     39     "import numpy as np\n",
     40     "import pandas as pd\n",
     41     "\n",
     42     "# Visualization\n",
     43     "from ipywidgets import interact, FloatSlider\n",
     44     "import matplotlib.pyplot as plt\n",
     45     "import seaborn as sns"
     46    ]
     47   },
     48   {
     49    "cell_type": "code",
     50    "execution_count": 3,
     51    "metadata": {
     52     "ExecuteTime": {
     53      "end_time": "2018-11-30T18:32:57.047779Z",
     54      "start_time": "2018-11-30T18:32:57.038865Z"
     55     }
     56    },
     57    "outputs": [],
     58    "source": [
     59     "%matplotlib inline\n",
     60     "plt.style.use('ggplot')\n",
     61     "plt.rcParams['figure.figsize'] = (14.0, 8.7)\n",
     62     "warnings.filterwarnings('ignore')\n",
     63     "pd.options.display.float_format = '{:,.2f}'.format"
     64    ]
     65   },
     66   {
     67    "cell_type": "markdown",
     68    "metadata": {
     69     "slideshow": {
     70      "slide_type": "slide"
     71     }
     72    },
     73    "source": [
     74     "## Simulate Dirichlet Distribution"
     75    ]
     76   },
     77   {
     78    "cell_type": "markdown",
     79    "metadata": {},
     80    "source": [
     81     "The Dirichlet distribution produces probability vectors that can be used with discrete distributions. That is, it randomly generates a given number of values that are positive and sum to one. It has a parameter 𝜶  of positive real value that controls the concentration of the probabilities. Values closer to zero mean that only a few values will be positive and receive most probability mass. \n",
     82     "\n",
     83     "The following simulation let's you interactively explore how different parameter values affect the resulting probability distributions."
     84    ]
     85   },
     86   {
     87    "cell_type": "code",
     88    "execution_count": 4,
     89    "metadata": {
     90     "ExecuteTime": {
     91      "end_time": "2018-11-30T18:32:58.986986Z",
     92      "start_time": "2018-11-30T18:32:58.197387Z"
     93     },
     94     "hide_input": false,
     95     "scrolled": false,
     96     "slideshow": {
     97      "slide_type": "fragment"
     98     }
     99    },
    100    "outputs": [
    101     {
    102      "data": {
    103       "application/vnd.jupyter.widget-view+json": {
    104        "model_id": "1f076c57d1ff4decaae28960325cc703",
    105        "version_major": 2,
    106        "version_minor": 0
    107       },
    108       "text/plain": [
    109        "interactive(children=(FloatSlider(value=1.0, continuous_update=False, description='Alpha', min=0.01, step=0.01…"
    110       ]
    111      },
    112      "metadata": {},
    113      "output_type": "display_data"
    114     }
    115    ],
    116    "source": [
    117     "f=FloatSlider(value=1, min=1e-2, max=1e2, step=1e-2, continuous_update=False, description='Alpha')\n",
    118     "@interact(alpha=f)\n",
    119     "def sample_dirichlet(alpha):\n",
    120     "    topics = 10\n",
    121     "    draws= 9\n",
    122     "    alphas = np.full(shape=topics, fill_value=alpha)\n",
    123     "    samples = np.random.dirichlet(alpha=alphas, size=draws)\n",
    124     "    \n",
    125     "    fig, axes = plt.subplots(nrows=3, ncols=3, sharex=True, sharey=True)\n",
    126     "    axes = axes.flatten()\n",
    127     "    plt.setp(axes, ylim=(0, 1))\n",
    128     "    for i, sample in enumerate(samples):\n",
    129     "        axes[i].bar(x=list(range(10)), height=sample, color=sns.color_palette(\"Set2\", 10))\n",
    130     "    fig.suptitle('Dirichlet Allocation | 10 Topics, 9 Samples')\n",
    131     "    fig.tight_layout()\n",
    132     "    plt.subplots_adjust(top=.95)"
    133    ]
    134   },
    135   {
    136    "cell_type": "code",
    137    "execution_count": null,
    138    "metadata": {},
    139    "outputs": [],
    140    "source": []
    141   }
    142  ],
    143  "metadata": {
    144   "celltoolbar": "Slideshow",
    145   "hide_input": false,
    146   "kernelspec": {
    147    "display_name": "Python 3",
    148    "language": "python",
    149    "name": "python3"
    150   },
    151   "language_info": {
    152    "codemirror_mode": {
    153     "name": "ipython",
    154     "version": 3
    155    },
    156    "file_extension": ".py",
    157    "mimetype": "text/x-python",
    158    "name": "python",
    159    "nbconvert_exporter": "python",
    160    "pygments_lexer": "ipython3",
    161    "version": "3.6.8"
    162   },
    163   "name": "_merged",
    164   "toc": {
    165    "base_numbering": 1,
    166    "nav_menu": {},
    167    "number_sections": true,
    168    "sideBar": true,
    169    "skip_h1_title": false,
    170    "title_cell": "Table of Contents",
    171    "title_sidebar": "Contents",
    172    "toc_cell": false,
    173    "toc_position": {
    174     "height": "100px",
    175     "left": "85.9915px",
    176     "right": "1064px",
    177     "top": "66.3352px",
    178     "width": "241.562px"
    179    },
    180    "toc_section_display": true,
    181    "toc_window_display": true
    182   }
    183  },
    184  "nbformat": 4,
    185  "nbformat_minor": 2
    186 }