Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
twolodzko mannequin opened this issue Oct 15, 2019 · 12 comments
Closed

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

twolodzko mannequin opened this issue Oct 15, 2019 · 12 comments
Assignees
Labels
3.9 stdlib type-feature

Comments

@twolodzko
Copy link
Mannequin

@twolodzko twolodzko mannequin commented Oct 15, 2019

BPO 38490
Nosy @tim-one, @rhettinger, @taleinat, @stevendaprano, @csabella, @twolodzko
PRs
  • #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:

    assignee = 'https://github.com/stevendaprano'
    closed_at = <Date 2021-04-25.11:47:06.058>
    created_at = <Date 2019-10-15.21:40:09.493>
    labels = ['type-feature', 'library', '3.9']
    title = "statistics: add covariance, Pearson's correlation, and simple linear regression"
    updated_at = <Date 2021-04-25.11:47:06.057>
    user = 'https://github.com/twolodzko'

    bugs.python.org fields:

    activity = <Date 2021-04-25.11:47:06.057>
    actor = 'taleinat'
    assignee = 'steven.daprano'
    closed = True
    closed_date = <Date 2021-04-25.11:47:06.058>
    closer = 'taleinat'
    components = ['Library (Lib)']
    creation = <Date 2019-10-15.21:40:09.493>
    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']

    @twolodzko
    Copy link
    Mannequin Author

    @twolodzko twolodzko mannequin commented Oct 15, 2019

    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

    @twolodzko twolodzko mannequin added 3.8 3.9 type-feature labels Oct 15, 2019
    @rhettinger
    Copy link
    Contributor

    @rhettinger rhettinger commented Oct 16, 2019

    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.

    @rhettinger rhettinger added stdlib and removed 3.8 labels Oct 16, 2019
    @twolodzko
    Copy link
    Mannequin Author

    @twolodzko twolodzko mannequin commented Oct 16, 2019

    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 (
    #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
    Copy link
    Member

    @stevendaprano stevendaprano commented Oct 16, 2019

    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.

    @twolodzko
    Copy link
    Mannequin Author

    @twolodzko twolodzko mannequin commented Oct 16, 2019

    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\>


    @twolodzko
    Copy link
    Mannequin Author

    @twolodzko twolodzko mannequin commented Oct 17, 2019

    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
    Copy link
    Member

    @tim-one tim-one commented Oct 18, 2019

    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.

    @twolodzko twolodzko mannequin changed the title statistics: add covariance and Pearson's correlation statistics: add covariance, Pearson's correlation, and simple linear regression Oct 22, 2019
    @csabella
    Copy link
    Contributor

    @csabella csabella commented May 22, 2020

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

    @twolodzko
    Copy link
    Mannequin Author

    @twolodzko twolodzko mannequin commented Jul 11, 2020

    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
    Copy link
    Contributor

    @taleinat taleinat commented Oct 5, 2020

    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
    Copy link
    Contributor

    @taleinat taleinat commented Apr 25, 2021

    New changeset 09aa6f9 by Tymoteusz Wołodźko in branch 'master':
    bpo-38490: statistics: Add covariance, Pearson's correlation, and simple linear regression (bpo-16813)
    09aa6f9

    @taleinat
    Copy link
    Contributor

    @taleinat taleinat commented Apr 25, 2021

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

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 stdlib type-feature
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants