-
Notifications
You must be signed in to change notification settings - Fork 15
/
forecast.py
30 lines (26 loc) · 1.1 KB
/
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
import pandas as pd
from math import inf
from statsmodels.tsa.ar_model import AutoReg
# A US manufacturer buys raw materials in multiple currencies
purchases = pd.read_excel('Purchases.xlsx')
# For each of those currencies, find the best model to forecast prices
best_model = {}
for currency in purchases.currency:
data = pd.read_excel(f'{currency}.xlsx')
data = data[data[currency] > 0]
best_aic, best_fit = inf, None
for lags in (3, 5, 7, 10, 14, 28, 60, 90, 120, 183, 365, 730, 1095):
model = AutoReg(data[currency], lags=lags)
fit = model.fit()
if fit.aic < best_aic:
best_aic, best_fit = fit.aic, fit
best_model[currency] = best_fit
# Estimate next month's price increase assuming the same volume as today
forecasted_value = 0
for index, row in purchases.iterrows():
fit = best_model[row.currency]
prices = fit.predict(fit.model.nobs, fit.model.nobs + 30)
change = prices.iloc[-1] / prices.iloc[0]
forecasted_value += row.value * change
print('Sales value will move from {:,.0f} to {:,.0f}'.format(
purchases.value.sum(), forecasted_value))