Skip to content

Instantly share code, notes, and snippets.

@TaylorOshan
Created June 4, 2016 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TaylorOshan/42d90dbf219b50f3b0d54e06ba4e8b5b to your computer and use it in GitHub Desktop.
Save TaylorOshan/42d90dbf219b50f3b0d54e06ba4e8b5b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"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",
"from gravity import Gravity, Production, Attraction, Doubly, BaseGravity\n",
"from entropy import Unconstrained, ProductionConstrained, AttractionConstrained, DoublyConstrained\n",
"import statsmodels.formula.api as smf\n",
"from statsmodels.api import families\n",
"import matplotlib.pyplot as plt\n",
"%pylab inline\n",
"\n",
"import time \n",
"\n",
"def timeit(method):\n",
"\n",
" def timed(*args, **kw):\n",
" ts = time.time()\n",
" result = method(*args, **kw)\n",
" te = time.time()\n",
" elapsed = te-ts\n",
"\n",
" #print '%2.8f sec' % \\\n",
" #(elapsed)\n",
" return result, elapsed\n",
"\n",
" return timed"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"@timeit\n",
"def gravity(f ,o, d, o_vars, d_vars, dij, cost='exp', framework='glm'):\n",
" results = Gravity(f, o_vars, d_vars, dij, cost, framework=framework)\n",
" return results\n",
" \n",
"@timeit \n",
"def production(f ,o, d, o_vars, d_vars, dij, cost='exp', framework='glm'):\n",
" results = Production(f, o, d_vars, dij, 'exp', framework=framework)\n",
" return results\n",
"\n",
"@timeit \n",
"def attraction(f ,o, d, o_vars, d_vars, dij, cost='exp', framework='glm'):\n",
" results = Attraction(f, d, o_vars, dij, 'exp', framework=framework)\n",
" return results\n",
"\n",
"@timeit \n",
"def doubly(f ,o, d, o_vars, d_vars, dij, cost='exp', framework='glm'):\n",
" results = Doubly(f, o, d, dij, 'exp', framework=framework)\n",
" return results\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"n = 5000\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": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5043.84268093\n",
"[ 8.18336042e+00 -5.00054911e-05]\n"
]
}
],
"source": [
"out, elapsed = doubly(f, o, d, o_vars, d_vars, dij, 'exp', framework='glm')\n",
"print elapsed\n",
"print out.params[-2:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment