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-011977-01-011980-01-011983-01-011986-01-01330335340345350co2

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)
330335340345350Data330335340345Trend−3−2−10123Seasonal1974-01-011977-01-011980-01-011983-01-011986-01-01−2−1012Remainder

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-30369121518Lag−0.20.00.20.40.60.81.0Cross-CorrelationCI %95.0CI %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)
14710131619Lag−0.20.00.20.40.60.81.0Stepwise and Real PACF |s|r|CI %95.0CI %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 days100 days150 days200 daysTime−40−30−20−10010Forecasting 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 days80 days90 days100 days110 daysTime−20−1001020Forecasting 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 days40 days60 days80 days100 days120 days140 daysTime−60−3003060Forecasting ar(X, order=1, constant=true)x1x2