Skip to content

Numerical stability #4694

Closed
Closed
@Lysovenko

Description

@Lysovenko

It is no numerical stability in the NumPy (version1.6.2 but perhaps also in future fersions ).

>>> d=array([ 253.,  253.,  253.,  253.,  253.,  253.,  252.,  253.,  252.,
        252.,  253.,  253.,  252.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  252.,  253.,  253.,  252.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  254.,  253.,  253.,  253.,  252.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  252.,  251.,  254.,  254.,  254.,
        252.,  252.,  253.,  253.,  254.,  253.,  253.,  253.,  254.,
        253.,  252.,  253.,  254.,  253.,  252.,  253.,  253.,  253.,
        253.,  253.,  253.,  252.,  253.,  253.,  253.,  252.,  253.,
        254.,  252.,  252.,  253.,  253.,  253.,  253.,  253.,  252.,
        253.,  253.,  253.,  252.,  253.,  253.,  253.,  252.,  253.,
        253.,  253.,  253.,  253.,  253.,  254.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  252.,  253.,
        253.,  253.,  252.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  252.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  252.,  253.,  253.,  253.,  253.,  253.,  253.,  254.,
        253.,  253.,  253.,  253.,  253.,  254.,  253.,  253.,  253.,
        253.,  253.,  253.,  252.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        254.,  253.,  253.,  253.,  253.,  254.,  253.,  253.,  253.,
        253.,  254.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  254.,  254.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        254.,  254.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  254.,  254.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  254.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        253.,  253.,  253.,  253.,  253.,  254.,  253.,  253.,  253.,
        253.,  252.,  252.,  253.,  253.,  253.,  253.,  254.,  253.,
        253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,  253.,
        252.,  253.,  253.,  253.,  253.,  252.,  253.,  253.,  253.,
        253.,  252.,  253.,  253.,  252.,  252.,  252.,  252.,  252.,
        253.,  252.,  253.,  252.,  253.,  253.,  252.,  253.,  253.,
        252.,  252.,  252.,  252.,  252.,  253.,  253.,  253.,  252.,
        252.,  251.,  252.,  252.,  252.,  252.,  252.,  252.,  253.,
        252.,  252.,  252.,  252.,  252.,  252.,  253.,  253.,  253.,
        252.,  252.,  252.,  253.,  253.,  252.], dtype=float32)
>>> d.sum() / len(d) == d.mean()
True
# it is wrong: when used huge number of numbers last digits can be lost
>>> (d ** 2).mean() - d.mean() ** 2
-0.072070897236699238
# unreal bullshit happens: dispersion can't be negative
>>> x = 0.
>>> for i, v in enumerate(d, 1):
...  x += (v - x) / i
... 
>>> x
252.90442890442881
>>> d.mean()
252.9044289044289
'# x is the numerically stable d.mean()
>>> y = 0.
>>> for i, v in enumerate(d ** 2, 1):
...  y += (v - y) / i
... 
>>> y
63960.853146853144
>>> (d ** 2).mean()
63960.578088578091
>>> y - x ** 2
0.20298737785924459
>>> d.std() ** 2
0.20298681725988854
# at least d.std() have some stability

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions