{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Paramnormal Activity\n", "\n", "Perhaps the most convenient way to access the functionality of `paramnormal` is through the `activity` module.\n", "\n", "Random number generation, distribution fitting, and basic plotting are exposed through `activity`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import warnings\n", "warnings.simplefilter('ignore')\n", "\n", "from numpy.random import seed\n", "from scipy import stats\n", "from matplotlib import pyplot\n", "import seaborn\n", "\n", "import paramnormal\n", "\n", "clean_bkgd = {'axes.facecolor':'none', 'figure.facecolor':'none'}\n", "seaborn.set(style='ticks', rc=clean_bkgd)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Random number generation\n", "Through the top-level API, you could do the following to generate lognormal random numbers." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "seed(0)\n", "paramnormal.lognormal(mu=0.75, sigma=1.25).rvs(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What's happening here is that `paramnormal.lognormal(mu=0.75, sigma=1.25)` translates the arguments, passes them to `scipy.stats.lognorm`, and returns scipy's distribution object. Then we call the `rvs` method of that object to generate five random numbers in an array.\n", "\n", "Through the `activity` API, that equivalent to:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "seed(0)\n", "paramnormal.activity.random('lognormal', mu=0.75, sigma=1.25, shape=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And of course, Greek letters are still supported." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "seed(0)\n", "paramnormal.activity.random('lognormal', μ=0.75, σ=1.25, shape=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lastly, you can reuse an already full-specified distribution and the `shape` parameter can take a tuple to return *N*-dimensional arrays." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "seed(0)\n", "my_dist = paramnormal.lognormal(μ=0.75, σ=1.25)\n", "paramnormal.activity.random(my_dist, shape=(2, 4))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fitting distributions\n", "Fitting distributions to data follows a similar pattern." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data = paramnormal.activity.random('beta', α=3, β=2, shape=37)\n", "paramnormal.activity.fit('beta', data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Equivalent command to perform the same fits in raw scipy is shown below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# constrained loc and scale\n", "stats.beta.fit(data, floc=0, fscale=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can still fix the primary parameters and unconstrain the defaults." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "paramnormal.activity.fit('beta', data, β=2, loc=None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And again in raw scipy:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# constrained beta and scale, unconstrained loc\n", "stats.beta.fit(data, f1=2, fscale=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting\n", "There is very limited plotting functionality built into paramnormal.\n", "The probability distribution function (PDF) is plotted by default, but any other method of the distributions can be plotted by specifying the ``which`` parameters." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ax = paramnormal.activity.plot('beta', α=3, β=2)\n", "paramnormal.activity.plot('beta', α=3, β=2, ax=ax, which='CDF')\n", "ax.legend()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can plot on an existing figure through the `ax` argument and control the line style through `line_opts`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, (ax, ax2) = pyplot.subplots(nrows=2, sharex=True, sharey=True)\n", "paramnormal.activity.plot('beta', α=6, β=2, ax=ax, line_opts=dict(color='firebrick', lw=3))\n", "paramnormal.activity.plot('beta', α=2, β=6, ax=ax2, line_opts=dict(color='forestgreen', lw=1.25))\n", "ax.set_ylabel('α=6, β=2')\n", "ax2.set_ylabel('α=2, β=6')\n", "seaborn.despine(fig)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course, you can create a fully-specified distribtion and omit the distribution parameters." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "beta = paramnormal.beta(α=3, β=2)\n", "ax = paramnormal.activity.plot(beta)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And finally, you can pass an array of data and an unfrozen distribution, and a new distribution will be fit to your data." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data = paramnormal.activity.random('beta', α=2, β=6, shape=37) + \\\n", " paramnormal.activity.random('normal', μ=5, σ=1, shape=37)\n", "ax = paramnormal.activity.plot('normal', data=data, line_opts=dict(label='Emperical Fit'))\n", "ax = paramnormal.activity.plot('normal', μ=5, σ=1, line_opts=dict(label='Theoretical'))\n", "ax.legend()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }