python / cpython

The Python programming language
https://www.python.org
Other
63.08k stars 30.21k forks source link

statistics: add covariance, Pearson's correlation, and simple linear regression #82671

Closed 024652b8-4113-4039-883d-d74acef92980 closed 3 years ago

024652b8-4113-4039-883d-d74acef92980 commented 5 years ago
BPO 38490
Nosy @tim-one, @rhettinger, @taleinat, @stevendaprano, @csabella, @twolodzko
PRs
  • python/cpython#16813
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = 'https://github.com/stevendaprano' closed_at = created_at = labels = ['type-feature', 'library', '3.9'] title = "statistics: add covariance, Pearson's correlation, and simple linear regression" updated_at = user = 'https://github.com/twolodzko' ``` bugs.python.org fields: ```python activity = actor = 'taleinat' assignee = 'steven.daprano' closed = True closed_date = closer = 'taleinat' components = ['Library (Lib)'] creation = creator = 'twolodzko' dependencies = [] files = [] hgrepos = [] issue_num = 38490 keywords = ['patch'] message_count = 12.0 messages = ['354754', '354763', '354775', '354786', '354790', '354842', '354863', '369647', '373529', '378061', '391856', '391857'] nosy_count = 6.0 nosy_names = ['tim.peters', 'rhettinger', 'taleinat', 'steven.daprano', 'cheryl.sabella', 'twolodzko'] pr_nums = ['16813'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue38490' versions = ['Python 3.9'] ```

    024652b8-4113-4039-883d-d74acef92980 commented 5 years ago

    Covariance and Pearson's correlation are one of the most basic bivariate statistics.

    https://en.wikipedia.org/wiki/Covariance https://en.wikipedia.org/wiki/Pearson_correlation_coefficient

    rhettinger commented 5 years ago

    These two functions are right on the boundary edge of what the statistics module is trying to do, """The module is not intended to be a competitor to third-party libraries such as NumPy, SciPy, or proprietary full-featured statistics packages aimed at professional statisticians such as Minitab, SAS and Matlab. It is aimed at the level of graphing and scientific calculators."""

    It is debatable which side of the boundary they belong to. MS Excel has both of these functions, but it isn't a given that a scientific calculator would have them.

    Also, the current functions require only high-school level knowledge, but these two require more skill to learn, use, and interpret.

    024652b8-4113-4039-883d-d74acef92980 commented 5 years ago

    I think I see your point, that the module is intended for the most basic features, but I would argue that correlation is one of such "most basic" statistics.

    Correlation and covariance can be thought as equivalents for standard deviation and variance, but when we are talking about pairs of variables. Moreover it is probably used much more often then things like geometric or harmonic mean, that are implemented in the package. Covariance is probably more esoteric for most non-statisticians, but in my PR ( https://github.com/python/cpython/pull/16813 ) I implemented it for completeness and as a helper function for the correlation coefficient.

    TL;DR those are basic statistics. They would make the module much more helpful, since the basic statistics could be calculated using base python w/o external libraries. They are also trivial to implemented (done this in PR) and do not need much more maintenance then the other functionalities in the module.

    Tim

    śr., 16 paź 2019 o 02:50 Raymond Hettinger \report@bugs.python.org\ napisał(a):

    Raymond Hettinger \raymond.hettinger@gmail.com\ added the comment:

    These two functions are right on the boundary edge of what the statistics module is trying to do, """The module is not intended to be a competitor to third-party libraries such as NumPy, SciPy, or proprietary full-featured statistics packages aimed at professional statisticians such as Minitab, SAS and Matlab. It is aimed at the level of graphing and scientific calculators."""

    It is debatable which side of the boundary they belong to. MS Excel has both of these functions, but it isn't a given that a scientific calculator would have them.

    Also, the current functions require only high-school level knowledge, but these two require more skill to learn, use, and interpret.

    ---------- assignee: -> steven.daprano nosy: +rhettinger, steven.daprano


    Python tracker \report@bugs.python.org\ \https://bugs.python.org/issue38490\


    stevendaprano commented 5 years ago

    I can't speak for other countries, but in Australia, secondary school mathematics teaches correlation coefficient and linear regression from Year 11 onwards (typically ages 16 or 17). Covariance is not itself taught, and as far as I can tell neither the TI-83 nor NSpire provides a built-in covariance command.

    On the other hand, other calculators such as the HP-48GX do.

    Oddly, Excel provides the population (not sample) covariance:

    https://support.office.com/en-us/article/COVARIANCE-P-function-6F0E1E6D-956D-4E4B-9943-CFEF0BF9EDFC

    OpenOffice and LibreOffice also provide a covariance function.

    I think that supporting correlation coefficient r and linear regression would be clear wins, from the perspective of secondary school maths. But as far as covariance goes, it would help convince me if you had either:

    024652b8-4113-4039-883d-d74acef92980 commented 5 years ago

    In case there is agreement with Steven, I will add simple linear regression ( https://en.wikipedia.org/wiki/Simple_linear_regression ) in the same PR, since it is just:

    slope = correlation(x, y) * ( stdev(y) / stdev(x) )
    intercept = mean(y) - slope * mean(x)

    As about covariance, I see your points, but why not keeping it "because we can"? It can be useful for some users and the functionality still needs to be implemented to have correlation coefficient.

    On Wed, Oct 16, 2019 at 10:47 AM Steven D'Aprano \report@bugs.python.org\ wrote:

    Steven D'Aprano steve+python@pearwood.info added the comment:

    I can't speak for other countries, but in Australia, secondary school mathematics teaches correlation coefficient and linear regression from Year 11 onwards (typically ages 16 or 17). Covariance is not itself taught, and as far as I can tell neither the TI-83 nor NSpire provides a built-in covariance command.

    On the other hand, other calculators such as the HP-48GX do.

    Oddly, Excel provides the population (not sample) covariance:

    https://support.office.com/en-us/article/COVARIANCE-P-function-6F0E1E6D-956D-4E4B-9943-CFEF0BF9EDFC

    OpenOffice and LibreOffice also provide a covariance function.

    I think that supporting correlation coefficient r and linear regression would be clear wins, from the perspective of secondary school maths. But as far as covariance goes, it would help convince me if you had either:

    • evidence that covariance is taught in secondary schools, or at least first year undergraduate statistics;

    • that it has use-cases beyond "helper for calculating r";

    • or that there is demand for it from people who want covariance but can't, or don't want to, use numpy/scipy.

    ----------


    Python tracker \report@bugs.python.org\ \https://bugs.python.org/issue38490\


    024652b8-4113-4039-883d-d74acef92980 commented 5 years ago

    I expanded my PR to add simple linear regression. I also created documentation for the new functionalities.

    As about covariance, we can simply not expose it to the users, but I'm not convinced that there is any gain in keeping it hidden from the users.

    Tim

    On Wed, Oct 16, 2019 at 11:25 AM Tymek Wołodźko \report@bugs.python.org\ wrote:

    Tymek Wołodźko twolodzko@gmail.com added the comment:

    In case there is agreement with Steven, I will add simple linear regression ( https://en.wikipedia.org/wiki/Simple_linear_regression ) in the same PR, since it is just:

    slope = correlation(x, y) ( stdev(y) / stdev(x) ) intercept = mean(y) - slope mean(x)

    As about covariance, I see your points, but why not keeping it "because we can"? It can be useful for some users and the functionality still needs to be implemented to have correlation coefficient.

    On Wed, Oct 16, 2019 at 10:47 AM Steven D'Aprano report@bugs.python.org wrote:

    Steven D'Aprano steve+python@pearwood.info added the comment:

    I can't speak for other countries, but in Australia, secondary school mathematics teaches correlation coefficient and linear regression from Year 11 onwards (typically ages 16 or 17). Covariance is not itself taught, and as far as I can tell neither the TI-83 nor NSpire provides a built-in covariance command.

    On the other hand, other calculators such as the HP-48GX do.

    Oddly, Excel provides the population (not sample) covariance:

    https://support.office.com/en-us/article/COVARIANCE-P-function-6F0E1E6D-956D-4E4B-9943-CFEF0BF9EDFC

    OpenOffice and LibreOffice also provide a covariance function.

    I think that supporting correlation coefficient r and linear regression would be clear wins, from the perspective of secondary school maths. But as far as covariance goes, it would help convince me if you had either:

    • evidence that covariance is taught in secondary schools, or at least first year undergraduate statistics;

    • that it has use-cases beyond "helper for calculating r";

    • or that there is demand for it from people who want covariance but can't, or don't want to, use numpy/scipy.

    ---------- > > > Python tracker \report@bugs.python.org\ > \https://bugs.python.org/issue38490\ > >

    ----------


    Python tracker \report@bugs.python.org\ \https://bugs.python.org/issue38490\


    tim-one commented 5 years ago

    I'm in favor of adding all of this (covariance, coefficient, linear regression). It's still at the level of elementary statistics, and even taught in watered down "business statistics" classes. It's about the minimum that can be done beyond single-variable stats.

    But I also think this defines the limit of what the core "should" include in this area.

    csabella commented 4 years ago

    @steven.daprano and @tim.peters, please take a look at the PR as it is just waiting on your approval. Thanks!

    024652b8-4113-4039-883d-d74acef92980 commented 4 years ago

    Is there anything more I should do about the PR?

    Sincerely, Tim

    On Fri, May 22, 2020 at 10:45 PM Cheryl Sabella \report@bugs.python.org\ wrote:

    Cheryl Sabella \cheryl.sabella@gmail.com\ added the comment:

    @steven.daprano and @tim.peters, please take a look at the PR as it is just waiting on your approval. Thanks!

    ---------- nosy: +cheryl.sabella


    Python tracker \report@bugs.python.org\ \https://bugs.python.org/issue38490\


    taleinat commented 4 years ago

    Given the discussion here and the state of the attached PR, I intend to merge the PR in several weeks, unless someone has anything else to say.

    taleinat commented 3 years ago

    New changeset 09aa6f914dc313875ff18474770a0a7c13ea8dea by Tymoteusz Wołodźko in branch 'master': bpo-38490: statistics: Add covariance, Pearson's correlation, and simple linear regression (bpo-16813) https://github.com/python/cpython/commit/09aa6f914dc313875ff18474770a0a7c13ea8dea

    taleinat commented 3 years ago

    Thanks for the suggestion, PR, and great heaps of patience and perseverance, Tymek!