@@ -338,25 +338,19 @@ julia> g_formula!(m2)
338
338
```
339
339
"""
340
340
@inline function g_formula! (g) # Keeping this separate for reuse with S-Learning
341
- covariates , y = hcat (g. X, g. T), g. Y
341
+ vars , y = hcat (g. X, g. T), g. Y
342
342
x₁, x₀ = hcat (g. X, ones (size (g. X, 1 ))), hcat (g. X, zeros (size (g. X, 1 )))
343
343
344
344
if g. quantity_of_interest ∈ (" ITT" , " ATE" , " CATE" )
345
- Xₜ = hcat (covariates [:, 1 : (end - 1 )], ones (size (covariates , 1 )))
346
- Xᵤ = hcat (covariates [:, 1 : (end - 1 )], zeros (size (covariates , 1 )))
345
+ Xₜ = hcat (vars [:, 1 : (end - 1 )], ones (size (vars , 1 )))
346
+ Xᵤ = hcat (vars [:, 1 : (end - 1 )], zeros (size (vars , 1 )))
347
347
else
348
- Xₜ = hcat (covariates [g. T .== 1 , 1 : (end - 1 )], ones (size (g. T[g. T .== 1 ], 1 )))
349
- Xᵤ = hcat (covariates [g. T .== 1 , 1 : (end - 1 )], zeros (size (g. T[g. T .== 1 ], 1 )))
348
+ Xₜ = hcat (vars [g. T .== 1 , 1 : (end - 1 )], ones (size (g. T[g. T .== 1 ], 1 )))
349
+ Xᵤ = hcat (vars [g. T .== 1 , 1 : (end - 1 )], zeros (size (g. T[g. T .== 1 ], 1 )))
350
350
end
351
351
352
352
g. ensemble = ELMEnsemble (
353
- covariates,
354
- y,
355
- g. sample_size,
356
- g. num_machines,
357
- g. num_feats,
358
- g. num_neurons,
359
- g. activation
353
+ vars, y, g. sample_size, g. num_machines, g. num_feats, g. num_neurons, g. activation
360
354
)
361
355
362
356
fit! (g. ensemble)
@@ -383,22 +377,20 @@ julia> estimate_causal_effect!(m2)
383
377
"""
384
378
@inline function estimate_causal_effect! (DML:: DoubleMachineLearning )
385
379
X, T, Y = generate_folds (DML. X, DML. T, DML. Y, DML. folds)
386
- DML. causal_effect, DML. marginal_effect = 0 , 0
387
- Δ = var_type (DML. T) isa Binary ? 1.0 : 1.5e-8 mean (DML. T)
380
+ DML. causal_effect, DML. marginal_effect, Δ = 0 , 0 , 1.5e-8 mean (DML. T)
388
381
389
382
# Cross fitting by training on the main folds and predicting residuals on the auxillary
390
383
for fold in 1 : (DML. folds)
391
384
X_train, X_test = reduce (vcat, X[1 : end .!= = fold]), X[fold]
392
385
Y_train, Y_test = reduce (vcat, Y[1 : end .!= = fold]), Y[fold]
393
386
T_train, T_test = reduce (vcat, T[1 : end .!= = fold]), T[fold]
394
- T_train₊ = var_type (DML. T) isa Binary ? T_train .* 0 : T_train .+ Δ
395
387
396
- Ỹ, T̃, T̃₊ = predict_residuals (
397
- DML, X_train, X_test, Y_train, Y_test, T_train, T_test, T_train₊
388
+ Ỹ, T̃, T̃₊, T̃₋ = predict_residuals (
389
+ DML, X_train, X_test, Y_train, Y_test, T_train, T_test, Δ
398
390
)
399
391
400
392
DML. causal_effect += T̃\ Ỹ
401
- DML. marginal_effect += (T̃₊\ Ỹ - DML . causal_effect ) / Δ
393
+ DML. marginal_effect += (T̃₊\ Ỹ - T̃₋ \ Ỹ ) / 2 Δ
402
394
end
403
395
404
396
DML. causal_effect /= DML. folds
@@ -408,7 +400,7 @@ julia> estimate_causal_effect!(m2)
408
400
end
409
401
410
402
"""
411
- predict_residuals(D, x_train, x_test, y_train, y_test, t_train, t_test, t_train₊ )
403
+ predict_residuals(D, x_train, x_test, y_train, y_test, t_train, t_test, Δ )
412
404
413
405
Predict treatment, outcome, and marginal effect residuals for double machine learning or
414
406
R-learning.
@@ -423,7 +415,7 @@ julia> x_train, x_test = X[1:80, :], X[81:end, :]
423
415
julia> y_train, y_test = Y[1:80], Y[81:end]
424
416
julia> t_train, t_test = T[1:80], T[81:100]
425
417
julia> m1 = DoubleMachineLearning(X, T, Y)
426
- julia> predict_residuals(m1, x_train, x_test, y_train, y_test, t_train, t_test , zeros(100))
418
+ julia> predict_residuals(m1, x_tr, x_te, y_tr, y_te, t_tr, t_te , zeros(100), 1e-5 )
427
419
```
428
420
"""
429
421
@inline function predict_residuals (
@@ -433,28 +425,19 @@ julia> predict_residuals(m1, x_train, x_test, y_train, y_test, t_train, t_test,
433
425
yₜᵣ:: Vector{Float64} ,
434
426
yₜₑ:: Vector{Float64} ,
435
427
tₜᵣ:: Vector{Float64} ,
436
- tₜₑ:: Vector{Float64} ,
437
- tₜᵣ₊ :: Vector{ Float64}
428
+ tₜₑ:: Vector{Float64} ,
429
+ Δ :: Float64
438
430
)
439
- y = ELMEnsemble (
440
- xₜᵣ, yₜᵣ, D. sample_size, D. num_machines, D. num_feats, D. num_neurons, D. activation
441
- )
442
-
443
- t = ELMEnsemble (
444
- xₜᵣ, tₜᵣ, D. sample_size, D. num_machines, D. num_feats, D. num_neurons, D. activation
445
- )
446
-
447
- t₊ = ELMEnsemble (
448
- xₜᵣ, tₜᵣ₊, D. sample_size, D. num_machines, D. num_feats, D. num_neurons, D. activation
449
- )
450
-
451
- fit! (y)
452
- fit! (t)
453
- fit! (t₊) # Estimate a model with T + a finite difference
431
+ args = D. sample_size, D. num_machines, D. num_feats, D. num_neurons, D. activation
432
+ y = ELMEnsemble (xₜᵣ, yₜᵣ, args... )
433
+ t = ELMEnsemble (xₜᵣ, tₜᵣ, args... )
454
434
455
- yₚᵣ, tₚᵣ, tₚᵣ₊ = predict (y, xₜₑ), predict (t, xₜₑ), predict (t₊, xₜₑ)
435
+ fit! (y); fit! (t)
436
+ yₚᵣ, tₚᵣ = predict (y, xₜₑ), predict (t, xₜₑ)
437
+ tₜₑ₊ = var_type (tₜₑ) isa Binary ? ones (size (tₜₑ)) : tₜₑ .+ Δ
438
+ tₜₑ₋ = var_type (tₜₑ) isa Binary ? ones (size (tₜₑ)) : tₜₑ .- Δ
456
439
457
- return yₜₑ - yₚᵣ, tₜₑ - tₚᵣ, tₜₑ - tₚᵣ₊
440
+ return yₜₑ - yₚᵣ, tₜₑ - tₚᵣ, tₜₑ₊ - tₚᵣ, tₜₑ₋ - tₚᵣ
458
441
end
459
442
460
443
"""
0 commit comments