Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pymc3 as pm\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Working data\n",
"bins_A = [207763, 223077, 210613, 181571, 168385, 159171, 146068, 128502,\n",
" 110505, 94379, 79315, 67084, 57527, 48867, 41862]\n",
"bins_B = [219812, 228003, 208490, 182409, 173357, 164470, 151033, 132412,\n",
" 113750, 95835, 81206, 67876, 58057, 49005, 41808]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Not working data\n",
"bins_A = [1750102, 286721, 122232, 53109, 35203, 23628, 16135,\n",
" 18991, 24309, 11363, 9732, 8494, 5911, 4374,\n",
" 3526, 2462, 2186, 1909, 1811, 1684]\n",
"bins_B = [1726921, 279424, 111627, 48393, 29513, 20356, 13086,\n",
" 18364, 23361, 10805, 8752, 10323, 6007, 4252,\n",
" 3039, 2172, 1829, 1670, 1617, 1569]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Applied stickbreaking-transform to p_A and added transformed p_A_stickbreaking to model.\n",
"Applied stickbreaking-transform to p_B and added transformed p_B_stickbreaking to model.\n",
" [-----------------100%-----------------] 2000 of 2000 complete in 15.8 sec"
]
}
],
"source": [
"clicks = range(1, len(bins_A)+1)\n",
"\n",
"with pm.Model() as model:\n",
" # Start with uniform probability over the bins\n",
" p_A = pm.Dirichlet(\"p_A\", np.ones(len(bins_A)), shape=len(bins_A))\n",
" p_B = pm.Dirichlet(\"p_B\", np.ones(len(bins_B)), shape=len(bins_B))\n",
"\n",
" # A multimodal dist. using the probabilitys of bins\n",
" obs_A = pm.Multinomial(\"obs_A\", p=p_A, n=sum(bins_A), observed=bins_A)\n",
" obs_B = pm.Multinomial(\"obs_B\", p=p_B, n=sum(bins_B), observed=bins_B)\n",
"\n",
" percent_better = pm.Deterministic('percent_better', pm.dot(p_B, clicks)/pm.dot(p_A, clicks)*100.0 - 100 ) \n",
" \n",
" start = pm.find_MAP()\n",
" step = pm.NUTS(state=start)\n",
" trace = pm.sample(2000, step, start=start, njobs=3)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"p_A_stickbreaking:\n",
"\n",
" Mean SD MC Error 95% HPD interval\n",
" -------------------------------------------------------------------\n",
" \n",
" 3.960 0.001 0.000 [3.957, 3.963]\n",
" 2.699 0.002 0.000 [2.694, 2.704]\n",
" 2.224 0.004 0.000 [2.217, 2.231]\n",
" 1.599 0.005 0.000 [1.589, 1.609]\n",
" 1.353 0.006 0.000 [1.342, 1.366]\n",
" 1.075 0.007 0.000 [1.061, 1.089]\n",
" 0.774 0.009 0.000 [0.757, 0.790]\n",
" 1.075 0.008 0.000 [1.060, 1.091]\n",
" 1.610 0.008 0.000 [1.595, 1.626]\n",
" 0.993 0.010 0.000 [0.973, 1.012]\n",
" 0.996 0.011 0.000 [0.975, 1.019]\n",
" 1.046 0.012 0.000 [1.024, 1.074]\n",
" 0.835 0.015 0.000 [0.805, 0.863]\n",
" 0.659 0.017 0.000 [0.625, 0.692]\n",
" 0.562 0.019 0.000 [0.524, 0.601]\n",
" 0.260 0.024 0.000 [0.215, 0.306]\n",
" 0.193 0.025 0.000 [0.142, 0.241]\n",
" 0.089 0.029 0.001 [0.030, 0.145]\n",
" 0.073 0.034 0.001 [0.006, 0.137]\n",
"\n",
" Posterior quantiles:\n",
" 2.5 25 50 75 97.5\n",
" |--------------|==============|==============|--------------|\n",
" \n",
" 3.957 3.959 3.960 3.961 3.963\n",
" 2.695 2.698 2.699 2.701 2.704\n",
" 2.217 2.221 2.224 2.226 2.231\n",
" 1.589 1.595 1.599 1.602 1.609\n",
" 1.341 1.349 1.353 1.357 1.365\n",
" 1.061 1.070 1.075 1.080 1.089\n",
" 0.757 0.768 0.774 0.779 0.790\n",
" 1.060 1.070 1.075 1.080 1.091\n",
" 1.595 1.605 1.610 1.615 1.626\n",
" 0.973 0.986 0.993 1.000 1.013\n",
" 0.973 0.988 0.996 1.003 1.018\n",
" 1.022 1.038 1.047 1.055 1.071\n",
" 0.805 0.825 0.835 0.845 0.864\n",
" 0.625 0.647 0.659 0.670 0.693\n",
" 0.522 0.549 0.562 0.575 0.600\n",
" 0.213 0.244 0.260 0.276 0.306\n",
" 0.144 0.176 0.193 0.210 0.244\n",
" 0.032 0.069 0.088 0.108 0.147\n",
" 0.008 0.049 0.073 0.096 0.141\n",
"\n",
"\n",
"p_B_stickbreaking:\n",
"\n",
" Mean SD MC Error 95% HPD interval\n",
" -------------------------------------------------------------------\n",
" \n",
" 4.008 0.001 0.000 [4.005, 4.011]\n",
" 2.765 0.003 0.000 [2.760, 2.770]\n",
" 2.225 0.004 0.000 [2.218, 2.232]\n",
" 1.597 0.005 0.000 [1.587, 1.608]\n",
" 1.247 0.007 0.000 [1.235, 1.261]\n",
" 0.981 0.008 0.000 [0.967, 0.996]\n",
" 0.596 0.009 0.000 [0.578, 0.614]\n",
" 1.072 0.008 0.000 [1.056, 1.088]\n",
" 1.597 0.008 0.000 [1.582, 1.612]\n",
" 0.963 0.011 0.000 [0.941, 0.983]\n",
" 0.886 0.012 0.000 [0.862, 0.907]\n",
" 1.316 0.012 0.000 [1.292, 1.339]\n",
" 0.957 0.015 0.000 [0.928, 0.987]\n",
" 0.763 0.017 0.000 [0.730, 0.798]\n",
" 0.540 0.021 0.000 [0.500, 0.581]\n",
" 0.262 0.025 0.000 [0.213, 0.311]\n",
" 0.121 0.027 0.000 [0.067, 0.172]\n",
" 0.047 0.030 0.001 [-0.013, 0.104]\n",
" 0.030 0.036 0.001 [-0.042, 0.097]\n",
"\n",
" Posterior quantiles:\n",
" 2.5 25 50 75 97.5\n",
" |--------------|==============|==============|--------------|\n",
" \n",
" 4.005 4.007 4.008 4.009 4.011\n",
" 2.760 2.763 2.765 2.767 2.770\n",
" 2.217 2.222 2.225 2.227 2.232\n",
" 1.587 1.594 1.597 1.601 1.608\n",
" 1.234 1.242 1.247 1.251 1.260\n",
" 0.966 0.976 0.981 0.986 0.996\n",
" 0.578 0.589 0.596 0.602 0.613\n",
" 1.056 1.067 1.072 1.078 1.089\n",
" 1.582 1.592 1.597 1.602 1.612\n",
" 0.942 0.956 0.963 0.970 0.984\n",
" 0.863 0.878 0.886 0.894 0.909\n",
" 1.292 1.307 1.316 1.324 1.339\n",
" 0.928 0.947 0.957 0.967 0.987\n",
" 0.727 0.751 0.762 0.775 0.797\n",
" 0.498 0.526 0.540 0.554 0.581\n",
" 0.212 0.246 0.261 0.279 0.311\n",
" 0.068 0.103 0.122 0.140 0.174\n",
" -0.012 0.027 0.046 0.068 0.107\n",
" -0.041 0.007 0.030 0.052 0.097\n",
"\n",
"\n",
"p_A:\n",
"\n",
" Mean SD MC Error 95% HPD interval\n",
" -------------------------------------------------------------------\n",
" \n",
" 0.734 0.000 0.000 [0.734, 0.735]\n",
" 0.120 0.000 0.000 [0.120, 0.121]\n",
" 0.051 0.000 0.000 [0.051, 0.052]\n",
" 0.022 0.000 0.000 [0.022, 0.022]\n",
" 0.015 0.000 0.000 [0.015, 0.015]\n",
" 0.010 0.000 0.000 [0.010, 0.010]\n",
" 0.007 0.000 0.000 [0.007, 0.007]\n",
" 0.008 0.000 0.000 [0.008, 0.008]\n",
" 0.010 0.000 0.000 [0.010, 0.010]\n",
" 0.005 0.000 0.000 [0.005, 0.005]\n",
" 0.004 0.000 0.000 [0.004, 0.004]\n",
" 0.004 0.000 0.000 [0.003, 0.004]\n",
" 0.002 0.000 0.000 [0.002, 0.003]\n",
" 0.002 0.000 0.000 [0.002, 0.002]\n",
" 0.001 0.000 0.000 [0.001, 0.002]\n",
" 0.001 0.000 0.000 [0.001, 0.001]\n",
" 0.001 0.000 0.000 [0.001, 0.001]\n",
" 0.001 0.000 0.000 [0.001, 0.001]\n",
" 0.001 0.000 0.000 [0.001, 0.001]\n",
" 0.001 0.000 0.000 [0.001, 0.001]\n",
"\n",
" Posterior quantiles:\n",
" 2.5 25 50 75 97.5\n",
" |--------------|==============|==============|--------------|\n",
" \n",
" 0.734 0.734 0.734 0.734 0.735\n",
" 0.120 0.120 0.120 0.120 0.121\n",
" 0.051 0.051 0.051 0.051 0.052\n",
" 0.022 0.022 0.022 0.022 0.022\n",
" 0.015 0.015 0.015 0.015 0.015\n",
" 0.010 0.010 0.010 0.010 0.010\n",
" 0.007 0.007 0.007 0.007 0.007\n",
" 0.008 0.008 0.008 0.008 0.008\n",
" 0.010 0.010 0.010 0.010 0.010\n",
" 0.005 0.005 0.005 0.005 0.005\n",
" 0.004 0.004 0.004 0.004 0.004\n",
" 0.003 0.004 0.004 0.004 0.004\n",
" 0.002 0.002 0.002 0.003 0.003\n",
" 0.002 0.002 0.002 0.002 0.002\n",
" 0.001 0.001 0.001 0.001 0.002\n",
" 0.001 0.001 0.001 0.001 0.001\n",
" 0.001 0.001 0.001 0.001 0.001\n",
" 0.001 0.001 0.001 0.001 0.001\n",
" 0.001 0.001 0.001 0.001 0.001\n",
" 0.001 0.001 0.001 0.001 0.001\n",
"\n",
"\n",
"p_B:\n",
"\n",
" Mean SD MC Error 95% HPD interval\n",
" -------------------------------------------------------------------\n",
" \n",
" 0.743 0.000 0.000 [0.743, 0.744]\n",
" 0.120 0.000 0.000 [0.120, 0.121]\n",
" 0.048 0.000 0.000 [0.048, 0.048]\n",
" 0.021 0.000 0.000 [0.021, 0.021]\n",
" 0.013 0.000 0.000 [0.013, 0.013]\n",
" 0.009 0.000 0.000 [0.009, 0.009]\n",
" 0.006 0.000 0.000 [0.006, 0.006]\n",
" 0.008 0.000 0.000 [0.008, 0.008]\n",
" 0.010 0.000 0.000 [0.010, 0.010]\n",
" 0.005 0.000 0.000 [0.005, 0.005]\n",
" 0.004 0.000 0.000 [0.004, 0.004]\n",
" 0.004 0.000 0.000 [0.004, 0.005]\n",
" 0.003 0.000 0.000 [0.003, 0.003]\n",
" 0.002 0.000 0.000 [0.002, 0.002]\n",
" 0.001 0.000 0.000 [0.001, 0.001]\n",
" 0.001 0.000 0.000 [0.001, 0.001]\n",
" 0.001 0.000 0.000 [0.001, 0.001]\n",
" 0.001 0.000 0.000 [0.001, 0.001]\n",
" 0.001 0.000 0.000 [0.001, 0.001]\n",
" 0.001 0.000 0.000 [0.001, 0.001]\n",
"\n",
" Posterior quantiles:\n",
" 2.5 25 50 75 97.5\n",
" |--------------|==============|==============|--------------|\n",
" \n",
" 0.743 0.743 0.743 0.744 0.744\n",
" 0.120 0.120 0.120 0.120 0.121\n",
" 0.048 0.048 0.048 0.048 0.048\n",
" 0.021 0.021 0.021 0.021 0.021\n",
" 0.013 0.013 0.013 0.013 0.013\n",
" 0.009 0.009 0.009 0.009 0.009\n",
" 0.006 0.006 0.006 0.006 0.006\n",
" 0.008 0.008 0.008 0.008 0.008\n",
" 0.010 0.010 0.010 0.010 0.010\n",
" 0.005 0.005 0.005 0.005 0.005\n",
" 0.004 0.004 0.004 0.004 0.004\n",
" 0.004 0.004 0.004 0.004 0.005\n",
" 0.003 0.003 0.003 0.003 0.003\n",
" 0.002 0.002 0.002 0.002 0.002\n",
" 0.001 0.001 0.001 0.001 0.001\n",
" 0.001 0.001 0.001 0.001 0.001\n",
" 0.001 0.001 0.001 0.001 0.001\n",
" 0.001 0.001 0.001 0.001 0.001\n",
" 0.001 0.001 0.001 0.001 0.001\n",
" 0.001 0.001 0.001 0.001 0.001\n",
"\n",
"\n",
"percent_better:\n",
"\n",
" Mean SD MC Error 95% HPD interval\n",
" -------------------------------------------------------------------\n",
" \n",
" -1.924 0.104 0.001 [-2.129, -1.719]\n",
"\n",
" Posterior quantiles:\n",
" 2.5 25 50 75 97.5\n",
" |--------------|==============|==============|--------------|\n",
" \n",
" -2.128 -1.994 -1.923 -1.856 -1.714\n",
"\n"
]
}
],
"source": [
"pm.plots.summary(trace[1000:])"
]
},
{
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@Pythpnone
Copy link

Pythpnone commented Jun 28, 2017

I stay in China,it is my lucky that browsed your code. my email is 494987201qq@gmail.com,we can chat AI and ML.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment