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

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.09224793 -0.07841491 -0.61449963 -0.61265812 -0.29733573
[1] -0.17752084 -0.32003889 -0.95147651  0.96727188  0.06645169

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]
[1,] -0.14766597 -0.171955111 -0.176805327 -0.12823637 -0.1163988628
[2,] -0.02894937 -0.002121951 -0.031031125 -0.00139308 -0.0014023976
[3,] -0.78638673 -0.231883517 -0.002886313 -0.18317660 -0.0008399116
[4,] -0.73403714 -1.049471134 -2.483170113 -1.18998022 -0.1652475736
[5,] -1.02336896 -0.410337832 -0.862200596 -0.27341211 -0.3820683298
            [,6]          [,7]        [,8]        [,9]       [,10]
[1,] -0.32268648 -0.0244595777 -0.07616704 -0.01438995 -0.25864750
[2,] -0.00162101 -0.0005650281 -0.15868235 -0.03381123 -0.05810192
[3,] -0.09260564 -0.1799037046 -0.19096407 -0.05401341 -0.38812971
[4,] -0.09286372 -1.0475310954 -0.27527648 -0.50266507 -0.42230418
[5,] -0.09144819 -0.2284374436 -0.35160449 -0.57014718 -0.27351032
           [,1]        [,2]       [,3]        [,4]        [,5]        [,6]
[1,] -0.2254650 -0.26664073 -0.1791661 -0.26230257 -0.20980269 -0.23814106
[2,] -0.1662970  0.04420315 -0.1747484  0.04955969 -0.03996319 -0.02474118
[3,] -0.9601093 -0.45279062 -0.0577278 -0.45506892  0.03455956 -0.30255102
[4,]  1.1957901  1.36299239  1.6793332  1.29831129  0.49030704  0.40565129
[5,] -0.4212243 -0.10878321  0.1593056  0.13326111 -0.00386368  0.11207186
            [,7]        [,8]       [,9]       [,10]
[1,] -0.08293451 -0.23950306 -0.1048198 -0.42256700
[2,] -0.02567391 -0.41574474 -0.2447510 -0.25960088
[3,] -0.58283887 -0.45410828 -0.2403047 -0.82265869
[4,]  1.40903997  0.42823874  0.7656327  0.84865601
[5,]  0.10698003 -0.09683064 -0.1706944  0.01531075

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.382436 
        d 
0.0909643 

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.3962097 0.3839485 0.3585427 0.3934784 0.3809234 0.3796847 0.4433454
          [,8]      [,9]     [,10]
[1,] 0.4628215 0.3745868 0.3574766
          [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,] 0.0897143 0.08716036 0.09233011 0.08606981 0.09296757 0.08392676
           [,7]       [,8]       [,9]     [,10]
[1,] 0.08989668 0.09746813 0.09181437 0.0950041

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.40723    0.08767   4.645  3.4e-06 ***
---
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.40723    0.08767   4.645  3.4e-06 ***
---
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.3825     0.1232   3.106   0.0019 **
---
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.3941     0.2416   1.632    0.103


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.37116    0.09179   4.044 5.27e-05 ***
---
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.