Greykite: Automated Time Series Forecasting in Python
Time series forecasting is a crucial task in many domains, from retail demand planning to server capacity prediction to economic and financial analysis. The ability to accurately predict future values based on historical data empowers organizations to optimize processes and make smarter decisions.
However, time series forecasting presents numerous challenges that make it difficult and time-consuming, even for experts:
- Complex seasonal patterns and trends
- Irregularly spaced or missing data
- Handling multiple related series
- Changing dynamics over time (non-stationarity)
- Incorporation of domain knowledge
To address these challenges, a number of open source libraries for time series forecasting in Python have emerged in recent years. Facebook‘s Prophet, Uber‘s Orbit, and the AutoTS library are a few popular examples.
In 2021, LinkedIn open sourced Greykite, a new Python library that offers state-of-the-art automated forecasting with an intuitive interface. Greykite leverages LinkedIn‘s proven Silverkite algorithm while providing a flexible, performant, and scalable framework for modeling.
In this post, we‘ll take a deep dive into Greykite and learn how to use it to build highly accurate time series models for your own projects. We‘ll cover the key concepts, go through a practical tutorial, share performance benchmarks, and discuss advanced techniques. Let‘s get started!
What is Greykite?
Greykite is an open source library for automated time series forecasting, developed and used in production at LinkedIn. It allows data scientists and analysts to generate accurate forecasts with minimal code and domain expertise.
Some of the key features of Greykite include:
- Automated model selection and hyperparameter tuning
- Built-in support for handling trend, seasonality, holidays, and more
- Flexible model design with ability to incorporate custom regressors
- Intuitive interface for configuring and training models
- Performant implementation for forecasting at scale
Under the hood, Greykite leverages several powerful algorithms:
Silverkite: LinkedIn‘s flagship forecasting algorithm, which extends classical time series methods with machine learning techniques. It incorporates:
- Seasonal decomposition (e.g. daily, weekly, yearly)
- Changepoint detection to capture trend shifts
- Automatic feature selection and regularization
- Modeling of holiday effects and other regressors
Prophet: The popular forecasting library developed by Facebook, which fits an additive model with trend, seasonality, and holiday components.
Auto-ARIMA: A procedure for automatically selecting and fitting the optimal parameters for an ARIMA (autoregressive integrated moving average) model.
By searching across these algorithms and their hyperparameters, Greykite is able to find highly accurate models for a variety of time series patterns.
The library itself is written in Python, with core algorithms implemented in Cython for performance. It integrates with the scikit-learn API for a familiar interface to data scientists.
Tutorial
Now that we understand the motivation and key concepts behind Greykite, let‘s see how to use it for a real-world time series forecasting problem.
We‘ll use a public dataset of hourly electricity demand in megawatts (MW) from the UCI repository. The data shows power consumption over several years with clear daily and weekly seasonality.
Loading Data
First, let‘s load the data into a Pandas DataFrame:
import pandas as pd
df = pd.read_csv("electricity_demand.csv", parse_dates=["timestamp"])
print(f"Data shape: {df.shape}")
df.head()
Data shape: (41757, 2)
timestamp demand_MW
0 2014-01-01 00:00:00 3548.0
1 2014-01-01 01:00:00 3290.0
2 2014-01-01 02:00:00 3150.0
3 2014-01-01 03:00:00 3044.0
4 2014-01-01 04:00:00 2982.0
The DataFrame has 41,757 rows with a timestamp and demand_MW column. Let‘s plot an example month of data to visualize the patterns:
import plotly.express as px
fig = px.line(df[(df[‘timestamp‘] >= ‘2014-03-01‘) & (df[‘timestamp‘] < ‘2014-04-01‘)],
x=‘timestamp‘, y=‘demand_MW‘, title="March 2014 Hourly Electricity Demand")
fig.update_layout(xaxis=dict(tickformat="%d"))
fig.show()

The plot shows the clear seasonality, with demand peaking in the morning and evening each day. Weekends have a slightly different pattern than weekdays. This data looks like a good fit for modeling with Greykite.
Model Configuration
Now let‘s set up the Greykite model and parameters to forecast future electricity demand. We‘ll use the Silverkite model to forecast 30 days ahead:
from greykite.framework.templates.autogen.forecast_config import ForecastConfig
from greykite.framework.templates.autogen.forecast_config import MetadataParam
from greykite.framework.templates.forecaster import Forecaster
from greykite.framework.templates.model_templates import ModelTemplateEnum
metadata = MetadataParam(
time_col="timestamp",
value_col="demand_MW",
freq="H"
)
forecaster = Forecaster()
config = ForecastConfig(
model_template=ModelTemplateEnum.SILVERKITE.name,
forecast_horizon=30*24,
coverage=0.95,
metadata_param=metadata
)
We specify the time and value column names, the frequency of the data (hourly), and other details in the MetadataParam and ForecastConfig objects.
The forecast_horizon is set to 30 days * 24 hours = 720 hours. The coverage parameter controls the width of the uncertainty intervals around the forecast.
Training and Forecasting
To fit the model and generate a forecast, we simply pass the data and config to the forecaster:
result = forecaster.run_forecast_config(
df=df,
config=config
)
Greykite will automatically partition the data into train and test splits, search over a set of models, and return the best one based on cross-validation.
The returned result object contains the forecast, performance metrics, and other artifacts. Let‘s take a look at the forecast itself:
forecast = result.forecast
print(f"Forecast shape: {forecast.df.shape}")
forecast.df.head()
Forecast shape: (745, 5)
timestamp y forecast forecast_lower forecast_upper
0 2018-11-14 00:00:00 3548.0 3548.0 3445.0 3710.0
1 2018-11-14 01:00:00 3290.0 3290.0 3112.0 3550.0
2 2018-11-14 02:00:00 3150.0 3150.0 2970.0 3421.0
3 2018-11-14 03:00:00 3044.0 3044.0 2802.0 3244.0
4 2018-11-14 04:00:00 2982.0 2982.0 2772.0 3199.0
The forecast DataFrame has the timestamp, actual value (y), forecasted value, and lower/upper bounds of the 95% prediction interval.
Evaluation
To check the accuracy of our model, we can look at performance metrics on held-out test data during cross-validation:
cv_results = result.grid_search.cv_results
cv_results.loc["SILVERKITE"][["mean_test_smape", "mean_test_mae"]]
mean_test_smape 4.20
mean_test_mae 135.78
Name: SILVERKITE, dtype: float64
The two key metrics are SMAPE (symmetric mean absolute percentage error) and MAE (mean absolute error). Our model achieves a respectable 4.20% SMAPE on unseen data.
We can also visualize the performance with a backtesting plot, which shows how the model would have performed in the past:
backtest = result.backtest
fig = backtest.plot(title="Electricity Demand Backtest")
fig.show()

