Functional Weave
Code in Rust

stats.moving-average@2.0.0

vectors.json

3,112 bytes · view raw

[
  { "name": "simple average of three over 1..5", "args": [[1, 2, 3, 4, 5], 3, "simple", 2], "expect": [2, 3, 4] },
  { "name": "exponential over 1..5 with window 3 (alpha 0.5)", "args": [[1, 2, 3, 4, 5], 3, "exponential", 2], "expect": [2, 3, 4] },
  { "name": "exponential is seeded with the first window's mean, not the first value", "args": [[2, 4, 6, 8], 3, "exponential", 4], "expect": [4, 6] },
  { "name": "exponential with window 4 (alpha 0.4) reacts to a jump", "args": [[1, 2, 3, 4, 10], 4, "exponential", 2], "expect": [2.5, 5.5] },
  { "name": "exponential with window 2 (alpha 2/3)", "args": [[10, 20, 30, 40], 2, "exponential", 4], "expect": [15, 25, 35] },
  { "name": "exponential with window 1 is the series itself", "args": [[1, 5, 3], 1, "exponential", 2], "expect": [1, 5, 3] },
  { "name": "simple with window 1 rounds each value", "args": [[1.26, -0.5], 1, "simple", 1], "expect": [1.3, -0.5] },
  { "name": "a window as long as the series gives one value", "args": [[3, 5], 2, "simple", 0], "expect": [4] },
  { "name": "a series shorter than the window gives nothing", "args": [[3, 5], 3, "simple", 2], "expect": [] },
  { "name": "an empty series gives nothing", "args": [[], 2, "exponential", 2], "expect": [] },
  { "name": "a half rounds away from zero", "args": [[0, 1], 2, "simple", 0], "expect": [1] },
  { "name": "a negative half rounds away from zero", "args": [[-1, 0], 2, "simple", 0], "expect": [-1] },
  { "name": "float drift is rounded away: (0.1 + 0.2) / 2 is 0.15", "args": [[0.1, 0.2], 2, "simple", 6], "expect": [0.15] },
  { "name": "5/3 to two places", "args": [[1, 2, 2], 3, "simple", 2], "expect": [1.67] },
  { "name": "1.005 is stored just below 1.005, so it rounds to 1.00", "args": [[1.005], 1, "simple", 2], "expect": [1] },
  {"name": "2.67 and 2.68 average to 2.675, stored as 2.67499999..., so it rounds to 2.67 (1.x gave 2.68)", "args": [[2.67, 2.68], 2, "simple", 2], "expect": [2.67]},
  {"name": "the same below-the-tie average of negatives rounds to -2.67 (1.x gave -2.68)", "args": [[-2.67, -2.68], 2, "simple", 2], "expect": [-2.67]},
  {"name": "exponential outputs are rounded on the exact double too: 2.675 gives 2.67 (1.x gave 2.68)", "args": [[0, 2.675], 1, "exponential", 2], "expect": [0, 2.67]},
  {"name": "1.45 is stored just below the tie, so it rounds to 1.4 at one place (1.x gave 1.5)", "args": [[1.45], 1, "simple", 1], "expect": [1.4]},
  {"name": "8.345 is stored just above the tie, so it still rounds up to 8.35", "args": [[8.345], 1, "simple", 2], "expect": [8.35]},
  { "name": "a window of 0 is an error", "args": [[1, 2], 0, "simple", 2], "expectError": "window must be a whole number of 1 or greater" },
  { "name": "an unknown kind is an error", "args": [[1, 2], 2, "weighted", 2], "expectError": "unknown moving average kind \"weighted\"" },
  { "name": "decimals above 12 is an error", "args": [[1, 2], 2, "simple", 13], "expectError": "decimals must be a whole number from 0 to 12" },
  { "name": "a non-number value is an error", "args": [[1, null], 1, "simple", 2], "expectError": "values must be finite numbers" }
]