/
inferences.py
97 lines (87 loc) · 4.3 KB
/
inferences.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
import numpy as np
import pandas as pd
from causalimpact.misc import unstandardize
def compile_posterior_inferences(results, data, df_pre, df_post,
post_period_response, alpha,
orig_std_params, estimation):
if estimation == "MLE":
# Compute point predictions of counterfactual (in standardized space)
if df_post is not None:
predict = results.get_prediction()
forecast = results.get_forecast(
steps=len(df_post), exog=df_post.iloc[:, 1:], alpha=alpha)
else:
pre_len = results.model.nobs - len(post_period_response)
predict = results.get_prediction(end=pre_len - 1)
forecast = results.get_prediction(start=pre_len)
# Compile summary statistics (in original space)
pre_pred = unstandardize(predict.predicted_mean, orig_std_params)
pre_pred.index = df_pre.index
post_pred = unstandardize(forecast.predicted_mean, orig_std_params)
post_pred.index = df_post.index
point_pred = pd.concat([pre_pred, post_pred])
pre_ci = unstandardize(predict.conf_int(alpha=alpha), orig_std_params)
pre_ci.index = df_pre.index
post_ci = unstandardize(forecast.conf_int(alpha=alpha),
orig_std_params)
post_ci.index = df_post.index
ci = pd.concat([pre_ci, post_ci])
point_pred_lower = ci.iloc[:,0].to_frame()
point_pred_upper = ci.iloc[:,1].to_frame()
response = data.iloc[:, 0]
response_index = data.index
response = pd.DataFrame(response)
cum_response = np.cumsum(response)
cum_pred = np.cumsum(point_pred)
cum_pred_upper = np.cumsum(point_pred_upper)
cum_pred_lower = np.cumsum(point_pred_lower)
data = pd.concat([point_pred, point_pred_upper,
point_pred_lower, cum_pred, cum_pred_lower,
cum_pred_upper], axis=1)
data = pd.concat([response,
cum_response], axis=1).join(data, lsuffix='l')
data.columns = ["response", "cum_response", "point_pred",
"point_pred_upper", "point_pred_lower", "cum_pred",
"cum_pred_lower", "cum_pred_upper"]
point_effect = (data.response - data.point_pred).to_frame()
point_effect_upper = (data.response -
data.point_pred_upper).to_frame()
point_effect_lower = (data.response -
data.point_pred_lower).to_frame()
cum_effect = point_effect.copy()
cum_effect.loc[df_pre.index[0]:df_pre.index[-1]] = 0
cum_effect = np.cumsum(cum_effect)
cum_effect_upper = point_effect_upper.copy()
cum_effect_upper.loc[df_pre.index[0]:df_pre.index[-1]] = 0
cum_effect_upper = np.cumsum(cum_effect_upper)
cum_effect_lower = point_effect_lower.copy()
cum_effect_lower.loc[df_pre.index[0]:df_pre.index[-1]] = 0
cum_effect_lower = np.cumsum(cum_effect_lower)
data = pd.concat([data, point_effect, point_effect_upper,
point_effect_lower, cum_effect, cum_effect_lower,
cum_effect_upper], axis=1)
# Create DataFrame of results
data.columns = ["response", "cum_response", "point_pred",
"point_pred_upper", "point_pred_lower", "cum_pred",
"cum_pred_lower", "cum_pred_upper", "point_effect",
"point_effect_lower", "point_effect_upper",
"cum_effect", "cum_effect_lower", "cum_effect_upper"]
data.index = response_index
# index = np.concatenate([results.data.orig_endog.index,
# df_post.iloc[:, 0].index])
# data.index = index
# import pdb
# pdb.set_trace()
# Undo standardization (if any)
series = data
# summary = compile_summary_table(data_post, predict_mean, alpha)
# report = interpret_summary_table(summary)
inferences = {"series": series,
# "summary": summary,
# "report": report
}
return inferences
else:
raise NotImplementedError()
def compile_na_inferences():
pass