User Tools

Site Tools


unitimeseries
snippet.juliarepl
julia> pkgchk( [ "julia" => v"1.0.2", "Distributions" => v"0.15.0", "TimeSeries" => v"0.11.1" ] )

Multi Time Series

Panel Data

Arrelano-Bond (1991)

Two-Step Procedures for Instrumental Variables for Endogenous Corrections

Time-Series

FIXME Unfortunately, the TimeSeries package is not working in 1.0.0 as of September 2018.

Tests for AutoCorrelation

HypothesisTests offers tests for whether there is autocorrelation in values.

Augmented Dickey-Fuller Test: Unit-Root (Is x like a random walk?)

snippet.juliarepl
julia> ADFTest( x, :none, 0 )
Augmented Dickey-Fuller unit root test
--------------------------------------
Population details:
    parameter of interest:   coefficient on lagged non-differenced variable
    value under h_0:         0
    point estimate:          –0.9506767150463514

Test summary:
    outcome with 95% confidence: reject h_0
    p-value:                     <1e–15

Details:
    sample size in regression:          99
    number of lags:                     0
    ADF statistic:                      –9.41503163545725
    Critical values at 1%, 5%, and 10%: [2.588461.943991.61441]

LjungBox/BoxPierce: Does X seem to have Autocorrelation or Serial Uncorrelatedness?

julia> LjungBoxTest( x, 1 )
Ljung-Box autocorrelation test
------------------------------
Population details:
    parameter of interest:   autocorrelations up to lag k
    value under h_0:         all zero
    point estimate:          NaN

Test summary:
    outcome with 95% confidence: fail to reject h_0
    one-sided p-value:           0.6249

Details:
    number of observations:         100
    number of lags:                 1
    degrees of freedom correction:  0
    Q statistic:                    0.23906259199508553


julia> BoxPierceTest( x, 1 )
Box-Pierce autocorrelation test
-------------------------------
Population details:
    parameter of interest:   autocorrelations up to lag k
    value under h_0:         all zero
    point estimate:          NaN

Test summary:
    outcome with 95% confidence: fail to reject h_0
    one-sided p-value:           0.6300

Details:
    number of observations:         100
    number of lags:                 1
    degrees of freedom correction:  0
    Q statistic:                    0.23203133928934774

Basic

StatsBase offers autocov, autocor, and cross-covariance/cross-correlations (with lags), all with options to demean.

In the Box-Jenkins spirit, it also calculates partial autocorrelation functions (pacf)

snippet.juliarepl
julia> x= zeros(10_002); for i=3:10_002; x[i]= 0.3*x[i–1]+ 0.1*x[i–2] + rand( Normal(0,1) ); end; x= x[3:10_000];

julia> #pacf( x, [10] )

FIXME This is not what I wanted. I wanted 10 lags of PACF

FIXME show autocor and autocov

Generate a Random Walk or Autoregressive or ARMA Process

Unlike interpreted languages, there is little penalty in julia to explicit specification:

FIXME this oulines how one could generate an arama.

snippet.juliafix
[download only julia statements]
using Distributions:Normal
 
function arma( n::Int, ar::Vector{Float64}= [ 0.0 ] , ma::Vector{Float64}= [ 0.0 ],
	       sigma::Float64= 1.0, startvalues::Vector{Float64}= [])
    ## AbstractVector, rather than Vector?
    ## check that we have enough valid starting values
    ## check that we want enough values to use up ar and ma parameters
    ## check that sigma is positive
    ## check that scalars are allowed.
 
    err= rand(Normal(0,sigma), n )
    rv= vcat( startvalues, zeros( n - length(startvalues) ) )
    for i=(length(startvalues)+1):n;
      	rv[i]= (ar .* rv[ (i-length(ar)):(i-1) ] + ma .* err[ (i-length(ar)):(i-1) ] + err[i])[]
    end#for
    rv
end#function arma
 
julia> rw= ar(10, [ 1.0 ], 10.0 )
...
 
julia>

Full Time-Series Analysis with TimeSeries Package

The TimeSeries.jl Package facilitates more involved time-series analysis. Each value in a time-series object must be accompanied by a date.

snippet.juliarepl
julia> using TimeSeries:TimeArray

julia> x = TimeArray([Date(2000, 1, 1), Date(2000, 1, 2), Date(2000, 1, 3)], [11,22,33])
3x1 TimeSeries.TimeArray{Int64,1,Date,Array{Int64,1}} 20000101 to 20000103
│            │       │
├────────────┼───────┤
│ 2000010111    │
│ 2000010222    │
│ 2000010333

The simplest functionality is leading and lagging:

snippet.juliarepl
julia> using TimeSeries

julia> x = TimeArray([Date(2000, 1, 1), Date(2000, 1, 2), Date(2000, 1, 3)], [11,22,33]);

julia> lead(x)
2x1 TimeSeries.TimeArray{Int64,1,Date,Array{Int64,1}} 20000101 to 20000102
│            │       │
├────────────┼───────┤
│ 2000010122    │
│ 2000010233julia> lag(x)
2x1 TimeSeries.TimeArray{Int64,1,Date,Array{Int64,1}} 20000102 to 20000103
│            │       │
├────────────┼───────┤
│ 2000010211    │
│ 2000010322

For simpler, equal-spaced data, it is often easier to work with plain arrays. You can then use the simpler lead and lag functions in Functions.

Estimating Auto-Regressive and Moving-Average Models

FIXME Estimating ARMA models is to be written.

Backmatter

Useful Packages on Julia Repository

Notes

References

unitimeseries.txt · Last modified: 2018/11/22 20:49 (external edit)