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) )
Saving the plot in a file.
png("extras/edmgen_example")
"extras\\edmgen_example.png"
Classic estimators
Variance plot in Nile River data
log_variance_plot( NileData().NileMin; m=300, slope = true, slope2 = true )
plot!(size = (500, 300) )
png("extras/Nile_variance_plot")
"extras\\Nile_variance_plot.png"
Computing the long memory parameter by the log-variance plot.
log_variance_est( NileData().NileMin; m = 300 )
0.15619757726603672
Rescaled range analysis in Nile River data
Estimation by rescaled range, R/S
rescaled_range_est( NileData().NileMin ; k = 300)
0.4254606013817649
Rescaled range plot
rescaled_range_plot( NileMin[:,2]; k = 300, slope = true, slope2 = true )
Semiparametric estimators
Log-periodogram regression in Nile River data
GPH and bias-reduced estimator with one polynomial term
gph_est( NileData().NileMin ), gph_est( NileData().NileMin ; br = 1)) (
(0.37449410505423664, 0.39745526593583125)
Variance estimates
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)
(Exact) Local Whittle estimator in Nile River data
Local Whittle and exact local Whittle estimators
whittle_est( NileData().NileMin ), exact_whittle_est( NileData().NileMin )) (
(0.37635955766433826, 0.4088495239569418)
whittle_est_variance( NileData().NileMin ), exact_whittle_est_variance( NileData().NileMin )) (
(0.0013812154696132596, 0.0013812154696132596)
Parametric estimators
Fractional differencing MLE in Nile River data
= fi_mle_est( NileData().NileMin ) dmle, sigfi
(0.3925714993964694, 69.95632676539786)
CSA MLE in Nile River data
= csa_mle_est( NileData().NileMin ) pmle, qmle, sigcsa
(1.0000010468704805, 2.447721694890551, 106.79804259351367)
Custom-HAR in Nile River data
= har_est( NileData().NileMin; m = [1, 7] ) beta_har, sighar
([254.23541690816745, 0.40096895301134294, 0.377482428389992], 69.6124509836161)
Forecasting
Illustration of the forecasting methods in Nile River data
Fractional differencing
= fi_forecast_plot( NileData().NileMin , 30, dmle, sigfi) p1
Cross-sectional aggregation
= csa_forecast_plot( NileData().NileMin , 30, pmle, qmle, sigcsa) p2
HAR forecasting
= har_forecast_plot( NileData().NileMin , 30) p3
Comparing the forecasting methods
= @layout [a b c]
l theme(:ggplot2)
plot(p1, p2, p3, layout = l, size = (800, 300) )
xlims!(500, 673)
xlabel!("")
ylabel!("")
png("extras/Nile_forecasting_plot")
"extras\\Nile_forecasting_plot.png"