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

Illustrative example

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.

Citation

If you use this package in your research, please cite it as:

Vera-Valdés, J.E. (2024). “LongMemory.jl: Generating, Estimating, and Forecasting Long Memory Models in Julia”. arXiv 2401.14077. https://arxiv.org/abs/2401.14077

@article{VERAVALDES2024a,
title = {LongMemory.jl: Generating, Estimating, and Forecasting Long Memory Models in Julia},
year = {2024},
author = {Vera-Valdés, J.E.},
journal = {arXiv preprint arXiv:2401.14077},
url = {https://arxiv.org/abs/2401.14077}
}

Nile River minima data

Plotting the data, its autocorrelation function and spectral density

Loading the packages.

using LongMemory, Plots, DataFrames, CSV

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()

Long memory generation

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.9706563288552146
 -0.6880215128786354
  0.7973733442603006
  0.19192066674807962
 -0.46144511739243677
 -1.514799781765257
  2.2501256016290645
  2.0467649947720434
  1.5280686228970994
 -0.1269305253098677
  1.0031743811925413
  1.5910264204337021
  1.5990097697535517
  ⋮
  0.16749590571039433
 -1.1000791653801492
 -0.2066614957376673
 -0.9301341055606994
 -1.3332191794608566
 -0.4967820828987304
 -1.0341442518867014
 -1.0834385445422616
 -1.0557333271693792
 -0.08535062227423523
 -1.783593060065521
  0.11117399279145923

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) )

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}:
 -0.4367168712381375
 -0.27487348832431535
 -1.4597720231887006
 -1.9543596033122075
 -1.7243709928522584
 -2.4428086493018686
 -0.9191629868753005
  0.7415293874324638
  1.134795740715502
  0.6279072264178107
  0.41369790962276587
  0.08485016809582824
 -0.6521029064165207
  ⋮
  4.478681154035048
  3.081813222701477
  3.06039500311814
  3.1571288452388684
  1.9223537754308386
  1.9141232652309057
  2.469011881695831
  3.0677520738078097
  2.6416344731566843
  2.089792892141115
  1.6435055320286303
  1.2060575790942034

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) )

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) )