7
7
from sklearn .model_selection import KFold
8
8
9
9
# From the Public API
10
- from SparseSC .lambda_utils import get_max_lambda , L2_pen_guestimate
10
+ from SparseSC .lambda_utils import get_max_v_pen , w_pen_guestimate
11
11
from SparseSC .cross_validation import CV_score
12
12
from SparseSC .tensor import tensor
13
13
from SparseSC .weights import weights
@@ -23,8 +23,8 @@ def fit(X,Y,
23
23
covariate_penalties = None , # Float or an array of floats
24
24
# PARAMETERS USED TO CONSTRUCT DEFAULT GRID COVARIATE_PENALTIES
25
25
grid = None , # USER SUPPLIED GRID OF COVARIATE PENALTIES
26
- Lambda_min = 1e-6 ,
27
- Lambda_max = 1 ,
26
+ min_v_pen = 1e-6 ,
27
+ max_v_pen = 1 ,
28
28
grid_points = 20 ,
29
29
choice = "min" ,
30
30
cv_folds = 10 ,
@@ -54,36 +54,36 @@ def fit(X,Y,
54
54
55
55
:param weight_penalty: Penalty applied to the difference
56
56
between the current weights and the null weights (1/n). default
57
- provided by :func:``L2_pen_guestimate ``.
57
+ provided by :func:``w_pen_guestimate ``.
58
58
:type weight_penalty: float, Optional
59
59
60
60
:param covariate_penalties: penalty
61
61
(penalties) applied to the magnitude of the covariate weights.
62
62
Defaults to ``[ Lambda_c_max * g for g in grid]``, where
63
- `Lambda_c_max` is determined via :func:`get_max_lambda ` .
63
+ `Lambda_c_max` is determined via :func:`get_max_v_pen ` .
64
64
:type covariate_penalties: float | float[], optional
65
65
66
66
:param grid: only used when `covariate_penalties` is not provided.
67
- Defaults to ``np.exp(np.linspace(np.log(Lambda_min ),np.log(Lambda_max ),grid_points))``
67
+ Defaults to ``np.exp(np.linspace(np.log(min_v_pen ),np.log(max_v_pen ),grid_points))``
68
68
:type grid: float | float[], optional
69
69
70
- :param Lambda_min : Lower bound for ``grid`` when
70
+ :param min_v_pen : Lower bound for ``grid`` when
71
71
``covariate_penalties`` and ``grid`` are not provided. Must be in the
72
72
range ``(0,1)``
73
- :type Lambda_min : float, default = 1e-6
73
+ :type min_v_pen : float, default = 1e-6
74
74
75
- :param Lambda_max : Upper bound for ``grid`` when
75
+ :param max_v_pen : Upper bound for ``grid`` when
76
76
``covariate_penalties`` and ``grid`` are not provided. Must be in the
77
77
range ``(0,1]``
78
- :type Lambda_max : float, default = 1
78
+ :type max_v_pen : float, default = 1
79
79
80
80
:param grid_points: number of points in the ``grid`` parameter when
81
81
``covariate_penalties`` and ``grid`` are not provided
82
82
:type grid_points: int, default = 20
83
83
84
84
:param choice: Method for choosing from among the
85
85
covariate_penalties. Only used when covariate_penalties is an
86
- iterable. Defaults to ``"min"`` which selects the lambda parameter
86
+ iterable. Defaults to ``"min"`` which selects the v_pen parameter
87
87
associated with the lowest cross validation error.
88
88
:type choice: str or function. default = ``"min"``
89
89
@@ -193,14 +193,14 @@ def fit(X,Y,
193
193
# --------------------------------------------------
194
194
# (sensible?) defaults
195
195
# --------------------------------------------------
196
- # Get the L2 penalty guestimate: very quick ( milliseconds )
196
+ # Get the weight penalty guestimate: very quick ( milliseconds )
197
197
if weight_penalty is None :
198
- weight_penalty = L2_pen_guestimate (Xtrain )
198
+ weight_penalty = w_pen_guestimate (Xtrain )
199
199
if covariate_penalties is None :
200
200
if grid is None :
201
- grid = np .exp (np .linspace (np .log (Lambda_min ),np .log (Lambda_max ),grid_points ))
201
+ grid = np .exp (np .linspace (np .log (min_v_pen ),np .log (max_v_pen ),grid_points ))
202
202
# GET THE MAXIMUM v_penS: quick ~ ( seconds to tens of seconds )
203
- v_pen_max = get_max_lambda (Xtrain ,
203
+ v_pen_max = get_max_v_pen (Xtrain ,
204
204
Ytrain ,
205
205
w_pen = weight_penalty ,
206
206
grad_splits = gradient_folds ,
@@ -212,7 +212,7 @@ def fit(X,Y,
212
212
# Retrospective Treatment Effects: ( *model_type = "prospective"*)
213
213
214
214
# --------------------------------------------------
215
- # Phase 1: extract cross fold residual errors for each lambda
215
+ # Phase 1: extract cross fold residual errors for each v_pen
216
216
# --------------------------------------------------
217
217
218
218
# SCORES FOR EACH VALUE OF THE GRID: very slow ( minutes to hours )
@@ -228,15 +228,15 @@ def fit(X,Y,
228
228
** kwargs )
229
229
230
230
# GET THE INDEX OF THE BEST SCORE
231
- best_V_lambda = __choose (scores , covariate_penalties , choice )
231
+ best_v_pen = __choose (scores , covariate_penalties , choice )
232
232
233
233
# --------------------------------------------------
234
234
# Phase 2: extract V and weights: slow ( tens of seconds to minutes )
235
235
# --------------------------------------------------
236
236
237
237
best_V = tensor (X = Xtrain ,
238
238
Y = Ytrain ,
239
- v_pen = best_V_lambda ,
239
+ v_pen = best_v_pen ,
240
240
grad_splits = gradient_folds ,
241
241
random_state = gradient_seed , # TODO: Cleanup Task 1
242
242
** kwargs )
@@ -270,7 +270,7 @@ def fit(X,Y,
270
270
gradient_folds .append ([control_units , treated_units ])
271
271
272
272
# --------------------------------------------------
273
- # Phase 1: extract cross fold residual errors for each lambda
273
+ # Phase 1: extract cross fold residual errors for each v_pen
274
274
# --------------------------------------------------
275
275
276
276
# SCORES FOR EACH VALUE OF THE GRID: very slow ( minutes to hours )
@@ -286,15 +286,15 @@ def fit(X,Y,
286
286
** kwargs )
287
287
288
288
# GET THE INDEX OF THE BEST SCORE
289
- best_V_lambda = __choose (scores , covariate_penalties , choice )
289
+ best_v_pen = __choose (scores , covariate_penalties , choice )
290
290
291
291
# --------------------------------------------------
292
292
# Phase 2: extract V and weights: slow ( tens of seconds to minutes )
293
293
# --------------------------------------------------
294
294
295
295
best_V = tensor (X = X ,
296
296
Y = Y ,
297
- v_pen = best_V_lambda ,
297
+ v_pen = best_v_pen ,
298
298
grad_splits = gradient_folds ,
299
299
random_state = gradient_seed , # TODO: Cleanup Task 1
300
300
** kwargs )
@@ -306,7 +306,7 @@ def fit(X,Y,
306
306
# unobserved ( || Y_treat - W Y_ctrl || ) in counter factual
307
307
308
308
# --------------------------------------------------
309
- # Phase 1: extract cross fold residual errors for each lambda
309
+ # Phase 1: extract cross fold residual errors for each v_pen
310
310
# --------------------------------------------------
311
311
312
312
# SCORES FOR EACH VALUE OF THE GRID: very slow ( minutes to hours )
@@ -322,7 +322,7 @@ def fit(X,Y,
322
322
** kwargs )
323
323
324
324
# GET THE INDEX OF THE BEST SCORE
325
- best_V_lambda = __choose (scores , covariate_penalties , choice )
325
+ best_v_pen = __choose (scores , covariate_penalties , choice )
326
326
327
327
# --------------------------------------------------
328
328
# Phase 2: extract V and weights: slow ( tens of seconds to minutes )
@@ -332,7 +332,7 @@ def fit(X,Y,
332
332
Y = Ytrain ,
333
333
X_treat = Xtest ,
334
334
Y_treat = Ytest ,
335
- v_pen = best_V_lambda ,
335
+ v_pen = best_v_pen ,
336
336
** kwargs )
337
337
338
338
@@ -368,21 +368,21 @@ def fit(X,Y,
368
368
# --------------------------------------------------
369
369
if covariate_penalties is None :
370
370
if grid is None :
371
- grid = np .exp (np .linspace (np .log (Lambda_min ),np .log (Lambda_max ),grid_points ))
371
+ grid = np .exp (np .linspace (np .log (min_v_pen ),np .log (max_v_pen ),grid_points ))
372
372
# GET THE MAXIMUM v_penS: quick ~ ( seconds to tens of seconds )
373
- v_pen_max = get_max_lambda (X ,
373
+ v_pen_max = get_max_v_pen (X ,
374
374
Y ,
375
375
w_pen = weight_penalty ,
376
376
grad_splits = gradient_folds ,
377
377
verbose = verbose )
378
378
covariate_penalties = grid * v_pen_max
379
379
380
- # Get the L2 penalty guestimate: very quick ( milliseconds )
380
+ # Get the weight penalty guestimate: very quick ( milliseconds )
381
381
if weight_penalty is None :
382
- weight_penalty = L2_pen_guestimate (X )
382
+ weight_penalty = w_pen_guestimate (X )
383
383
384
384
# --------------------------------------------------
385
- # Phase 1: extract cross fold residual errors for each lambda
385
+ # Phase 1: extract cross fold residual errors for each v_pen
386
386
# --------------------------------------------------
387
387
388
388
# SCORES FOR EACH VALUE OF THE GRID: very slow ( minutes to hours )
@@ -398,15 +398,15 @@ def fit(X,Y,
398
398
** kwargs )
399
399
400
400
# GET THE INDEX OF THE BEST SCORE
401
- best_V_lambda = __choose (scores , covariate_penalties , choice )
401
+ best_v_pen = __choose (scores , covariate_penalties , choice )
402
402
403
403
# --------------------------------------------------
404
404
# Phase 2: extract V and weights: slow ( tens of seconds to minutes )
405
405
# --------------------------------------------------
406
406
407
407
best_V = tensor (X = X ,
408
408
Y = Y ,
409
- v_pen = best_V_lambda ,
409
+ v_pen = best_v_pen ,
410
410
grad_splits = gradient_folds ,
411
411
random_state = gradient_seed , # TODO: Cleanup Task 1
412
412
** kwargs )
@@ -423,7 +423,7 @@ def fit(X,Y,
423
423
treated_units ,
424
424
model_type ,
425
425
# fitting parameters
426
- best_V_lambda ,
426
+ best_v_pen ,
427
427
weight_penalty ,
428
428
covariate_penalties ,
429
429
best_V ,
@@ -437,18 +437,18 @@ def __choose(scores, covariate_penalties, choice):
437
437
try :
438
438
iter (covariate_penalties )
439
439
except TypeError :
440
- best_lambda = scores
440
+ best_v_pen = scores
441
441
else :
442
442
if choice == "min" :
443
443
best_i = np .argmin (scores )
444
- best_lambda = (covariate_penalties )[best_i ]
444
+ best_v_pen = (covariate_penalties )[best_i ]
445
445
elif callable (choice ):
446
- best_lambda = choice (scores )
446
+ best_v_pen = choice (scores )
447
447
else :
448
448
# TODO: this is a terrible place to throw this error
449
449
raise ValueError ("Unexpected value for choice parameter: %s" % choice )
450
450
451
- return best_lambda
451
+ return best_v_pen
452
452
453
453
454
454
class SparseSCFit (object ):
0 commit comments