Using PyFlux To Predict Temperature Data
By: Michael Grogan
Tags: Bayesian Statistics, Machine Learning, Data Science, Time Series, Probability

Using PyFlux To Predict Temperature Data

Probabilistic ARIMA modelling using PyFlux

Source: Photo by Peter-Lomas from Pixabay

PyFlux is a time series library built for Python, which integrates probability modelling with time series analysis.

In this example, let us see how this library can be used to predict temperature patterns for Dublin Airport, Ireland, using monthly data from November 1941 — January 2018.

Here is a short overview of the data:

Source: Jupyter Notebook Output

meant stands for mean temperature, and it is this variable that we are interested in forecasting using PyFlux.

Model Configuration

To configure the ARIMA model within PyFlux, we must specify:

the probability distribution

the autoregressive (AR) term

the moving average (MA) term

Let’s plot a histogram of the mean monthly temperature data:

Source: Jupyter Notebook Output

While no set of data ever perfectly approximates a normal distribution, the shape of the histogram above broadly indicates that the data follows this pattern. In this regard, pf.Normal() will be specified as the distribution when building the model.

The lag drop-off for the partial autocorrelation function will be used to determine the AR term, while the lag drop-off for the autocorrelation function on the differenced series is used to determine the MA term. More information on determination of AR and MA terms can be found here.

Source: Jupyter Notebook Output

From examining the partial autocorrelation function, we can see that the correlation has dropped off at lag 2. In this regard, the AR term in the model is set to 2.

Now, let’s examine the autocorrelation function for the differenced series.

Source: Jupyter Notebook Output

Here, we see that the correlation has dropped off at lag 3. Accordingly, the MA term in the model is set to 3.

Results

Now that the probability distribution, along with the AR and MA terms have been identified, the model can be defined accordingly.

model = pf.ARIMA(data=train, ar=2, ma=3, target='meant', family=pf.Normal())

The maximum likelihood point mass estimate is used to estimate the latent variables, in the same manner as used in the PyFlux documentation:

>>> x = model.fit("MLE")

>>> x.summary() Normal ARIMA(2,0,3)

======================================================= ==================================================

Dependent Variable: meant Method: MLE

Start Date: 4 Log Likelihood: -1552.9713

End Date: 821 AIC: 3119.9426

Number of observations: 818 BIC: 3152.8907

====================================================================

Latent Variable Estimate Std Error z P>|z| 95% C.I.

======================================== ========== ==========

Constant 2.1524 0.0809 26.597 0.0 (1.9938 | 2.311)

AR(1) 1.6733 0.0168 99.6646 0.0 (1.6404 | 1.7063)

AR(2) -0.8992 0.0029 -310.825 0.0 (-0.9048 | -0.8935)

MA(1) -0.6943 0.0466 -14.8874 0.0 (-0.7857 | -0.6029)

MA(2) 0.3329 0.048 6.9348 0.0 (0.2388 | 0.427)

MA(3) 0.0402 0.0547 0.7348 0.4625 (-0.067 | 0.1475)

Normal Scale 1.9222

====================================================================

Now, the model fit and predictions can be plotted:

model.plot_fit(figsize=(15,10))

Source: Jupyter Notebook Output

Source: Jupyter Notebook Output

Source: Jupyter Notebook Output

Given that a seasonal component has not been specified in this model, the probability forecast does seem to pick up the highs and lows in the data quite well. Moreover, the probability forecasts can be of use in predicting extreme weather patterns.

For instance, we see that during lower temperatures in winter, the graph illustrates a mean temperature of about 5°C. However, the probability bounds also illustrate that during particularly cold winters, this could drop to between 0°C and -5°C.

Conversely, the summer months show a mean temperature of 15°C, but this could potentially rise to between 20°C and 25°C during hotter summer months.

Conclusion

In this example, you have seen:

How PyFlux can be used to probabilistically make forecasts using an ARIMA model

Configuration of an ARIMA model within PyFlux

Interpretation of forecasts made using the model

Many thanks for your time, and the relevant code and datasets can be found here.

Disclaimer: This article is written on an “as is” basis and without warranty. It was written with the intention of providing an overview of data science concepts, and should not be interpreted as professional advice. The findings and interpretations in this article are those of the author and are not endorsed by or affiliated with any third parties mentioned in this article.

References