-
Notifications
You must be signed in to change notification settings - Fork 8
/
experiment.py
312 lines (279 loc) · 10.8 KB
/
experiment.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
Copyright (C) eqtgroup.com Ltd 2022
https://github.com/EQTPartners/sire
License: MIT, https://github.com/EQTPartners/sire/LICENSE.md
"""
from datetime import datetime
import warnings
import altair as alt
import pandas as pd
from dateutil.relativedelta import *
from extrapolations import Extrapolations
from dataset import Dataset
from typing import Tuple, Union
# warnings.filterwarnings("ignore")
pd.options.mode.chained_assignment = None # default='warn'
class Experiment:
"""The abstraction class of an experiment for a company:
i.e. extrapolations from all possible dates,
from which metrics are calculated and visualizations are made."""
def __init__(
self,
dataset: Dataset,
org_id: str,
extrapolate_len: int,
n_trials: int,
method: str,
yoy_step: int = 12,
) -> None:
"""Initialize Experiment class.
Args:
dataset (Dataset): a Dataset instance.
org_id (str): the ID of the company to be extrapolated.
extrapolate_len (int): the number of data points to be extrapolated.
n_trials (int): the number of trials to carry out.
method (str): the method to sample metric multiply for the next period;
the recommended value is "probability_matching".
yoy_step (int, optional): the time steps (months) used for calculating growth metric.
Defaults to 12.
"""
self.dataset = dataset
self.method = method
self.org_id = org_id
self.extrapolate_len = extrapolate_len
self.n_trials = n_trials
self.yoy_step = yoy_step
self.reset()
def reset(self) -> None:
"""Reset the whole experiment object."""
self.run_dates = self.get_run_dates()
self.extrapolations = []
self.org_metric_df = None
self.neg_likelihood_matrix = None
def get_run_dates(self) -> list:
"""Obtain the dates that may be possible to run extrapolation.
Returns:
list: the list of dates.
"""
_df = self.dataset.data
return list(
_df[_df.id == self.org_id].sort_values(by="date", ascending=True)["date"]
)
def has_run(self) -> bool:
"""Check if the experiment has been run.
Returns:
bool: has been run (True) or not (False).
"""
return len(self.extrapolations) == len(self.run_dates)
def is_empty(self) -> bool:
"""Check if all extrapolations are empty.
Returns:
bool: all empty (True) or not (False).
"""
n_empty_extrapolations = 0
for extrapolations in self.extrapolations:
if extrapolations.is_empty():
n_empty_extrapolations += 1
return n_empty_extrapolations == len(self.extrapolations)
def get_extrapolations_starts_fr(
self, start_date: datetime
) -> Union[Extrapolations, None]:
"""Get an extrapolation that starts from a particular date.
Args:
start_date (datetime): a calendar date when the extrapolation shall starts from.
Returns:
Extrapolations: an extrapolation starting from start_date.
"""
for entry in self.extrapolations:
if entry.trials[0].first_dt == start_date:
return entry
return None
def run(self, params: dict = {}) -> None:
"""Run this experiment.
Args:
params (dict, optional): additional parameters such as {"filter_type": "smooth"};
Defaults to {}.
"""
self.run_params = params
if self.has_run():
print("Reset Extrapolations for {} !".format(self.org_id))
self.extrapolations = []
for dt in self.run_dates:
extrapolations = Extrapolations(
self.dataset,
self.org_id,
dt,
self.extrapolate_len,
self.n_trials,
self.method,
yoy_step=self.yoy_step,
)
print(str(dt)[:7], end=" ", flush=True)
extrapolations.run(params=params)
self.extrapolations.append(extrapolations)
print()
if self.is_empty():
warnings.warn(
"No valid extrapolation was generated for company {}, please check.".format(
self.org_id
)
)
def get_extrapolations_metric_mean_confidence_interval_df_fr(
self, start_date: datetime, confidence: float = 0.95
) -> Union[pd.DataFrame, None]:
"""Obtain the stats (e.g. mean and confidence interval bounds) for an extrapolation
starting from a particular date.
Args:
start_date (datetime): a calendar date when the extrapolation shall starts from.
confidence (int, optional): the confidence interval. Defaults to 0.95.
Returns:
pd.DataFrame: the result DataFrame with mean, min, max, upper&lower CI bounds;
if no valid extrapolation is available, return None.
"""
extrapolation = self.get_extrapolations_starts_fr(start_date)
if extrapolation is None:
return None
else:
return extrapolation.get_extrapolations_metric_mean_confidence_interval(
confidence
)
def get_org_metric_df(self) -> pd.DataFrame:
"""Get all time-series data for the company in scope.
Returns:
pd.DataFrame: the time-series data for the company in scope.
"""
if self.org_metric_df is not None:
return self.org_metric_df
tmp_df = self.dataset.data
metric_name = self.dataset.metric_name
self.org_metric_df = tmp_df[(tmp_df.id == self.org_id)][["date", metric_name]]
return self.org_metric_df
def get_smooth_metric_mean_confidence_interval_df_fr(
self, start_date: datetime, confidence: float = 0.95
) -> Union[pd.DataFrame, None]:
"""Obtain the smoothed know data points (e.g. mean and confidence interval bounds)
from an extrapolation starting from a particular date.
Args:
start_date (datetime): a calendar date when the extrapolation shall starts from.
confidence (int, optional): the confidence interval. Defaults to 0.95.
Returns:
pd.DataFrame: the result DataFrame with mean, min, max, upper&lower CI bounds;
if no valid extrapolation is available, return None.
"""
extrapolation = self.get_extrapolations_starts_fr(start_date)
if extrapolation is None:
return None
else:
return extrapolation.get_smooth_metric_mean_confidence_interval(confidence)
def plot_extrapolations_start_from(
self,
start_date: datetime,
confidence: float = 0.95,
width: int = 750,
height: int = 500,
) -> alt.LayerChart:
"""Plot the extrapolation from a particular date.
Args:
start_date (datetime): a calendar date when the extrapolation shall starts from.
confidence (int, optional): the confidence interval. Defaults to 0.95.
width (int, optional): the width of the chart. Defaults to 750.
height (int, optional): the height of the chart. Defaults to 500.
Returns:
alt.LayerChart: the chart to be rendered.
"""
gt_df = self.get_org_metric_df()
mean_conf_band = self.get_extrapolations_metric_mean_confidence_interval_df_fr(
start_date, confidence
)
if mean_conf_band is None or len(mean_conf_band) < 1:
return None
metric_name = self.dataset.metric_name
# add a dummy row for know metric
known_metric_dt = start_date + relativedelta(months=-1)
known_metric_df = gt_df[gt_df["date"] == known_metric_dt]
if len(known_metric_df) > 0:
known_metric = known_metric_df.iloc[0][metric_name]
mean_conf_band = mean_conf_band.append(
{
"prediction_date": known_metric_dt,
"{}_mean".format(metric_name): known_metric,
"{}_{}_lower_bound".format(
metric_name, str(int(confidence * 100))
): known_metric,
"{}_{}_upper_bound".format(
metric_name, str(int(confidence * 100))
): known_metric,
"{}_min".format(metric_name): known_metric,
"{}_max".format(metric_name): known_metric,
},
ignore_index=True,
)
gt_line = (
alt.Chart(gt_df)
.mark_line()
.encode(x="date", y="{}:Q".format(metric_name), color=alt.value("#00AAFF"))
)
mean_line = (
alt.Chart(mean_conf_band)
.mark_line(strokeDash=[5, 2])
.encode(
x="prediction_date",
y="{}_mean:Q".format(metric_name),
color=alt.value("#4b0082"),
)
)
conf_band_area = (
alt.Chart(mean_conf_band)
.mark_area(opacity=0.3, color="green")
.encode(
x="prediction_date",
y="{}_{}_lower_bound:Q".format(metric_name, str(int(confidence * 100))),
y2="{}_{}_upper_bound:Q".format(
metric_name, str(int(confidence * 100))
),
)
)
band_area = (
alt.Chart(mean_conf_band)
.mark_area(opacity=0.3, color="green")
.encode(
x="prediction_date",
y="{}_min:Q".format(metric_name),
y2="{}_max:Q".format(metric_name),
)
)
smooth_mean_conf_band = self.get_smooth_metric_mean_confidence_interval_df_fr(
start_date, confidence
)
smooth_band_area = (
alt.Chart(smooth_mean_conf_band)
.mark_area(opacity=0.3, color="black")
.encode(
x="date",
y="{}_min:Q".format(metric_name),
y2="{}_max:Q".format(metric_name),
)
)
smooth_mean_line = (
alt.Chart(smooth_mean_conf_band)
.mark_line(strokeDash=[5, 2])
.encode(
x="date",
y="{}_mean:Q".format(metric_name),
color=alt.value("#373737"),
)
)
return alt.layer(
conf_band_area,
band_area,
smooth_band_area,
smooth_mean_line,
mean_line,
gt_line,
).properties(
width=width,
height=height,
title="Extrapolations for {} starting from {}".format(
self.org_id, start_date
),
)