Skip to content

WIP for base class TestResult - #5001

Closed
N-Wouda wants to merge 27 commits into
statsmodels:mainfrom
N-Wouda:test-result-base-class
Closed

N-Wouda wants to merge 27 commits into
statsmodels:mainfrom
N-Wouda:test-result-base-class

Conversation

@N-Wouda

@N-Wouda N-Wouda commented Aug 24, 2018 •

Copy link
Copy Markdown
Contributor

Following #4994, which references to i.a. #1182, I decided to open a new PR for this issue. While #1182 aspires to mostly the same thing, it appears largely stale. I intend to use this base class in multiple places, and have several locations inherit from, or directly use this base class.

After that is set-up, a next step will be to consolidate the children classes and negotiate a proper protocol per module (e.g., the tsa tests might need a different parent class than do most regression tests and the like).

@josef-pkt input is much appreciated! :)

@N-Wouda

N-Wouda commented Aug 24, 2018 •

Copy link
Copy Markdown
Contributor Author

An example of output can be obtained with e.g.,

if __name__ == "__main__":
    stats = Statistics(t=1.96, R=1.00)  # a list of test results

    hypothesis = Hypothesis(null="Null hypothesis",
                            alternative="Something else")

    crit_vals = CriticalValues({"5%": 0.9, "10%": 1.5})

    result = TestResult("Example test", hypothesis, stats, crit_vals)

    print(result)

Which returns,

Example test

Hypotheses:
	* H0: Null hypothesis
	* H1: Something else

Statistics:
t = 1.96, R = 1.0

Critical values:
[5%] = 0.9, [10%] = 1.5

@josef-pkt

Copy link
Copy Markdown
Member

I think a more likely location is statsmodels.stats.base
Generally this looks fine.

We need some optional attributes that can be listed by default after the main attributes, e.g. attributes in my latest Holder https://github.com/statsmodels/statsmodels/pull/4829/files#diff-36a55e208f6d1db357321ee87ef3ab77R1385

Also in most cases we don't specify critical values but report p-values and include the distribution, e.g. normal, t, F, chi2. In those cases we can leave critical values at None and don't print them.
Examples associated with model test results are in stats.contrast (but t-test results do a lot more than what standard test results show, e.g. computing confint)

@coveralls

coveralls commented Aug 24, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) to 84.257% when pulling 817fab0 on N-Wouda:test-result-base-class into 5b12177 on statsmodels:master.

@N-Wouda

N-Wouda commented Aug 25, 2018

Copy link
Copy Markdown
Contributor Author

My current thinking is to then maintain a list of optional fields on a TestResult instance, such that each individual test can mix-and-match where appropriate. This way we ensure test results remain flexible, but also adhere to a common structure.

I've updated my code with one way to do so. My idea is to introduce lightweight classes for all relevant test information (e.g. for the confidence interval, or the distribution, and perhaps one Other for those values we do not really know where to group). This would structure the tests to take those elements, but does not enforce a test to actually use all af them.

Comment thread statsmodels/stats/base.py
if key in TestResult._options:
setattr(self, key, value)

def __getattr__(self, item):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking here is to preserve interface compatibility between old-style test results (e.g. Bunch, or ResStore) and new-style tests where possible. I intend Statistics to be a new-style store object, against which the TestResult will always check if the attribute cannot be found on itself.

If it is found in the statistics attribute, the item there is returned and a DepreciationWarning is raised explaining the new behaviour. This way we can start using the new-style test results without breaking too much of existing, external code.

@N-Wouda

N-Wouda commented Aug 27, 2018 •

Copy link
Copy Markdown
Contributor Author

@josef-pkt I'll start writing docstrings soon, explaining the design/intended use.

For now, I've replaced several ResultStores and the like in various tests. Do you have suggestions where to look next? I'd love to extend the number of use cases this TestResult can support!

As an aside the linter seems to think,

def test_raises_missing_value_access(self):
    with assert_raises(AttributeError):
      self.test_result.some_missing_statistic

has no effect (L104 in statsmodels/stats/tests/test_base.py) . It is of course mistaken, as this causes an attribute look-up that I wish to test. How do I tell the thing such?

@jbrockmendel

Copy link
Copy Markdown
Contributor

It might be worth looking at how arch does this. A lot of thought went into that.

@N-Wouda

N-Wouda commented Aug 28, 2018

Copy link
Copy Markdown
Contributor Author

It might be worth looking at how arch does this. A lot of thought went into that.

I'll have a look at that later today.

@N-Wouda N-Wouda closed this Aug 28, 2018
@N-Wouda N-Wouda reopened this Aug 28, 2018
@codecov

codecov Bot commented Sep 1, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #5001 into master will decrease coverage by 0.34%.
The diff coverage is 89.18%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5001      +/-   ##
==========================================
- Coverage   81.75%   81.41%   -0.35%     
==========================================
  Files         569      583      +14     
  Lines       89824    90253     +429     
  Branches    10022    10111      +89     
==========================================
+ Hits        73440    73476      +36     
- Misses      14117    14468     +351     
- Partials     2267     2309      +42
Impacted Files Coverage Δ
statsmodels/stats/tests/test_weightstats.py 98.57% <100%> (+0.04%) ⬆️
statsmodels/stats/tests/test_base.py 100% <100%> (ø)
statsmodels/tsa/tests/test_stattools.py 98.43% <100%> (-0.38%) ⬇️
statsmodels/tsa/_bds.py 77.46% <44.44%> (-5.35%) ⬇️
statsmodels/sandbox/stats/diagnostic.py 58.44% <52.63%> (-0.2%) ⬇️
statsmodels/stats/weightstats.py 90.86% <55.55%> (-1.82%) ⬇️
statsmodels/tsa/stattools.py 84.5% <95.23%> (-3.23%) ⬇️
statsmodels/stats/base.py 80.89% <98.21%> (+27.95%) ⬆️
statsmodels/tsa/kalmanf/kalmanfilter.py 34.82% <0%> (-54.27%) ⬇️
statsmodels/tools/linalg.py 70.21% <0%> (-16.89%) ⬇️
... and 243 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5b12177...817fab0. Read the comment docs.

@josef-pkt

Copy link
Copy Markdown
Member

This should be merged early in 0.11 release cycle, so we can add more usage of it in existing hypothesis tests and see how well it works.

@N-Wouda

N-Wouda commented Dec 6, 2018

Copy link
Copy Markdown
Contributor Author

@josef-pkt I agree, but before then I'd like to continue working on it for a bit - there's some additional ideas I want to test, but I have not yet had the time to implement (including the optional unpacking for backwards compatibility that I'd like to test some more and implement in more places).

I should be able to make the time during my upcoming Christmas break.

@josef-pkt

Copy link
Copy Markdown
Member

Thanks,
I will not have much time until after Christmas break.
One of my targets for 0.11 is to continue with diagnostic and specification tests and get them better organized. This and open PRs for hypothesis tests for proportions and rates would make good use cases for this.

@N-Wouda

N-Wouda commented May 13, 2019

Copy link
Copy Markdown
Contributor Author

I recently realised I never did quite finish this. I have some time over the summer, and hope to continue this then (might refactor some of this as I have a couple of new ideas on how to proceed).

One thing I'd like to do is make the return of a test object non-optional. Code that currently depends on the return of a tuple may still be supported by having the test object unpack itself if requested, by say making the test object inherit from abc.Sequence. Upon such request we can then raise a deprecation warning that these tests now return proper test objects, and code should be updated to support this.

@josef-pkt josef-pkt modified the milestones: 0.11, josef Oct 5, 2019
@josef-pkt josef-pkt mentioned this pull request Oct 5, 2019
@N-Wouda N-Wouda closed this by deleting the head repository Nov 27, 2022
@josef-pkt

Copy link
Copy Markdown
Member

FYI: the default class for hypothesis test and similar returns is now a HolderTuple class or a subclass of it with additional methods
It has mainly tuple unpacking and repr.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants