<p dir="auto">This may be trivial problem, but there seems to be inconsistency between variance and covariance</p>
<p dir="auto">import numpy as np<br>
x = [1,2,3,4,5,6,7]<br>
np.var(x)</p>
<p dir="auto">produces 4.0, but then</p>
<p dir="auto">np.cov(x,x)</p>
<p dir="auto">produces</p>
<p dir="auto">array([[4.66666667, 4.66666667],<br>
[4.66666667, 4.66666667]])</p>
<p dir="auto">Of course, mathematically var(x) = cov(x,x). The difference here is clearly because in np.var the sum seems to be divided by n while in np.cov the sum seems to be divided by n - 1. For a naive user without paying attention to the detail, this inconsistency may cause some problem.</p>
<p dir="auto">cov() uses a bias of 1 by default.<br>
var() uses a bias of 0 by default.</p>
<p dir="auto">Such that</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np

x = np.random.rand(100)

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

if np.isclose(np.cov([x,x],ddof=0)[0,0], np.var(x,ddof=0))
    print(&quot;Consistent.&quot;)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">rand</span>(<span class="pl-c1">100</span>)

<span class="pl-k">if</span> <span class="pl-s1">np</span>.<span class="pl-en">isclose</span>(<span class="pl-s1">np</span>.<span class="pl-en">cov</span>([<span class="pl-s1">x</span>,<span class="pl-s1">x</span>])[<span class="pl-c1">0</span>,<span class="pl-c1">0</span>], <span class="pl-s1">np</span>.<span class="pl-en">var</span>(<span class="pl-s1">x</span>)):
    <span class="pl-en">print</span>(<span class="pl-s">"Consistent by default."</span>)

<span class="pl-k">if</span> <span class="pl-s1">np</span>.<span class="pl-en">isclose</span>(<span class="pl-s1">np</span>.<span class="pl-en">cov</span>([<span class="pl-s1">x</span>,<span class="pl-s1">x</span>],<span class="pl-s1">ddof</span><span class="pl-c1">=</span><span class="pl-c1">0</span>)[<span class="pl-c1">0</span>,<span class="pl-c1">0</span>], <span class="pl-s1">np</span>.<span class="pl-en">var</span>(<span class="pl-s1">x</span>,<span class="pl-s1">ddof</span><span class="pl-c1">=</span><span class="pl-c1">0</span>))
    <span class="pl-s1">print</span>(<span class="pl-s">"Consistent."</span>)</pre></div>
<p dir="auto">will only print the second line.</p>