Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: numpy.std returns wrong result with an identity array #27782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AnonymousPlayer2000 opened this issue Nov 17, 2024 · 3 comments
Closed

BUG: numpy.std returns wrong result with an identity array #27782

AnonymousPlayer2000 opened this issue Nov 17, 2024 · 3 comments
Labels
33 - Question Question about NumPy usage or development

Comments

@AnonymousPlayer2000
Copy link

Describe the issue:

I created a (7,)-shape array with the same number. Then I called numpy.std, and it returned 4 (far from the expected 0).

Reproduce the code example:

import numpy as np
np.std(np.prod(np.full((7, 10), 45, dtype="float64"), axis=1),axis=0)

Error message:

No response

Python and NumPy Versions:

I tried this in two versions:

2.1.0
3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0]
1.26.3
3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]

Runtime Environment:

[{'numpy_version': '2.1.0',
  'python': '3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0]',
  'uname': uname_result(system='Linux', node='1091aa609208', release='6.8.0-48-generic', version='#48~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct  7 11:24:13 UTC 2', machine='x86_64')},
 {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2'],
                      'not_found': ['AVX512F',
                                    'AVX512CD',
                                    'AVX512_KNL',
                                    'AVX512_KNM',
                                    'AVX512_SKX',
                                    'AVX512_CLX',
                                    'AVX512_CNL',
                                    'AVX512_ICL']}},
 {'architecture': 'Haswell',
  'filepath': '/usr/local/lib/python3.10/dist-packages/numpy.libs/libscipy_openblas64_-ff651d7f.so',
  'internal_api': 'openblas',
  'num_threads': 24,
  'prefix': 'libscipy_openblas',
  'threading_layer': 'pthreads',
  'user_api': 'blas',
  'version': '0.3.27'}]
[{'numpy_version': '1.26.3',
  'python': '3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]',
  'uname': uname_result(system='Linux', node='dc5b26f04603', release='6.8.0-48-generic', version='#48~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct  7 11:24:13 UTC 2', machine='x86_64')},
 {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2'],
                      'not_found': ['AVX512F',
                                    'AVX512CD',
                                    'AVX512_KNL',
                                    'AVX512_KNM',
                                    'AVX512_SKX',
                                    'AVX512_CLX',
                                    'AVX512_CNL',
                                    'AVX512_ICL']}},
 {'architecture': 'Prescott',
  'filepath': '/usr/local/lib/python3.10/dist-packages/numpy.libs/libopenblas64_p-r0-0cf96a72.3.23.dev.so',
  'internal_api': 'openblas',
  'num_threads': 24,
  'prefix': 'libopenblas',
  'threading_layer': 'pthreads',
  'user_api': 'blas',
  'version': '0.3.23.dev'}]

Context for the issue:

No response

@rkern
Copy link
Member

rkern commented Nov 17, 2024

You're getting floating point errors. Each value in the array that you are passing to std() is very, very large, 3.40506289e+16. Floating point error on the order of 1e-16 times that value (i.e., around 4.0) accumulates within the calculations. Despite the fact that you've set up a situation where the ideal calculation done with infinite precision real numbers would give you exactly 0, with floating point arithmetic, you can only expect to get an answer near that ideal value to roughly 1e-16 times the magnitude of the inputs (very roughly, within an order of magnitude or two; you'd have to work through the numerical analysis to get better error bounds).

If you reduce the size of the inputs, you can see the error more clearly. For example, if instead I have values of 0.1 instead of ~3.4e+16, we get this:

>>> np.std(np.full((7, 10), 0.1))
np.float64(4.163336342344337e-17)

That is, near 0.1 * 1e-16, similar in relative error to what you are seeing with your inputs.

@rkern rkern closed this as not planned Won't fix, can't repro, duplicate, stale Nov 17, 2024
@rkern rkern added 33 - Question Question about NumPy usage or development and removed 00 - Bug labels Nov 17, 2024
@AnonymousPlayer2000
Copy link
Author

@rkern Thanks!

@WarrenWeckesser
Copy link
Member

FYI: This issue is a duplicate of #9631. The problem arises in the two pass calculation of the variance when the calculated mean of the constant array does not equal the value in the array.

In [65]: x = np.prod(np.full((7, 10), 45, dtype="float64"), axis=1)

In [66]: x
Out[66]: 
array([3.40506289e+16, 3.40506289e+16, 3.40506289e+16, 3.40506289e+16,
       3.40506289e+16, 3.40506289e+16, 3.40506289e+16])

In [67]: x[0]
Out[67]: np.float64(3.4050628916015624e+16)

In [68]: np.mean(x)
Out[68]: np.float64(3.405062891601562e+16)
In [69]: y = np.full((7, 10), 0.1)

In [70]: y[0,0]
Out[70]: np.float64(0.1)

In [71]: np.mean(y)
Out[71]: np.float64(0.09999999999999996)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
33 - Question Question about NumPy usage or development
Projects
None yet
Development

No branches or pull requests

3 participants