Skip to content

Instantly share code, notes, and snippets.

@sharanry
Last active June 4, 2020 21:14
Show Gist options
  • Save sharanry/9bbc02f855dffbe2b49db4e4ddec0fa9 to your computer and use it in GitHub Desktop.
Save sharanry/9bbc02f855dffbe2b49db4e4ddec0fa9 to your computer and use it in GitHub Desktop.
Simple Stan Test
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pystan"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"simple =\"\"\"\n",
"parameters {\n",
" real y;\n",
"}\n",
"model {\n",
" y ~ normal(0, 1);\n",
"}\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"simple_dat = {}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_031e94ff7ae9f2ee13f89c3744eeaf39 NOW.\n"
]
}
],
"source": [
"sm = pystan.StanModel(model_code=simple)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"fit = sm.sampling(data=simple_dat, iter=1000, chains=1)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.5"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fit.log_prob([1])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_f6d3ca67c9517308bdc118dba9474c3d NOW.\n"
]
}
],
"source": [
"simple2 =\"\"\"\n",
"parameters {\n",
" real y;\n",
"}\n",
"model {\n",
" y ~ normal(0, 1);\n",
" y ~ normal(0, 1);\n",
"}\n",
"\"\"\"\n",
"sm2 = pystan.StanModel(model_code=simple2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"fit2 = sm2.sampling(data=simple_dat, iter=1000, chains=1)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1.0"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fit2.log_prob([1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment