Skip to content

Commit ff75365

Browse files
adam2392bloebp
authored andcommitted
Add correction
Signed-off-by: Adam Li <[email protected]>
1 parent 92a01b7 commit ff75365

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

pywhy_stats/power_divergence.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def _power_divergence(
207207
Z: Optional[ArrayLike],
208208
method: str = "cressie-read",
209209
num_categories_allowed: int = 10,
210+
correction: bool = True,
210211
) -> PValueResult:
211212
"""Compute the Cressie-Read power divergence statistic.
212213
@@ -231,6 +232,11 @@ def _power_divergence(
231232
:footcite:`cressieread1984`"
232233
num_categories_allowed : int
233234
The maximum number of categories allowed in the input variables.
235+
correction : bool, optional
236+
If True, *and* the degrees of freedom is 1, apply Yates' correction
237+
for continuity. The effect of the correction is to adjust each
238+
observed value by 0.5 towards the corresponding expected value.
239+
See `scipy.stats.power_divergence` for more details.
234240
235241
Returns
236242
-------
@@ -265,7 +271,7 @@ def _power_divergence(
265271
if Z is None:
266272
# Compute the contingency table
267273
observed_xy, _, _ = np.histogram2d(X, Y, bins=(np.unique(X).size, np.unique(Y).size))
268-
chi, p_value, dof, expected = stats.chi2_contingency(observed_xy, method=method)
274+
chi, p_value, dof, expected = stats.chi2_contingency(observed_xy, correction=correction, lambda_=method)
269275

270276
# Step 2: If there are conditionals variables, iterate over unique states and do
271277
# the contingency test.
@@ -310,7 +316,7 @@ def _power_divergence(
310316
sub_table_z = (
311317
df.groupby(X_columns + Y_columns).size().unstack(Y_columns, fill_value=1e-7)
312318
)
313-
c, _, d, _ = stats.chi2_contingency(sub_table_z, method=method)
319+
c, _, d, _ = stats.chi2_contingency(sub_table_z, correction=correction, lambda_=method)
314320
chi += c
315321
dof += d
316322
except ValueError:

0 commit comments

Comments
 (0)