Skip to content

Instantly share code, notes, and snippets.

@aakhmetz
Last active October 14, 2022 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aakhmetz/86f39f412a85f346e200d300bd384367 to your computer and use it in GitHub Desktop.
Save aakhmetz/86f39f412a85f346e200d300bd384367 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 19,
"id": "a9368988-586b-4a7f-89f0-2c9560a5aaca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on PyMC v4.2.1\n",
"Running on Aesara v2.8.6\n"
]
}
],
"source": [
"import arviz as az\n",
"import numpy as np\n",
"import pymc as pm\n",
"\n",
"import aesara as ae\n",
"import aesara.tensor.math as tt\n",
"import aesara.tensor as at\n",
"\n",
"print(f\"Running on PyMC v{pm.__version__}\")\n",
"print(f\"Running on Aesara v{ae.__version__}\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "3c995bbb-08b1-49fc-8829-d37678ac5454",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Only 3 samples in chain.\n",
"Sequential sampling (1 chains in 1 job)\n",
"CompoundStep\n",
">Metropolis: [μ]\n",
">Metropolis: [σ]\n",
"Sampling 1 chain for 0 tune and 3 draw iterations (0 + 3 draws total) took 0 seconds.\n",
"/home/aakhmetz/.local/lib/python3.10/site-packages/arviz/data/base.py:220: UserWarning: More chains (3) than draws (2). Passed array should have shape (chains, draws, *shape)\n",
" warnings.warn(\n",
"/tmp/ipykernel_1256085/528024374.py:14: UserWarning: The number of samples is too small to check convergence reliably.\n",
" trace = pm.sample(3, pm.Metropolis(), tune=0, chains=1, progressbar=False, init='advi')\n"
]
}
],
"source": [
"with pm.Model() as model: \n",
" D = tt.as_tensor_variable(5, dtype=\"int64\") \n",
" \n",
" μ = pm.HalfCauchy('μ', 5.0, initval = 1.0)\n",
" σ = pm.HalfCauchy('σ', 6.0, initval = 2.0)\n",
" \n",
" ### Lognormal distribution\n",
" σlog = tt.sqrt(tt.log((σ / μ)**2 + 1));\n",
" μlog = tt.log(μ) - σlog**2 / 2;\n",
" comp = pm.Lognormal.dist(mu = μlog, sigma = σlog)\n",
" p, _ = ae.scan(lambda t: tt.exp(pm.logcdf(comp, t + 0.5)), sequences = tt.arange(0, D + 1))\n",
" pm.Deterministic('p', p)\n",
" \n",
" trace = pm.sample(3, pm.Metropolis(), tune=0, chains=1, progressbar=False, init='advi')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "b6f75e40-8be2-4cf8-9d4d-c3b132ed30f7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1. , 3.25747633, 9.57658511])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([11.4065048 , 11.4065048 , 9.12795217])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([[0.0005593 , 0.01719953, 0.0566348 , 0.10870443, 0.16555878,\n",
" 0.22273289],\n",
" [0.0005593 , 0.01719953, 0.0566348 , 0.10870443, 0.16555878,\n",
" 0.22273289],\n",
" [0.0005593 , 0.01719953, 0.0566348 , 0.10870443, 0.16555878,\n",
" 0.22273289]])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"display(trace.posterior['μ'][0].values)\n",
"display(trace.posterior['σ'][0].values)\n",
"trace.posterior['p'][0].values"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7faf18ef-7a13-4ea7-a79b-1111afb7d317",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment