Skip to content

Commit

Permalink
Add the ClarkWestTest for time series (#203)
Browse files Browse the repository at this point in the history
* add skeleton for implementing the ClarkWestTest

* update cw function skeleton

* Add modifications from Paul Soderlind

* Update src/clark_west.jl

Co-authored-by: Moritz Schauer <moritzschauer@web.de>
  • Loading branch information
guilhermebodin and mschauer committed Sep 19, 2020
1 parent aef116c commit 68bf986
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/src/time_series.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ JarqueBeraTest
ADFTest
```

## Clark-West test
```@docs
ClarkWestTest
```

## Diebold-Mariano test
```@docs
DieboldMarianoTest
Expand Down
1 change: 1 addition & 0 deletions src/HypothesisTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ include("wald_wolfowitz.jl")
include("f.jl")
include("correlation.jl")
include("diebold_mariano.jl")
include("clark_west.jl")
include("white.jl")
include("var_equality.jl")

Expand Down
76 changes: 76 additions & 0 deletions src/clark_west.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# clark_west.jl
# Clark-West
#
# Copyright (C) 2020 Guilherme Bodin, Paul Söderlind
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

export ClarkWestTest

struct ClarkWestTest <: ZTest
n::Int # number of observations
xbar::Real # estimated mean
stderr::Real # population standard error
z::Real # t-statistic
μ0::Real # mean under h_0
end

"""
ClarkWestTest(e1::AbstractVector{<:Real}, e2::AbstractVector{<:Real}, lookahead::Integer=1)
Perform the Clark-West test of equal performance of two nested prediction models, in terms of the
out-of-sample mean squared prediction errors.
`e1` is a vector of forecasts from the smaller (nested) model, `e2` is a vector of forecast
errors from the larger model, and `lookahead` is the number of steps ahead of the forecast.
Typically, the null hypothesis is that the two models perform equally well (a two-sided test),
but sometimes we test whether the larger model performs better, which is indicated by a
positive test statistic, for instance, above 1.645 for the 5% significance level (right tail test).
Implements: [`pvalue`](@ref)
# References
* Clark, T. E., West, K. D. 2006, Using out-of-sample mean squared prediction errors to test
the martingale difference hypothesis. Journal of Econometrics, 135(1): 155–186.
* Clark, T. E., West, K. D. 2007, Approximately normal tests for equal predictive accuracy
in nested models. Journal of Econometrics, 138(1): 291–311.
"""
function ClarkWestTest(e1::AbstractVector{<:Real}, e2::AbstractVector{<:Real},
lookahead::Integer=1)
length(e1) == length(e2) || throw(DimensionMismatch("inputs must have the same length"))
n = length(e1)
d = 2*e1.*(e1 - e2)
cw_cov = autocov(d, 0:lookahead-1)
cw_var = (cw_cov[1] + 2*sum(@view(cw_cov[2:end])))/n
xbar = mean(d)
stderr = sqrt(cw_var)
statistic_cw = xbar/stderr
return ClarkWestTest(n, xbar, stderr, statistic_cw, 0)
end

testname(::ClarkWestTest) = "Clark West test"
population_param_of_interest(x::ClarkWestTest) = ("Mean", x.μ0, x.xbar)
default_tail(::ClarkWestTest) = :both

function show_params(io::IO, x::ClarkWestTest, ident)
println(io, ident, "number of observations: $(x.n)")
println(io, ident, "CW statistic: $(x.z)")
println(io, ident, "population standard error: $(x.stderr)")
end
2 changes: 1 addition & 1 deletion src/z.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct OneSampleZTest <: ZTest
n::Int # number of observations
xbar::Real # estimated mean
stderr::Real # population standard error
z::Real # t-statistic
z::Real # z-statistic
μ0::Real # mean under h_0
end

Expand Down
28 changes: 28 additions & 0 deletions test/clark_west.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using HypothesisTests, Test

@testset "Clark-West tests" begin

e1 = [ 9.78432, 12.73500, 8.67224, 2.62740, 5.60947,
7.57809, 4.53774, 2.51084, 0.49290, 3.48394,
9.46152, 9.41220, 2.36289, 0.34495, 3.33599,
5.31357, 4.28219, -3.74471, -5.73575, -3.71781 ]

e2 = [ 2.82053, 4.39754, -1.78647, -4.30662, 3.69526,
3.37259, -1.09002, -0.50984, -0.78973, 3.89077,
7.52849, 2.82373, -3.95838, -0.13606, 4.54444,
4.18216, 1.67993, -5.38077, -0.85686, 2.70479 ]

atol = 1e-4
cw_test = ClarkWestTest(e1, e2)
@test pvalue(cw_test) 0.0002 atol=atol
@test pvalue(cw_test, tail=:right) 0.0001 atol=atol

cw_test = ClarkWestTest(e1, e2, 3)
@test pvalue(cw_test) 0.0157 atol=atol
@test pvalue(cw_test, tail=:right) 0.0079 atol=atol

show(IOBuffer(), cw_test)

@test_throws DimensionMismatch ClarkWestTest(rand(3), rand(4))

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ include("wilcoxon.jl")
include("z.jl")
include("f.jl")
include("diebold_mariano.jl")
include("clark_west.jl")
include("white.jl")
include("var_equality.jl")

0 comments on commit 68bf986

Please sign in to comment.