Created
June 4, 2016 05:07
-
-
Save TaylorOshan/13c3b4a1d9489e03ea70ee52ecb0b61d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[-1.14993102 0.69084953 0.68523832]\n", | |
"(-2.7430635540781623e-10, -2.5915536383536164e-10, -4.730811298259141e-10)\n" | |
] | |
} | |
], | |
"source": [ | |
"austria = pd.read_csv('http://dl.dropbox.com/u/8649795/AT_Austria.csv')\n", | |
"austria = austria[austria['Origin'] != austria['Destination']]\n", | |
"f = np.reshape(austria['Data'].values, (-1,1))\n", | |
"o = austria['Origin'].values\n", | |
"d = austria['Destination'].values\n", | |
"dij = np.reshape(austria['Dij'].values, (-1,1))\n", | |
"o_vars = np.reshape(austria['Oi2007'].values, (-1,1))\n", | |
"d_vars = np.reshape(austria['Dj2007'].values, (-1,1))\n", | |
"dij = np.reshape(austria['Dij'].values, (-1,1))\n", | |
"o_vars = np.reshape(austria['Oi2007'].values, (-1,1))\n", | |
"d_vars = np.reshape(austria['Dj2007'].values, (-1,1))\n", | |
"\n", | |
"def newton(f, x0):\n", | |
" # wrap scipy.optimize.newton with our automatic derivatives\n", | |
" params = scipy.optimize.fsolve(f, x0)\n", | |
" return params\n", | |
"\n", | |
"def poiss_loglike(mu, sig, ep, x, inputs):\n", | |
" a,b,c = inputs[:,0], inputs[:,1], inputs[:,2]\n", | |
" predict = sig*a + ep*b + mu*c\n", | |
" predict = np.reshape(predict, (-1,1))\n", | |
" return -np.sum(x*np.log(predict)-predict)\n", | |
"\n", | |
"#def loglike(mu, k, x, inputs):\n", | |
" #return np.sum(poiss_loglike(mu, k, x, inputs))\n", | |
"\n", | |
"\n", | |
"def fit_maxlike(x, inputs, mu_guess, o_guess, d_guess):\n", | |
" prime = lambda p: multigrad(poiss_loglike, argnums=[0,1,2])(p[0], p[1], p[2], x, inputs)\n", | |
" params = newton(prime, (mu_guess, o_guess, d_guess))\n", | |
" return params\n", | |
"\n", | |
"\n", | |
"if __name__ == \"__main__\":\n", | |
" \n", | |
" x=np.log(f)\n", | |
" inputs = np.hstack((np.log(o_vars), np.log(d_vars), np.log(dij)))\n", | |
" params = fit_maxlike(x, inputs, mu_guess=0.0, o_guess=1.0, d_guess=1.0)\n", | |
" print(params)\n", | |
" \n", | |
" prime = lambda p: multigrad(poiss_loglike, argnums=[0,1,2])(p[0], p[1], p[2], x, inputs)\n", | |
" print(prime(params))\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"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