using Pkg
Pkg.activate(pwd())
LongMemory.jl: Generating, Estimating, and Forecasting Long Memory Models in Julia
Illustrative example on the package usage
J. Eduardo Vera-Valdés (eduardo@math.aau.dk)
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:
@article{VeraValdes2024longmemory,
title={LongMemory.jl: Generating, Estimating, and Forecasting Long Memory Models in Julia},
author={Vera-Vald{\'e}s, J Eduardo},
year={2024},
}
Nile River minima data, its autocorrelation function and spectral density
Packages loading and plot.
using LongMemory, Plots, DataFrames, CSV
= NileData()
NileMin theme(:ggplot2)
= plot( NileMin.Year , NileMin.NileMin, xlabel="Year" , ylabel = "Nile River minima" , legend = false ) p1
Plotting the autocorrelation function using the autocorrelation_plot() function from LongMemory.jl.
= autocorrelation_plot( NileMin.NileMin , 50 ) p2
Plotting the spectral density with LongMemory.jl’s periodogram() function.
= periodogram_plot(NileMin.NileMin, slope = true) p3
= @layout [a; b c]
l theme(:ggplot2)
plot(p1, p2, p3, layout = l, size = (700, 500) )
Saving the plot in a file.
png("extras/NileRiverMin")
"extras\\NileRiverMin.png"
All the above plus the log-variance plot can be done in a single plot.
NileDataPlot()
Fractional differencing generation and plotting
Loading the packages, setting the seed, and generating the fractional differencing series.
using Random
Random.seed!(1234)
= fi_gen(500,0.3) dx
500-element Vector{Float64}:
0.9706563288552146
-0.6880215128786354
0.7973733442603006
0.19192066674807962
-0.46144511739243677
-1.514799781765257
2.2501256016290645
2.0467649947720434
1.5280686228970994
-0.1269305253098677
⋮
-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 from LongMemory.jl.
= plot( dx , label = "Fractionally differenced data" )
p1 = plot( 0:50 , autocorrelation( dx , 51 ) , label = "Sample autocorrelation function", line = :stem , marker = :circle)
p2 plot!( 0:50, fi_cor_vals( 51, 0.3 ), linewidth = 2, line = :dash, label = "Theoretical autocorrelation function")
= @layout [a b]
l theme(:ggplot2)
plot(p1, p2, layout = l, size = (700, 250) )
Saving the plot in a file.
png("extras/figen_example")
"extras\\figen_example.png"
Cross-sectional aggregation generation and plotting
Generating the cross-sectional aggregation series using the finite and asymptotic methods.
Random.seed!(1234)
= csa_gen(1000,1000,1.3,1.5)
csa_fin = csa_gen(1000,1.3,1.5) csa_asym
1000-element Vector{Float64}:
-1.434679999957066
0.5962989958783722
0.1418324402922586
0.40804516361116644
0.23480352350419353
0.7451556571924364
-0.2941583523329968
-0.4777412339927196
-1.517659837349598
-2.113784945213323
⋮
-2.7406120122495956
-2.280351869139533
-2.5191991540873353
-3.405192265416972
-4.311258066542951
-2.9105934330779
-2.171456426631365
-1.3123848584664004
-0.6638330178874884
Plotting the autocorrelation function of the finite and asymptotic series. The theoretical autocorrelation function is also plotted by using the csa_var_vals() function from LongMemory.jl.
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) )
Save the plot in a file.
png("extras/csagen_example")
"extras\\csagen_example.png"
Stochastic error duration generation and plotting
Data generation and plotting.
LMPlot(sds_gen(1000,0.45), name = "Stochastic duration shock")
theme(:ggplot2)
plot!( size = (700, 500) )