Skip to content

Commit 2bca91f

Browse files
committed
Revert prewhiten to prewhite
1 parent 5d4a1dd commit 2bca91f

File tree

6 files changed

+68
-68
lines changed

6 files changed

+68
-68
lines changed

src/EWC.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Base.@propagate_inbounds function Λ(j::Integer, m::AbstractMatrix{F}) where F<:
1010
return L *= sqrt(2/T)
1111
end
1212

13-
function avar(k::EWC, X::Matrix{F}; prewhiten=false) where {F<:AbstractFloat}
14-
if prewhiten
13+
function avar(k::EWC, X::Matrix{F}; prewhite=false) where {F<:AbstractFloat}
14+
if prewhite
1515
Z, D = fit_var(X)
1616
else
1717
Z = X

src/HAC.jl

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
function avar(k::T, X::AbstractMatrix{F}; prewhiten=false) where {T<:HAC, F<:AbstractFloat}
2-
if prewhiten
1+
function avar(k::T, X::AbstractMatrix{F}; prewhite=false) where {T<:HAC, F<:AbstractFloat}
2+
if prewhite
33
Z, D = fit_var(X)
44
else
55
Z = X
@@ -9,9 +9,9 @@ function avar(k::T, X::AbstractMatrix{F}; prewhiten=false) where {T<:HAC, F<:Abs
99
resize!(k.weights, size(Z,2))
1010
fill!(k.weights, 1.0)
1111
end
12-
k.bw .= _optimalbandwidth(k, Z, prewhiten)
12+
k.bw .= _optimalbandwidth(k, Z, prewhite)
1313
V = _avar(k, Z)
14-
if prewhiten
14+
if prewhite
1515
v = inv(I-D')
1616
return v*V*v'
1717
else
@@ -38,7 +38,7 @@ function _avar(k::HAC, Z::AbstractMatrix{T}) where T<:AbstractFloat
3838
Symmetric(V)
3939
end
4040

41-
avarscaler(K::HAC, X; prewhiten=false) = size(X, 1)
41+
avarscaler(K::HAC, X; prewhite=false) = size(X, 1)
4242

4343
"""
4444
Γplus!(Q, A::AbstractMatrix{T}, j::Int) where T
@@ -135,19 +135,19 @@ end
135135
function workingoptimalbw(
136136
k::HAC{T},
137137
m::AbstractMatrix;
138-
prewhiten::Bool=false,
138+
prewhite::Bool=false,
139139
) where T<:Union{Andrews, NeweyWest}
140-
X, D = prewhiter(m, prewhiten)
140+
X, D = prewhiter(m, prewhite)
141141
setkernelweights!(k, X)
142-
bw = _optimalbandwidth(k, X, prewhiten)
142+
bw = _optimalbandwidth(k, X, prewhite)
143143
return X, D, bw
144144
end
145145

146146
workingoptimalbw(k::HAC{T}, m::AbstractMatrix; kwargs...) where T<:Fixed = (m, Matrix{eltype{m}}(undef,0,0), first(k.bw))
147147

148148
"""
149-
optimalbandwidth(k::HAC{T}, mm; prewhiten::Bool=false) where {T<:Andrews}
150-
optimalbandwidth(k::HAC{T}, mm; prewhiten::Bool=false) where {T<:NeweyWest}
149+
optimalbandwidth(k::HAC{T}, mm; prewhite::Bool=false) where {T<:Andrews}
150+
optimalbandwidth(k::HAC{T}, mm; prewhite::Bool=false) where {T<:NeweyWest}
151151
152152
153153
Calculate the optimal bandwidth according to either Andrews or Newey-West.
@@ -158,29 +158,29 @@ function optimalbw(
158158
demean::Bool = false,
159159
dims::Int = 1,
160160
means::Union{Nothing, AbstractArray} = nothing,
161-
prewhiten::Bool=false
161+
prewhite::Bool=false
162162
) where T<:Union{Andrews, NeweyWest}
163163
X = demean ? demeaner(m; means=means, dims=dims) : m
164-
_, _, bw = workingoptimalbw(k, X; prewhiten=prewhiten)
164+
_, _, bw = workingoptimalbw(k, X; prewhite=prewhite)
165165
return bw
166166
end
167167

168-
_optimalbandwidth(k::HAC{T}, mm, prewhiten) where {T<:NeweyWest} = bwNeweyWest(k, mm, prewhiten)
169-
_optimalbandwidth(k::HAC{T}, mm, prewhiten) where {T<:Andrews} = bwAndrews(k, mm, prewhiten)
170-
_optimalbandwidth(k::HAC{T}, mm, prewhiten) where {T<:Fixed} = first(k.bw)
168+
_optimalbandwidth(k::HAC{T}, mm, prewhite) where {T<:NeweyWest} = bwNeweyWest(k, mm, prewhite)
169+
_optimalbandwidth(k::HAC{T}, mm, prewhite) where {T<:Andrews} = bwAndrews(k, mm, prewhite)
170+
_optimalbandwidth(k::HAC{T}, mm, prewhite) where {T<:Fixed} = first(k.bw)
171171

172-
function bwAndrews(k::HAC, mm, prewhiten::Bool)
172+
function bwAndrews(k::HAC, mm, prewhite::Bool)
173173
n, p = size(mm)
174174
a1, a2 = getalpha(k, mm)
175175
k.bw[1] = bw_andrews(k, a1, a2, n)
176176
return k.bw[1]
177177
end
178178

179-
function bwNeweyWest(k::HAC, mm, prewhiten::Bool)
179+
function bwNeweyWest(k::HAC, mm, prewhite::Bool)
180180
w = kernelweights(k)
181181
bw = bandwidth(k)
182182
n, _ = size(mm)
183-
l = getrates(k, mm, prewhiten)
183+
l = getrates(k, mm, prewhite)
184184
xm = mm*w
185185
a = Vector{eltype(xm)}(undef, l+1)
186186
@inbounds for j in 0:l
@@ -190,7 +190,7 @@ function bwNeweyWest(k::HAC, mm, prewhiten::Bool)
190190
a0 = a[1] + 2*sum(aa)
191191
a1 = 2*sum((1:l) .* aa)
192192
a2 = 2*sum((1:l).^2 .* aa)
193-
bw[1] = bwnw(k, a0, a1, a2)*(n+prewhiten)^growthrate(k)
193+
bw[1] = bwnw(k, a0, a1, a2)*(n+prewhite)^growthrate(k)
194194
return bw[1]
195195
end
196196

@@ -217,11 +217,11 @@ function getalpha(k, mm)
217217
return α₁, α₂
218218
end
219219

220-
function getrates(k, mm, prewhiten::Bool)
220+
function getrates(k, mm, prewhite::Bool)
221221
n, _ = size(mm)
222222
lrate = lagtruncation(k)
223-
adj = prewhiten ? 3 : 4
224-
floor(Int, adj*((n+prewhiten)/100)^lrate)
223+
adj = prewhite ? 3 : 4
224+
floor(Int, adj*((n+prewhite)/100)^lrate)
225225
end
226226

227227
@inline bwnw(k::BartlettKernel, s0, s1, s2) = 1.1447*((s1/s0)^2)^growthrate(k)
@@ -298,8 +298,8 @@ end
298298
# -----------------------------------------------------------------------------
299299
# Prewhiter
300300
# -----------------------------------------------------------------------------
301-
function prewhiter(M::AbstractMatrix{T}, prewhiten::Bool) where T<:AbstractFloat
302-
if prewhiten
301+
function prewhiter(M::AbstractMatrix{T}, prewhite::Bool) where T<:AbstractFloat
302+
if prewhite
303303
return fit_var(M)
304304
else
305305
if eltype(M) (Float32, Float64)

src/aVar.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Asymptotic Variance Estimators
33
4-
aVar(k::AVarEstimator, m::AbstractMatrix{T}; demean::Bool=true, dims::Int=1, means::Union{Nothing, AbstractArray}=nothing, prewhiten::Bool=false, scale=true)
4+
aVar(k::AVarEstimator, m::AbstractMatrix{T}; demean::Bool=true, dims::Int=1, means::Union{Nothing, AbstractArray}=nothing, prewhite::Bool=false, scale=true)
55
66
Caclulate the asymptotic variance of \\bar{X}_{,j}=\\frac{1}{b_n}\\sum{i=1}^n X_{i,j}, where \\bar{X}_{,j} is the j-th column of `X`.
77
@@ -14,10 +14,10 @@ Caclulate the asymptotic variance of \\bar{X}_{,j}=\\frac{1}{b_n}\\sum{i=1}^n X_
1414

1515
aVar(k::AVarEstimator, m::AbstractMatrix; kwargs...) = aVar(k, float.(m), kwargs...)
1616

17-
function aVar(k::AVarEstimator, m::AbstractMatrix{T}; demean::Bool=true, dims::Int=1, means::Union{Nothing, AbstractArray}=nothing, prewhiten::Bool=false, scale=true) where T<:AbstractFloat
17+
function aVar(k::AVarEstimator, m::AbstractMatrix{T}; demean::Bool=true, dims::Int=1, means::Union{Nothing, AbstractArray}=nothing, prewhite::Bool=false, scale=true) where T<:AbstractFloat
1818
Base.require_one_based_indexing(m)
1919
X = demean ? demeaner(m; means=means, dims=dims) : m
20-
Shat = avar(k, X; prewhiten=prewhiten)
20+
Shat = avar(k, X; prewhite=prewhite)
2121
return scale ? Shat./size(X,dims) : Shat
2222
end
2323

src/burger.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## The bun and the meat!
2-
function burger(K::AVarEstimator, m::AbstractMatrix{T}, bun::Union{AbstractMatrix{F}, Factorization{F}}; demean=false, prewhiten::Bool=false, dof::Int64=0) where {T<:Real, F<:Real}
3-
kwargs = (demean=demean, prewhiten=prewhiten, dof=dof, unscaled=true)
2+
function burger(K::AVarEstimator, m::AbstractMatrix{T}, bun::Union{AbstractMatrix{F}, Factorization{F}}; demean=false, prewhite::Bool=false, dof::Int64=0) where {T<:Real, F<:Real}
3+
kwargs = (demean=demean, prewhite=prewhite, dof=dof, unscaled=true)
44
P = patty(K, m; kwargs...)
55
## Form A^{-1}BA^{-1}'
66
return (bun\P)/bun
@@ -10,7 +10,7 @@ patty(K::AVarEstimator, m::AbstractMatrix; kwargs...) = aVar(K, m; kwargs...)
1010

1111
## And with this, we rule the world, not really...
1212
"""
13-
burger(K::AVarEstimator, residuals::AbstractVector, modelmatrix::AbstractMatrix, bun::Union{AbstractMatrix{F}, Factorization{F}}; demean=false, prewhiten::Bool=false, dof::Int64=0)
13+
burger(K::AVarEstimator, residuals::AbstractVector, modelmatrix::AbstractMatrix, bun::Union{AbstractMatrix{F}, Factorization{F}}; demean=false, prewhite::Bool=false, dof::Int64=0)
1414
1515
Burger function for generalized linear model.
1616
@@ -22,8 +22,8 @@ Burger function for generalized linear model.
2222
"""
2323

2424

25-
# function burger(K::AVarEstimator, residuals::AbstractVector, modelmatrix::AbstractMatrix, bun::Union{AbstractMatrix, Factorization}; demean=false, prewhiten::Bool=false, dof::Int64=0)
26-
# kwargs = (demean=demean, prewhiten=prewhiten, dof=dof, unscaled=true)
25+
# function burger(K::AVarEstimator, residuals::AbstractVector, modelmatrix::AbstractMatrix, bun::Union{AbstractMatrix, Factorization}; demean=false, prewhite::Bool=false, dof::Int64=0)
26+
# kwargs = (demean=demean, prewhite=prewhite, dof=dof, unscaled=true)
2727
# P = patty(K, residuals, modelmatrix; kwargs...)
2828
# ## Form A^{-1}BA^{-1}'
2929
# return (bun\P)/bun

src/types.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Truncated{NeweyWest}()
3636
struct TruncatedKernel{G <: BandwidthType} <: HAC{G}
3737
bw::Vector{WFLOAT}
3838
weights::Vector{WFLOAT}
39-
prewhiten::Base.RefValue{Bool}
39+
prewhite::Base.RefValue{Bool}
4040
end
4141

4242
const Truncated = TruncatedKernel
@@ -55,7 +55,7 @@ Bartlett(::Type{NeweyWest})
5555
struct BartlettKernel{G <: BandwidthType} <: HAC{G}
5656
bw::Vector{WFLOAT}
5757
weights::Vector{WFLOAT}
58-
prewhiten::Base.RefValue{Bool}
58+
prewhite::Base.RefValue{Bool}
5959
end
6060

6161
const Bartlett = BartlettKernel
@@ -74,7 +74,7 @@ Parzen(::Type{NeweyWest})
7474
struct ParzenKernel{G <: BandwidthType} <: HAC{G}
7575
bw::Vector{WFLOAT}
7676
weights::Vector{WFLOAT}
77-
prewhiten::Base.RefValue{Bool}
77+
prewhite::Base.RefValue{Bool}
7878
end
7979

8080
const Parzen = ParzenKernel
@@ -92,7 +92,7 @@ TukeyHanning(::Type{NeweyWest})
9292
struct TukeyHanningKernel{G <: BandwidthType} <: HAC{G}
9393
bw::Vector{WFLOAT}
9494
weights::Vector{WFLOAT}
95-
prewhiten::Base.RefValue{Bool}
95+
prewhite::Base.RefValue{Bool}
9696
end
9797

9898
const TukeyHanning = TukeyHanningKernel
@@ -111,7 +111,7 @@ QuadraticSpectral(::Type{NeweyWest})
111111
struct QuadraticSpectralKernel{G <: BandwidthType} <: HAC{G}
112112
bw::Vector{WFLOAT}
113113
weights::Vector{WFLOAT}
114-
prewhiten::Base.RefValue{Bool}
114+
prewhite::Base.RefValue{Bool}
115115
end
116116

117117
const QuadraticSpectral = QuadraticSpectralKernel

test/runtests.jl

+29-29
Original file line numberDiff line numberDiff line change
@@ -25,83 +25,83 @@ end
2525
𝒦 = Bartlett{NeweyWest}()
2626
Σ = a𝕍ar(𝒦, X)
2727
@test 𝒦.bw[1] 5.326955 atol=1e-6
28-
@test optimalbw(𝒦, X; prewhiten=false, demean=true) 𝒦.bw[1] rtol=1e-9
28+
@test optimalbw(𝒦, X; prewhite=false, demean=true) 𝒦.bw[1] rtol=1e-9
2929

3030
𝒦 = Parzen{NeweyWest}()
3131
Σ = a𝕍ar(𝒦, X)
3232
@test 𝒦.bw[1] 9.72992 atol=1e-6
33-
@test optimalbw(𝒦, X; prewhiten=false, demean=true) 𝒦.bw[1] rtol=1e-9
33+
@test optimalbw(𝒦, X; prewhite=false, demean=true) 𝒦.bw[1] rtol=1e-9
3434

3535
𝒦 = QuadraticSpectral{NeweyWest}()
3636
Σ = a𝕍ar(𝒦, X)
3737
@test 𝒦.bw[1] 4.833519 atol=1e-6
38-
@test optimalbw(𝒦, X; prewhiten=false, demean=true) 𝒦.bw[1] rtol=1e-9
38+
@test optimalbw(𝒦, X; prewhite=false, demean=true) 𝒦.bw[1] rtol=1e-9
3939
## ---
4040
𝒦 = Bartlett{NeweyWest}()
41-
Σ = a𝕍ar(𝒦, X; prewhiten=true)
41+
Σ = a𝕍ar(𝒦, X; prewhite=true)
4242
@test 𝒦.bw[1] 1.946219 rtol=1e-7
43-
@test optimalbw(𝒦, X; prewhiten=true) == 𝒦.bw[1]
43+
@test optimalbw(𝒦, X; prewhite=true) == 𝒦.bw[1]
4444

4545
𝒦 = Parzen{NeweyWest}()
46-
Σ = a𝕍ar(𝒦, X; prewhiten=true)
46+
Σ = a𝕍ar(𝒦, X; prewhite=true)
4747
@test 𝒦.bw[1] 6.409343 rtol=1e-7
48-
@test optimalbw(𝒦, X; prewhiten=true) == 𝒦.bw[1]
48+
@test optimalbw(𝒦, X; prewhite=true) == 𝒦.bw[1]
4949

5050
𝒦 = QuadraticSpectral{NeweyWest}()
51-
Σ = a𝕍ar(𝒦, X; prewhiten=true)
51+
Σ = a𝕍ar(𝒦, X; prewhite=true)
5252
@test 𝒦.bw[1] 3.183961 atol=1e-6
53-
@test optimalbw(𝒦, X; prewhiten=true) == 𝒦.bw[1]
53+
@test optimalbw(𝒦, X; prewhite=true) == 𝒦.bw[1]
5454
end
5555

5656
@testset "OB Andrews" begin
5757
𝒦 = Bartlett{Andrews}()
58-
Σ = a𝕍ar(𝒦, X; prewhiten=false);
58+
Σ = a𝕍ar(𝒦, X; prewhite=false);
5959
@test 𝒦.bw[1] 2.329739 rtol=1e-6
60-
@test optimalbw(𝒦, X; prewhiten=false) == 𝒦.bw[1]
60+
@test optimalbw(𝒦, X; prewhite=false) == 𝒦.bw[1]
6161

6262
𝒦 = Parzen{Andrews}()
63-
Σ = a𝕍ar(𝒦, X; prewhiten=false);
63+
Σ = a𝕍ar(𝒦, X; prewhite=false);
6464
@test 𝒦.bw[1] 4.81931 rtol=1e-6
65-
@test CovarianceMatrices.optimalbw(𝒦, X; prewhiten=false) == 𝒦.bw[1]
65+
@test CovarianceMatrices.optimalbw(𝒦, X; prewhite=false) == 𝒦.bw[1]
6666

6767
𝒦 = QuadraticSpectral{Andrews}()
68-
Σ = a𝕍ar(𝒦, X; prewhiten=false)
68+
Σ = a𝕍ar(𝒦, X; prewhite=false)
6969
@test 𝒦.bw[1] 2.394082 atol=1e-6
7070
@test optimalbw(𝒦, X) == 𝒦.bw[1]
7171

7272
𝒦 = TukeyHanning{Andrews}()
73-
Σ = a𝕍ar(𝒦, X; prewhiten=false)
73+
Σ = a𝕍ar(𝒦, X; prewhite=false)
7474
@test 𝒦.bw[1] 3.162049 rtol=1e-6
7575
@test optimalbw(𝒦, X) == 𝒦.bw[1]
7676

7777
𝒦 = Truncated{Andrews}()
78-
Σ = a𝕍ar(𝒦, X; prewhiten=false)
78+
Σ = a𝕍ar(𝒦, X; prewhite=false)
7979
@test 𝒦.bw[1] 1.197131 rtol=1e-6
8080
@test optimalbw(𝒦, X) == 𝒦.bw[1]
8181

8282
## --
8383
𝒦 = Bartlett{Andrews}()
84-
Σ = a𝕍ar(𝒦, X; prewhiten=true);
84+
Σ = a𝕍ar(𝒦, X; prewhite=true);
8585
@test 𝒦.bw[1] 0.3836096 rtol=1e-6
86-
@test optimalbw(𝒦, X; prewhiten=true) == 𝒦.bw[1]
86+
@test optimalbw(𝒦, X; prewhite=true) == 𝒦.bw[1]
8787

8888
𝒦 = Parzen{Andrews}()
89-
Σ = a𝕍ar(𝒦, X; prewhiten=true);
89+
Σ = a𝕍ar(𝒦, X; prewhite=true);
9090
@test 𝒦.bw[1] 1.380593 rtol=1e-6
91-
@test CovarianceMatrices.optimalbw(𝒦, X; prewhiten=true) == 𝒦.bw[1]
91+
@test CovarianceMatrices.optimalbw(𝒦, X; prewhite=true) == 𝒦.bw[1]
9292

9393
𝒦 = QuadraticSpectral{Andrews}()
94-
Σ = a𝕍ar(𝒦, X; prewhiten=true)
94+
Σ = a𝕍ar(𝒦, X; prewhite=true)
9595
@test 𝒦.bw[1] 0.6858351 atol=1e-6
9696
@test optimalbw(𝒦, X) == 𝒦.bw[1]
9797

9898
𝒦 = TukeyHanning{Andrews}()
99-
Σ = a𝕍ar(𝒦, X; prewhiten=true)
99+
Σ = a𝕍ar(𝒦, X; prewhite=true)
100100
@test 𝒦.bw[1] 0.9058356 rtol=1e-6
101101
@test optimalbw(𝒦, X) == 𝒦.bw[1]
102102

103103
𝒦 = Truncated{Andrews}()
104-
Σ = a𝕍ar(𝒦, X; prewhiten=true)
104+
Σ = a𝕍ar(𝒦, X; prewhite=true)
105105
@test 𝒦.bw[1] 0.3429435 rtol=1e-6
106106
@test optimalbw(𝒦, X) == 𝒦.bw[1]
107107

@@ -201,8 +201,8 @@ QuadraticSpectral{NeweyWest}())
201201
pre = (false, true)
202202

203203
@testset "aVar HAC" begin
204-
for ((𝒦, prewhiten), Σ₀) in zip(Iterators.product(kernels, pre), Σ₀₀)
205-
Σ = a𝕍ar(𝒦, X; prewhiten=prewhiten)
204+
for ((𝒦, prewhite), Σ₀) in zip(Iterators.product(kernels, pre), Σ₀₀)
205+
Σ = a𝕍ar(𝒦, X; prewhite=prewhite)
206206
@test Σ Σ₀ rtol=1e-6
207207
end
208208
end
@@ -290,7 +290,7 @@ function fopt!(u; weighted=false)
290290
eval(quote
291291
ols = glm(@formula(y~x1+x2+x3), $df, Normal(), IdentityLink(), wts=$weighted ? $(df).w : Float64[])
292292
𝒦 = ($k){Andrews}()
293-
tmp = vcov(𝒦, ols; prewhiten=$pre, dofadjust=false)
293+
tmp = vcov(𝒦, ols; prewhite=$pre, dofadjust=false)
294294
da[String($k)] = Dict{String, Any}("bw" => CM.bandwidth(𝒦), "V" => tmp)
295295
end)
296296
end
@@ -299,7 +299,7 @@ function fopt!(u; weighted=false)
299299
eval(quote
300300
𝒦 = ($k){NeweyWest}()
301301
## To get the same results of R, the weights given to the intercept should be 0
302-
tmp = vcov(𝒦, ols; prewhiten=$pre, dofadjust=false)
302+
tmp = vcov(𝒦, ols; prewhite=$pre, dofadjust=false)
303303
dn[String($k)] = Dict{String, Any}("bw" => CM.bandwidth(𝒦), "V" => tmp)
304304
end)
305305
end
@@ -341,15 +341,15 @@ function ffix!(u; weighted=false)
341341
eval(quote
342342
ols = glm(@formula(y~x1+x2+x3), $df, Normal(), IdentityLink(), wts=$weighted ? $(df).w : Float64[])
343343
𝒦 = ($k)(3)
344-
tmp = vcov(𝒦, ols; prewhiten=$pre, dofadjust=false)
344+
tmp = vcov(𝒦, ols; prewhite=$pre, dofadjust=false)
345345
da[String($k)] = Dict{String, Any}("bw" => CM.bandwidth(𝒦), "V" => tmp)
346346
end)
347347
end
348348
for k in neweywest_kernels
349349
eval(quote
350350
𝒦 = ($k)(3)
351351
## To get the same results of R, the weights given to the intercept should be 0
352-
tmp = vcov(𝒦, ols; prewhiten=$pre, dofadjust=false)
352+
tmp = vcov(𝒦, ols; prewhite=$pre, dofadjust=false)
353353
dn[String($k)] = Dict{String, Any}("bw" => CM.bandwidth(𝒦), "V" => tmp)
354354
end)
355355
end

0 commit comments

Comments
 (0)