11import numpy as np
22import pytest
3+ from scipy .stats import norm
34
45from doubleml .utils ._estimation import _aggregate_coefs_and_ses , _var_est
56
@@ -14,19 +15,24 @@ def n_coefs(request):
1415 return request .param
1516
1617
18+ @pytest .fixture (scope = "module" , params = [0.9 , 0.95 , 0.975 ])
19+ def level (request ):
20+ return request .param
21+
22+
1723@pytest .fixture (scope = "module" )
18- def test_var_est_and_aggr_fixture (n_rep , n_coefs ):
24+ def test_var_est_and_aggr_fixture (n_rep , n_coefs , level ):
1925 np .random .seed (42 )
2026
2127 all_thetas = np .full ((n_coefs , n_rep ), np .nan )
2228 all_ses = np .full ((n_coefs , n_rep ), np .nan )
23- expected_all_var = np .full ((n_coefs , n_rep ), np .nan )
29+ expected_all_upper_bounds = np .full ((n_coefs , n_rep ), np .nan )
2430 all_var_scaling_factors = np .full ((n_coefs , n_rep ), np .nan )
2531
2632 for i_coef in range (n_coefs ):
2733 n_obs = np .random .randint (100 , 200 )
2834 for i_rep in range (n_rep ):
29- psi = np .random .normal (size = (n_obs ))
35+ psi = np .random .normal (size = (n_obs ), loc = i_coef , scale = i_coef + 1 )
3036 psi_deriv = np .ones ((n_obs ))
3137
3238 all_thetas [i_coef , i_rep ] = np .mean (psi )
@@ -37,27 +43,23 @@ def test_var_est_and_aggr_fixture(n_rep, n_coefs):
3743 all_var_scaling_factors [i_coef , i_rep ] = var_scaling_factor
3844
3945 expected_theta = np .median (all_thetas , axis = 1 )
46+ critical_value = norm .ppf (level )
4047 for i_coef in range (n_coefs ):
4148 for i_rep in range (n_rep ):
42- theta_deviation = np .square (all_thetas [i_coef , i_rep ] - expected_theta [i_coef ])
43- expected_all_var [i_coef , i_rep ] = np .square (all_ses [i_coef , i_rep ]) + np .divide (
44- theta_deviation , all_var_scaling_factors [i_coef , i_rep ]
45- )
49+ expected_all_upper_bounds [i_coef , i_rep ] = all_thetas [i_coef , i_rep ] + critical_value * all_ses [i_coef , i_rep ]
4650
47- expected_se = np .sqrt (np .median (expected_all_var , axis = 1 ))
51+ expected_upper_bounds = np .median (expected_all_upper_bounds , axis = 1 )
52+ expected_se = (expected_upper_bounds - expected_theta ) / critical_value
4853
49- # without n_rep
5054 theta , se = _aggregate_coefs_and_ses (
5155 all_coefs = all_thetas ,
5256 all_ses = all_ses ,
53- var_scaling_factors = all_var_scaling_factors [:, 0 ],
5457 )
5558
5659 # with n_rep
5760 theta_2 , se_2 = _aggregate_coefs_and_ses (
5861 all_coefs = all_thetas ,
5962 all_ses = all_ses ,
60- var_scaling_factors = all_var_scaling_factors ,
6163 )
6264
6365 result_dict = {
0 commit comments