Skip to content

binned edf test ? cdf version of chisquare test ? proportions #1288

Open

Description

@josef-pkt

I'm not sure what I want here.

The use case:
I have a monte carlo of a distribution and want to compare it with a theoretical distribution, either cdf or sf.
The theoretical distribution is expensive to calculate, so I don't want to calculate the cdf/sf at all sample points to run a ks_test or similar ECDF test.

What I would like is to have hypothesis tests for joint and pointwise equality of distributions using the cdf/sf at a few values (10 to 50 instead of nobs=20000).

chisquare test version:

>>> efreq = -np.diff(np.concatenate((esf, [0])))
>>> tfreq = -np.diff(np.concatenate((tsf, [0])))
>>> stats.chisquare(efreq * nobs, tfreq * nobs)
(10.534192022890188, 0.30899244414970101)

where esf and tsf are empirical and theoretical survival function respectively.

using pointwise confidence interval for proportion

>>> from statsmodels.stats.proportion import proportion_confint
>>> nobs = 20000
>>> pci = proportion_confint(tsf * nobs, nobs, 0.05)
>>> ((pci[0] < esf) & (esf < pci[1])).all()
False
>>> pci = proportion_confint(tsf * nobs, nobs, 0.01)
>>> ((pci[0] < esf) & (esf < pci[1])).all()
True

binom_test and proportions_ztest are not vectorized to allow multiple hypothesis

>>> prop.binom_test(esf * nobs, nobs * np.ones(len(tsf)), tsf)
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    prop.binom_test(esf * nobs, nobs * np.ones(len(tsf)), tsf)
  File "e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new2_py27\statsmodels\statsmodels\stats\proportion.py", line 384, in binom_test
    pval = stats.binom_test(count, n=nobs, p=prop)
  File "C:\Programs\Python27\lib\site-packages\scipy\stats\morestats.py", line 1033, in binom_test
    raise ValueError("Incorrect length for x.")
ValueError: Incorrect length for x.
>>> prop.proportions_ztest(esf * nobs, nobs * np.ones(len(tsf)), tsf)
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    prop.proportions_ztest(esf * nobs, nobs * np.ones(len(tsf)), tsf)
  File "e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new2_py27\statsmodels\statsmodels\stats\proportion.py", line 580, in proportions_ztest
    raise NotImplementedError(msg)
NotImplementedError: more than two samples are not implemented yet

looping

>>> r = np.array([prop.proportions_ztest(e, n, value=t) for (e,n,t) in zip(esf * nobs, nobs * np.ones(len(tsf)), tsf)])
>>> pandas.DataFrame(np.round(np.column_stack((esf, tsf, r)), 6), columns='esf tsf zstat pval'.split())
       esf       tsf     zstat      pval
0  1.00000  1.000000       inf  0.000000
1  0.94355  0.943928 -0.231824  0.816674
2  0.47545  0.470093  1.517125  0.129235
3  0.13535  0.135336  0.005904  0.995289
4  0.03325  0.030465  2.196859  0.028031
5  0.00680  0.006037  1.312614  0.189313
6  0.00130  0.001108  0.753831  0.450951
7  0.00020  0.000193  0.068221  0.945610
8  0.00005  0.000032  0.350501  0.725963
9  0.00000  0.000005      -inf  0.000000

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions