1
|
|
|
# --- |
2
|
|
|
# jupyter: |
3
|
|
|
# jupytext: |
4
|
|
|
# cell_metadata_json: true |
5
|
|
|
# formats: ipynb,py:percent |
6
|
|
|
# notebook_metadata_filter: language_info |
7
|
|
|
# text_representation: |
8
|
|
|
# extension: .py |
9
|
|
|
# format_name: percent |
10
|
|
|
# format_version: '1.3' |
11
|
|
|
# jupytext_version: 1.5.2 |
12
|
|
|
# kernelspec: |
13
|
|
|
# display_name: Python 3 |
14
|
|
|
# language: python |
15
|
|
|
# name: python3 |
16
|
|
|
# language_info: |
17
|
|
|
# codemirror_mode: |
18
|
|
|
# name: ipython |
19
|
|
|
# version: 3 |
20
|
|
|
# file_extension: .py |
21
|
|
|
# mimetype: text/x-python |
22
|
|
|
# name: python |
23
|
|
|
# nbconvert_exporter: python |
24
|
|
|
# pygments_lexer: ipython3 |
25
|
|
|
# version: 3.6.6 |
26
|
|
|
# --- |
27
|
|
|
|
28
|
|
|
# %% {"tags": ["parameters"]} |
29
|
|
|
# Our default parameters |
30
|
|
|
# This cell has a "parameters" tag, means that it defines the parameters for use in the notebook |
31
|
|
|
run_date = '2018-11-18' |
32
|
|
|
source_id = 'sensor1' |
33
|
|
|
nb_days = 7 |
34
|
|
|
|
35
|
|
|
# %% |
36
|
|
|
from datetime import datetime, timedelta |
37
|
|
|
import matplotlib.pyplot as plt |
38
|
|
|
import os |
39
|
|
|
import pandas as pd |
40
|
|
|
from pylab import rcParams |
41
|
|
|
import scrapbook as sb |
42
|
|
|
import statsmodels.api as sm |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
# %% |
46
|
|
|
data_dir = "../data/input/step1" |
47
|
|
|
data = None |
48
|
|
|
run_datetime = datetime.strptime(run_date, '%Y-%m-%d') |
49
|
|
|
for i in range(nb_days): |
50
|
|
|
deltatime = run_datetime - timedelta(i) |
51
|
|
|
month_partition = deltatime.strftime("%Y-%m") |
52
|
|
|
delta = datetime.strftime(deltatime, '%Y-%m-%d') |
53
|
|
|
file = os.path.join(data_dir, month_partition, delta + "-" + source_id + ".csv") |
54
|
|
|
if os.path.exists(file): |
55
|
|
|
print("Loading " + file) |
56
|
|
|
new = pd.read_csv(file) |
57
|
|
|
if data is not None: |
58
|
|
|
data = pd.concat([data, new]) |
59
|
|
|
else: |
60
|
|
|
data = new |
61
|
|
|
|
62
|
|
|
# %% |
63
|
|
|
data['date'] = data['date'].apply(lambda x : datetime.strptime(x, "%Y-%m-%d %H:%M:%S")) |
64
|
|
|
print(data['date'].describe()) |
65
|
|
|
data.describe() |
66
|
|
|
|
67
|
|
|
# %% |
68
|
|
|
data = data.sort_values('date').set_index('date', drop=True) |
69
|
|
|
data = data.asfreq(freq="5min") |
70
|
|
|
data.head(5) |
71
|
|
|
|
72
|
|
|
# %% |
73
|
|
|
pred = sm.load("../data/input/step2/prediction_model_" + run_date + "-" + source_id) |
74
|
|
|
|
75
|
|
|
# %% |
76
|
|
|
pred_ci = pred.conf_int() |
77
|
|
|
rcParams['figure.figsize'] = 18, 8 |
78
|
|
|
fig, ax = plt.subplots() |
79
|
|
|
ax.plot(data[data.index > (run_datetime - timedelta(3))]['mydata'], label='observed') |
80
|
|
|
ax.plot(pred.predicted_mean, label='One-step ahead Forecast', alpha=.7) |
81
|
|
|
ax.fill_between(pred_ci.index, |
82
|
|
|
pred_ci.iloc[:, 0], |
83
|
|
|
pred_ci.iloc[:, 1], color='k', alpha=.2) |
84
|
|
|
ax.set_xlabel('Date') |
85
|
|
|
ax.set_ylabel('mydata') |
86
|
|
|
ax.set(title='Results on {}'.format(run_date)) |
87
|
|
|
fig.legend() |
88
|
|
|
|
89
|
|
|
# %% |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|