Coath, Christopher D., Robert CJ Steele, and W. Fred Lunnon. "Statistical bias in isotope ratios." Journal of Analytical Atomic Spectrometry 28.1 (2013): 52-58.

https://stats.stackexchange.com/questions/10951/what-is-the-distribution-of-the-ratio-of-two-poisson-random-variables

$\frac{X}{Y+1}$ has the expectation value of $(\lambda_1/\lambda_2)(1-\exp{-\lambda_2})$ NOT CONVINCED THIS IS TRUE


In [68]:
import pymc3 as pm
import numpy as np
import seaborn as nsn
import pandas as pd
import matplotlib.pyplot as plt
import scipy
import seaborn as sns

In [69]:
rate1 = 15
rate2 = rate1/11
print(rate1, rate2, rate1/rate2, (rate1/rate2)*(1-np.exp(-rate2)))
d1 = np.random.poisson(rate1, size=1000)
d2 = np.random.poisson(rate2, size=1000)


15 1.3636363636363635 11.0 8.186979240955893

In [80]:
with pm.Model() as model1:
    r = pm.Uniform('rate', 0, 100, shape=2)
    p  = pm.Poisson('data', r, shape=2, observed=np.vstack((d1, d2)).T)
    ratio = pm.Deterministic('ratio', r[0]/(r[1]+1))
    sum1 = pm.Deterministic('sum1', pm.math.sum(r[0]))
    sum2 = pm.Deterministic('sum2', pm.math.sum(r[1]))

    start = pm.find_MAP()
    trace1 = pm.sample(10000, start=start, njobs=5)


logp = -4,291.2, ||grad|| = 2.565e-06: 100%|██████████| 12/12 [00:00<00:00, 1865.66it/s]  
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
/Users/balarsen/miniconda3/lib/python3.6/site-packages/pymc3/model.py:384: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  if not np.issubdtype(var.dtype, float):
Multiprocess sampling (5 chains in 5 jobs)
NUTS: [rate_interval__]
100%|██████████| 10500/10500 [00:10<00:00, 1024.62it/s]

In [81]:
pm.traceplot(trace1, combined=True)


Out[81]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x113d37a90>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x115fe2ac8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x116009da0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11636b0b8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x116389da0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1163b5080>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x11743c358>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x117465630>]],
      dtype=object)

In [82]:
pm.summary(trace1)


Out[82]:
mean sd mc_error hpd_2.5 hpd_97.5 n_eff Rhat
rate__0 14.798581 0.121712 0.000576 14.561327 15.041364 50000.0 1.000056
rate__1 1.383237 0.037288 0.000148 1.310939 1.456894 50000.0 0.999968
ratio 6.210959 0.109472 0.000473 5.993945 6.421136 50000.0 0.999976
sum1 14.798581 0.121712 0.000576 14.561327 15.041364 50000.0 1.000056
sum2 1.383237 0.037288 0.000148 1.310939 1.456894 50000.0 0.999968

In [83]:
(trace1['rate'][:,0].mean(), 
 trace1['rate'][:,1].mean(), 
 (trace1['rate'][:,0].mean()/trace1['rate'][:,1].mean()), 
 (1-np.exp(-trace1['rate'][:,1].mean())), 
(trace1['rate'][:,0].mean()/trace1['rate'][:,1].mean()) * (1-np.exp(-trace1['rate'][:,1].mean())) )


Out[83]:
(14.798580511406922,
 1.3832373317031792,
 10.69851150791704,
 0.7492345732759196,
 8.015694704321739)

In [84]:
sns.distplot(trace1['ratio'])
plt.axvline(  (trace1['rate'][:,0].mean()/trace1['rate'][:,1].mean()) * (1-np.exp(-trace1['rate'][:,1].mean())) )


/Users/balarsen/miniconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py:6462: UserWarning: The 'normed' kwarg is deprecated, and has been replaced by the 'density' kwarg.
  warnings.warn("The 'normed' kwarg is deprecated, and has been "
Out[84]:
<matplotlib.lines.Line2D at 0x1186cd6a0>

In [85]:
sns.distplot(trace1['sum1']/trace1['sum2'])
plt.axvline(11)


/Users/balarsen/miniconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py:6462: UserWarning: The 'normed' kwarg is deprecated, and has been replaced by the 'density' kwarg.
  warnings.warn("The 'normed' kwarg is deprecated, and has been "
Out[85]:
<matplotlib.lines.Line2D at 0x117f45828>

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [6]:
rate1 = 15*np.sin(np.linspace(0, np.pi, 1000))+4
rate2 = rate1/11
d1 = scipy.random.poisson(rate1)
d2 = scipy.random.poisson(rate2)
plt.plot(d1)
plt.plot(d2)


Out[6]:
[<matplotlib.lines.Line2D at 0x10d017710>]

In [7]:
plt.plot(d1/d2)


Out[7]:
[<matplotlib.lines.Line2D at 0x10c13ff98>]

In [8]:
plt.hist((d1/d2)[np.isfinite(d1/d2)], 20)


Out[8]:
(array([20., 36., 86., 95., 59., 76., 46., 64., 37., 17., 46., 19., 41.,
         7.,  7., 18.,  6.,  6.,  1.,  1.]),
 array([ 1. ,  2.4,  3.8,  5.2,  6.6,  8. ,  9.4, 10.8, 12.2, 13.6, 15. ,
        16.4, 17.8, 19.2, 20.6, 22. , 23.4, 24.8, 26.2, 27.6, 29. ]),
 <a list of 20 Patch objects>)

In [87]:
with pm.Model() as model2:
    r1 = pm.Uniform('rate1', 0, 100, shape=len(d1))
    r2 = pm.Uniform('rate2', 0, 100, shape=len(d2))    
    p1  = pm.Poisson('data1', r1, shape=len(d1), observed=d1)
    p2  = pm.Poisson('data2', r2, shape=len(d2), observed=d2)
    ratio = pm.Deterministic('ratio', r1/(r2+1))
    sum1 = pm.Deterministic('sum1', pm.math.sum(r1))
    sum2 = pm.Deterministic('sum2', pm.math.sum(r2))

    start = pm.find_MAP()
    trace2 = pm.sample(10000, start=start, njobs=5)


logp = -12,361, ||grad|| = 0.03406: 100%|██████████| 49/49 [00:00<00:00, 1114.22it/s]  
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
/Users/balarsen/miniconda3/lib/python3.6/site-packages/pymc3/model.py:384: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  if not np.issubdtype(var.dtype, float):
Multiprocess sampling (5 chains in 5 jobs)
NUTS: [rate2_interval__, rate1_interval__]
100%|██████████| 10500/10500 [01:13<00:00, 142.99it/s]

In [88]:
pm.summary(trace2, varnames=['ratio'])


Out[88]:
mean sd mc_error hpd_2.5 hpd_97.5 n_eff Rhat
ratio__0 5.963010 2.962315 0.011623 1.211042 11.773512 44797.0 0.999967
ratio__1 5.949422 2.956897 0.011780 1.159451 11.717227 50000.0 1.000025
ratio__2 4.432831 2.365179 0.011870 0.825138 9.124040 41733.0 0.999963
ratio__3 3.263896 1.557333 0.006787 0.951528 6.365638 42172.0 1.000008
ratio__4 7.271029 3.617559 0.019456 1.727042 14.525479 39814.0 0.999961
ratio__5 6.470389 3.282151 0.015550 1.510771 13.084846 39143.0 0.999957
ratio__6 8.478160 4.137771 0.021298 2.190019 16.889750 39805.0 0.999998
ratio__7 8.094944 3.969028 0.018746 2.122146 16.232836 41717.0 0.999990
ratio__8 6.567837 3.221529 0.014017 1.320111 12.830520 50000.0 0.999967
ratio__9 7.681356 3.793134 0.018816 1.883729 15.362071 37210.0 1.000000
ratio__10 4.169194 2.182955 0.010892 0.971588 8.561766 41288.0 0.999961
ratio__11 5.063930 2.568261 0.012608 1.254431 10.264290 39746.0 0.999980
ratio__12 1.722678 0.921372 0.004396 0.383495 3.562522 46293.0 0.999995
ratio__13 7.670536 3.782348 0.018117 1.795030 15.225243 41765.0 0.999999
ratio__14 9.584037 4.383683 0.018282 2.222760 17.954243 45243.0 0.999975
ratio__15 3.738340 1.855066 0.009314 0.973415 7.468375 39231.0 1.000041
ratio__16 5.662732 2.939568 0.013280 1.210575 11.511038 43181.0 0.999979
ratio__17 11.281402 4.960172 0.021144 2.780720 20.735882 50000.0 0.999961
ratio__18 8.947787 4.100292 0.020196 2.007860 16.763169 50000.0 0.999969
ratio__19 2.336687 1.256667 0.005457 0.536059 4.851378 43071.0 0.999975
ratio__20 5.050137 2.529652 0.011803 1.255534 10.083694 38444.0 0.999985
ratio__21 4.455906 2.299106 0.010949 1.107641 9.080276 39455.0 0.999957
ratio__22 2.372514 1.027538 0.004846 0.779826 4.411700 46192.0 0.999987
ratio__23 9.566963 4.326957 0.017607 2.271187 17.989066 48809.0 0.999960
ratio__24 10.115431 4.537703 0.020663 2.517235 18.909527 46185.0 0.999958
ratio__25 8.360968 3.898901 0.016633 1.817018 15.828330 50000.0 1.000034
ratio__26 6.474190 3.280619 0.015194 1.496982 13.081048 39269.0 0.999998
ratio__27 5.677997 2.955154 0.012362 1.239753 11.623797 39315.0 1.000033
ratio__28 6.437982 3.246473 0.014831 1.414157 12.927016 42500.0 0.999963
ratio__29 3.263294 1.761490 0.009108 0.688986 6.799672 42558.0 1.000007
... ... ... ... ... ... ... ...
ratio__970 3.522872 1.788661 0.009322 0.921743 7.066139 39394.0 1.000024
ratio__971 3.876131 2.029330 0.010177 0.916624 7.976516 42877.0 0.999975
ratio__972 6.843749 3.418186 0.017073 1.627157 13.719981 42381.0 1.000033
ratio__973 2.427370 1.134002 0.005427 0.692470 4.643653 46665.0 1.000012
ratio__974 2.691871 1.514349 0.007303 0.514064 5.707165 41679.0 1.000041
ratio__975 5.239943 2.752048 0.013798 1.178505 10.818631 42429.0 1.000048
ratio__976 5.952289 2.937795 0.017366 1.645035 12.016931 36240.0 1.000022
ratio__977 3.584910 1.914510 0.010176 0.806774 7.478465 39670.0 1.000026
ratio__978 11.931340 5.213940 0.023707 3.049839 21.936661 44229.0 0.999979
ratio__979 4.850428 2.589527 0.012459 1.004233 10.085084 37716.0 1.000035
ratio__980 4.443456 2.165511 0.010680 1.212524 8.725025 40578.0 1.000004
ratio__981 4.179588 2.165962 0.010692 0.932436 8.474448 40073.0 1.000058
ratio__982 7.163868 3.432123 0.015935 1.536361 13.854515 44043.0 0.999989
ratio__983 5.379816 2.702138 0.013330 1.351213 10.796287 39406.0 1.000047
ratio__984 10.746248 4.766636 0.022456 2.638271 19.862963 44898.0 0.999992
ratio__985 5.236990 2.738734 0.012199 1.075041 10.674518 46302.0 1.000013
ratio__986 10.131211 4.540551 0.021148 2.446275 18.855008 50000.0 1.000082
ratio__987 3.963516 1.922835 0.009060 1.079638 7.810839 45715.0 1.000057
ratio__988 3.277428 1.761463 0.008351 0.686420 6.801347 43253.0 0.999962
ratio__989 2.093407 0.989049 0.004540 0.580343 4.047643 50000.0 1.000003
ratio__990 2.907160 1.303949 0.005935 0.893509 5.480844 43930.0 1.000056
ratio__991 2.291814 1.155536 0.004810 0.579697 4.571906 46363.0 0.999963
ratio__992 4.466494 2.295636 0.012675 1.040649 9.076623 40267.0 0.999954
ratio__993 4.841249 2.545341 0.012648 1.097931 10.003506 37329.0 1.000023
ratio__994 4.491937 2.331590 0.011724 1.125703 9.225476 38777.0 0.999975
ratio__995 7.283142 3.647711 0.015853 1.685997 14.565933 43810.0 0.999994
ratio__996 11.954936 5.253234 0.025677 2.935912 21.945015 44227.0 0.999983
ratio__997 7.246472 3.609262 0.017839 1.795529 14.617100 40364.0 0.999988
ratio__998 10.159656 4.551031 0.018593 2.455027 18.968368 46383.0 1.000001
ratio__999 6.854211 3.425230 0.018207 1.590773 13.698546 42300.0 1.000030

1000 rows × 7 columns


In [90]:
sns.distplot(trace2['sum1'].flatten()/trace2['sum2'].flatten())
plt.axvline(11)


/Users/balarsen/miniconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py:6462: UserWarning: The 'normed' kwarg is deprecated, and has been replaced by the 'density' kwarg.
  warnings.warn("The 'normed' kwarg is deprecated, and has been "
Out[90]:
<matplotlib.lines.Line2D at 0x1196253c8>

In [ ]: