Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions statsmodels/stats/effect_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 5 12:36:54 2020

Author: Josef Perktold
License: BSD-3

"""
import numpy as np
from scipy import special
from statsmodels.stats.base import Holder


def _noncentrality_chisquare(chi2_stat, df, alpha=0.05):
"""noncentrality parameter for chi-square statistic

`nc` is zero-truncated umvue

Parameters
----------
chi2_stat : float
Chisquare-statistic, for example from a hypothesis test
df : int or float
Degrees of freedom
alpha : float in (0, 1)
Significance level for the confidence interval, covarage is 1 - alpha.

Returns
-------
HolderTuple
The main attributes are

- ``nc`` : estimate of noncentrality parameter
- ``confint`` : lower and upper bound of confidence interval for `nc``

Other attributes are estimates for nc by different methods.

References
----------
.. [1] Kubokawa, T., C.P. Robert, and A.K.Md.E. Saleh. 1993. “Estimation of
Noncentrality Parameters.”
Canadian Journal of Statistics 21 (1): 45–57.
https://doi.org/10.2307/3315657.

.. [2] Li, Qizhai, Junjian Zhang, and Shuai Dai. 2009. “On Estimating the
Non-Centrality Parameter of a Chi-Squared Distribution.” S
tatistics & Probability Letters 79 (1): 98–104.
https://doi.org/10.1016/j.spl.2008.07.025.

"""
alpha_half = alpha / 2

nc_umvue = chi2_stat - df
nc = np.maximum(nc_umvue, 0)
nc_lzd = np.maximum(nc_umvue, chi2_stat / (df + 1))
nc_krs = np.maximum(nc_umvue, chi2_stat * 2 / (df + 2))
nc_median = special.chndtrinc(chi2_stat, df, 0.5)
ci = special.chndtrinc(chi2_stat, df, [1 - alpha_half, alpha_half])

res = Holder(nc=nc,
confint=ci,
nc_umvue=nc_umvue,
nc_lzd=nc_lzd,
nc_krs=nc_krs,
nc_median=nc_median,
name="Noncentrality for chisquare-distributed random variable"
)
return res


def _noncentrality_f(f_stat, df1, df2, alpha=0.05):
"""noncentrality parameter for f statistic

`nc` is zero-truncated umvue

Parameters
----------
fstat : float
f-statistic, for example from a hypothesis test
df : int or float
Degrees of freedom
alpha : float in (0, 1)
Significance level for the confidence interval, covarage is 1 - alpha.

Returns
-------
HolderTuple
The main attributes are

- ``nc`` : estimate of noncentrality parameter
- ``confint`` : lower and upper bound of confidence interval for `nc``

Other attributes are estimates for nc by different methods.

References
----------
.. [1] Kubokawa, T., C.P. Robert, and A.K.Md.E. Saleh. 1993. “Estimation of
Noncentrality Parameters.” Canadian Journal of Statistics 21 (1): 45–57.
https://doi.org/10.2307/3315657.
"""
alpha_half = alpha / 2

x_s = f_stat * df1 / df2
nc_umvue = (df2 - 2) * x_s - df1
nc = np.maximum(nc_umvue, 0)
nc_krs = np.maximum(nc_umvue, x_s * 2 * (df2 - 1) / (df1 + 2))
nc_median = special.ncfdtrinc(df1, df2, 0.5, f_stat)
ci = special.ncfdtrinc(df1, df2, [1 - alpha_half, alpha_half], f_stat)

res = Holder(nc=nc,
confint=ci,
nc_umvue=nc_umvue,
nc_krs=nc_krs,
nc_median=nc_median,
name="Noncentrality for F-distributed random variable"
)
return res


def _noncentrality_t(t_stat, df, alpha=0.05):
"""noncentrality parameter for t statistic

Parameters
----------
fstat : float
f-statistic, for example from a hypothesis test
df : int or float
Degrees of freedom
alpha : float in (0, 1)
Significance level for the confidence interval, covarage is 1 - alpha.

Returns
-------
HolderTuple
The main attributes are

- ``nc`` : estimate of noncentrality parameter
- ``confint`` : lower and upper bound of confidence interval for `nc``

Other attributes are estimates for nc by different methods.

References
----------
.. [1] Hedges, Larry V. 2016. “Distribution Theory for Glass’s Estimator of
Effect Size and Related Estimators:”
Journal of Educational Statistics, November.
https://doi.org/10.3102/10769986006002107.

"""
alpha_half = alpha / 2

gfac = np.exp(special.gammaln(df/2.-0.5) - special.gammaln(df/2.))
c11 = np.sqrt(df/2.) * gfac
nc = t_stat / c11
nc_median = special.nctdtrinc(df, 0.5, t_stat)
ci = special.nctdtrinc(df, [1 - alpha_half, alpha_half], t_stat)

res = Holder(nc=nc,
confint=ci,
nc_median=nc_median,
name="Noncentrality for t-distributed random variable"
)
return res
75 changes: 75 additions & 0 deletions statsmodels/stats/tests/test_effectsize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 5 13:13:59 2020

Author: Josef Perktold
License: BSD-3

"""

from scipy import stats
from numpy.testing import assert_allclose

from statsmodels.stats.effect_size import (
_noncentrality_chisquare, _noncentrality_f, _noncentrality_t)


def test_noncent_chi2():
# > lochi(7.5,2,.95)
# [1] 0.03349255 0.97499458
# > hichi(7.5,2,.95)
# [1] 20.76049805 0.02500663

chi2_stat, df = 7.5, 2
ci_nc = [0.03349255, 20.76049805]
res = _noncentrality_chisquare(chi2_stat, df, alpha=0.05)
assert_allclose(res.confint, ci_nc, rtol=0.005)
# verify umvue unbiased
mean = stats.ncx2.mean(df, res.nc)
assert_allclose(chi2_stat, mean, rtol=1e-8)

assert_allclose(stats.ncx2.cdf(chi2_stat, df, res.confint), [0.975, 0.025],
rtol=1e-8)


def test_noncent_f():
# F(4, 75) = 3.5, confidence level = .95, two-sided CI:
# > lof(3.5,4,75,.95)
# [1] 0.7781436 0.9750039
# > hif(3.5,4,75,.95)
# [1] 29.72949219 0.02499965
f_stat, df1, df2 = 3.5, 4, 75

ci_nc = [0.7781436, 29.72949219]
res = _noncentrality_f(f_stat, df1, df2, alpha=0.05)
assert_allclose(res.confint, ci_nc, rtol=0.005)
# verify umvue unbiased
mean = stats.ncf.mean(df1, df2, res.nc)
assert_allclose(f_stat, mean, rtol=1e-8)

assert_allclose(stats.ncf.cdf(f_stat, df1, df2, res.confint),
[0.975, 0.025], rtol=1e-10)


def test_noncent_t():
# t(98) = 1.5, confidence level = .95, two-sided CI:
# > lot(1.5,98,.95)
# [1] -0.4749756 0.9750024
# > hit(1.5,98,.95)
# [1] 3.467285 0.025005

# > conf.limits.nct(1.5,98,.95)
# Lower.Limit Prob.Low.Limit Upper.Limit Prob.Up.Limit
# Values -0.474934 0.975 3.467371 0.02499999

t_stat, df = 1.5, 98

ci_nc = [-0.474934, 3.467371]
res = _noncentrality_t(t_stat, df, alpha=0.05)
assert_allclose(res.confint, ci_nc, rtol=0.005)
# verify umvue unbiased
mean = stats.nct.mean(df, res.nc)
assert_allclose(t_stat, mean, rtol=1e-8)

assert_allclose(stats.nct.cdf(t_stat, df, res.confint), [0.975, 0.025],
rtol=1e-10)