9. Sample-splitting, cross-fitting and repeated cross-fitting#

Sample-splitting and the application of cross-fitting is a central part of double/debiased machine learning (DML). For all DML models DoubleMLPLR, DoubleMLPLIV, DoubleMLIRM, and DoubleMLIIVM, the specification is done via the parameters n_folds and n_rep. Advanced resampling techniques can be obtained via the boolean parameters draw_sample_splitting and apply_cross_fitting as well as the methods draw_sample_splitting() and set_sample_splitting().

As an example we consider a partially linear regression model (PLR) implemented in DoubleMLPLR.

In [1]: import doubleml as dml

In [2]: import numpy as np

In [3]: from doubleml.datasets import make_plr_CCDDHNR2018

In [4]: from sklearn.ensemble import RandomForestRegressor

In [5]: from sklearn.base import clone

In [6]: learner = RandomForestRegressor(n_estimators=100, max_features=20, max_depth=5, min_samples_leaf=2)

In [7]: ml_l = clone(learner)

In [8]: ml_m = clone(learner)

In [9]: np.random.seed(1234)

In [10]: obj_dml_data = make_plr_CCDDHNR2018(alpha=0.5, n_obs=100)
library(DoubleML)
library(mlr3)
lgr::get_logger("mlr3")$set_threshold("warn")
library(mlr3learners)
library(data.table)

learner = lrn("regr.ranger", num.trees = 100, mtry = 20, min.node.size = 2, max.depth = 5)
ml_l = learner
ml_m = learner
data = make_plr_CCDDHNR2018(alpha=0.5, n_obs=100, return_type = "data.table")
obj_dml_data = DoubleMLData$new(data,
                                y_col = "y",
                                d_cols = "d")

9.1. Cross-fitting with K folds#

The default setting is n_folds = 5 and n_rep = 1, i.e., \(K=5\) folds and no repeated cross-fitting.

In [11]: dml_plr_obj = dml.DoubleMLPLR(obj_dml_data, ml_l, ml_m, n_folds = 5, n_rep = 1)

In [12]: print(dml_plr_obj.n_folds)
5

In [13]: print(dml_plr_obj.n_rep)
1
dml_plr_obj = DoubleMLPLR$new(obj_dml_data, ml_l, ml_m, n_folds = 5, n_rep = 1)
print(dml_plr_obj$n_folds)
print(dml_plr_obj$n_rep)
[1] 5
[1] 1

During the initialization of a DML model like DoubleMLPLR a \(K\)-fold random partition \((I_k)_{k=1}^{K}\) of observation indices is generated. The \(K\)-fold random partition is stored in the smpls attribute of the DML model object.

In [14]: print(dml_plr_obj.smpls)
[[(array([ 0,  2,  3,  4,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 19,
       21, 22, 23, 24, 25, 27, 28, 29, 31, 32, 34, 35, 36, 37, 38, 40, 44,
       46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 58, 59, 60, 61, 62, 63, 64,
       65, 67, 68, 69, 70, 71, 72, 74, 75, 76, 77, 79, 80, 81, 82, 84, 85,
       86, 88, 89, 90, 91, 92, 93, 94, 95, 97, 98, 99]), array([ 1,  5, 18, 20, 26, 30, 33, 39, 41, 42, 43, 45, 56, 57, 66, 73, 78,
       83, 87, 96])), (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 26, 29, 30, 31, 32, 33, 34, 36, 37, 38, 39, 40,
       41, 42, 43, 44, 45, 46, 48, 53, 54, 55, 56, 57, 58, 60, 61, 62, 63,
       65, 66, 69, 70, 71, 72, 73, 77, 78, 79, 80, 81, 82, 83, 84, 85, 87,
       88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]), array([ 9, 23, 24, 25, 27, 28, 35, 47, 49, 50, 51, 52, 59, 64, 67, 68, 74,
       75, 76, 86])), (array([ 0,  1,  2,  3,  5,  6,  7,  9, 10, 11, 12, 14, 16, 17, 18, 20, 21,
       22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, 35, 38, 39, 40, 41, 42,
       43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 61, 62,
       63, 64, 65, 66, 67, 68, 69, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81,
       83, 84, 86, 87, 88, 89, 90, 91, 92, 96, 98, 99]), array([ 4,  8, 13, 15, 19, 32, 34, 36, 37, 46, 54, 55, 70, 72, 82, 85, 93,
       94, 95, 97])), (array([ 0,  1,  3,  4,  5,  6,  8,  9, 13, 14, 15, 17, 18, 19, 20, 21, 22,
       23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 39, 41,
       42, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 55, 56, 57, 59, 64,
       65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 82,
       83, 85, 86, 87, 88, 89, 93, 94, 95, 96, 97, 98]), array([ 2,  7, 10, 11, 12, 16, 38, 40, 48, 58, 60, 61, 62, 63, 81, 84, 90,
       91, 92, 99])), (array([ 1,  2,  4,  5,  7,  8,  9, 10, 11, 12, 13, 15, 16, 18, 19, 20, 23,
       24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
       43, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 59, 60, 61,
       62, 63, 64, 66, 67, 68, 70, 72, 73, 74, 75, 76, 78, 81, 82, 83, 84,
       85, 86, 87, 90, 91, 92, 93, 94, 95, 96, 97, 99]), array([ 0,  3,  6, 14, 17, 21, 22, 29, 31, 44, 53, 65, 69, 71, 77, 79, 80,
       88, 89, 98]))]]
dml_plr_obj$smpls
  1. $train_ids
      1. 2
      2. 11
      3. 14
      4. 16
      5. 20
      6. 21
      7. 25
      8. 32
      9. 48
      10. 50
      11. 65
      12. 66
      13. 69
      14. 76
      15. 77
      16. 86
      17. 88
      18. 95
      19. 97
      20. 98
      21. 1
      22. 3
      23. 4
      24. 7
      25. 17
      26. 19
      27. 22
      28. 28
      29. 33
      30. 49
      31. 56
      32. 57
      33. 58
      34. 61
      35. 64
      36. 71
      37. 84
      38. 89
      39. 96
      40. 99
      41. 5
      42. 9
      43. 10
      44. 18
      45. 23
      46. 29
      47. 35
      48. 37
      49. 38
      50. 41
      51. 43
      52. 45
      53. 53
      54. 59
      55. 67
      56. 73
      57. 75
      58. 82
      59. 91
      60. 94
      61. 6
      62. 8
      63. 12
      64. 15
      65. 24
      66. 27
      67. 30
      68. 31
      69. 40
      70. 42
      71. 44
      72. 46
      73. 54
      74. 55
      75. 70
      76. 74
      77. 83
      78. 85
      79. 90
      80. 100
      1. 13
      2. 26
      3. 34
      4. 36
      5. 39
      6. 47
      7. 51
      8. 52
      9. 60
      10. 62
      11. 63
      12. 68
      13. 72
      14. 78
      15. 79
      16. 80
      17. 81
      18. 87
      19. 92
      20. 93
      21. 1
      22. 3
      23. 4
      24. 7
      25. 17
      26. 19
      27. 22
      28. 28
      29. 33
      30. 49
      31. 56
      32. 57
      33. 58
      34. 61
      35. 64
      36. 71
      37. 84
      38. 89
      39. 96
      40. 99
      41. 5
      42. 9
      43. 10
      44. 18
      45. 23
      46. 29
      47. 35
      48. 37
      49. 38
      50. 41
      51. 43
      52. 45
      53. 53
      54. 59
      55. 67
      56. 73
      57. 75
      58. 82
      59. 91
      60. 94
      61. 6
      62. 8
      63. 12
      64. 15
      65. 24
      66. 27
      67. 30
      68. 31
      69. 40
      70. 42
      71. 44
      72. 46
      73. 54
      74. 55
      75. 70
      76. 74
      77. 83
      78. 85
      79. 90
      80. 100
      1. 13
      2. 26
      3. 34
      4. 36
      5. 39
      6. 47
      7. 51
      8. 52
      9. 60
      10. 62
      11. 63
      12. 68
      13. 72
      14. 78
      15. 79
      16. 80
      17. 81
      18. 87
      19. 92
      20. 93
      21. 2
      22. 11
      23. 14
      24. 16
      25. 20
      26. 21
      27. 25
      28. 32
      29. 48
      30. 50
      31. 65
      32. 66
      33. 69
      34. 76
      35. 77
      36. 86
      37. 88
      38. 95
      39. 97
      40. 98
      41. 5
      42. 9
      43. 10
      44. 18
      45. 23
      46. 29
      47. 35
      48. 37
      49. 38
      50. 41
      51. 43
      52. 45
      53. 53
      54. 59
      55. 67
      56. 73
      57. 75
      58. 82
      59. 91
      60. 94
      61. 6
      62. 8
      63. 12
      64. 15
      65. 24
      66. 27
      67. 30
      68. 31
      69. 40
      70. 42
      71. 44
      72. 46
      73. 54
      74. 55
      75. 70
      76. 74
      77. 83
      78. 85
      79. 90
      80. 100
      1. 13
      2. 26
      3. 34
      4. 36
      5. 39
      6. 47
      7. 51
      8. 52
      9. 60
      10. 62
      11. 63
      12. 68
      13. 72
      14. 78
      15. 79
      16. 80
      17. 81
      18. 87
      19. 92
      20. 93
      21. 2
      22. 11
      23. 14
      24. 16
      25. 20
      26. 21
      27. 25
      28. 32
      29. 48
      30. 50
      31. 65
      32. 66
      33. 69
      34. 76
      35. 77
      36. 86
      37. 88
      38. 95
      39. 97
      40. 98
      41. 1
      42. 3
      43. 4
      44. 7
      45. 17
      46. 19
      47. 22
      48. 28
      49. 33
      50. 49
      51. 56
      52. 57
      53. 58
      54. 61
      55. 64
      56. 71
      57. 84
      58. 89
      59. 96
      60. 99
      61. 6
      62. 8
      63. 12
      64. 15
      65. 24
      66. 27
      67. 30
      68. 31
      69. 40
      70. 42
      71. 44
      72. 46
      73. 54
      74. 55
      75. 70
      76. 74
      77. 83
      78. 85
      79. 90
      80. 100
      1. 13
      2. 26
      3. 34
      4. 36
      5. 39
      6. 47
      7. 51
      8. 52
      9. 60
      10. 62
      11. 63
      12. 68
      13. 72
      14. 78
      15. 79
      16. 80
      17. 81
      18. 87
      19. 92
      20. 93
      21. 2
      22. 11
      23. 14
      24. 16
      25. 20
      26. 21
      27. 25
      28. 32
      29. 48
      30. 50
      31. 65
      32. 66
      33. 69
      34. 76
      35. 77
      36. 86
      37. 88
      38. 95
      39. 97
      40. 98
      41. 1
      42. 3
      43. 4
      44. 7
      45. 17
      46. 19
      47. 22
      48. 28
      49. 33
      50. 49
      51. 56
      52. 57
      53. 58
      54. 61
      55. 64
      56. 71
      57. 84
      58. 89
      59. 96
      60. 99
      61. 5
      62. 9
      63. 10
      64. 18
      65. 23
      66. 29
      67. 35
      68. 37
      69. 38
      70. 41
      71. 43
      72. 45
      73. 53
      74. 59
      75. 67
      76. 73
      77. 75
      78. 82
      79. 91
      80. 94
    $test_ids
      1. 13
      2. 26
      3. 34
      4. 36
      5. 39
      6. 47
      7. 51
      8. 52
      9. 60
      10. 62
      11. 63
      12. 68
      13. 72
      14. 78
      15. 79
      16. 80
      17. 81
      18. 87
      19. 92
      20. 93
      1. 2
      2. 11
      3. 14
      4. 16
      5. 20
      6. 21
      7. 25
      8. 32
      9. 48
      10. 50
      11. 65
      12. 66
      13. 69
      14. 76
      15. 77
      16. 86
      17. 88
      18. 95
      19. 97
      20. 98
      1. 1
      2. 3
      3. 4
      4. 7
      5. 17
      6. 19
      7. 22
      8. 28
      9. 33
      10. 49
      11. 56
      12. 57
      13. 58
      14. 61
      15. 64
      16. 71
      17. 84
      18. 89
      19. 96
      20. 99
      1. 5
      2. 9
      3. 10
      4. 18
      5. 23
      6. 29
      7. 35
      8. 37
      9. 38
      10. 41
      11. 43
      12. 45
      13. 53
      14. 59
      15. 67
      16. 73
      17. 75
      18. 82
      19. 91
      20. 94
      1. 6
      2. 8
      3. 12
      4. 15
      5. 24
      6. 27
      7. 30
      8. 31
      9. 40
      10. 42
      11. 44
      12. 46
      13. 54
      14. 55
      15. 70
      16. 74
      17. 83
      18. 85
      19. 90
      20. 100

