[Numpy-discussion] Combining covariance and correlation coefficient into one numpy.cov call

Mathew S. Madhavacheril mathewsyriac at gmail.com
Wed Oct 26 14:26:32 EDT 2016


On Wed, Oct 26, 2016 at 2:13 PM, Stephan Hoyer <shoyer at gmail.com> wrote:

> On Wed, Oct 26, 2016 at 11:03 AM, Mathew S. Madhavacheril <
> mathewsyriac at gmail.com> wrote:
>
>> On Wed, Oct 26, 2016 at 1:46 PM, Stephan Hoyer <shoyer at gmail.com> wrote:
>>
>>> I wonder if the goals of this addition could be achieved by simply
>>> adding an optional `cov` argument
>>>
>> to np.corr, which would provide a pre-computed covariance.
>>>
>>
>> That's a fair suggestion which I'm happy to switch to. This eliminates
>> the need for two new functions.
>> I'll add an optional `cov = False` argument to numpy.corrcoef that
>> returns a tuple (corr, cov) instead.
>>
>>
>>>
>>> Either way, `covcorr` feels like a helper function that could exist in
>>> user code rather than numpy proper.
>>>
>>
>> The user would have to re-implement the part that converts the covariance
>> matrix to a correlation
>> coefficient. I made this PR to avoid that code duplication.
>>
>
> With the API I was envisioning (or even your proposed API, for that
> matter), this function would only be a few lines, e.g.,
>
> def covcorr(x):
>     cov = np.cov(x)
>     corr = np.corrcoef(x, cov=cov)
>     return (cov, corr)
>
> Generally, functions this short should be provided as recipes (if at all)
> rather than be added to numpy proper, unless the need for them is extremely
> common.
>

Ah, I see what you were suggesting now. I agree that a function like
covcorr need not be provided
by numpy itself, but it would be tremendously useful if a pre-computed
covariance could
be provided to np.corrcoef. I can update this PR to just add `cov = None`
to numpy.corrcoef and
do an `if cov is not None` before calculating the covariance. Note however
that in the case
that `cov` is specified for np.corrcoef, the non-optional `x` argument is
redundant.



>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20161026/176dad9a/attachment.html>


More information about the NumPy-Discussion mailing list