@@ -59,10 +59,13 @@ def idata(self):
59
59
60
60
return self .model .idata
61
61
62
- def print_coefficients (self ) -> None :
62
+ def print_coefficients (self , round_to = None ) -> None :
63
63
"""
64
64
Prints the model coefficients
65
65
66
+ :param round_to:
67
+ Number of decimals used to round results. Defaults to 2. Use "none" to return raw numbers.
68
+
66
69
Example
67
70
--------
68
71
>>> import causalpy as cp
@@ -80,13 +83,13 @@ def print_coefficients(self) -> None:
80
83
... "progressbar": False
81
84
... }),
82
85
... )
83
- >>> result.print_coefficients() # doctest: +NUMBER
86
+ >>> result.print_coefficients(round_to=1 ) # doctest: +NUMBER
84
87
Model coefficients:
85
- Intercept 1.0 , 94% HDI [1.0, 1. 1]
86
- post_treatment[T.True] 0.9 , 94% HDI [0.9, 1.0 ]
87
- group 0.1 , 94% HDI [0.0 , 0.2]
88
+ Intercept 1, 94% HDI [1, 1]
89
+ post_treatment[T.True] 1 , 94% HDI [0.9, 1]
90
+ group 0.2 , 94% HDI [0.09 , 0.2]
88
91
group:post_treatment[T.True] 0.5, 94% HDI [0.4, 0.6]
89
- sigma 0.0 , 94% HDI [0.0 , 0.1]
92
+ sigma 0.08 , 94% HDI [0.07 , 0.1]
90
93
"""
91
94
print ("Model coefficients:" )
92
95
coeffs = az .extract (self .idata .posterior , var_names = "beta" )
@@ -95,13 +98,13 @@ def print_coefficients(self) -> None:
95
98
for name in self .labels :
96
99
coeff_samples = coeffs .sel (coeffs = name )
97
100
print (
98
- 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
101
+ f"{ name : <30} { round_num ( coeff_samples .mean ().data , round_to ) } , 94% HDI [{ round_num ( coeff_samples .quantile (0.03 ).data , round_to ) } , { round_num ( coeff_samples .quantile (1 - 0.03 ).data , round_to ) } ]" # noqa: E501
99
102
)
100
103
# add coeff for measurement std
101
104
coeff_samples = az .extract (self .model .idata .posterior , var_names = "sigma" )
102
105
name = "sigma"
103
106
print (
104
- 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
107
+ f"{ name : <30} { round_num ( coeff_samples .mean ().data , round_to ) } , 94% HDI [{ round_num ( coeff_samples .quantile (0.03 ).data , round_to ) } , { round_num ( coeff_samples .quantile (1 - 0.03 ).data , round_to ) } ]" # noqa: E501
105
108
)
106
109
107
110
@@ -138,18 +141,18 @@ class PrePostFit(ExperimentalDesign):
138
141
... }
139
142
... ),
140
143
... )
141
- >>> result.summary() # doctest: +NUMBER
144
+ >>> result.summary(round_to=1 ) # doctest: +NUMBER
142
145
==================================Pre-Post Fit==================================
143
146
Formula: actual ~ 0 + a + b + c + d + e + f + g
144
147
Model coefficients:
145
- a 0.3, 94% HDI [0.3, 0.3 ]
146
- b 0.0 , 94% HDI [0.0 , 0.0 ]
147
- c 0.3, 94% HDI [0.2 , 0.3]
148
- d 0.0 , 94% HDI [0.0 , 0.1]
149
- e 0.0 , 94% HDI [0.0 , 0.0 ]
150
- f 0.1 , 94% HDI [0.1, 0.2 ]
151
- g 0.0 , 94% HDI [0.0 , 0.0 ]
152
- sigma 0.2 , 94% HDI [0.2, 0.3]
148
+ a 0.3, 94% HDI [0.3, 0.4 ]
149
+ b 0.05 , 94% HDI [0.009 , 0.09 ]
150
+ c 0.3, 94% HDI [0.3 , 0.3]
151
+ d 0.05 , 94% HDI [0.01 , 0.1]
152
+ e 0.03 , 94% HDI [0.001 , 0.07 ]
153
+ f 0.2 , 94% HDI [0.1, 0.3 ]
154
+ g 0.04 , 94% HDI [0.003 , 0.09 ]
155
+ sigma 0.3 , 94% HDI [0.2, 0.3]
153
156
"""
154
157
155
158
def __init__ (
@@ -336,15 +339,18 @@ def plot(self, counterfactual_label="Counterfactual", round_to=None, **kwargs):
336
339
337
340
return fig , ax
338
341
339
- def summary (self ) -> None :
342
+ def summary (self , round_to = None ) -> None :
340
343
"""
341
344
Print text output summarising the results
345
+
346
+ :param round_to:
347
+ Number of decimals used to round results. Defaults to 2. Use "none" to return raw numbers.
342
348
"""
343
349
344
350
print (f"{ self .expt_type :=^80} " )
345
351
print (f"Formula: { self .formula } " )
346
352
# TODO: extra experiment specific outputs here
347
- self .print_coefficients ()
353
+ self .print_coefficients (round_to )
348
354
349
355
350
356
class InterruptedTimeSeries (PrePostFit ):
@@ -733,17 +739,19 @@ def _causal_impact_summary_stat(self, round_to=None) -> str:
733
739
causal_impact = f"{ round_num (self .causal_impact .mean (), round_to )} , "
734
740
return f"Causal impact = { causal_impact + ci } "
735
741
736
- def summary (self ) -> None :
742
+ def summary (self , round_to = None ) -> None :
737
743
"""
738
- Print text output summarising the results
744
+ Print text output summarising the results.
745
+
746
+ :param round_to:
747
+ Number of decimals used to round results. Defaults to 2. Use "none" to return raw numbers.
739
748
"""
740
749
741
750
print (f"{ self .expt_type :=^80} " )
742
751
print (f"Formula: { self .formula } " )
743
752
print ("\n Results:" )
744
- # TODO: extra experiment specific outputs here
745
- print (self ._causal_impact_summary_stat ())
746
- self .print_coefficients ()
753
+ print (round_num (self ._causal_impact_summary_stat (), round_to ))
754
+ self .print_coefficients (round_to )
747
755
748
756
749
757
class RegressionDiscontinuity (ExperimentalDesign ):
@@ -785,20 +793,20 @@ class RegressionDiscontinuity(ExperimentalDesign):
785
793
... ),
786
794
... treatment_threshold=0.5,
787
795
... )
788
- >>> result.summary() # doctest: +NUMBER
796
+ >>> result.summary(round_to=1 ) # doctest: +NUMBER
789
797
============================Regression Discontinuity============================
790
798
Formula: y ~ 1 + x + treated + x:treated
791
799
Running variable: x
792
800
Threshold on running variable: 0.5
793
801
<BLANKLINE>
794
802
Results:
795
- Discontinuity at threshold = 0.91
803
+ Discontinuity at threshold = 0.9
796
804
Model coefficients:
797
- Intercept 0.0 , 94% HDI [0.0 , 0.1 ]
798
- treated[T.True] 2.4 , 94% HDI [1.6 , 3.2 ]
799
- x 1.3 , 94% HDI [1.1, 1.5 ]
800
- x:treated[T.True] -3.0 , 94% HDI [-4.1 , -2.0 ]
801
- sigma 0.3 , 94% HDI [0.3, 0.4]
805
+ Intercept 0.09 , 94% HDI [-0.001 , 0.2 ]
806
+ treated[T.True] 2, 94% HDI [2 , 3]
807
+ x 1, 94% HDI [1, 2 ]
808
+ x:treated[T.True] -3, 94% HDI [-4, -2]
809
+ sigma 0.4 , 94% HDI [0.3, 0.4]
802
810
"""
803
811
804
812
def __init__ (
@@ -962,9 +970,12 @@ def plot(self, round_to=None):
962
970
)
963
971
return fig , ax
964
972
965
- def summary (self ) -> None :
973
+ def summary (self , round_to : None ) -> None :
966
974
"""
967
975
Print text output summarising the results
976
+
977
+ :param round_to:
978
+ Number of decimals used to round results. Defaults to 2. Use "none" to return raw numbers.
968
979
"""
969
980
970
981
print (f"{ self .expt_type :=^80} " )
@@ -973,9 +984,9 @@ def summary(self) -> None:
973
984
print (f"Threshold on running variable: { self .treatment_threshold } " )
974
985
print ("\n Results:" )
975
986
print (
976
- f"Discontinuity at threshold = { self .discontinuity_at_threshold .mean ():.2f } "
987
+ f"Discontinuity at threshold = { round_num ( self .discontinuity_at_threshold .mean (), round_to ) } "
977
988
)
978
- self .print_coefficients ()
989
+ self .print_coefficients (round_to )
979
990
980
991
981
992
class RegressionKink (ExperimentalDesign ):
@@ -1179,9 +1190,12 @@ def plot(self, round_to=None):
1179
1190
)
1180
1191
return fig , ax
1181
1192
1182
- def summary (self ) -> None :
1193
+ def summary (self , round_to = None ) -> None :
1183
1194
"""
1184
1195
Print text output summarising the results
1196
+
1197
+ :param round_to:
1198
+ Number of decimals used to round results. Defaults to 2. Use "none" to return raw numbers.
1185
1199
"""
1186
1200
1187
1201
print (
@@ -1192,10 +1206,10 @@ def summary(self) -> None:
1192
1206
Kink point on running variable: { self .kink_point }
1193
1207
1194
1208
Results:
1195
- Change in slope at kink point = { self .gradient_change .mean ():.2f }
1209
+ Change in slope at kink point = { round_num ( self .gradient_change .mean (), round_to ) }
1196
1210
"""
1197
1211
)
1198
- self .print_coefficients ()
1212
+ self .print_coefficients (round_to )
1199
1213
1200
1214
1201
1215
class PrePostNEGD (ExperimentalDesign ):
@@ -1232,17 +1246,17 @@ class PrePostNEGD(ExperimentalDesign):
1232
1246
... }
1233
1247
... )
1234
1248
... )
1235
- >>> result.summary() # doctest: +NUMBER
1249
+ >>> result.summary(round_to=1 ) # doctest: +NUMBER
1236
1250
==================Pretest/posttest Nonequivalent Group Design===================
1237
1251
Formula: post ~ 1 + C(group) + pre
1238
1252
<BLANKLINE>
1239
1253
Results:
1240
- Causal impact = 1.8 , $CI_{94%}$[1.7 , 2.1 ]
1254
+ Causal impact = 2 , $CI_{94%}$[2 , 2]
1241
1255
Model coefficients:
1242
- Intercept -0.4 , 94% HDI [-1. 1, 0.2]
1243
- C(group)[T.1] 1.8 , 94% HDI [1.6 , 2.0 ]
1244
- pre 1.0 , 94% HDI [0.9, 1. 1]
1245
- sigma 0.5, 94% HDI [0.4 , 0.5 ]
1256
+ Intercept -0.5 , 94% HDI [-1, 0.2]
1257
+ C(group)[T.1] 2 , 94% HDI [2 , 2]
1258
+ pre 1, 94% HDI [1, 1]
1259
+ sigma 0.5, 94% HDI [0.5 , 0.6 ]
1246
1260
"""
1247
1261
1248
1262
def __init__ (
@@ -1381,20 +1395,23 @@ def _causal_impact_summary_stat(self, round_to) -> str:
1381
1395
r"$CI_{94%}$"
1382
1396
+ f"[{ round_num (percentiles [0 ], round_to )} , { round_num (percentiles [1 ], round_to )} ]"
1383
1397
)
1384
- causal_impact = f"{ self .causal_impact .mean ():.2f } , "
1398
+ causal_impact = f"{ round_num ( self .causal_impact .mean (), round_to ) } , "
1385
1399
return f"Causal impact = { causal_impact + ci } "
1386
1400
1387
1401
def summary (self , round_to = None ) -> None :
1388
1402
"""
1389
1403
Print text output summarising the results
1404
+
1405
+ :param round_to:
1406
+ Number of decimals used to round results. Defaults to 2. Use "none" to return raw numbers.
1390
1407
"""
1391
1408
1392
1409
print (f"{ self .expt_type :=^80} " )
1393
1410
print (f"Formula: { self .formula } " )
1394
1411
print ("\n Results:" )
1395
1412
# TODO: extra experiment specific outputs here
1396
1413
print (self ._causal_impact_summary_stat (round_to ))
1397
- self .print_coefficients ()
1414
+ self .print_coefficients (round_to )
1398
1415
1399
1416
def _get_treatment_effect_coeff (self ) -> str :
1400
1417
"""Find the beta regression coefficient corresponding to the
@@ -1471,7 +1488,6 @@ class InstrumentalVariable(ExperimentalDesign):
1471
1488
... formula=formula,
1472
1489
... model=InstrumentalVariableRegression(sample_kwargs=sample_kwargs),
1473
1490
... )
1474
-
1475
1491
"""
1476
1492
1477
1493
def __init__ (
0 commit comments