For each \(k \in [K] = \lbrace 1, \ldots, K]\) the nuisance ML estimator

\[\hat{\eta}_{0,k} = \hat{\eta}_{0,k}\big((W_i)_{i\not\in I_k}\big)\]

is based on the observations of all other \(k-1\) folds. The values of the two score function components \(\psi_a(W_i; \hat{\eta}_0)\) and \(\psi_b(W_i; \hat{\eta}_0))\) for each observation index \(i \in I_k\) are computed and stored in the attributes psi_a and psi_b.

In [15]: dml_plr_obj.fit();

In [16]: print(dml_plr_obj.psi_elements['psi_a'][:5])
[[[-1.78711285e+00]]

 [[-1.41918406e+00]]

 [[-1.95062986e-02]]

 [[-5.41989983e-04]]

 [[-5.91438767e+00]]]

In [17]: print(dml_plr_obj.psi_elements['psi_b'][:5])
[[[-0.38818693]]

 [[-1.14000073]]

 [[ 0.06692492]]

 [[ 0.00778625]]

 [[ 3.58241568]]]
dml_plr_obj$fit()
print(dml_plr_obj$psi_a[1:5, ,1])
print(dml_plr_obj$psi_b[1:5, ,1])
[1] -0.04792887 -0.83812317 -0.20885627 -1.04759138 -0.21254389
[1] -0.01120478 -0.50430434  0.07663698 -0.24657516 -1.32221349

9.2. Repeated cross-fitting with K folds and M repetitions#

Repeated cross-fitting is obtained by choosing a value \(M>1\) for the number of repetition n_rep. It results in \(M\) random \(K\)-fold partitions being drawn.

In [18]: dml_plr_obj = dml.DoubleMLPLR(obj_dml_data, ml_l, ml_m, n_folds = 5, n_rep = 10)

In [19]: print(dml_plr_obj.n_folds)
5

In [20]: print(dml_plr_obj.n_rep)
10
dml_plr_obj = DoubleMLPLR$new(obj_dml_data, ml_l, ml_m, n_folds = 5, n_rep = 10)
print(dml_plr_obj$n_folds)
print(dml_plr_obj$n_rep)
[1] 5
[1] 10

For each of the \(M\) partitions, the nuisance ML models are estimated and score functions computed as described in Cross-fitting with K folds. The resulting values of the score functions are stored in 3-dimensional arrays psi_a and psi_b, where the row index corresponds the observation index \(i \in [N] = \lbrace 1, \ldots, N\rbrace\) and the column index to the partition \(m \in [M] = \lbrace 1, \ldots, M\rbrace\). The third dimension refers to the treatment variable and becomes non-singleton in case of multiple treatment variables.

In [21]: dml_plr_obj.fit();

In [22]: print(dml_plr_obj.psi_elements['psi_a'][:5, :, 0])
[[-1.80696592e+00 -1.41525168e+00 -2.19509680e+00 -1.95401167e+00
  -9.11199615e-01 -1.46722576e+00 -1.77289874e+00 -1.40127723e+00
  -1.69840389e+00 -1.56387280e+00]
 [-2.76702611e+00 -2.56390147e+00 -1.64476745e+00 -1.52343523e+00
  -2.50398782e+00 -1.49596416e+00 -1.21669513e+00 -2.19374710e+00
  -1.82666866e+00 -2.50093148e+00]
 [-4.38973512e-02 -1.01916030e-01 -4.23690345e-03 -1.51243406e-03
  -3.19073905e-02 -1.94309994e-04 -4.68562150e-02 -8.22507006e-02
  -2.03920960e-02 -5.99549118e-02]
 [-4.92881435e-03 -3.43231359e-03 -7.36566025e-03 -3.21257396e-02
  -2.72875815e-02 -3.36557195e-04 -1.49270769e-02 -9.33335939e-02
  -9.09879814e-04 -1.77401500e-05]
 [-4.07564554e+00 -4.12223182e+00 -5.79792890e+00 -4.88125046e+00
  -5.10637173e+00 -6.12980769e+00 -4.77227783e+00 -5.63245862e+00
  -3.84930915e+00 -5.41798768e+00]]

