{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "//anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.\n", " warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "import os\n", "os.chdir('../')\n", "from gravity import Gravity, Production, Attraction, Doubly, BaseGravity\n", "import statsmodels.formula.api as smf\n", "from statsmodels.api import families\n", "import matplotlib.pyplot as plt\n", "%pylab inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\n", "n = 3000\n", "o = np.tile(np.arange(n),n)\n", "d = np.repeat(np.arange(n),n)\n", "loc_size = np.random.randint(25000,500000, n)\n", "o_vars = np.tile(loc_size, n)\n", "d_vars = np.repeat(loc_size, n)\n", "dij = np.random.exponential(2500, n**2)\n", "f = o_vars**.3*d_vars**.4*np.exp(dij*-.00005)\n", "o = np.reshape(o, (-1,1))\n", "d = np.reshape(d, (-1,1))\n", "o_vars = np.reshape(o_vars, (-1,1))\n", "d_vars = np.reshape(d_vars, (-1,1))\n", "dij = np.reshape(dij, (-1,1))\n", "f = np.reshape(f, (-1,1))\n", "f = f.astype(np.int64)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 14.3 s, sys: 1.46 s, total: 15.8 s\n", "Wall time: 7.41 s\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time Gravity(f, o_vars, d_vars, dij, 'exp')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 38.3 s, sys: 4 s, total: 42.3 s\n", "Wall time: 24.6 s\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time Production(f, o, d_vars, dij, 'exp')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 36 s, sys: 4.25 s, total: 40.2 s\n", "Wall time: 21.4 s\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time Attraction(f, d, o_vars, dij, 'exp')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1min 19s, sys: 6.3 s, total: 1min 25s\n", "Wall time: 37.4 s\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time Doubly(f, o, d, dij, 'exp')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from glm import GLM\n", "from iwls import iwls\n", "import line_profiler\n", "import IPython\n", "ip = IPython.get_ipython()\n", "ip.define_magic('lprun', line_profiler.magic_lprun)\n", "instance = Production(f, o, d_vars, dij, 'exp')\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "correct sparse\n" ] } ], "source": [ "%lprun -f BaseGravity.__init__ instance.__init__(f, o, d_vars, dij, 'exp')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "glm_inst = GLM(instance.y, instance.X, family=families.Poisson())\n", "%lprun -f GLM.__init__ glm_inst.__init__(instance.y, instance.X, family=families.Poisson())" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%lprun -f iwls iwls(instance.y, instance.X, family=families.Poisson(), offset=None, y_fix=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }