Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
arnim committed Jan 25, 2019
0 parents commit 73a67f2
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
143 changes: 143 additions & 0 deletions beta-bernoulli.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Beta-bernoulli Model in Stan"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\\begin{aligned}\n",
"\\theta &\\sim Beta(2,2) \\\\\n",
"x_i &\\sim Bernoulli(\\theta)\n",
"\\end{aligned}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pystan"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"code = \"\"\"\n",
"data {\n",
" int<lower=0> N;\n",
" int<lower=0,upper=1> x[N];\n",
"}\n",
"parameters {\n",
" real<lower=0,upper=1> theta;\n",
"}\n",
"model {\n",
" theta ~ beta(2,2);\n",
" for (i in 1:N)\n",
" x[i] ~ bernoulli(theta);\n",
"}\n",
"\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sm = pystan.StanModel(model_code=code)\n",
"fit = sm.sampling(data={'N': 10,'x': [1,1,1,0,0,0,0,0,0,0]})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"fit.plot();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Vectorized version \n",
"Stan supports [vectorization](https://mc-stan.org/docs/2_18/stan-users-guide/vectorization.html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"code_vec = \"\"\"\n",
"data {\n",
" int<lower=0> N;\n",
" int<lower=0,upper=1> x[N];\n",
"}\n",
"parameters {\n",
" real<lower=0,upper=1> theta;\n",
"}\n",
"model {\n",
" theta ~ beta(2,2);\n",
" x ~ bernoulli(theta);\n",
"}\n",
"\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sm_vec = pystan.StanModel(model_code=code_vec)\n",
"fit_vec = sm_vec.sampling(data={'N': 10,'x': [1,1,1,0,0,0,0,0,0,0]})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fit_vec.plot();"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1 change: 1 addition & 0 deletions binder/apt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
htop
2 changes: 2 additions & 0 deletions binder/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pystan
arviz

0 comments on commit 73a67f2

Please sign in to comment.