In [86]:
import pymc3 as pm
import numpy
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
sns.set(font_scale=1.5)
%matplotlib inline
In [25]:
d = np.random.rand(10,10)
d[d>0.75] = 1
d[d<=0.75] = 0
plt.pcolormesh(d)
plt.colorbar()
Out[25]:
In [26]:
with pm.Model() as model:
p = pm.Uniform('p', 0 ,1, shape=(10,10))
b = pm.Bernoulli('b', p=p, shape=(10,10), observed=d)
trace = pm.sample(5000)
In [52]:
pm.traceplot(trace, combined=True)
# pm.summary(trace)
Out[52]:
In [53]:
trace['p'].mean(axis=0).shape
Out[53]:
In [54]:
ppc = pm.sample_ppc(trace, samples=1000, model=model)
In [55]:
np.asarray(ppc['b']).shape
Out[55]:
In [59]:
np.asarray(ppc['b']).mean(axis=0)
Out[59]:
In [61]:
fig, ax = plt.subplots(1,3,figsize=(14,6))
ax[0].pcolormesh(d)
ax[0].set_aspect('equal')
ax[1].pcolormesh(trace['p'].mean(axis=0))
ax[1].set_aspect('equal')
a = ax[2].pcolormesh(np.asarray(ppc['b']).mean(axis=0))
ax[2].set_aspect('equal')
In [47]:
trace['p'].shape
Out[47]:
In [49]:
plt.hist(trace['p'][:,0,9])
plt.hist(trace['p'][:,1,9])
Out[49]:
In [51]:
plt.hist(np.asarray(ppc['b'])[:,0,9], 20)
plt.hist(np.asarray(ppc['b'])[:,1,9], 20)
Out[51]:
In [79]:
a = np.array([[1, 2, 0, 0],
[5, 3, 0, 4],
[0, 0, 0, 7],
[9, 3, 0, 0]])
k = np.array([[0,1,0],[1,1,1],[0,1,0]])
from scipy import ndimage
k
Out[79]:
In [80]:
fig, ax = plt.subplots(1,3, figsize=(12,5))
ax[0].pcolormesh(d)
ax[1].pcolormesh(ndimage.convolve(d, k, mode='reflect',))# cval=0.0))
ax[2].pcolormesh(ndimage.convolve(d, k, mode='constant'))#, cval=0.0))
ax[0].set_aspect('equal')
ax[1].set_aspect('equal')
ax[2].set_aspect('equal')
In [95]:
with pm.Model() as model:
p = pm.Uniform('p', 0 ,1, shape=(10,10))
b = pm.Bernoulli('b', p=p, shape=(10,10), observed=d)
sm = pm.Deterministic('sm', ndimage.convolve(b, k, mode='constant') )
trace = pm.sample(5000)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: