Skip to content

Instantly share code, notes, and snippets.

@seibert
Created June 3, 2015 01:37
Show Gist options
  • Save seibert/17ce9cc40898545b779b to your computer and use it in GitHub Desktop.
Save seibert/17ce9cc40898545b779b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"recurrence relation example\n",
"https://jakevdp.github.io/blog/2013/06/15/numba-vs-cython-take-2/"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0 0.471435\n",
"1 -1.190976\n",
"2 1.432707\n",
"3 -0.312652\n",
"4 -0.720589\n",
"5 0.887163\n",
" ... \n",
"99994 -0.940113\n",
"99995 -1.478211\n",
"99996 0.279401\n",
"99997 0.029286\n",
"99998 -1.220531\n",
"99999 0.384112\n",
"dtype: float64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"from pandas import Series\n",
"from numba import jit\n",
"\n",
"np.random.seed(1234)\n",
"pd.set_option('max_row',12)\n",
"s = Series(np.random.randn(1e5))\n",
"com = 0.5\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def python(s):\n",
" output = Series(index=range(len(s)))\n",
"\n",
" alpha = 1. / (1. + com)\n",
" old_weight = 1.0\n",
" new_weight = 1.0\n",
" weighted_avg = s[0]\n",
" output[0] = weighted_avg\n",
" \n",
" for i in xrange(1,len(s)):\n",
" v = s[i]\n",
" old_weight *= (1-alpha)\n",
" weighted_avg = ((old_weight * weighted_avg) + (new_weight * v)) / (old_weight + new_weight)\n",
" old_weight += new_weight\n",
" output[i] = weighted_avg\n",
" \n",
" return output\n",
"\n",
"def cython(s):\n",
" return pd.ewma(s,com=com,adjust=True)\n",
"\n",
"@jit\n",
"def f(arr, output):\n",
" alpha = 1. / (1. + com)\n",
" old_weight = 1.0\n",
" new_weight = 1.0\n",
" weighted_avg = arr[0]\n",
" output[0] = weighted_avg\n",
"\n",
" for i in range(1,arr.shape[0]):\n",
" v = arr[i]\n",
" old_weight *= (1-alpha)\n",
" weighted_avg = ((old_weight * weighted_avg) + (new_weight * v)) / (old_weight + new_weight)\n",
" old_weight += new_weight\n",
" output[i] = weighted_avg\n",
" \n",
"def numba(s): \n",
" output = np.empty(len(s),dtype='float64')\n",
" f(s.values, output)\n",
" return Series(output)\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result1 = python(s)\n",
"result2 = cython(s)\n",
"result3 = numba(s)\n",
"result1.equals(result2) and result1.equals(result3)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loops, best of 3: 1.73 s per loop\n"
]
}
],
"source": [
"%timeit python(s)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 loops, best of 3: 4.82 ms per loop\n"
]
}
],
"source": [
"%timeit cython(s)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1000 loops, best of 3: 1.01 ms per loop\n"
]
}
],
"source": [
"%timeit numba(s)"
]
},
{
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment