/
test_time_series_indices.py
318 lines (256 loc) · 10.4 KB
/
test_time_series_indices.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
313
314
315
316
317
318
import os
from typing import Any
import pandas as pd
import pytest
from pycaret.datasets import get_data
from pycaret.time_series import TSForecastingExperiment
os.environ["PYCARET_TESTING"] = "1"
# =============================================================================#
# Data set generation begins here
# =============================================================================#
def _get_univar_noexo_data_with_index_index():
"""Generates multiple datasets of univariate data without exogenous variables
where the index has been set to various types of indices.
"""
# PeriodIndex
data1 = get_data("airline")
# DatetimeIndex
data2 = data1.copy()
data2 = data2.to_timestamp()
# Int64Index
data3 = data1.copy()
data3.reset_index(drop=True, inplace=True)
ids = ["Period", "Datetime", "Int"]
# DateTimeIndex is coerced and returned as PeriodIndex in PyCaret
return [
[(data1, pd.PeriodIndex), (data2, pd.PeriodIndex), (data3, pd.Index)],
ids,
]
def _get_univar_noexo_data_with_index_column():
"""Generates multiple datasets of univariate data without exogenous variables
where the index is specified through a column of different.
"""
# PeriodIndex column
data1 = pd.DataFrame(get_data("airline"))
# DatetimeIndex column
data2 = data1.copy()
data2 = data2.to_timestamp()
# Int64Index column
data3 = data1.copy()
data3.reset_index(drop=True, inplace=True)
data1.reset_index(inplace=True)
data2.reset_index(inplace=True)
data3.reset_index(inplace=True)
data1.rename(columns={"Period": "index"}, inplace=True)
data2.rename(columns={"Period": "index"}, inplace=True)
# String Index (created from datetime index)
data4 = data2.copy()
data4["index"] = data4["index"].dt.strftime("%m/%d/%Y")
ids = ["Period", "Datetime", "Int", "String"]
# DateTimeIndex & String index column is coerced and returned as PeriodIndex
# in PyCaret
return [
[
(data1, pd.PeriodIndex),
(data2, pd.PeriodIndex),
(data3, pd.Index),
(data4, pd.PeriodIndex),
],
ids,
]
def _get_univar_exo_data_with_index_index():
"""Generates multiple datasets of univariate data with exogenous variables
where the index has been set to various types of indices.
"""
# TODO: Find a better source of data ----
data1 = pd.read_csv(
"https://raw.githubusercontent.com/ngupta23/DS6373_TimeSeries/2b40f0071c3b7ec6a05dc0106f64e041f8cbaaef/Projects/gdp_prediction/data/economic_indicators_all_ex_3mo_china_inc_treas3mo.csv"
)
data1["date"] = pd.to_datetime(data1["date"].str.replace(" ", "-"))
data1.set_index("date", inplace=True)
# PeriodIndex
data1.index = data1.index.to_period()
# DatetimeIndex
data2 = data1.copy()
data2 = data2.to_timestamp()
# Int64Index
data3 = data1.copy()
data3.reset_index(drop=True, inplace=True)
ids = ["Period", "Datetime", "Int"]
# DateTimeIndex is coerced and returned as PeriodIndex in PyCaret
return [
[(data1, pd.PeriodIndex), (data2, pd.PeriodIndex), (data3, pd.Index)],
ids,
]
def _get_univar_exo_data_with_index_column():
"""Generates multiple datasets of univariate data with exogenous variables
where the index is specified through a column of different.
"""
# TODO: Find a better source of data ----
data1 = pd.read_csv(
"https://raw.githubusercontent.com/ngupta23/DS6373_TimeSeries/2b40f0071c3b7ec6a05dc0106f64e041f8cbaaef/Projects/gdp_prediction/data/economic_indicators_all_ex_3mo_china_inc_treas3mo.csv"
)
data1["date"] = pd.to_datetime(data1["date"].str.replace(" ", "-"))
data1.set_index("date", inplace=True)
# PeriodIndex
data1.index = data1.index.to_period()
# DatetimeIndex column
data2 = data1.copy()
data2 = data2.to_timestamp()
# Int64Index column
data3 = data1.copy()
data3.reset_index(drop=True, inplace=True)
data1.reset_index(inplace=True)
data2.reset_index(inplace=True)
data3.reset_index(inplace=True)
data1.rename(columns={"date": "index"}, inplace=True)
data2.rename(columns={"date": "index"}, inplace=True)
# String Index (created from datetime index)
data4 = data2.copy()
data4["index"] = data4["index"].dt.strftime("%m/%d/%Y")
ids = ["Period", "Datetime", "Int", "String"]
# DateTimeIndex & String index column is coerced and returned as PeriodIndex
# in PyCaret
return [
[
(data1, pd.PeriodIndex),
(data2, pd.PeriodIndex),
(data3, pd.Index),
(data4, pd.PeriodIndex),
],
ids,
]
# =============================================================================#
# Checker function(s)
# =============================================================================#
def _check_model_creation_and_indices(
exp: TSForecastingExperiment, model: str, expected_return_index_type: Any
):
"""Function to create a few trial models that support both univariate and
multivariate forecasting.
Parameters
----------
exp : TSForecastingExperiment
The Time Series experiment object
model : str
The model to create using the experiment provided
expected_return_index_type: Any
The expected return type of the index of the predictions dataframe
"""
if model in exp.models().index:
model = exp.create_model(model)
preds = exp.predict_model(model)
assert isinstance(preds.index, expected_return_index_type)
exp.plot_model(model)
# =============================================================================#
# Tests begin here
# =============================================================================#
# Includes models from statistical family, reduced regression family and prophet
# since prophet has special handling for indices in patched version in pycaret.
# ETS and Exponential Smoothing are included since ETS was failing in manual testing.
models = ["arima", "ets", "exp_smooth", "lr_cds_dt", "prophet"]
# -----------------------------------------------------------------------------#
# Test 1: Univariate No Exogenous Variables with Data Index set to various types
# -----------------------------------------------------------------------------#
(
univar_noexo_data_with_index_index_plus_return_type,
ids_univar_noexo_data_with_index_index,
) = _get_univar_noexo_data_with_index_index()
@pytest.mark.parametrize("model", models)
@pytest.mark.parametrize(
"data, expected_return_index_type",
univar_noexo_data_with_index_index_plus_return_type,
ids=ids_univar_noexo_data_with_index_index,
)
def test_ts_indices_univar_noexo_index_index(data, model, expected_return_index_type):
"""
Checks working with various types of indices with univariate data without
exogenous variables when index is already set in the dataframe.
"""
exp = TSForecastingExperiment()
exp.setup(data=data, fh=12, fold=2, session_id=42)
_check_model_creation_and_indices(
exp, model=model, expected_return_index_type=expected_return_index_type
)
# -----------------------------------------------------------------------------#
# Test 2: Univariate No Exogenous Variables with Data Index provided through column
# -----------------------------------------------------------------------------#
(
univar_noexo_data_with_index_column_plus_return_type,
ids_univar_noexo_data_with_index_column,
) = _get_univar_noexo_data_with_index_column()
@pytest.mark.parametrize("model", models)
@pytest.mark.parametrize(
"data, expected_return_index_type",
univar_noexo_data_with_index_column_plus_return_type,
ids=ids_univar_noexo_data_with_index_column,
)
def test_ts_indices_univar_noexo_index_column(data, model, expected_return_index_type):
"""
Checks working with various types of indices with univariate data without
exogenous variables when index is provided through a column.
"""
exp = TSForecastingExperiment()
exp.setup(data=data, index="index", fh=12, fold=2, session_id=42)
_check_model_creation_and_indices(
exp, model=model, expected_return_index_type=expected_return_index_type
)
# -----------------------------------------------------------------------------#
# Test 3: Univariate with Exogenous Variables with Data Index set to various types
# -----------------------------------------------------------------------------#
(
univar_exo_data_with_index_index_plus_return_type,
ids_univar_exo_data_with_index_index,
) = _get_univar_exo_data_with_index_index()
@pytest.mark.parametrize("model", models)
@pytest.mark.parametrize(
"data, expected_return_index_type",
univar_exo_data_with_index_index_plus_return_type,
ids=ids_univar_exo_data_with_index_index,
)
def test_ts_indices_univar_exo_index_index(data, model, expected_return_index_type):
"""
Checks working with various types of indices with univariate data with exogenous
variables when index is already set in the dataframe.
"""
exp = TSForecastingExperiment()
exp.setup(
data=data,
target="gdp_change",
fh=2,
fold=2,
session_id=42,
)
_check_model_creation_and_indices(
exp, model=model, expected_return_index_type=expected_return_index_type
)
# -----------------------------------------------------------------------------#
# Test 4: Univariate with Exogenous Variables with Data Index provided through column
# -----------------------------------------------------------------------------#
(
univar_exo_data_with_index_column_plus_return_type,
ids_univar_exo_data_with_index_column,
) = _get_univar_exo_data_with_index_column()
@pytest.mark.parametrize("model", models)
@pytest.mark.parametrize(
"data, expected_return_index_type",
univar_exo_data_with_index_column_plus_return_type,
ids=ids_univar_exo_data_with_index_column,
)
def test_ts_indices_univar_exo_index_column(data, model, expected_return_index_type):
"""
Checks working with various types of indices with univariate data with exogenous
variables when index is already set in the dataframe.
"""
exp = TSForecastingExperiment()
exp.setup(
data=data,
target="gdp_change",
index="index",
fh=2,
fold=2,
session_id=42,
)
_check_model_creation_and_indices(
exp, model=model, expected_return_index_type=expected_return_index_type
)