Skip to content

Commit aef072c

Browse files
committed
improve print_coefficients method for pymc experiments + rerun pymc notebooks
1 parent 2916688 commit aef072c

File tree

10 files changed

+550
-698
lines changed

10 files changed

+550
-698
lines changed

causalpy/pymc_experiments.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,36 @@ def print_coefficients(self, round_to=None) -> None:
112112
"""
113113
print("Model coefficients:")
114114
coeffs = az.extract(self.idata.posterior, var_names="beta")
115-
# Note: f"{name: <30}" pads the name with spaces so that we have alignment of
116-
# the stats despite variable names of different lengths
115+
116+
# Determine the width of the longest label
117+
max_label_length = max(len(name) for name in self.labels + ["sigma"])
118+
117119
for name in self.labels:
118120
coeff_samples = coeffs.sel(coeffs=name)
119121
print(
120-
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
122+
f" {name: <{max_label_length}}\t{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
121123
)
122-
# add coeff for measurement std
124+
125+
# Add coefficient for measurement std
123126
coeff_samples = az.extract(self.model.idata.posterior, var_names="sigma")
124127
name = "sigma"
125128
print(
126-
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
127-
)
129+
f" {name: <{max_label_length}}\t{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
130+
)
131+
132+
# # Note: f"{name: <30}" pads the name with spaces so that we have alignment of
133+
# # the stats despite variable names of different lengths
134+
# for name in self.labels:
135+
# coeff_samples = coeffs.sel(coeffs=name)
136+
# print(
137+
# 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
138+
# )
139+
# # add coeff for measurement std
140+
# coeff_samples = az.extract(self.model.idata.posterior, var_names="sigma")
141+
# name = "sigma"
142+
# print(
143+
# 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
144+
# )
128145

129146

130147
class PrePostFit(ExperimentalDesign, PrePostFitDataValidator):

docs/source/notebooks/ancova_pymc.ipynb

Lines changed: 41 additions & 38 deletions
Large diffs are not rendered by default.

docs/source/notebooks/did_pymc.ipynb

Lines changed: 27 additions & 34 deletions
Large diffs are not rendered by default.

docs/source/notebooks/did_pymc_banks.ipynb

Lines changed: 58 additions & 72 deletions
Large diffs are not rendered by default.

docs/source/notebooks/its_pymc.ipynb

Lines changed: 51 additions & 58 deletions
Large diffs are not rendered by default.

docs/source/notebooks/rd_pymc.ipynb

Lines changed: 93 additions & 121 deletions
Large diffs are not rendered by default.

docs/source/notebooks/rd_pymc_drinking.ipynb

Lines changed: 73 additions & 124 deletions
Large diffs are not rendered by default.

docs/source/notebooks/rkink_pymc.ipynb

Lines changed: 95 additions & 124 deletions
Large diffs are not rendered by default.

docs/source/notebooks/sc_pymc.ipynb

Lines changed: 47 additions & 62 deletions
Large diffs are not rendered by default.

docs/source/notebooks/sc_pymc_brexit.ipynb

Lines changed: 42 additions & 59 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)