Skip to content

Commit

Permalink
Simplified confidence interval tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dscolby committed Nov 6, 2024
1 parent 7ab2fb7 commit c6f91b8
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions test/test_inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ Float64.([rand() < 0.4 for i in 1:100])

g_computer = GComputation(x, t, y)
estimate_causal_effect!(g_computer)
g_inference = CausalELM.generate_null_distribution(g_computer, 1000)
g_inference = CausalELM.generate_null_distribution(g_computer, 100)
p1, stderr1 = CausalELM.p_value_and_std_err(g_inference, CausalELM.mean(g_inference))
lb1, ub1 = CausalELM.confidence_interval(g_inference, g_computer.causal_effect)
p11, stderr11, lb11, ub11 = CausalELM.quantities_of_interest(g_computer, 100)
summary1 = summarize(g_computer, n=100, inference=true)

dm = DoubleMachineLearning(x, t, y)
estimate_causal_effect!(dm)
dm_inference = CausalELM.generate_null_distribution(dm, 1000)
dm_inference = CausalELM.generate_null_distribution(dm, 100)
p2, stderr2 = CausalELM.p_value_and_std_err(dm_inference, CausalELM.mean(dm_inference))
lb2, ub2 = CausalELM.confidence_interval(dm_inference, dm.causal_effect)
summary2 = summarize(dm, n=100)

# With a continuous treatment variable
dm_continuous = DoubleMachineLearning(x, t, rand(1:4, 100))
estimate_causal_effect!(dm_continuous)
dm_continuous_inference = CausalELM.generate_null_distribution(dm_continuous, 1000)
dm_continuous_inference = CausalELM.generate_null_distribution(dm_continuous, 100)
p3, stderr3 = CausalELM.p_value_and_std_err(
dm_continuous_inference, CausalELM.mean(dm_continuous_inference)
)
Expand All @@ -39,7 +39,7 @@ summary4 = summarize(its, n=100)
summary4_inference = summarize(its, n=100, inference=true)

# Null distributions for the mean and cummulative changes
its_inference1 = CausalELM.generate_null_distribution(its, 1000, true)
its_inference1 = CausalELM.generate_null_distribution(its, 100, true)
its_inference2 = CausalELM.generate_null_distribution(its, 10, false)
lb4, ub4 = CausalELM.confidence_interval(
its_inference1, CausalELM.mean(its.causal_effect)
Expand All @@ -53,7 +53,7 @@ summary5 = summarize(slearner, n=100)

tlearner = TLearner(x, t, y)
estimate_causal_effect!(tlearner)
tlearner_inference = CausalELM.generate_null_distribution(tlearner, 1000)
tlearner_inference = CausalELM.generate_null_distribution(tlearner, 100)
lb6, ub6 = CausalELM.confidence_interval(
tlearner_inference, CausalELM.mean(tlearner.causal_effect)
)
Expand All @@ -65,7 +65,7 @@ summary6 = summarize(tlearner, n=100)

xlearner = XLearner(x, t, y)
estimate_causal_effect!(xlearner)
xlearner_inference = CausalELM.generate_null_distribution(xlearner, 1000)
xlearner_inference = CausalELM.generate_null_distribution(xlearner, 100)
lb7, ub7 = CausalELM.confidence_interval(
xlearner_inference, CausalELM.mean(xlearner.causal_effect)
)
Expand All @@ -81,7 +81,7 @@ summary9 = summarize(rlearner, n=100)

dr_learner = DoublyRobustLearner(x, t, y)
estimate_causal_effect!(dr_learner)
dr_learner_inference = CausalELM.generate_null_distribution(dr_learner, 1000)
dr_learner_inference = CausalELM.generate_null_distribution(dr_learner, 100)
lb8, ub8 = CausalELM.confidence_interval(
dr_learner_inference, CausalELM.mean(dr_learner.causal_effect)
)
Expand All @@ -91,21 +91,21 @@ p8, stderr8 = CausalELM.p_value_and_std_err(
summary10 = summarize(dr_learner, n=100)

@testset "Generating Null Distributions" begin
@test size(g_inference, 1) === 1000
@test size(g_inference, 1) === 100
@test g_inference isa Array{Float64}
@test size(dm_inference, 1) === 1000
@test size(dm_inference, 1) === 100
@test dm_inference isa Array{Float64}
@test size(dm_continuous_inference, 1) === 1000
@test size(dm_continuous_inference, 1) === 100
@test dm_continuous_inference isa Array{Float64}
@test size(its_inference1, 1) === 1000
@test size(its_inference1, 1) === 100
@test its_inference1 isa Array{Float64}
@test size(its_inference2, 1) === 10
@test its_inference2 isa Array{Float64}
@test size(tlearner_inference, 1) === 1000
@test size(tlearner_inference, 1) === 100
@test tlearner_inference isa Array{Float64}
@test size(xlearner_inference, 1) === 1000
@test size(xlearner_inference, 1) === 100
@test xlearner_inference isa Array{Float64}
@test size(dr_learner_inference, 1) === 1000
@test size(dr_learner_inference, 1) === 100
@test dr_learner_inference isa Array{Float64}
end

Expand All @@ -127,23 +127,24 @@ end
end

@testset "Confidence Intervals" begin
@test lb1 < g_computer.causal_effect < ub1
@test lb2 < dm.causal_effect < ub2
@test lb3 < dm_continuous.causal_effect < ub3
@test lb4 < CausalELM.mean(its.causal_effect) < ub4
@test lb6 < CausalELM.mean(tlearner.causal_effect) < ub6
@test lb7 < CausalELM.mean(xlearner.causal_effect) < ub7
@test lb8 < CausalELM.mean(dr_learner.causal_effect) < ub8
# Making sure a confidence interval has changed from infinity to a valid number
@test isinf(lb1) == false && isinf(ub1) == false
@test isinf(lb2) == false && isinf(ub2) == false
@test isinf(lb3) == false && isinf(ub3) == false
@test isinf(lb4) == false && isinf(ub4) == false
@test isinf(lb6) == false && isinf(ub6) == false
@test isinf(lb7) == false && isinf(ub7) == false
@test isinf(lb8) == false && isinf(ub8) == false
end

@testset "All Quantities of Interest" begin
@test lb11 < g_computer.causal_effect < ub11
@test isinf(lb11) == false && isinf(ub11) == false
@test 1 >= p11 >= 0
@test stderr11 > 0
@test lb44 < CausalELM.mean(its.causal_effect) < ub44
@test isinf(lb44) == false && isinf(ub44) == false
@test 1 >= p44 >= 0
@test stderr44 > 0
@test lb66 < CausalELM.mean(tlearner.causal_effect) < ub66
@test isinf(lb66) == false && isinf(ub66) == false
@test 1 >= p66 >= 0
@test stderr66 > 0
end
Expand Down

0 comments on commit c6f91b8

Please sign in to comment.