Time Series for Climate Change: Forecasting Energy Demand

Author:Murphy  |  View: 26932  |  Time: 2025-03-23 18:44:45
Photo by Matthew Henry on Unsplash

This is Part 4 of the series Time Series for Climate Change. List of articles:

So far, we explored how Forecasting is important to integrate clean energy sources into the electricity grid.

Forecasting also plays a key role on the demand side of energy systems.


Balancing the demand and supply of energy

Power systems need to ensure the balance between the supply and demand of energy at all times. This balance is critical for the reliability of the electricity grid. If demand is greater than supply, this leads to power outages. When supply exceeds demand, there's a surplus of energy which often goes to waste.

Power systems use forecasting models to help them predict the demand for energy. Accurate demand forecasts contribute to more efficient production and use of energy. This has a direct impact on climate because of waste reduction.

Analyzing energy consumption is also valuable within households. For example, individuals can examine which appliances consume more energy, and use this information to avoid higher costs during peak hours. As a bit of trivia: It's estimated that about 8% of residential electricity demand comes from standby power consumption [4].

Forecasting energy demand

Forecasting energy demand is a difficult problem.

Energy consumption depends on several factors, some of which might not be readily available for modeling. Examples include weather and economic conditions that affect the use of electronic devices for heating or cooling. Weather is characterized by highly variable patterns. These make it difficult to predict the magnitude of weather impact on energy demand.

Energy demand data exhibit seasonal patterns at different time scales, including daily, monthly, or yearly scales. In winter, for example, energy demand increases for heating purposes.

Distribution of energy demand by month. Image by author.

Capturing all seasonal effects is important in building accurate energy demand forecasting models.

How can you do that?


Hands-on: Forecasting Energy Demand

In the rest of this article, we'll develop an energy demand forecasting model. You'll learn how to:

  • analyze multiple seasonal effects using auto-correlation and data visualization;
  • do feature extraction from the date and time information to handle multiple seasonal effects.

The full code used in this tutorial is available on Github:

Data set

We'll use a data set that represents hourly power consumption, in megawatts, in Kentucky, USA [1]. The data was collected from 2013 until 2018, leading to a total of 45.344 observations.

Here's what the data looks like:

Hourly energy demand time series in Kentucky, USA. The yellow line is the daily average demand. Data source in reference [1]. Image by author.

Visualizing seasonality

The plot above indicates that they're yearly regular patterns in the series.

Another way to visualize seasonality is with a graphic called seasonal plot:

A seasonal plot of average monthly energy demand by year. Image by author.

The seasonal plot makes it easy to see year-on-year monthly patterns. For example, energy consumption increases during the winter and summer months and decreases in spring and fall. This is probably related to heating (winter) or cooling (summer) reasons.

The seasonal sub-series plot below is also helpful to analyze the dynamics of data within and across months:

A seasonal sub-series plot of average monthly energy demand by year. Image by author.

Analyzing seasonality using ACF

You can also use auto-correlation to analyze seasonality. Seasonal time series will show higher auto-correlation in each seasonal lag.

Auto-correlation plot. The oscillations indicate daily seasonality. Image by author.

The figure above shows the auto-correlation plot up to 48 lags. The auto-correlation shows an oscillation pattern that is caused by daily seasonality.

So, besides yearly regular variations, there's also a noticeable daily seasonality. The value observed in a given hour is correlated with the value captured in the same hour on the previous day.

Auto-regression

As a starting point, we'll develop an auto-regressive model for forecasting power consumption. You can check a previous post for details on this type of modeling.

from sklearn.model_selection import train_test_split

from src.tde import time_delay_embedding

# Train / test split
train, test = train_test_split(series, test_size=0.2, shuffle=False)

# using past 12 observations as explanatory variables
N_LAGS = 12
# using the next 12 hours as the forecasting horizon
HORIZON = 12

# transforming time series into a tabular format for supervised learning
X_train, Y_train = time_delay_embedding(train, n_lags=N_LAGS, horizon=HORIZON, return_Xy=True)
X_test, Y_test = time_delay_embedding(test, n_lags=N_LAGS, horizon=HORIZON, return_Xy=True)

Dealing with multiple seasonal effects

Most time series decomposition or forecasting methods are designed to handle a single seasonal period.

As data collection became cheap, it enabled the collection of time series at a high sampling frequency, such as daily or hourly. High-frequency time series provide a larger amount of data, which is an important factor in training machine learning models. Yet, these also contain complex seasonal patterns that can be tricky to model.