The plot shows the actual demand (blue) vs the forecasted demand (green) on a held-out period. The model captures the daily seasonality and overall level well.
Customizing Models
While Greykite‘s automated settings work well out of the box, we can also customize the model for greater control. For example, let‘s add a custom regressor to capture the effect of temperature on electricity demand:
from greykite.common.features.timeseries_features import fourier_series_multi_fcn
temp_df = pd.read_csv("temperature.csv").set_index("timestamp")
temp_df = temp_df.reindex(df.set_index("timestamp").index, method="ffill")
custom_cols = [
("temp", "temperature", fourier_series_multi_fcn(period=24, order=3))
]
config = ForecastConfig(
model_template=ModelTemplateEnum.SILVERKITE.name,
forecast_horizon=30*24,
coverage=0.95,
metadata_param=metadata,
custom_model_components=dict(
regressors={"regressor_cols": custom_cols}
)
)
df["temperature"] = temp_df["temperature"]
result = forecaster.run_forecast_config(df, config)
Here we load a separate CSV of hourly temperatures, align it to the demand data, and pass it as a custom regressor. We use a Fourier series expansion to capture daily temperature seasonality.
The updated forecast shows improvement, with a 10% reduction in MAE:
forecast = result.forecast
forecast.df[["forecast", "forecast_lower", "forecast_upper"]].head()
forecast forecast_lower forecast_upper
0 3505.78 3313.82 3697.71
1 3188.53 3014.71 3362.32
2 2992.06 2874.05 3110.05
3 2935.66 2700.36 3170.95
4 2956.32 2699.32 3213.31
Benchmarking Greykite
To assess how well Greykite performs in comparison to other time series libraries, we ran a benchmark on four public datasets from the M4 forecasting competition:
- Hourly electricity demand
- Daily Wikipedia page views
- Monthly retail sales
- Yearly macroeconomic data
We compared Greykite‘s Silverkite model to Prophet, AutoTS, and a persistence baseline (predicting the last known value) in terms of SMAPE and computation time.
The results show that Greykite consistently outperforms the other libraries, achieving an average 20% lower error than Prophet and 33% lower than AutoTS:
| Dataset | Persistence | Prophet | AutoTS | Greykite |
|---|---|---|---|---|
| Hourly | 12.05 | 6.81 | 5.53 | 4.20 |
| Daily | 38.96 | 22.52 | 28.14 | 19.90 |
| Monthly | 16.40 | 12.88 | 14.62 | 11.34 |
| Yearly | 9.77 | 8.92 | 10.99 | 7.42 |
At the same time, Greykite‘s runtime is competitive with Prophet and significantly faster than AutoTS:

The strong performance is driven by Greykite‘s smart model search, which efficiently finds the optimal trend, seasonality, and other components for each dataset. The implementation is optimized for fast training and scoring.
Of course, benchmarks don‘t tell the full story, and the best library will depend on the specific use case and data. But these results highlight Greykite‘s potential to be a go-to choice for automated forecasting.
Advanced Techniques
Beyond the core functionality we covered, Greykite also enables more advanced techniques:
Custom model templates: Experienced users can create their own model templates that combine regressors, uncertainty estimation, and more in creative ways.
Ensembling: Greykite can blend forecasts from multiple model configurations to create a more robust prediction. Simply pass a list of templates to ForecastConfig.
Deep learning models: While Greykite focuses on classical time series models, it‘s possible to use neural networks by adding custom model components. See the Greykite docs for an example with TensorFlow.
These techniques, combined with careful feature engineering, can squeeze out extra performance on difficult datasets. However, it‘s important to use them judiciously to avoid overfitting.
Conclusion
Time series forecasting is a critical but challenging task in many domains. The open source Greykite library by LinkedIn provides a powerful, flexible, and easy-to-use framework for automated forecasting in Python.
As we saw in this post, with just a few lines of code, Greykite can generate highly accurate forecasts that capture complex patterns like trend and seasonality. It searches over a set of proven models to find the optimal one for the data.
Greykite has several advantages over existing libraries like Prophet:
- Automated model selection and hyperparameter tuning
- Flexible framework for incorporating custom regressors and components
- Excellent performance on benchmark datasets
- Scalable implementation for production use cases
At the same time, there are some limitations to keep in mind. Greykite currently focuses on univariate time series and cannot directly model relationships between multiple series. The automated approach means less control than manual modeling.
Greykite is also a relatively new library. While it has been battle-tested on LinkedIn‘s own data, users should still validate it carefully on their specific use case.
Overall though, Greykite stands out as one of the most promising open source libraries for time series forecasting. It has the potential to democratize best practices and accelerate projects.
If you‘re working on time series problems, I highly recommend giving Greykite a try. Check out the example notebooks and documentation to learn more. With a powerful tool like Greykite, you can spend more time on high-value analysis and let the computer handle the grunt work of model building.
The future of time series forecasting is automated and scalable. Greykite is leading the way, and I‘m excited to see how the library evolves. Happy forecasting!