In [23]: print(dml_plr_obj.psi_elements['psi_b'][:5, :, 0])
[[-2.88988263e-01 -2.65557405e-01 -7.79458848e-02 -4.18888149e-01
  -4.13137893e-01 -3.94441007e-01 -3.39010121e-02 -1.74461783e-01
  -2.85911521e-01 -3.36231307e-01]
 [-9.79338596e-01 -1.44563945e+00 -1.35620768e+00 -1.44713577e+00
  -1.16539906e+00 -1.07544271e+00 -8.08602774e-01 -1.47966100e+00
  -1.08181827e+00 -9.22272803e-01]
 [ 3.30031116e-02  5.04682310e-02 -1.69562150e-02  1.27951256e-02
  -5.69140475e-02  1.72155839e-03  9.32950022e-02  1.12196389e-02
   8.90963122e-02  3.13034980e-02]
 [-1.80714504e-02 -1.98505871e-02  4.07727773e-02  7.39621961e-02
   9.95372559e-02 -4.59307502e-03 -4.86424193e-02  1.57592948e-01
  -1.68554404e-02  1.99571372e-03]
 [ 1.54517706e+00  1.76444177e+00  1.87674597e+00  1.76419024e+00
   2.02079162e+00  2.87290240e+00  1.59199423e+00  2.23751359e+00
   7.73764317e-01  3.18678094e+00]]
dml_plr_obj$fit()
print(dml_plr_obj$psi_a[1:5, ,1])
print(dml_plr_obj$psi_b[1:5, ,1])
              [,1]        [,2]         [,3]        [,4]        [,5]        [,6]
[1,] -2.624983e-02 -0.03120321 -0.009100952 -0.05255651 -0.08410699 -0.00672053
[2,] -3.568916e-01 -0.39304610 -0.637487172 -0.52198357 -0.19928927 -1.69117507
[3,] -5.667841e-01 -0.83237186 -0.170538822 -0.15522412 -0.12721233 -0.89966756
[4,] -1.535726e+00 -0.80285489 -0.814376920 -0.75989902 -1.15999624 -1.46239231
[5,] -2.417335e-09 -0.04146556 -0.157054750 -0.01260674 -0.44239736 -0.07253912
             [,7]        [,8]       [,9]       [,10]
[1,] -0.015895367 -0.23990390 -0.2695553 -0.04874241
[2,] -0.825677479 -1.01221971 -1.0945694 -0.40997195
[3,] -0.007284174 -0.26739906 -0.1940317 -0.52984981
[4,] -0.629776408 -1.63013496 -1.0992582 -0.75285767
[5,] -0.114467523 -0.01504119 -0.1564156 -0.01098691
              [,1]       [,2]       [,3]       [,4]        [,5]         [,6]
[1,] -0.0590439788 -0.1183717 -0.0305736 -0.1105579 -0.04052906  0.005571649
[2,]  0.2422992546 -0.3028560 -0.2501763  0.1087617 -0.26184834 -0.835949099
[3,] -0.3082218005  0.1963211  0.2007768  0.1423550  0.06548413 -0.045068548
[4,] -0.7069966693 -0.7584718 -0.4313332 -0.8534789 -0.59867547 -0.597236182
[5,]  0.0001110464 -0.5550134 -0.9959607  0.3355408 -1.92116343 -0.644796183
            [,7]        [,8]        [,9]       [,10]
[1,]  0.06332389 -0.07267712 -0.20869125 -0.04661445
[2,]  0.03476507 -0.24942269 -0.20197670 -0.21799899
[3,] -0.01920056  0.17784378  0.04886405  0.38700566
[4,] -0.37782452 -0.44488518 -0.62014070 -0.37790426
[5,] -0.98872270 -0.38118359 -1.07742043 -0.25037345

We estimate the causal parameter \(\tilde{\theta}_{0,m}\) for each of the \(M\) partitions with a DML algorithm as described in Double machine learning algorithms. Standard errors are obtained as described in Variance estimation and confidence intervals. The aggregation of the estimates of the causal parameter and its standard errors is done using the median

