Skip to content

Commit 3ee524d

Browse files
committed
implement dd estimator
1 parent 66b2baa commit 3ee524d

File tree

6 files changed

+60
-22
lines changed

6 files changed

+60
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# v0.1.7 (Upcoming Release)
22

3+
- Implement IV (Instrumental Variables) regression estimator
4+
- Implement DD (Dagenais and Dagenais, 1997) moments estimator
35

46
# v0.1.6
57

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ErrorsInVariables"
22
uuid = "faeb8267-8352-4c3e-81c7-04150084d5bf"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
authors = ["jbytecode <mhsatman@gmail.com>"]
55

66
[deps]

src/dd.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ Computes the Higher Moment Estimator for a linear regression model with errors i
1717
- Dagenais, Marcel G., and Denyse L. Dagenais. "Higher moment estimators for linear regression models with errors in the variables."
1818
Journal of Econometrics 76.1-2 (1997): 193-221.
1919
"""
20-
function dd(xvec::Vector, yvec::Vector)::Matrix
20+
function dd(xmat::Matrix, yvec::Vector)::Matrix
2121

22-
n = length(xvec)
22+
n, p = size(xmat)
2323

24-
IK = ones(n)
24+
o = ones(p)
25+
IK = I(p)
2526

26-
x = xvec .- mean(xvec)
27+
x = similar(xmat)
28+
for i in 1:p
29+
x[:, i] = xmat[:, i] .- mean(xmat[:, i])
30+
end
2731

2832
y = yvec .- mean(yvec)
2933

@@ -33,11 +37,15 @@ function dd(xvec::Vector, yvec::Vector)::Matrix
3337

3438
z3 = y .* y
3539

36-
z4 = x .* x .* x .- 3 .* x .* (mean(x'x / n) .* IK)
40+
z4 = x .* x .* x .- 3 .* x * (mean(x'x ./ n) * IK)
3741

38-
# z5 = x .* x .* y .- 2 .* x .* (mean(y'x / n) .* IK) .- y * (IK' * mean(x'x / n) .* IK)
42+
z5 = x .* x .* y .- 2 .* x * (mean(y'x ./ n) .* IK) .- y .* (mean(x'x / n) .* ones(n))
3943

40-
return hcat(z1, z2, z3, z4)
44+
z6 = x .* y .* y .- x * (mean(y'y ./ n) .* IK) .- 2 * y .* (mean(y'x / n) .* ones(n))
45+
46+
z7 = y .* y .* y .- 3 .* y .* (mean(y'y ./ n))
47+
48+
return hcat(z1, z2, z3, z4, z5, z6, z7)
4149

4250
end
4351

src/iv.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ computed using the formula:
1818
If the number of instruments is greater than the number of predictors, the IV estimator is
1919
computed using the formula:
2020
```julia
21-
part = X' * Z * inv(Z' * Z) * Z' * X
21+
part = X' * Z * inv(Z' * Z) * Z'
2222
betas = inv(part * X) * part * y
2323
```
2424
"""
@@ -33,7 +33,7 @@ function iv(X::Matrix, y::Vector, Z::Matrix)::Vector
3333
if numberofinstruments == numberofpredictors
3434
return inv(Z'X)Z'y
3535
else
36-
part = X' * Z * inv(Z' * Z) * Z' * X
36+
part = X' * Z * inv(Z' * Z) * Z'
3737
betas = inv(part * X) * part * y
3838
return betas
3939
end

test/runtests.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ function euclidean(v1::Vector, v2::Vector)
77
end
88

99
include("testdd.jl")
10-
#include("testiv.jl")
11-
#include("testcga.jl")
12-
#include("testestimator.jl")
13-
#include("testmeive.jl")
14-
#include("testorthogonalregression.jl")
15-
#include("testdeming.jl")
16-
#include("testsimex.jl")
10+
include("testiv.jl")
11+
include("testcga.jl")
12+
include("testestimator.jl")
13+
include("testmeive.jl")
14+
include("testorthogonalregression.jl")
15+
include("testdeming.jl")
16+
include("testsimex.jl")
1717

1818

test/testdd.jl

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
11
using Test
22
using ErrorsInVariables
3+
using Random
34

45

56
@testset "DD estimator" verbose=true begin
67

78
@testset "Simple Example" verbose=true begin
89

9-
X = [1.0, 2.0, 3.0, 4.0, 5.0, 6, 7, 8]
10+
eps = 0.0001
1011

11-
y = [2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0]
12+
rng = Random.MersenneTwister(1234)
1213

13-
result = dd(X, y)
14+
n = 30
15+
16+
deltax = randn(rng, n) * sqrt(3.0)
17+
18+
cleanx = randn(rng, n) * sqrt(7.0)
19+
cleanx2 = randn(rng, n) * sqrt(7.0)
20+
cleanx3 = randn(rng, n) * sqrt(7.0)
21+
22+
e = randn(rng, n) * sqrt(5.0)
23+
24+
y = 20.0 .+ 10.0 .* cleanx .+ 15.0 .* cleanx2 .+ 13.0 .* cleanx3 .+ e
25+
26+
dirtyx = cleanx .+ deltax
27+
28+
Xd = hcat(ones(n), dirtyx, cleanx2, cleanx3)
29+
Xc = hcat(ones(n), cleanx, cleanx2, cleanx3)
30+
31+
dirtybetas = Xd \ y
32+
33+
cleanbetas = Xc \ y
34+
35+
Z = dd(hcat(dirtyx, cleanx2, cleanx3), y)
36+
37+
result = iv(Xd, y, Z)
38+
39+
expected_dd_betas = [18.488555621677015, 6.283197918953448, 15.765140901269259, 12.061208813549317]
40+
41+
for i in eachindex(expected_dd_betas)
42+
@test isapprox(result[i], expected_dd_betas[i], atol=eps)
43+
end
1444

15-
println("-----------------------")
16-
display(result)
1745
end
1846
end

0 commit comments

Comments
 (0)