question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

numpy.cov() and numpy.var() default bias are inconsistent (numpy 1.9.2)

See original GitHub issue

cov() uses a bias of 1 by default. var() uses a bias of 0 by default.

Such that

import numpy as np

x = np.random.rand(100)

if np.isclose(np.cov([x,x])[0,0], np.var(x)):
    print("Consistent by default.")

if np.isclose(np.cov([x,x],ddof=0)[0,0], np.var(x,ddof=0))
    print("Consistent.")

will only print the second line.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
AllenDowneycommented, Sep 23, 2020

@rgommers Yeah, “never break valid code” is a pretty good rule.

How about a warning if you call cov without bias or ddof, and then never change the behavior?

0reactions
rgommerscommented, Sep 23, 2020

How about a warning if you call cov without bias or ddof, and then never change the behavior?

That does seem like a reasonable thing to do. Better than deprecating cov. Having to add ddof=0/1 is a slight annoyance but makes the code more understandable, so I like the idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allen Downey on Twitter: "I ran into a NumPy gotcha today: np ...
numpy.cov() and numpy.var() default bias are inconsistent (numpy 1.9.2) · Issue #5835 · numpy/numpy. cov() uses a bias of 1 by default.
Read more >
numpy.cov — NumPy v1.24 Manual
If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column ......
Read more >
Var(x) and cov(x, x) don't give the same result in numpy
You must use z=cov(x,bias=1) in order to normalize by N ,because var is also norm by N (according to this.
Read more >
Release Notes — NumPy v1.14 Manual
In the future, calling .item() on arrays or scalars of np.void datatype will return a bytes object instead of a buffer or int...
Read more >
Release Notes — NumPy v1.10 Manual
There was inconsistent behavior between x.ravel() and np.ravel(x), ... If this variable is set to 1, then numpy will consider more arrays to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found