\[ \begin{align}\begin{aligned}\tilde{\theta}_{0} &= \text{Median}\big((\tilde{\theta}_{0,m})_{m \in [M]}\big),\\\hat{\sigma} &= \sqrt{\text{Median}\big((\hat{\sigma}_m^2 + (\tilde{\theta}_{0,m} - \tilde{\theta}_{0})^2)_{m \in [M]}\big)}.\end{aligned}\end{align} \]

The estimate of the causal parameter \(\tilde{\theta}_{0}\) is stored in the coef attribute and the asymptotic standard error \(\hat{\sigma}/\sqrt{N}\) in se.

In [24]: print(dml_plr_obj.coef)
[0.45957837]

In [25]: print(dml_plr_obj.se)
[0.08005229]
print(dml_plr_obj$coef)
print(dml_plr_obj$se)
       d 
0.398835 
        d 
0.1091551 

The parameter estimates \((\tilde{\theta}_{0,m})_{m \in [M]}\) and asymptotic standard errors \((\hat{\sigma}_m/\sqrt{N})_{m \in [M]}\) for each of the \(M\) partitions are stored in the attributes _all_coef and _all_se, respectively.

In [26]: print(dml_plr_obj._all_coef)
[[0.45467447 0.4065173  0.46709481 0.47857478 0.41093655 0.47759584
  0.46618738 0.38990574 0.44890536 0.46448227]]

In [27]: print(dml_plr_obj._all_se)
[[0.07978296 0.07919896 0.08031571 0.08664208 0.08154161 0.07691847
  0.07436521 0.08091581 0.08333617 0.07685043]]
print(dml_plr_obj$all_coef)
print(dml_plr_obj$all_se)
          [,1]      [,2]      [,3]      [,4]      [,5]     [,6]      [,7]
[1,] 0.4197205 0.3550164 0.4254397 0.4083642 0.3567657 0.297136 0.4482213
          [,8]      [,9]     [,10]
[1,] 0.4105307 0.3660403 0.3893058
          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]     [,7]
[1,] 0.1154153 0.1161849 0.1079462 0.1149293 0.1161246 0.1036504 0.101875
           [,8]      [,9]     [,10]
[1,] 0.09758533 0.1102699 0.1042349

In python, the confidence intervals and p-values are based on the doubleml.DoubleMLFramework object. This class provides methods such as confint, bootstrap or p_adjust. For different repetitions, the computations are done seperately and combined via the median (as based on Chernozhukov et al., 2018).

9.3. Externally provide a sample splitting / partition#

All DML models allow a partition to be provided externally via the method set_sample_splitting(). In Python we can for example use the K-Folds cross-validator of sklearn KFold in order to generate a sample splitting and provide it to the DML model object. Note that by setting draw_sample_splitting = False one can prevent that a partition is drawn during initialization of the DML model object. The following calls are equivalent. In the first sample code, we use the standard interface and draw the sample-splitting with \(K=4\) folds during initialization of the DoubleMLPLR object.

In [28]: np.random.seed(314)

In [29]: dml_plr_obj_internal = dml.DoubleMLPLR(obj_dml_data, ml_l, ml_m, n_folds = 4)

In [30]: print(dml_plr_obj_internal.fit().summary)
       coef   std err         t         P>|t|     2.5 %    97.5 %
d  0.424127  0.082297  5.153639  2.554793e-07  0.262829  0.585426
set.seed(314)
dml_plr_obj_internal = DoubleMLPLR$new(obj_dml_data, ml_l, ml_m, n_folds = 4)
dml_plr_obj_internal$fit()
dml_plr_obj_internal$summary()
Estimates and significance testing of the effect of target variables
  Estimate. Std. Error t value Pr(>|t|)   
d    0.2991     0.1072   2.791  0.00526 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


In the second sample code, we use the K-Folds cross-validator of sklearn KFold and set the partition via the set_sample_splitting() method.

In [31]: dml_plr_obj_external = dml.DoubleMLPLR(obj_dml_data, ml_l, ml_m, draw_sample_splitting = False)

In [32]: from sklearn.model_selection import KFold

In [33]: np.random.seed(314)

In [34]: kf = KFold(n_splits=4, shuffle=True)

In [35]: smpls = [(train, test) for train, test in kf.split(obj_dml_data.x)]

In [36]: dml_plr_obj_external.set_sample_splitting(smpls);

In [37]: print(dml_plr_obj_external.fit().summary)
       coef   std err         t         P>|t|     2.5 %    97.5 %
d  0.424127  0.082297  5.153639  2.554793e-07  0.262829  0.585426
dml_plr_obj_external = DoubleMLPLR$new(obj_dml_data, ml_l, ml_m, draw_sample_splitting = FALSE)

set.seed(314)
# set up a task and cross-validation resampling scheme in mlr3
my_task = Task$new("help task", "regr", data)
my_sampling = rsmp("cv", folds = 4)$instantiate(my_task)

train_ids = lapply(1:4, function(x) my_sampling$train_set(x))
test_ids = lapply(1:4, function(x) my_sampling$test_set(x))
smpls = list(list(train_ids = train_ids, test_ids = test_ids))

dml_plr_obj_external$set_sample_splitting(smpls)
dml_plr_obj_external$fit()
dml_plr_obj_external$summary()
Estimates and significance testing of the effect of target variables
  Estimate. Std. Error t value Pr(>|t|)   
d    0.2991     0.1072   2.791  0.00526 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


9.4. Sample-splitting without cross-fitting#

The boolean flag apply_cross_fitting allows to estimate DML models without applying cross-fitting. It results in randomly splitting the sample into two parts. The first half of the data is used for the estimation of the nuisance ML models and the second half for estimating the causal parameter. Note that cross-fitting performs well empirically and is recommended to remove bias induced by overfitting, see also Sample splitting to remove bias induced by overfitting.

Note

The flag apply_cross_fitting is deprecated for the python package. To avoid cross-fitting, please use the option to set external predictions.

dml_plr_obj_external = DoubleMLPLR$new(obj_dml_data, ml_l, ml_m,
                                    n_folds = 2, apply_cross_fitting = FALSE)
dml_plr_obj_external$fit()
dml_plr_obj_external$summary()
Estimates and significance testing of the effect of target variables
  Estimate. Std. Error t value Pr(>|t|)   
d    0.4022     0.1361   2.956  0.00312 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


Note, that in order to split data unevenly into train and test sets the interface to externally set the sample splitting via set_sample_splitting() needs to be applied, like for example:

dml_plr_obj_external = DoubleMLPLR$new(obj_dml_data, ml_l, ml_m,
                                        n_folds = 2, apply_cross_fitting = FALSE,
                                        draw_sample_splitting = FALSE)

set.seed(314)
# set up a task and cross-validation resampling scheme in mlr3
my_task = Task$new("help task", "regr", data)
my_sampling = rsmp("holdout", ratio = 0.8)$instantiate(my_task)

train_ids = list(my_sampling$train_set(1))
test_ids = list(my_sampling$test_set(1))
smpls = list(list(train_ids = train_ids, test_ids = test_ids))

dml_plr_obj_external$set_sample_splitting(smpls)
dml_plr_obj_external$fit()
dml_plr_obj_external$summary()
Estimates and significance testing of the effect of target variables
  Estimate. Std. Error t value Pr(>|t|)
d    0.3347     0.2408    1.39    0.164


9.5. Estimate DML models without sample-splitting#

The implementation of the DML models allows the estimation without sample splitting, i.e., all observations are used for learning the nuisance models as well as for the estimation of the causal parameter. Note that this approach usually results in a bias and is therefore not recommended without appropriate theoretical justification, see also Sample splitting to remove bias induced by overfitting.

Note

The flag apply_cross_fitting is deprecated for the python package. To avoid cross-fitting, please use the option to set external predictions. Additionally, the number of folds n_folds is expected to be at least 2.

dml_plr_no_split = DoubleMLPLR$new(obj_dml_data, ml_l, ml_m,
                                n_folds = 1, apply_cross_fitting = FALSE)

set.seed(314)
dml_plr_no_split$fit()
dml_plr_no_split$summary()
Estimates and significance testing of the effect of target variables
  Estimate. Std. Error t value Pr(>|t|)    
d   0.35688    0.09792   3.644 0.000268 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


9.6. References#

  • Chernozhukov, Victor and Demirer, Mert and Duflo, Esther and Fernández-Val, Iván (2018), Generic Machine Learning Inference on Heterogeneous Treatment Effects in Randomized Experiments, with an Application to Immunization in India, National Bureau of Economic Research, doi: 10.3386/w24678.