classification
Title: statistics: add covariance, Pearson's correlation, and simple linear regression
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: steven.daprano Nosy List: cheryl.sabella, rhettinger, steven.daprano, tim.peters, twolodzko
Priority: normal Keywords: patch

Created on 2019-10-15 21:40 by twolodzko, last changed 2020-07-11 17:26 by twolodzko.

Pull Requests
URL Status Linked Edit
PR 16813 open python-dev, 2019-10-15 21:46
Messages (9)
msg354754 - (view) Author: Tymek Wołodźko (twolodzko) * Date: 2019-10-15 21:40
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
msg354763 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-10-16 00:50
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.
msg354775 - (view) Author: Tymek Wołodźko (twolodzko) * Date: 2019-10-16 06:56
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>
> _______________________________________
>
msg354786 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-10-16 08:47
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.
msg354790 - (view) Author: Tymek Wołodźko (twolodzko) * Date: 2019-10-16 09:25
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>
> _______________________________________
>
msg354842 - (view) Author: Tymek Wołodźko (twolodzko) * Date: 2019-10-17 14:13
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>
> _______________________________________
>
msg354863 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2019-10-18 05:05
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.
msg369647 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2020-05-22 20:45
@steven.daprano and @tim.peters, please take a look at the PR as it is just waiting on your approval.  Thanks!
msg373529 - (view) Author: Tymek Wołodźko (twolodzko) * Date: 2020-07-11 17:26
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>
> _______________________________________
>
History
Date User Action Args
2020-07-11 17:26:07twolodzkosetmessages: + msg373529
2020-05-22 20:45:26cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg369647
2019-10-22 08:23:32twolodzkosettitle: statistics: add covariance and Pearson's correlation -> statistics: add covariance, Pearson's correlation, and simple linear regression
2019-10-18 05:05:03tim.peterssetnosy: + tim.peters
messages: + msg354863
2019-10-17 14:13:24twolodzkosetmessages: + msg354842
2019-10-16 09:25:16twolodzkosetmessages: + msg354790
2019-10-16 08:47:09steven.dapranosetmessages: + msg354786
2019-10-16 06:56:02twolodzkosetmessages: + msg354775
2019-10-16 05:41:34rhettingersetcomponents: + Library (Lib)
versions: - Python 3.8
2019-10-16 00:50:55rhettingersetassignee: steven.daprano

messages: + msg354763
nosy: + rhettinger, steven.daprano
2019-10-15 21:46:38python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request16364
2019-10-15 21:40:09twolodzkocreate