Quick Start

Installing Forecast.jl

To begin exploring the package functionality type the lines below in the Julia REPL:

Pkg> add https://github.com/viraltux/Forecast.jl

julia> using Forecast

Alternatively you can also type:

julia> using Pkg

julia> Pkg.add(url="https://github.com/viraltux/Forecast.jl")

julia> using Forecast

LOESS extrapolated

The example below compares LOESS result with extrapolated predictions compared to a simple moving average result with a window size of 100.

using Plots
using Forecast

n = 1000
axb = LinRange(-1/2,pi+1/2,n)
x = LinRange(0,pi,n)
y = sin.(x) .+ rand(n)
scatter(x, y, xlims=(-1/2,pi+1/2), ma=.5, label = "Data", color = :grey)
plot!(axb,loess(x,y,predict=axb), linewidth = 4, label = "LOESS", color = :blue)
plot!(x,sma(y,100,true), linewidth = 2, label= "Moving Avg 100", color = :orange)

STL on CO2 dataset

For this example we will be using the co2 data used by the creators of STL to demostrate its funcitonality, below we can see such time series.

using Plots
using Forecast

plot(co2(), legend=:bottomright)
1974-01-01 1977-01-01 1980-01-01 1983-01-01 1986-01-01 330 335 340 345 350 co2

The parameters used for STL they're also from the orginal paper, a period of 365 days is used (removing leap years extra day), a robust fit is required and seasonality post-smoothing is applied.

using Plots
using Forecast

stl_co2 = stl(co2(),365; robust=true, spm=true)
plot(stl_co2)
330 335 340 345 350 Data 330 335 340 345 Trend −3 −2 −1 0 1 2 3 Seasonal 1974-01-01 1977-01-01 1980-01-01 1983-01-01 1986-01-01 −2 −1 0 1 2 Remainder

The image below comes from the original paper for comparison purposes.

Cross-Correlation on shifted dasaset

Here we cross-correlate two identical series shifted by six positions, the plot shows how the peak correlation is at position six.

using Plots
using Random
using Forecast

Random.seed!(36)
x1 = rand(100)
x2 = circshift(x1,6)
res = ccf(x1, x2; type="cor")
plot(res)
-18 -15 -12 -9 -6 -3 0 3 6 9 12 15 18 Lag −0.2 0.0 0.2 0.4 0.6 0.8 1.0 Cross-Correlation CI %95.0 CI %99.0

PACF on random dataset

The pacf function is useful to identify significant parameters in ARIMA models. For instance, in R the default pacf function estimates partial auto-correlation in a stepwise fashion, however in cases where the model is highly correlated with many previous steps this approach identifies the first lag as highly correlated and the rest as near zeroes when, in reality, all partial auto-correlations should be around zero since that's the information left once taken away the linear influence from the all other lags. Below is an example of such effect where the stepwise (in blue) and real (in red) partial auto-correlations are compared for a series where all lags highly correlate to each other.

using Plots
using Random
using Forecast

Random.seed!(36)
x = collect(1:100) + rand(100)
res = pacf(x)
plot(res)
1 4 7 10 13 16 19 Lag −0.2 0.0 0.2 0.4 0.6 0.8 1.0 Stepwise and Real PACF |s|r| CI %95.0 CI %99.0

Seasonal Plot on Air Passengers dataset

To compare seasonal behavior we can use splot to display it side by side, in this case it seems the months of July and August are the ones with a higher number of airflight passengers.

using Plots
using Forecast
splot(air())

Autoregressive Models

Random Walk simulated, fitted and forecast plotted.

using Plots
using Forecast
plot(forecast(ar(arsim( 1.,0.,0.,100),1,false),100))
50 days 100 days 150 days 200 days Time −40 −30 −20 −10 0 10 Forecasting ar(X, order=1, constant=false) x1

Random Zigzag Walk simulated, fitted and forecast plotted.

using Plots
using Forecast
plot(forecast(ar(arsim(-1.,0.,0.,100),1),10))
70 days 80 days 90 days 100 days 110 days Time −20 −10 0 10 20 Forecasting ar(X, order=1, constant=true) x1

Bivariate dataset simulated, fitted and forecast plotted.

using Plots
using Forecast

Φ = [1 .1;
    -.2 1]
Φ0 = [2., 3.]
x0 = [.1, .1]
Σ2 = [.2 .05;
     .05 .2]
ar_model = ar(arsim(Φ,Φ0,x0,100;Σ2),1)
fc = forecast(ar_model,50)
plot(fc)
20 days 40 days 60 days 80 days 100 days 120 days 140 days Time −60 −30 0 30 60 Forecasting ar(X, order=1, constant=true) x1 x2