Skip to content

rolling().sum() is numerically unstable #7128

@ilemhadri

Description

@ilemhadri

What happened?

On an input array like

array([0.        , 0.        , 0.        , 0.57392103, 0.57392103,
       0.57392103, 0.57392103, 0.57392103, 0.57392103, 0.57392103,
       0.57392103, 0.57392103, 0.        , 0.57392103, 0.57392103,
       0.57392103, 0.57392103, 0.57392103, 0.57392103, 2.29551022,
       2.29551022, 2.29551022, 2.29551022, 2.29551022, 2.29551022,
       2.29551022, 2.29551022, 2.29551022, 2.29551022, 2.29551022,
       2.29551022, 2.29551022, 2.29551022, 0.57383408, 0.57383408,
       0.57383408, 0.57383408, 0.57383408, 0.57383408, 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ])

that has all positive values, and then zeros, computing the rolling sum (or mean) leads to negative values

mydata.rolling(t=3, min_periods=1).sum().values[0,0,:]
array([-3.33066907e-14, -3.33066907e-14, -3.33066907e-14,  5.73921029e-01,                                                                                                                                                                                                                     
        2.14784206e+00,  1.72176309e+00,  1.72176309e+00,  1.72176309e+00,                                                                                                                                                                                                                     
        1.72176309e+00,  1.72176309e+00,  1.72176309e+00,  1.72176309e+00,                                                                                                                                                                                                                     
        1.14784206e+00,  1.14784206e+00,  1.14784206e+00,  1.72176309e+00,                                                                                                                                                                                                                     
        1.72176309e+00,  1.72176309e+00,  1.72176309e+00,  3.44335228e+00,
        5.16494146e+00,  6.88653065e+00,  6.88653065e+00,  6.88653065e+00,
        6.88653065e+00,  6.88653065e+00,  6.88653065e+00,  6.88653065e+00,
        6.88653065e+00,  6.88653065e+00,  6.88653065e+00,  6.88653065e+00,
        6.88653065e+00,  5.16485452e+00,  3.44317838e+00,  1.72150224e+00,
        1.72150224e+00,  1.72150224e+00,  1.72150224e+00,  1.14766816e+00,
        5.73834081e-01, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14,
       -3.35287353e-14, -3.35287353e-14, -3.35287353e-14, -3.35287353e-14])

Both arrays have dtype = float64.

The issue aggravates as the rolling window increases.

What did you expect to happen?

the rolling calculation could be more numerically precise by keeping track for instance of the Kahan compensation term.
https://en.wikipedia.org/wiki/Kahan_summation_algorithm

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • [X ] New issue — a search of GitHub Issues suggests this is not a duplicate.

Environment

this is reproducible across xarray versions, but mine is 2022.09.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions