Predict Electrical energy Consumption Utilizing Time Collection Evaluation





“Time series models are used to forecast future events based on previous events that have been observed (and data collected) at regular time intervals.”

We will probably be taking a small forecasting drawback and attempt to resolve it until the top studying time sequence forecasting alongside.


What’s Time Collection evaluation

Time sequence forecasting is a method for the prediction of occasions by way of a sequence of time. The method is used throughout many fields of research, from geology to habits to economics. The methods predict future occasions by analyzing the traits of the previous, on the belief that future traits will maintain much like historic traits.

Time sequence forecasting is carried out in quite a lot of functions together with:

  • Climate forecasting
  • Earthquake prediction
  • Astronomy
  • Statistics
  • Mathematical finance
  • Econometrics
  • Sample recognition
  • Signal processing
  • Management engineering

Time sequence forecasting is typically simply the evaluation of consultants finding out a area and providing their predictions. In lots of fashionable functions, nevertheless, time sequence forecasting makes use of pc applied sciences, together with:

There are two most important targets of time sequence evaluation: (a) figuring out the character of the phenomenon represented by the sequence of observations, and (b) forecasting (predicting future values of the time sequence variable). Each of those targets require that the sample of noticed time sequence information is recognized and kind of formally described. As soon as the sample is established, we are able to interpret and combine it with different information (i.e., use it in our concept of the investigated phenomenon, e.g., seasonal commodity costs). Whatever the depth of our understanding and the validity of our interpretation (concept) of the phenomenon, we are able to extrapolate the recognized sample to foretell future occasions.


Phases in Time Collection Forecasting

Fixing a time sequence drawback is a bit of completely different as in comparison with a daily modeling activity. A easy/fundamental journey of fixing a time sequence drawback will be demonstrated by way of the next processes. We are going to perceive about duties which one must carry out in each stage. We may even have a look at the python implementation of every stage of our problem-solving journey.

Steps are –

1. Visualizing time sequence

On this step, we attempt to visualize the sequence. We attempt to determine all of the underlying patterns associated to the sequence like development and seasonality. Don’t worry about these phrases proper now, as we’ll focus on them throughout implementation. You possibly can say that that is extra a sort of exploratory evaluation of time sequence information.

2. Stationarising time sequence

A stationary time sequence is one whose statistical properties akin to imply, variance, autocorrelation, and so on. are all fixed over time. Most statistical forecasting strategies are primarily based on the belief that the time sequence will be rendered roughly stationary (i.e., “stationarised”) by way of using mathematical transformations. A stationarised sequence is comparatively straightforward to foretell: you merely predict that its statistical properties would be the identical sooner or later as they’ve been previously! Another excuse for attempting to stationarise a time sequence is to have the ability to receive significant pattern statistics akin to means, variances, and correlations with different variables. Such statistics are helpful as descriptors of future habits provided that the sequence is stationary. For instance, if the sequence is persistently rising over time, the pattern imply and variance will develop with the scale of the pattern, and they’re going to all the time underestimate the imply and variance in future durations. And if the imply and variance of a sequence should not well-defined, then neither are its correlations with different variables.

3. Discovering the very best parameters for our mannequin

We have to discover optimum parameters for forecasting fashions one’s we now have a stationary sequence. These parameters come from the ACF and PACF plots. Therefore, this stage is extra about plotting above two graphs and extracting optimum mannequin parameters primarily based on them. Don’t worry, we’ll cowl on learn how to decide these parameters in the course of the implementation half beneath!

4. Becoming mannequin

As soon as we now have our optimum mannequin parameters, we are able to match an ARIMA mannequin to be taught the sample of the sequence. All the time keep in mind that time sequence algorithms work on stationary information solely therefore making a sequence stationary is a vital side

5. Predictions

After becoming our mannequin, we will probably be predicting the longer term on this stage. Since we at the moment are accustomed to a fundamental move of fixing a time sequence drawback, allow us to get to the implementation.


Drawback Assertion

The dataset will be downloaded from here. It incorporates solely 2 columns, one column is Date and the opposite column pertains to the consumption proportion.

It reveals the consumption of electrical energy from 1985 until 2018. The aim is to foretell electrical energy consumption for the following 6 years i.e. until 2024.

Load the dataset

import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt'fivethirtyeight')
# Above is a particular fashion template for matplotlib, extremely helpful for visualizing time sequence information
from pylab import rcParams
rcParams['figure.figsize'] = 10, 7df = pd.read_csv('/Customers/.../.../.../Electric_consumption.csv')

Now, Outline column names, drop nulls, convert Date to DateTime format and make Date as an index column as a result of it isn’t potential to plot the graph with out index.

df.columns=['Date', 'Consumption']
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True) #set date as index



Now, allow us to begin with our predefined steps:

1. Visualizing the time sequence.

plt.title("production graph")

Unique time sequence information factors


Keep in mind that for time sequence forecasting, a sequence must be stationary. The sequence ought to have a continuing imply, variance, and covariance.

There are few factors to notice right here, the imply isn’t fixed on this case as we are able to clearly see an upward development.

Therefore, we now have recognized that our sequence isn’t stationary. We have to have a stationary sequence to do time sequence forecasting. Within the subsequent stage, we’ll attempt to convert this right into a stationary sequence.

Lets us plot the scatterplot:


Scatter plot of time sequence information factors


We will additionally visualize the info in our sequence by way of a distribution too.


We will observe a near-normal distribution(bell-curve) over consumption values.

Additionally, a given time sequence is assumed to include three systematic elements together with degree, development, seasonality, and one non-systematic element known as noise.

These elements are outlined as follows:

  • Degree: The common worth within the sequence.
  • Development: The rising or lowering worth within the sequence.
  • Seasonality: The repeating short-term cycle within the sequence.
  • Noise: The random variation within the sequence.

So as to carry out a time sequence evaluation, we might have to separate seasonality and development from our sequence. The resultant sequence will grow to be stationary by way of this course of.

So allow us to separate Development and Seasonality from the time sequence.

from statsmodels.tsa.seasonal import seasonal_decompose
consequence = seasonal_decompose(df, mannequin='multiplicative')

This offers us extra perception into our information and real-world actions. Clearly, there’s an upward development and a recurring occasion the place electrical energy consumption shoots most yearly.

2. Stationarising the time sequence.

First, we have to examine if a sequence is stationary or not.

ADF (Augmented Dickey-Fuller) Check

The Dickey-Fuller take a look at is among the hottest statistical assessments. It may be used to find out the presence of unit root within the sequence, and therefore assist us perceive if the sequence is stationary or not. The null and alternate speculation of this take a look at is:

Null Speculation: The sequence has a unit root (worth of a =1)

Alternate Speculation: The sequence has no unit root.

If we fail to reject the null speculation, we are able to say that the sequence is non-stationary. Which means the sequence will be linear or distinction stationary (we’ll perceive extra about distinction stationary within the subsequent part).

If each imply and normal deviation are flat traces(fixed imply and fixed variance), the sequence turns into stationary.

The next operate is one that may plot a sequence with its rolling imply and normal deviation.

from statsmodels.tsa.stattools import adfuller
def test_stationarity(timeseries):
    #Determing rolling statistics
    rolmean = timeseries.rolling(12).imply()
    rolstd = timeseries.rolling(12).std()
    #Plot rolling statistics:
    plt.plot(timeseries, colour='blue',label='Unique')
    plt.plot(rolmean, colour='purple', label='Rolling Imply')
    plt.plot(rolstd, colour='black', label = 'Rolling Std')
    plt.title('Rolling Imply and Normal Deviation')
    #carry out dickey fuller take a look at  
    print("Results of dickey fuller test")
    adft = adfuller(timeseries['Consumption'],autolag='AIC')
    # output for dft will give us with out defining what the values are.
    #therefore we manually write what values does it explains utilizing a for loop
    output = pd.Collection(adft[0:4],index=['Test Statistics','p-value','No. of lags used','Number of observations used'])
    for key,values in adft[4].objects():
        output['critical value (%s)'%key] =  values


By means of the above graph, we are able to see the rising imply and normal deviation and therefore our sequence isn’t stationary.


Outcomes of Dicky-Fuller take a look at


We see that the p-value is bigger than zero.05 so we can’t reject the Null speculation. Additionally, the take a look at statistics is bigger than the important values. so the info is non-stationary.

To get a stationary sequence, we have to get rid of the development and seasonality from the sequence.

we begin by taking a log of the sequence to cut back the magnitude of the values and cut back the rising development within the sequence. Then after getting the log of the sequence, we discover the rolling common of the sequence. A rolling common is calculated by taking enter for the previous 12 months and giving a imply consumption worth at each level additional forward in sequence.

df_log = np.log(df)
moving_avg = df_log.rolling(12).imply()
std_dev = df_log.rolling(12).std()
plt.plot(moving_avg, colour="red")
plt.plot(std_dev, colour ="black")

After discovering the imply, we take the distinction of the sequence and the imply at each level within the sequence.

This fashion, we get rid of traits out of a sequence and procure a extra stationary sequence.

df_log_moving_avg_diff = df_log-moving_avg

Carry out the Dickey-Fuller take a look at (ADFT) as soon as once more. We have now to carry out this operate each time to examine whether or not the info is stationary or not.


From the above graph, we noticed that the info attained stationarity.

One of many modules is accomplished as we got here to a conclusion. We have to examine the weighted common, to know the development of the info in time sequence. Take the earlier log information and to carry out the next operation.

weighted_average = df_log.ewm(halflife=12, min_periods=zero,modify=True).imply()

The exponential transferring common (EMA) is a weighted common of the final n costs, the place the weighting decreases exponentially with every earlier worth/interval. In different phrases, the components offers current costs extra weight than previous costs.

Beforehand we subtracted df_log with transferring common, now take the identical df_log and subtract with weighted_average and carry out the Dickey-Fuller take a look at (ADFT) as soon as once more.

logScale_weightedMean = df_log-weighted_average
from pylab import rcParams
rcParams['figure.figsize'] = 10,6



Outcomes of Dicky-Fuller take a look at


From the above graph, we noticed that the info attained stationarity. We additionally see that the take a look at statistics and important worth is comparatively equal.

There will be instances when there’s a excessive seasonality within the information.

In these instances, simply eradicating the development is not going to assist a lot. We have to additionally care for the seasonality within the sequence. One such methodology for this activity is differencing.

Differencing is a technique of reworking a time sequence dataset.

It may be used to take away the sequence dependence on time, so-called temporal dependence. This consists of buildings like traits and seasonality. Differencing may also help stabilize the imply of the time sequence by eradicating modifications within the degree of a time sequence, and so eliminating (or lowering) development and seasonality.

Differencing is carried out by subtracting the earlier statement from the present statement.
Carry out the Dickey-Fuller take a look at (ADFT) as soon as once more.

df_log_diff = df_log - df_log.shift()
plt.title("Shifted timeseries")
plt.plot(df_log_diff)#Allow us to take a look at the stationarity of our resultant sequence


The subsequent step is to carry out decomposition which supplies a structured mind-set a few time sequence forecasting drawback, each typically by way of modeling complexity and particularly by way of learn how to finest seize every of those elements in a given mannequin. Lastly, carry out the Dickey-Fuller take a look at (ADFT) as soon as once more.

from chart_studio.plotly import plot_mpl
from statsmodels.tsa.seasonal import seasonal_decompose
consequence = seasonal_decompose(df_log, mannequin='additive', freq = 12)
plt.present()development = consequence.development
development.dropna(inplace=True)seasonality = consequence.seasonal
seasonality.dropna(inplace=True)residual = consequence.resid

