Path: blob/master/notebooks/book2/inf/advi_beta_binom_pymc.ipynb
1193 views
Kernel: Python [conda env:py3713_2]
pymc's ADVI implementation for coin toss problem
In [5]:
In [12]:
Dataset
Now, we will create the dataset. we sample theta_true
(probability of occurring head) random variable from the prior distribution which is Beta in this case. Then we sample n_samples
coin tosses from likelihood distribution which is Bernouli in this case.
In [10]:
Out[10]:
Prior, Likelihood, and True Posterior
For coin toss problem, since we know the closed form solution of posterior, we compare the distributions of Prior, Likelihood, and True Posterior below.
In [13]:
Out[13]:
In [14]:
Out[14]:
Finished [100%]: Average Loss = 4.9012
INFO:pymc.variational.inference:Finished [100%]: Average Loss = 4.9012
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/tmp/ipykernel_2708/1222123001.py in <module>
10
11 trace_approx = approx.sample(1000)
---> 12 thetas = trace_approx["theta"]
~/anaconda3/envs/py3713_2/lib/python3.7/site-packages/arviz/data/inference_data.py in __getitem__(self, key)
251 """Get item by key."""
252 if key not in self._groups_all:
--> 253 raise KeyError(key)
254 return getattr(self, key)
255
KeyError: 'theta'
In [38]:
In [39]:
Out[39]:
True posterior & pymc q(x)
In [40]:
Out[40]:
Plot of loc and scale for variational distribution
In [43]:
Out[43]:
In [ ]:
In [ ]: