Skip to content

Commit 56fbf2a

Browse files
BenjaminBornararslan
authored andcommitted
Add Breusch-Godfrey Test for Serial Correlation (#89)
1 parent 42ab418 commit 56fbf2a

3 files changed

Lines changed: 348 additions & 0 deletions

File tree

src/HypothesisTests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,5 @@ include("wilcoxon.jl")
131131
include("power_divergence.jl")
132132
include("anderson_darling.jl")
133133
include("box_test.jl")
134+
include("breusch_godfrey.jl")
134135
end

src/breusch_godfrey.jl

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# breusch_godfrey.jl
2+
# Breusch-Godfrey test for autocorrelation
3+
#
4+
# Copyright (C) 2017 Benjamin Born
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining
7+
# a copy of this software and associated documentation files (the
8+
# "Software"), to deal in the Software without restriction, including
9+
# without limitation the rights to use, copy, modify, merge, publish,
10+
# distribute, sublicense, and/or sell copies of the Software, and to
11+
# permit persons to whom the Software is furnished to do so, subject to
12+
# the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be
15+
# included in all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
export BreuschGodfreyTest
26+
27+
immutable BreuschGodfreyTest <: HypothesisTest
28+
n::Int # number of observations
29+
lag::Int # number of lags in test statistic
30+
BG::Float64 # test statistic
31+
end
32+
33+
"""
34+
BreuschGodfreyTest(X, e, lag, start0 = true)
35+
36+
Compute the Breusch-Godfrey test for serial correlation in the residuals
37+
of a regression model.
38+
39+
`X` is the matrix of regressors from the original model and `e` the vector of residuals.
40+
`lag` determines the number of lagged residuals included in the auxiliary regression.
41+
Set `start0` to specify how the starting values for the lagged residuals are handled.
42+
`start0 = true` (default) sets them to zero (as in Godfrey, 1978); `start0 = false`
43+
uses the first `lag` residuals as starting values, i.e. shortening the sample by `lag`.
44+
45+
External links
46+
47+
* [Breusch-Godfrey test on Wikipedia](https://en.wikipedia.org/wiki/Breusch–Godfrey_test)
48+
"""
49+
function BreuschGodfreyTest{T<:Real}(xmat::AbstractArray{T}, e::AbstractVector{T},
50+
lag::Int, start0::Bool=true)
51+
n = size(e,1)
52+
elag = zeros(Float64,n,lag)
53+
for ii = 1:lag # construct lagged residuals
54+
elag[ii+1:end,ii] = e[1:end-ii]
55+
end
56+
57+
offset = start0 ? 0 : lag
58+
59+
regmat = [xmat[offset+1:end,:] elag[offset+1:end,:]]
60+
regcoeff = regmat\e[offset+1:end]
61+
resid = e[offset+1:end] - regmat*regcoeff
62+
63+
rsq = 1 - dot(resid,resid)/dot(e[offset+1:end],e[offset+1:end]) # uncentered R^2
64+
BG = (n-offset)*rsq
65+
BreuschGodfreyTest(n-offset,lag,BG)
66+
end
67+
68+
testname(::BreuschGodfreyTest) = "Breusch-Godfrey autocorrelation test"
69+
population_param_of_interest(x::BreuschGodfreyTest) =
70+
("coefficients on lagged residuals up to lag p", "all zero", NaN)
71+
72+
function show_params(io::IO, x::BreuschGodfreyTest, ident)
73+
println(io, ident, "number of observations: ", x.n)
74+
println(io, ident, "number of lags: ", x.lag)
75+
println(io, ident, "T*R^2 statistic: ", x.BG)
76+
end
77+
78+
pvalue(x::BreuschGodfreyTest) = pvalue(Chisq(x.lag), x.BG; tail=:right)

0 commit comments

Comments
 (0)