So, how do you handle multiple seasonal effects?

We'll apply two feature extraction processes to do this:

  • extracting features based on date and time data;
  • using a trigonometric representation based on Fourier terms.

Extracting features based on date and time

A simple way of modeling multiple seasonal patterns is to summarise the date and time information at each time step. You can extract the relevant periods, such as the hour or day of the year, and use them as explanatory variables.

The library sktime provides a handy class to do this:

from sktime.transformations.series.date import DateTimeFeatures

hourly_feats = DateTimeFeatures(ts_freq='H',
                                keep_original_columns=False,
                                feature_scope='efficient')

dtime_train = hourly_feats.fit_transform(X_train)

Here's a sample of the output:

These features can be an effective approach for many time series with complex seasonality. But, they can miss the continuity of time. For example, suppose that you encode monthly information into integer numbers from 1 (January) to 12 (December):

Encoding month information as integers. Image by author.

In such a case, the model does not understand that December precedes January. Transforming monthly information with one-hot encoding also leads to this problem.

Fourier terms

Fourier terms are periodic and deterministic series based on sine and cosine waves. The smoothness of these terms enables the modeling of time continuity.

Encoding month information with Fourier terms. Image by author.

Here's a sklearn compatible class for extracting Fourier series:

from datetime import datetime

import numpy as np
import pandas as pd

class FourierTerms:

    def __init__(self, period: float, n_terms: int, prefix=''):
        self.period = period
        self.n_terms = n_terms
        self.prefix = prefix

    def transform(self, index: pd.DatetimeIndex, use_as_index: bool = True):
        t = np.array(
            (index - datetime(1970, 1, 1)).total_seconds().astype(float)
        ) / (3600 * 24.)

        fourier_x = np.column_stack([
            fun((2.0 * (i + 1) * np.pi * t / self.period))
            for i in range(self.n_terms)
            for fun in (np.sin, np.cos)
        ])

        col_names = [
            f'{self.prefix}{fun.__name__[0].upper()}{i}'
            for i in range(self.n_terms)
            for fun in (np.sin, np.cos)
        ]

        fourier_df = pd.DataFrame(fourier_x, columns=col_names)

        if use_as_index:
            fourier_df.index = index

        return fourier_df

The main input is the seasonal period (e.g. 12 for monthly time series), the date-time information, and the number of terms. The number of terms impacts the smoothness of the representation. The optimal number depends on the input data.

Several popular methods use Fourier terms to model complex seasonality. These include Prophet, TBATS, greykite, or time-varying regression.

Fourier terms are included as explanatory features as follows:

fourier_daily = FourierTerms(n_terms=2, period=24, prefix='D_')
fourier_monthly = FourierTerms(n_terms=2, period=24 * 30.5, prefix='M_')
fourier_yearly = FourierTerms(n_terms=2, period=24 * 365, prefix='Y_')

dfourier_train = fourier_daily.transform(X_train.index)
mfourier_train = fourier_monthly.transform(X_train.index)
yfourier_train = fourier_yearly.transform(X_train.index)

feats_train = pd.concat([X_train, dtime_train, dfourier_train,
                         mfourier_train, yfourier_train],
                        axis=1)

model = RandomForestRegressor()
model.fit(feats_train, Y_train)

So, auto-regression is coupled with features that summarise the date and time data. The feature importance scores below indicate that these provide relevant information to the model:

Random Forest importance scores. Image by author.

Like in the case of forecasting energy production, the energy demand model becomes less accurate for longer horizons:

MASE error by forecasting horizon. Image by author.

Key Takeaways

  • Energy demand forecasts are relevant to climate change. They enable power systems to make informed decisions and integrate clean energy sources into the grid;
  • Demand time series are affected by multiple factors and have a complex seasonality;
  • You can deal with multiple seasonal patterns by summarising date and time information. Fourier terms are a common approach to do this;
  • Energy demand is difficult to predict in the long term, i.e., more than a few hours. Improving long-term forecasts is important for the efficiency of power systems.

Thank you for reading, and see you in the next story!

References

[1] Hourly Energy Consumption from PJM in Megawatts (Licence: CC0: Public Domain)

[2] Rolnick, David, et al. "Tackling climate change with Machine Learning." ACM Computing Surveys (CSUR) 55.2 (2022): 1–96.

[3] MacKay, David. Sustainable Energy-without the hot air. UIT cambridge, 2008.

Tags: Artificial Intelligence Data Science Forecasting Machine Learning Time Series Analysis

Comment