Skip to content

Commit 12d2ff0

Browse files
committed
prototype for bayesian curve regression
1 parent aee64c7 commit 12d2ff0

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import pymc3 as pm\n",
10+
"import matplotlib.pyplot as plt\n",
11+
"import numpy as np\n",
12+
"from utils import ECDF\n",
13+
"from data import load_decay\n",
14+
"import pandas as pd\n",
15+
"import theano.tensor as tt\n",
16+
"\n",
17+
"%load_ext autoreload\n",
18+
"%autoreload 2\n",
19+
"%matplotlib inline\n",
20+
"%config InlineBackend.figure_format = 'retina'"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"np.random.seed(42)\n",
30+
"\n",
31+
"df = load_decay()\n",
32+
"df.head(5)"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"ax = df['activity'].plot()\n",
42+
"# ax.set_ylim(30, 40)"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"metadata": {},
49+
"outputs": [],
50+
"source": [
51+
"with pm.Model() as model:\n",
52+
" A = pm.HalfNormal('A', sd=100)\n",
53+
" tau = pm.Exponential('tau', lam=1)\n",
54+
" C = pm.Normal('C', sd=100)\n",
55+
" \n",
56+
" sd = pm.HalfCauchy('sd', beta=1)\n",
57+
" \n",
58+
" link = A * np.exp(-df['t'].values / tau) + C\n",
59+
" \n",
60+
" like = pm.Normal('activity', mu=link, sd=sd, observed=df['activity'].values)"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"with model:\n",
70+
" trace = pm.sample(2000, tune=2000)\n",
71+
" # Note: Sampler may pause for a while after finishing"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"traces = pm.traceplot(trace)\n",
81+
"# despine_trace(traces)"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"metadata": {},
88+
"outputs": [],
89+
"source": []
90+
}
91+
],
92+
"metadata": {
93+
"kernelspec": {
94+
"display_name": "bayesian-modelling-tutorial",
95+
"language": "python",
96+
"name": "bayesian-modelling-tutorial"
97+
},
98+
"language_info": {
99+
"codemirror_mode": {
100+
"name": "ipython",
101+
"version": 3
102+
},
103+
"file_extension": ".py",
104+
"mimetype": "text/x-python",
105+
"name": "python",
106+
"nbconvert_exporter": "python",
107+
"pygments_lexer": "ipython3",
108+
"version": "3.6.6"
109+
}
110+
},
111+
"nbformat": 4,
112+
"nbformat_minor": 2
113+
}

notebooks/data.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def load_finches_2012():
88
path = '../data/finch_beaks_2012.csv'
99
return load_finches(path)
1010

11+
1112
def load_finches_1975():
1213
path = '../data/finch_beaks_1975.csv'
1314
return load_finches(path)
@@ -50,3 +51,20 @@ def load_kruschke():
5051
df = pd.read_csv('../data/iq.csv', index_col=0) # comment out the path to the file for students.
5152
df = jn.DataFrame(df).label_encode('treatment')
5253
return df
54+
55+
56+
# Constants for load_decay
57+
tau = 71.9 # indium decay half life
58+
A = 60 # starting magnitude
59+
C = 2 # measurement error
60+
noise_scale = 1
61+
62+
63+
def load_decay():
64+
t = np.arange(0, 1000)
65+
def decay_func(ts, noise):
66+
return A * np.exp(-t/tau) + C + np.random.normal(0, noise, size=(len(t)))
67+
68+
data = {'t': t, 'activity': decay_func(t, noise_scale)}
69+
df = pd.DataFrame(data)
70+
return df

0 commit comments

Comments
 (0)