LongMemory.jl: Generating, Estimating, and Forecasting Long Memory Models in Julia

Illustrative example

Author
Affiliation

Aalborg University

Abstract

LongMemory.jl is a package for time series long memory modelling in Julia. The package provides functions for generating long memory, estimating the parameters of the models, and simulation. Generating methods include fractional differencing, stochastic error duration, and cross-sectional aggregation. Estimators include those inspired by the log-periodogram regression and parametric ones. This notebook provides an illustrative example on the use of the package.

1 Nile River minima data

1.1 Plotting the data, its autocorrelation function and spectral density

Loading the packages, if not installed, use the Pkg.add() function to install them. LongMemory.jl includes plotting functions tailored for long memory analysis. We use Plots.jl for additional plotting, and LongMemory.jl for the long memory analysis.

using LongMemory, Plots

Data loading, and plot.

NileMin = NileData()
theme(:ggplot2)
p1 = plot( NileMin.Year , NileMin.NileMin, xlabel="Year" , ylabel = "Nile River minima" , legend = false )

Plotting the autocorrelation function using the autocorrelation_plot() function from LongMemory.jl.

p2 = autocorrelation_plot(NileMin.NileMin, 50)

Plotting the spectral density with the periodogram() function.

p3 = periodogram_plot(NileMin.NileMin, slope = true)

Combining the plots.

l = @layout [a; b c]
theme(:ggplot2)
plot(p1, p2, p3, layout = l, size = (700, 500) )

All the above plus the log-variance plot can be done in a single plot.

NileDataPlot()

2 Long memory generation

2.1 Fractional differencing generation and plotting

Loading the package for random number generation, setting the seed, and generating the fractional differenced series using the fi_gen() function.

using Random
Random.seed!(1234)

dx = fi_gen(500, 0.3)
500-element Vector{Float64}:
  0.9706563288552144
 -0.6880215128786353
  0.7973733442603004
  0.19192066674807973
 -0.46144511739243654
 -1.514799781765257
  2.250125601629064
  2.0467649947720434
  1.5280686228970999
 -0.12693052530986765
  ⋮
 -0.930134105560699
 -1.333219179460856
 -0.49678208289873016
 -1.0341442518867012
 -1.0834385445422625
 -1.0557333271693794
 -0.08535062227423565
 -1.783593060065521
  0.11117399279145967

Plotting the fractional differenced series, and the sample autocorrelation function. The theoretical autocorrelation function is also plotted by using the fi_var_vals() function.

p1 = plot( dx , label = "Fractionally differenced data" )
p2 = plot( 0:50 , autocorrelation( dx , 51 ) , label = "Sample autocorrelation function", line = :stem , marker = :circle)
plot!( 0:50, fi_cor_vals( 51, 0.3 ), linewidth = 2, line = :dash, label = "Theoretical autocorrelation function")
l = @layout [a b]
theme(:ggplot2)
plot(p1, p2, layout = l, size = (700, 250) )

2.2 Cross-sectional aggregation generation and plotting

Generating the cross-sectional aggregation series using the csa_gen() function.

Multiple dispatch is used to generate the series. The finite approximation is used if we define the first two arguments as the sample size and the number of series. The asymptotic version is used if the number of series to use is not passed to the function. The final two arguments are the parameters of the Beta function.

csa_fin = csa_gen(1000,1000,1.3,1.5)
csa_asym = csa_gen(1000,1.3,1.5)
1000-element Vector{Float64}:
  1.6030252183868878
  0.8573306416834465
  1.5303712736128772
 -0.7891472078980322
 -0.882765121556601
  0.08140313409601763
  0.19004851299411052
  0.8054252402927868
  1.9809498685219085
  0.5463250128619025
  ⋮
  1.885482132160916
  3.3979888982746855
  1.8613002384806776
  2.1740032426543214
  1.15009413905651
  2.445556824444752
  2.2321630950017806
  2.081473846697694
  2.044650720085682

Plotting the autocorrelation function of the finite and asymptotic series. The theoretical autocorrelation function is also plotted by using the csa_var_vals() function.

plot( 0:50 , autocorrelation( csa_fin , 51 ) , label = "Sample autocorrelation function, finite approximation", line = :stem , marker = :circle)
plot!( 0:50 , autocorrelation( csa_asym , 51 ) , label = "Sample autocorrelation function, asymptotic model", line = :stem , marker = :circle, color = :black)
plot!( 0:50, csa_cor_vals( 51, 1.3, 1.5 ), linewidth = 3, line = :dash, label = "Theoretical autocorrelation function", color = :red)
theme(:ggplot2)
plot!(size = (500, 200) )

2.3 Stochastic duration shocks generation and plotting

Data generation and plotting. We use the sds_gen() function to generate the data, and the LMPlot() function to plot it along with three diagnostic plots.

LMPlot(sds_gen(1000,0.45), name = "Stochastic duration shock")
theme(:ggplot2)
plot!( size = (700, 500) )

3 Long memory estimation

3.1 Classic estimators

3.1.1 Variance plot in Nile River data

The variance plot is computed by the log_variance_plot() function. The number of subsamples to use is passed to the function as the m argument. The slope and slope2 arguments are used to plot the theoretical and estimated slopes, respectively.

log_variance_plot( NileData().NileMin; m=300, slope = true, slope2 = true )
plot!(size = (500, 300) )

Computing the long memory parameter by the log-variance plot.

log_variance_est( NileData().NileMin; m=300 )
0.15619757726603745

3.1.2 Rescaled range analysis in Nile River data

Estimation by the rescaled range, \(R/S\). The number of subsamples to use is passed to the function as the k argument.

rescaled_range_est( NileData().NileMin; k = 300 )
0.42546060138176556

Rescaled range plot. The number of subsamples to use is passed to the function as the k argument. The slope and slope2 arguments are used to plot the theoretical and estimated slopes, respectively.

rescaled_range_plot( NileData().NileMin; k = 300, slope = true, slope2 = true )

3.2 Semiparametric estimators

3.2.1 Log-periodogram regression in Nile River data

GPH and bias-reduced estimator with one polynomial term. The number of polynomial terms is passed to the function as the br argument. If not passed, the function uses the default value of 0, recovering the GPH estimator.

(gph_est( NileData().NileMin ),  gph_est( NileData().NileMin ; br = 1))
(0.3744941050542384, 0.397455265935833)

Variance estimates using the data.

(gph_est_variance( NileData().NileMin ),  gph_est_variance( NileData().NileMin ; br = 1))
(0.002272008379624622, 0.0051120188541553995)

Variance estimates using multiple dispatch on the sample size.

(gph_est_variance( length( NileData().NileMin ) ),  gph_est_variance( length( NileData().NileMin ) ; br = 1))
(0.002272008379624622, 0.0051120188541553995)

3.2.2 (Exact) Local Whittle estimator in Nile River data

The local Whittle and exact local Whittle estimators are computed by the whittle_est() and exact_whittle_est() functions, respectively.

(whittle_est( NileData().NileMin ),  exact_whittle_est( NileData().NileMin ))
(0.37635955766434004, 0.40884952395694235)

Variance estimates using the data.

(whittle_est_variance( NileData().NileMin ),  exact_whittle_est_variance( NileData().NileMin ))
(0.0013812154696132596, 0.0013812154696132596)

3.3 Parametric estimators

3.3.1 Fractional differencing MLE in Nile River data

The fractional differencing MLE is computed by the fi_mle_est() function. The function returns the estimated fractional differencing parameter and the estimated standard error in a tuple.

dmle, sigfi = fi_mle_est( NileData().NileMin )
(0.39257149939646996, 69.95632676539788)

3.3.2 CSA MLE in Nile River data

The MLE for the cross-sectionally aggregated process is computed by the csa_mle_est() function. The function returns the estimated parameters of the Beta distribution, and the estimated standard error in a tuple.

pmle, qmle, sigcsa = csa_mle_est( NileData().NileMin )
(1.000001198981061, 2.447941980497643, 106.80313057162732)

3.3.3 Custom-HAR in Nile River data

A custom HAR model can be estimated by the function har_est(). The lags for the custom HAR model are passed as an array to the function as the m argument. The function returns the estimated parameters of the model, and the estimated standard error in a tuple.

beta_har, sighar = har_est( NileData().NileMin; m = [1, 7] )
([254.23541690816782, 0.40096895301134383, 0.377482428389991], 69.6124509836161)

4 Long memory forecasting

4.1 Forecasting the Nile River minima data

4.1.1 Fractional differencing forecasting

The fi_forecast_() function returns the forecasted values, while the fi_forecast_plot() functions returns the plot. First argument is the time series to forecast. The second and third arguments are used to define the number of steps ahead to forecast and the long memory parameter. The optional fourth argument is used to define the standard deviation to plot the confidence bands.

p1 = fi_forecast_plot( NileData().NileMin , 30, dmle, sigfi)

4.1.2 Cross-sectional aggregation forecasting

The csa_forecast_() function returns the forecasted values, while the csa_forecast_plot() functions returns the plot. The arguments are analogous to the fractional differencing plot function, noting that two Beta parameters are needed.

p2 = csa_forecast_plot( NileData().NileMin , 30, pmle, qmle, sigcsa)

4.1.3 Custom-HAR forecasting

The har_forecast_() function returns the forecasted values, while the har_forecast_plot() functions returns the plot. Nonetheless, the arguments are not analogous to the fractional differencing and cross-sectional aggregation plot functions, given that the custom-HAR model is estimated as part of the forecast.

Hence, the first argument is the time series to forecast and the second argument is used to define the number of steps ahead to forecast as before. The third optional argument, flag, is used to determine if the the confidence bands should be included in the plot. The fourth optional argument, m, is used to define the number of lags to use in the custom-HAR model. The optional fifth argument is used to define the standard deviation to plot the confidence bands.

p3 = har_forecast_plot( NileData().NileMin , 30; flag=true, m=[1, 7])

4.1.4 Comparing the forecasts

We generate a plot with the three forecasts using the @layout macro from the Plots.jl package.

l = @layout [a b c]
theme(:ggplot2)
plot(p1, p2, p3, layout = l, size = (800, 300), xlabel = "", ylabel = "" )
xlims!(500, 673)

End of the notebook.

Citation

BibTeX citation:
@article{vera-valdés2025,
  author = {Vera-Valdés, J. Eduardo},
  title = {LongMemory.jl: {Generating,} {Estimating,} and {Forecasting}
    {Long} {Memory} {Models} in {Julia}},
  journal = {Journal of Open Source Software},
  volume = {10},
  number = {108},
  pages = {7708},
  date = {2025},
  url = {https://doi.org/10.21105/joss.07708},
  doi = {10.21105/joss.07708},
  langid = {en},
  abstract = {**LongMemory.jl** is a package for time series long memory
    modelling in ***Julia***. The package provides functions for
    generating long memory, estimating the parameters of the models, and
    simulation. Generating methods include fractional differencing,
    stochastic error duration, and cross-sectional aggregation.
    Estimators include those inspired by the log-periodogram regression
    and parametric ones. This notebook provides an illustrative example
    on the use of the package.}
}
For attribution, please cite this work as:
Vera-Valdés, J. Eduardo. 2025. “LongMemory.jl: Generating, Estimating, and Forecasting Long Memory Models in Julia.” Journal of Open Source Software 10 (108): 7708. https://doi.org/10.21105/joss.07708.