-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun_forecast.py
157 lines (135 loc) · 5.64 KB
/
run_forecast.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 10 23:15:09 2017
@author: gjacopo
"""
from __future__ import print_function
import requests
import pandas as pd
import numpy as np#analysis:ignore
from matplotlib import pyplot as plt
from fbprophet import Prophet
PROTOCOL = "http"
API_LANG = "en"
API_FMT = "json"
API_DOMAIN = 'ec.europa.eu/eurostat/wdds'
API_VERS = 2.1
API_URL = "{}://{}/rest/data/v{}/{}/{}".format(
PROTOCOL, API_DOMAIN, API_VERS, API_FMT, API_LANG
)
def build_url(indicator, **kwargs):
url = "{}/{}?".format(API_URL, indicator[0])
if 'geo' in kwargs:
url = "{}geo={}&".format(url, kwargs.pop('geo', None))
if 'time' in kwargs:
url = "{}time={}&".format(url, kwargs.pop('time', None))
#_izip_replicate = lambda d : [[(k,i) for i in d[k]] if isinstance(d[k], (tuple,list)) \
# else (k, d[k]) for k in d]
_no_replicate = lambda d : d.items()
filters = '&'.join(['{k}={v}'.format(k=k, v=v[0]) for (k, v) in _no_replicate(kwargs)])
url = "{}{}".format(url, filters)
return url
def get_response(url):
# request the URL
session = requests.session()
try:
response = session.head(url)
response.raise_for_status()
except:
raise IOError("ERROR: wrong request formulated")
else:
print ("OK: status={}".format(response.status_code))
# load the data
try:
response = session.get(url)
except:
raise IOError('error retrieveing response from URL')
if API_FMT == 'json':
resp = response.json()
elif API_FMT == 'unicode':
resp = response.text
return resp
def build_dataframe(resp):
lbl2idx = resp['dimension']['time']['category']['index']
idx2lbl = {v:k for (k,v) in lbl2idx.items()}
data = resp['value']
data = {idx2lbl[int(k)]:v for (k,v) in data.items()}
try:
assert None # not needed, and also because it adds a day date
from datetime import datetime
table = {datetime.strptime(k.replace('M','-'), '%Y-%m'): v \
for (k,v) in data.items()}
# instead we will use pandas.datetime below
except:
table = {k.replace('M','-'):v for (k,v) in data.items()}
# create data frame with columns ds (date type) and y, the time series.
df = pd.DataFrame(list(table.items()), columns=['ds','y'])
# df = df[df['y'].notnull()]
df.sort_values('ds', inplace=True)
ds_last = df['ds'].values[-1]
df['ds'] = pd.to_datetime(df['ds'])
return df, ds_last
def predict_prophet(df, nyears):
# we fit the model by instantiated a new Prophet object.
m = Prophet(growth = "linear", yearly_seasonality=True, weekly_seasonality=False)
# we call its fit method and pass in the historical dataframe
m.fit(df)
# we extend into the future a specified number of days
future = m.make_future_dataframe(periods=12*nyears, freq='M')
fcst = m.predict(future)
return m, fcst
def plot_historical(df, ylabel="", last=""):
xlabel = "Time"
fig = plt.figure(facecolor='w', figsize=(10, 6))
ax = fig.add_subplot(111)
ax.plot(df['ds'], df['y'], 'k.')
ax.plot(df['ds'], df['y'], ls='-', c='#0072B2')
ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
ax.set_xlabel(xlabel, fontsize=14);
ax.set_ylabel(ylabel, fontsize=14)
fig.suptitle("Historical data (last: {})".format(last), fontsize=16)
fig.tight_layout()
def plot_predict(m, fcst, ylabel="", period="", last=""):
xlabel = "Time"
fig = m.plot(fcst, uncertainty=True)
plt.axvline(pd.to_datetime(last), color='r', linestyle='--', lw=2)
plt.xlabel(xlabel, fontsize=14);
plt.ylabel(ylabel, fontsize=14)
fig.suptitle("Forecast data ({} years)".format(period), fontsize=16)
# fig.savefig('tour_occ_nim_predict.png')
fig = m.plot_components(fcst, uncertainty=True);
fig.suptitle("Forecast components", fontsize=16)
def run_forecast(indicator, geo, filters, period, label="indic_to"):
# input data loading and formatting
url = build_url(indicator, geo=geo, **filters)
resp = get_response(url)
df, ds_last = build_dataframe(resp)
df.head(); df.tail()
ylabel = "{} : {} - {}".format(indicator[0], filters[label][1], geo)
plot_historical(df, ylabel=ylabel, last=ds_last)
# forecast configuration and estimation
m, fcst = predict_prophet(df, period)
# fcst[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
plot_predict(m, fcst, ylabel=ylabel, period=period, last=ds_last)
GEO = "EU28"
# TIME = all
NYEARS = 1
## run tour_occ_nim estimation
indicator = (u'tour_occ_nim', "Tour accomodation")
## http://appsso.eurostat.ec.europa.eu/nui/show.do?dataset=tour_occ_nim&lang=en
filters = {'unit': (u'NR', "Number of nights"),
'nace_r2': (u'I551', "Hotels; holiday and other short-stay accommodation..."),
'indic_to': (u'B006', "Nights spent, total")
}
run_forecast(indicator, GEO, filters, NYEARS)
## run une_rt_m estimation
indicator = (u'une_rt_m', "Unemployment")
# http://appsso.eurostat.ec.europa.eu/nui/show.do?dataset=une_rt_m&unit=THS_PER&age=TOTAL&sex=T&s_adj=NSA
filters = {'unit': (u'THS_PER', "Population count"),
'age': (u'TOTAL', "Age"),
'sex': (u'T', "Sex"),
# 's_adj': (u'SA', "Seasonally adjusted data")
's_adj': (u'NSA', "Unadjusted data")
}
run_forecast(indicator, GEO, filters, NYEARS, label='s_adj')