After the decomposition, if we have a look at the residual then we now have clearly a flat line for each imply and normal deviation. We have now received our stationary sequence and now we are able to transfer to search out the very best parameters for our mannequin.

3. Discovering the very best parameters for our mannequin

Earlier than we go on to construct our forecasting mannequin, we have to decide optimum parameters for our mannequin. For these optimum parameters, we want ACF and PACF plots.

A nonseasonal ARIMA mannequin is assessed as an “ARIMA(p,d,q)” mannequin, the place:

p → Variety of autoregressive phrases,

d → Variety of nonseasonal variations wanted for stationarity, and

q → Variety of lagged forecast errors within the prediction equation.

Values of p and q come by way of ACF and PACF plots. So allow us to perceive each ACF and PACF!


Autocorrelation Operate(ACF)

Statistical correlation summarizes the power of the connection between two variables. Pearson’s correlation coefficient is a quantity between -1 and 1 that describes a damaging or constructive correlation respectively. A worth of zero signifies no correlation.

We will calculate the correlation for time sequence observations with earlier time steps, known as lags. As a result of the correlation of the time sequence observations is calculated with values of the identical sequence at earlier occasions, that is known as a serial correlation, or an autocorrelation.

A plot of the autocorrelation of a time sequence by lag is known as the AutoCorrelation Function, or the acronym ACF. This plot is typically known as a correlogram or an autocorrelation plot.


Partial Autocorrelation Operate(PACF)

A partial autocorrelation is a abstract of the connection between an statement in a time sequence with observations at prior time steps with the relationships of intervening observations eliminated.

The partial autocorrelation at lag okay is the correlation that outcomes after eradicating the impact of any correlations because of the phrases at shorter lags.

The autocorrelation for statement and statement at a previous time step is comprised of each the direct correlation and oblique correlations. It’s these oblique correlations that the partial autocorrelation operate seeks to take away.

Under code plots, each ACF and PACF plots for us:

from statsmodels.tsa.stattools import acf,pacf
# we use d worth right here(data_log_shift)
acf = acf(df_log_diff, nlags=15)
pacf= pacf(df_log_diff, nlags=15,methodology='ols')#plot PACF
plt.title('Auto corellation operate')
plt.tight_layout()#plot ACF
plt.title('Partially auto corellation operate')

4. Becoming mannequin

So as to discover the p and q values from the above graphs, we have to examine, the place the graph cuts off the origin or drops to zero for the primary time from the above graphs the p and q values are merely near 3 the place the graph cuts off the origin ( draw the road to x-axis) now we now have p,d,q values. So now we are able to substitute within the ARIMA mannequin and let’s have a look at the output.

from statsmodels.tsa.arima_model import ARIMA
mannequin = ARIMA(df_log, order=(3,1,3))
result_AR = mannequin.match(disp = zero)
plt.plot(result_AR.fittedvalues, colour='purple')
plt.title("sum of squares of residuals")
print('RSS : %f' %sum((result_AR.fittedvalues-df_log_diff["Consumption"])**2))

Much less the RSS worth, the more practical the mannequin is. You examine with (2,1,zero),(3,1,1), and so on to search for the smallest values of RSS.

5. Predictions

The next code helps us to forecast shampoo gross sales for the following 6 years.



From the above graph, we calculated the longer term predictions until 2024 the greyed out space is the boldness interval which means the predictions is not going to cross that space.



Lastly, we have been capable of construct an ARIMA mannequin and truly forecast for a future time interval. Hold be aware that it is a fundamental implementation to get one began with time sequence forecasting. There are a number of ideas like smoothening and so on and fashions like ARIMAX, prophet, and so on to construct your time sequence fashions.

Nicely, that’s all for this text hope you guys have loved studying this it, be happy to share your feedback/ideas/suggestions within the remark part.



You will discover full code on this GitHub link:

Comfortable Studying !!!

Bio: Nagesh Singh Chauhan is a Information Science fanatic. Excited about Large Information, Python, Machine Studying.

Original. Reposted with permission.


About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *