Skip to content

Commit

Permalink
Add modifications from Paul Soderlind
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermebodin committed May 25, 2020
1 parent 4dd86bd commit 066cd62
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/HypothesisTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,6 @@ include("wald_wolfowitz.jl")
include("f.jl")
include("correlation.jl")
include("diebold_mariano.jl")
include("clark_west.jl")

end
60 changes: 34 additions & 26 deletions src/clark_west.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,53 @@

export ClarkWestTest

struct ClarkWestTest <: TTest
n::Int # number of observations
xbar::Real # estimated mean
df::Int # degrees of freedom
t::Real # test statistic
stderr::Real # empirical standard error
μ0::Real # mean under h_0
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::Int=1)
ClarkWestTest(e1::AbstractVector{<:Real}, e2::AbstractVector{<:Real};lookahead::Integer=1)
Perform the Clark-West test ...
Perform the Clark-West test of equal performance of two nested predition models, in terms of the
out-of-sample mean squared prediction errors.
# References
`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).
* Clark, T. E., West, K. D. 2006, Using out-of-sample mean squared prediction errors to 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.
* 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::Int=1)

@assert length(e1) == length(e2)
n = length(e1)



return ClarkWestTest(n, xbar, n - 1, statistic_cw, stderr, 0.0)
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 = HypothesisTests.autocov(d, collect(0:lookahead-1))
cw_var = (cw_cov[1] + 2 * sum(cw_cov[2:end]))/n
xbar = mean(d)
stderr = sqrt(cw_var)
statistic_cw = xbar/stderr
return ClarkWestTest(n, xbar, stderr, statistic_cw, 0.0)
end

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

function show_params(io::IO, x::ClarkWestTest, ident)
println(io, ident, "number of observations: $(x.n)")
println(io, ident, "CW statistic: $(x.t)")
println(io, ident, "degrees of freedom: $(x.df)")
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
29 changes: 28 additions & 1 deletion test/clark_west.jl
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
# Add tests
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; lookahead=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,3 +37,4 @@ include("wilcoxon.jl")
include("z.jl")
include("f.jl")
include("diebold_mariano.jl")
include("clark_west.jl")

0 comments on commit 066cd62

Please sign in to comment.