-
Notifications
You must be signed in to change notification settings - Fork 0
/
pymc3_altair.py
130 lines (114 loc) · 3.67 KB
/
pymc3_altair.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import altair as alt
import pymc3 as pm
import pandas as pd
import numpy as np
def plot_fit_altair(m, t, df, x_range=(1, 134)):
with m:
posterior_predictor = pm.sample_posterior_predictive(t)
df = pd.DataFrame(
{
"min_posterior_predictor": np.min(posterior_predictor["y"], axis=0) * 10000,
"max_posterior_predictor": np.max(posterior_predictor["y"], axis=0) * 10000,
"mean_posterior_predictor": np.mean(posterior_predictor["y"], axis=0)
* 10000,
"Total_scaled": df["Total_scaled"].to_numpy() * 10000,
"Week_nr": df["Week_nr"],
}
)
tooltip = [
alt.Tooltip("Total_scaled:Q", title="Registered Decks", format=",.0f"),
alt.Tooltip("max_posterior_predictor:Q", title="Model Max", format=",.0f"),
alt.Tooltip("mean_posterior_predictor:Q", title="Model Mean", format=",.0f"),
alt.Tooltip("min_posterior_predictor:Q", title="Model Min", format=",.0f"),
]
x = alt.X(
"Week_nr:Q",
title="Week",
scale=alt.Scale(domain=x_range, zero=False, nice=False),
)
real_registrations = (
alt.Chart(df)
.mark_line()
.encode(
x=x,
y="Total_scaled",
tooltip=tooltip,
)
)
model_min_max = (
alt.Chart(df)
.mark_area(opacity=0.2, color="#333333")
.encode(
x=x,
y="min_posterior_predictor:Q",
y2="max_posterior_predictor:Q",
tooltip=tooltip,
)
)
model_mean = (
alt.Chart(df)
.mark_line(color="#333333", opacity=0.7)
.encode(
x=x,
y="mean_posterior_predictor",
tooltip=tooltip,
)
)
chart = model_min_max + model_mean + real_registrations
chart.layer[0].encoding.y.title = "Decks Registered"
return chart
def plot_prior_predictor_altair(m, df, x_range=(1, 134)):
with m:
posterior_predictor = pm.sample_prior_predictive()
df = pd.DataFrame(
{
"min_posterior_predictor": np.min(posterior_predictor["y"], axis=0) * 10000,
"max_posterior_predictor": np.max(posterior_predictor["y"], axis=0) * 10000,
"mean_posterior_predictor": np.mean(posterior_predictor["y"], axis=0)
* 10000,
"Total_scaled": df["Total_scaled"].to_numpy() * 10000,
"Week_nr": df["Week_nr"],
}
)
tooltip = [
alt.Tooltip("Total_scaled:Q", title="Registered Decks", format=",.0f"),
alt.Tooltip("max_posterior_predictor:Q", title="Model Max", format=",.0f"),
alt.Tooltip("mean_posterior_predictor:Q", title="Model Mean", format=",.0f"),
alt.Tooltip("min_posterior_predictor:Q", title="Model Min", format=",.0f"),
]
x = alt.X(
"Week_nr:Q",
title="Week",
scale=alt.Scale(domain=x_range, zero=False, nice=False),
)
real_registrations = (
alt.Chart(df)
.mark_line()
.encode(
x=x,
y="Total_scaled",
tooltip=tooltip,
)
)
model_min_max = (
alt.Chart(df)
.mark_area(opacity=0.2, color="#333333")
.encode(
x=x,
y="min_posterior_predictor:Q",
y2="max_posterior_predictor:Q",
tooltip=tooltip,
)
)
model_mean = (
alt.Chart(df)
.mark_line(color="#333333", opacity=0.7)
.encode(
x=x,
y="mean_posterior_predictor",
tooltip=tooltip,
)
)
chart = model_min_max + model_mean + real_registrations
chart.layer[0].encoding.y.title = "Decks Registered"
return chart