11using Test
22using 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
1846end
0 commit comments