15
15
class ExperimentalDesign :
16
16
"""Base class"""
17
17
18
- prediction_model = None
18
+ model = None
19
19
expt_type = None
20
20
21
- def __init__ (self , prediction_model = None , ** kwargs ):
22
- if prediction_model is not None :
23
- self .prediction_model = prediction_model
24
- if self .prediction_model is None :
21
+ def __init__ (self , model = None , ** kwargs ):
22
+ if model is not None :
23
+ self .model = model
24
+ if self .model is None :
25
25
raise ValueError ("fitting_model not set or passed." )
26
26
27
27
@property
28
28
def idata (self ):
29
29
"""Access to the InferenceData object"""
30
- return self .prediction_model .idata
30
+ return self .model .idata
31
31
32
32
def print_coefficients (self ):
33
33
"""Prints the model coefficients"""
@@ -41,9 +41,7 @@ def print_coefficients(self):
41
41
f"{ name : <30} { coeff_samples .mean ().data :.2f} , 94% HDI [{ coeff_samples .quantile (0.03 ).data :.2f} , { coeff_samples .quantile (1 - 0.03 ).data :.2f} ]" # noqa: E501
42
42
)
43
43
# add coeff for measurement std
44
- coeff_samples = az .extract (
45
- self .prediction_model .idata .posterior , var_names = "sigma"
46
- )
44
+ coeff_samples = az .extract (self .model .idata .posterior , var_names = "sigma" )
47
45
name = "sigma"
48
46
print (
49
47
f"{ name : <30} { coeff_samples .mean ().data :.2f} , 94% HDI [{ coeff_samples .quantile (0.03 ).data :.2f} , { coeff_samples .quantile (1 - 0.03 ).data :.2f} ]" # noqa: E501
@@ -58,10 +56,10 @@ def __init__(
58
56
data : pd .DataFrame ,
59
57
treatment_time : int ,
60
58
formula : str ,
61
- prediction_model = None ,
59
+ model = None ,
62
60
** kwargs ,
63
61
) -> None :
64
- super ().__init__ (prediction_model = prediction_model , ** kwargs )
62
+ super ().__init__ (model = model , ** kwargs )
65
63
self .treatment_time = treatment_time
66
64
# split data in to pre and post intervention
67
65
self .datapre = data [data .index <= self .treatment_time ]
@@ -86,17 +84,17 @@ def __init__(
86
84
# DEVIATION FROM SKL EXPERIMENT CODE =============================
87
85
# fit the model to the observed (pre-intervention) data
88
86
COORDS = {"coeffs" : self .labels , "obs_indx" : np .arange (self .pre_X .shape [0 ])}
89
- self .prediction_model .fit (X = self .pre_X , y = self .pre_y , coords = COORDS )
87
+ self .model .fit (X = self .pre_X , y = self .pre_y , coords = COORDS )
90
88
# ================================================================
91
89
92
90
# score the goodness of fit to the pre-intervention data
93
- self .score = self .prediction_model .score (X = self .pre_X , y = self .pre_y )
91
+ self .score = self .model .score (X = self .pre_X , y = self .pre_y )
94
92
95
93
# get the model predictions of the observed (pre-intervention) data
96
- self .pre_pred = self .prediction_model .predict (X = self .pre_X )
94
+ self .pre_pred = self .model .predict (X = self .pre_X )
97
95
98
96
# calculate the counterfactual
99
- self .post_pred = self .prediction_model .predict (X = self .post_X )
97
+ self .post_pred = self .model .predict (X = self .post_X )
100
98
101
99
# causal impact pre (ie the residuals of the model fit to observed)
102
100
pre_data = xr .DataArray (self .pre_y [:, 0 ], dims = ["obs_ind" ])
@@ -242,10 +240,10 @@ def __init__(
242
240
group_variable_name : str ,
243
241
treated : str ,
244
242
untreated : str ,
245
- prediction_model = None ,
243
+ model = None ,
246
244
** kwargs ,
247
245
):
248
- super ().__init__ (prediction_model = prediction_model , ** kwargs )
246
+ super ().__init__ (model = model , ** kwargs )
249
247
self .data = data
250
248
self .expt_type = "Difference in Differences"
251
249
self .formula = formula
@@ -291,7 +289,7 @@ def __init__(
291
289
292
290
# DEVIATION FROM SKL EXPERIMENT CODE =============================
293
291
COORDS = {"coeffs" : self .labels , "obs_indx" : np .arange (self .X .shape [0 ])}
294
- self .prediction_model .fit (X = self .X , y = self .y , coords = COORDS )
292
+ self .model .fit (X = self .X , y = self .y , coords = COORDS )
295
293
# ================================================================
296
294
297
295
# predicted outcome for control group
@@ -308,7 +306,7 @@ def __init__(
308
306
)
309
307
assert not self .x_pred_control .empty
310
308
(new_x ,) = build_design_matrices ([self ._x_design_info ], self .x_pred_control )
311
- self .y_pred_control = self .prediction_model .predict (np .asarray (new_x ))
309
+ self .y_pred_control = self .model .predict (np .asarray (new_x ))
312
310
313
311
# predicted outcome for treatment group
314
312
self .x_pred_treatment = (
@@ -324,7 +322,7 @@ def __init__(
324
322
)
325
323
assert not self .x_pred_treatment .empty
326
324
(new_x ,) = build_design_matrices ([self ._x_design_info ], self .x_pred_treatment )
327
- self .y_pred_treatment = self .prediction_model .predict (np .asarray (new_x ))
325
+ self .y_pred_treatment = self .model .predict (np .asarray (new_x ))
328
326
329
327
# predicted outcome for counterfactual
330
328
self .x_pred_counterfactual = (
@@ -346,7 +344,7 @@ def __init__(
346
344
(new_x ,) = build_design_matrices (
347
345
[self ._x_design_info ], self .x_pred_counterfactual
348
346
)
349
- self .y_pred_counterfactual = self .prediction_model .predict (np .asarray (new_x ))
347
+ self .y_pred_counterfactual = self .model .predict (np .asarray (new_x ))
350
348
351
349
# calculate causal impact
352
350
self .causal_impact = (
@@ -489,7 +487,7 @@ class RegressionDiscontinuity(ExperimentalDesign):
489
487
:param formula: A statistical model formula
490
488
:param treatment_threshold: A scalar threshold value at which the treatment
491
489
is applied
492
- :param prediction_model : A PyMC model
490
+ :param model : A PyMC model
493
491
:param running_variable_name: The name of the predictor variable that the treatment
494
492
threshold is based upon
495
493
@@ -504,11 +502,11 @@ def __init__(
504
502
data : pd .DataFrame ,
505
503
formula : str ,
506
504
treatment_threshold : float ,
507
- prediction_model = None ,
505
+ model = None ,
508
506
running_variable_name : str = "x" ,
509
507
** kwargs ,
510
508
):
511
- super ().__init__ (prediction_model = prediction_model , ** kwargs )
509
+ super ().__init__ (model = model , ** kwargs )
512
510
self .expt_type = "Regression Discontinuity"
513
511
self .data = data
514
512
self .formula = formula
@@ -527,11 +525,11 @@ def __init__(
527
525
# DEVIATION FROM SKL EXPERIMENT CODE =============================
528
526
# fit the model to the observed (pre-intervention) data
529
527
COORDS = {"coeffs" : self .labels , "obs_indx" : np .arange (self .X .shape [0 ])}
530
- self .prediction_model .fit (X = self .X , y = self .y , coords = COORDS )
528
+ self .model .fit (X = self .X , y = self .y , coords = COORDS )
531
529
# ================================================================
532
530
533
531
# score the goodness of fit to all data
534
- self .score = self .prediction_model .score (X = self .X , y = self .y )
532
+ self .score = self .model .score (X = self .X , y = self .y )
535
533
536
534
# get the model predictions of the observed data
537
535
xi = np .linspace (
@@ -543,7 +541,7 @@ def __init__(
543
541
{self .running_variable_name : xi , "treated" : self ._is_treated (xi )}
544
542
)
545
543
(new_x ,) = build_design_matrices ([self ._x_design_info ], self .x_pred )
546
- self .pred = self .prediction_model .predict (X = np .asarray (new_x ))
544
+ self .pred = self .model .predict (X = np .asarray (new_x ))
547
545
548
546
# calculate discontinuity by evaluating the difference in model expectation on
549
547
# either side of the discontinuity
@@ -558,7 +556,7 @@ def __init__(
558
556
}
559
557
)
560
558
(new_x ,) = build_design_matrices ([self ._x_design_info ], self .x_discon )
561
- self .pred_discon = self .prediction_model .predict (X = np .asarray (new_x ))
559
+ self .pred_discon = self .model .predict (X = np .asarray (new_x ))
562
560
self .discontinuity_at_threshold = (
563
561
self .pred_discon ["posterior_predictive" ].sel (obs_ind = 1 )["mu" ]
564
562
- self .pred_discon ["posterior_predictive" ].sel (obs_ind = 0 )["mu" ]
@@ -633,10 +631,10 @@ def __init__(
633
631
formula : str ,
634
632
group_variable_name : str ,
635
633
pretreatment_variable_name : str ,
636
- prediction_model = None ,
634
+ model = None ,
637
635
** kwargs ,
638
636
):
639
- super ().__init__ (prediction_model = prediction_model , ** kwargs )
637
+ super ().__init__ (model = model , ** kwargs )
640
638
self .data = data
641
639
self .expt_type = "Pretest/posttest Nonequivalent Group Design"
642
640
self .formula = formula
@@ -663,7 +661,7 @@ def __init__(
663
661
664
662
# fit the model to the observed (pre-intervention) data
665
663
COORDS = {"coeffs" : self .labels , "obs_indx" : np .arange (self .X .shape [0 ])}
666
- self .prediction_model .fit (X = self .X , y = self .y , coords = COORDS )
664
+ self .model .fit (X = self .X , y = self .y , coords = COORDS )
667
665
668
666
# Calculate the posterior predictive for the treatment and control for an
669
667
# interpolated set of pretest values
@@ -681,7 +679,7 @@ def __init__(
681
679
}
682
680
)
683
681
(new_x ,) = build_design_matrices ([self ._x_design_info ], x_pred_untreated )
684
- self .pred_untreated = self .prediction_model .predict (X = np .asarray (new_x ))
682
+ self .pred_untreated = self .model .predict (X = np .asarray (new_x ))
685
683
# treated
686
684
x_pred_untreated = pd .DataFrame (
687
685
{
@@ -690,7 +688,7 @@ def __init__(
690
688
}
691
689
)
692
690
(new_x ,) = build_design_matrices ([self ._x_design_info ], x_pred_untreated )
693
- self .pred_treated = self .prediction_model .predict (X = np .asarray (new_x ))
691
+ self .pred_treated = self .model .predict (X = np .asarray (new_x ))
694
692
695
693
# Evaluate causal impact as equal to the trestment effect
696
694
self .causal_impact = self .idata .posterior ["beta" ].sel (
0 commit comments