#!/usr/bin/env python # coding: utf-8 # In[9]: # Preliminaries import pandas as pd from exabel_data_sdk.client.api.export_api import ExportApi from exabel_data_sdk.query.column import Column export_api = ExportApi() # # Forecasting # Exabel offers multiple univariate forecast models directly available in the DSL, from the # [statsmodels](https://www.statsmodels.org) Python library and Facebook’s [Prophet](https://facebook.github.io/prophet/) library. # Forecasts are useful if you want to extend a time series into the future, e.g. for use in models. # ## Additive vs multiplicative models # All of these statistical methods are by default linear, meaning that they assume growth trends are linear with time and seasonality effects are additive. However, for typical financial KPIs such as revenue, it is more natural to assume that growth trends are exponential, meaning that a company’s revenue grows by a certain percentage each year rather than projecting that it will grow by a fixed amount per year. Similarly, it is more natural to assume that the seasonality trends are multiplicative, meaning that revenue in Q4 is modelled to be e.g. 20% higher than in Q3, rather than modelled to be $50M higher in Q4. # # Such time series can be modelled with exponential growth and multiplicative seasonality by taking the logarithm first, forecasting the logarithmic values, and finally exponentiating to get back the original values. # # Let's compare the difference in the produced forecasts with an example: # In[25]: company = "CMG US" sales_actual = export_api.signal_query("Sales_Actual_fiscal", company, start_time='2011-01-01') columns = [ Column("Additive", f"Sales_Actual_fiscal.forecast('theta', 'forecast', estimation_end='2016-12-31')"), Column("Multiplicative", f"Sales_Actual_fiscal.log().forecast('theta', 'forecast', estimation_end='2016-12-31').exp()"), ] forecasts = export_api.signal_query(columns, company) df = pd.concat([sales_actual, forecasts], axis=1) df.plot(figsize=(15, 5)); # ## Model types #
#
theta
#
The Theta model is a simple forecasting method that combines a linear time trend with a Simple Exponential Smoother.
# #
holt_winters
#
Holt’s Winters Seasonal Exponential Smoothing is simple exponential smoothing with the addition of trend and seasonality.
# #
unobserved
#
Unobserved Components is a classical time series model that breaks the time series into a trend component, a seasonal component, and a cyclical component.
# #
sarima
#
SARIMA is a classical time series model. It’s a form of regression model with autoregressive terms, meaning that the predictions at one time step depend on the values at previous time steps, and optionally seasonal components.
# #
prophet
#
The Prophet library uses an additive model, where a time series is modelled as the sum of a trend component and yearly and weekly seasonality components. The model also takes holiday effects into account.
# #
# # Let's see the forecasts produced by these methods in a practical example: # In[22]: methods = ["theta", "holt_winters", "unobserved", "sarima", "prophet"] columns = [Column(method, f"Sales_Actual_fiscal.log().forecast('{method}', 'forecast').exp()") for method in methods] forecasts = export_api.signal_query(columns, "AAPL US") forecasts # In[24]: forecasts.plot(figsize=(15, 5));