<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/numpy/ticket/435" rel="nofollow">http://projects.scipy.org/numpy/ticket/435</a> on 2007-01-24 by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/chanley/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/chanley">@chanley</a>, assigned to unknown.</em></p>
<p dir="auto">The accumulator used in the mean algorithm should not be single precision by default.  This default can cause unexpected results.  Please see the following example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [5]: a.dtype
Out[5]: dtype('&gt;f4')

In [6]: print a
[[ 132.  132.  132. ...,  132.  132.  132.]
 [ 132.  132.  132. ...,  132.  132.  132.]
 [ 132.  132.  132. ...,  132.  132.  132.]
 ..., 
 [ 132.  132.  132. ...,  132.  132.  132.]
 [ 132.  132.  132. ...,  132.  132.  132.]
 [ 132.  132.  132. ...,  132.  132.  132.]]

In [7]: a.min()
Out[7]: 132.0

In [8]: a.max()
Out[8]: 389.0

In [9]: a.mean()
Out[9]: 129.742439153"><pre class="notranslate"><code class="notranslate">In [5]: a.dtype
Out[5]: dtype('&gt;f4')

In [6]: print a
[[ 132.  132.  132. ...,  132.  132.  132.]
 [ 132.  132.  132. ...,  132.  132.  132.]
 [ 132.  132.  132. ...,  132.  132.  132.]
 ..., 
 [ 132.  132.  132. ...,  132.  132.  132.]
 [ 132.  132.  132. ...,  132.  132.  132.]
 [ 132.  132.  132. ...,  132.  132.  132.]]

In [7]: a.min()
Out[7]: 132.0

In [8]: a.max()
Out[8]: 389.0

In [9]: a.mean()
Out[9]: 129.742439153
</code></pre></div>
<p dir="auto">However, if you recast the array as float64 you get the correct result:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [11]: a.astype(numpy.float64).mean()
Out[11]: 132.062805059"><pre class="notranslate"><code class="notranslate">In [11]: a.astype(numpy.float64).mean()
Out[11]: 132.062805059
</code></pre></div>
<p dir="auto">I believe that double precision would be a more appropriate default type for the accumulator.</p>
<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/numpy/ticket/465" rel="nofollow">http://projects.scipy.org/numpy/ticket/465</a> on 2007-03-07 by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/chanley/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/chanley">@chanley</a>, assigned to unknown.</em></p>
<p dir="auto">The default data type for the accumulator variable in the mean method should be double precision.  The problem can best be illustrated with the following example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Python 2.4.3 (#2, Dec  7 2006, 11:01:45) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import numpy as n
&gt;&gt;&gt; n.__version__
'1.0.2.dev3571'
&gt;&gt;&gt; a = n.ones((1000,1000),dtype=n.float32)*132.00005
&gt;&gt;&gt; print a
[[ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]
 [ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]
 [ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]
 ..., 
 [ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]
 [ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]
 [ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]]
&gt;&gt;&gt; a.min()
132.000045776
&gt;&gt;&gt; a.max()
132.000045776
&gt;&gt;&gt; a.mean()
133.96639999999999"><pre class="notranslate"><code class="notranslate">Python 2.4.3 (#2, Dec  7 2006, 11:01:45) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import numpy as n
&gt;&gt;&gt; n.__version__
'1.0.2.dev3571'
&gt;&gt;&gt; a = n.ones((1000,1000),dtype=n.float32)*132.00005
&gt;&gt;&gt; print a
[[ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]
 [ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]
 [ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]
 ..., 
 [ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]
 [ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]
 [ 132.00004578  132.00004578  132.00004578 ...,  132.00004578
   132.00004578  132.00004578]]
&gt;&gt;&gt; a.min()
132.000045776
&gt;&gt;&gt; a.max()
132.000045776
&gt;&gt;&gt; a.mean()
133.96639999999999
</code></pre></div>
<p dir="auto">Having the mean be greater than the maximum is a tad odd.</p>
<p dir="auto">The calculation of the mean is occurring with a single precision accumulator variable.  A user can force a double precision calculation with the following command and receive a correct result:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; a.mean(dtype=n.float64)
132.00004577636719
&gt;&gt;&gt; "><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; a.mean(dtype=n.float64)
132.00004577636719
&gt;&gt;&gt; 
</code></pre></div>
<p dir="auto">However, this is not going to be obvious to the casual user and will appear to be an error.</p>
<p dir="auto">I realize that one reason for not doing all calculations as double precision is performance.  However, it is probably better to always receive the correct answer than to quickly arrive at the wrong one.</p>
<p dir="auto">The current default behavior needs to be changed.  All calculations should be done in double precision.  If performance is needed the "expert user" can go back and start setting data types after having shown that their application arrives at a correct result.</p>
<p dir="auto">Not having to worry about overflow problems in the accumulator variable would also make numpy consistent with numarray's behavior.</p>