Skip to content

feat(fees): add computeMovingAverage for fee trend smoothing - #833

Open
woahwhattheheck wants to merge 2 commits into
Stellar-split:mainfrom
woahwhattheheck:feat/781-fee-moving-average
Open

feat(fees): add computeMovingAverage for fee trend smoothing#833
woahwhattheheck wants to merge 2 commits into
Stellar-split:mainfrom
woahwhattheheck:feat/781-fee-moving-average

Conversation

@woahwhattheheck

Copy link
Copy Markdown

Closes #781.

fees/trend.ts exposed raw samples and percentile estimates but no smoothed
series, so callers had to implement their own smoothing to get a usable
forecasting signal.

Changes

src/fees/trend.ts — adds the exported pure function:

computeMovingAverage(samples: number[], windowSize: number): number[]
  • Returns a Simple Moving Average series the same length as the input, so
    it can be plotted directly against the samples without re-aligning indices.
  • The first windowSize - 1 entries are NaN. A partial average there would
    understate early movement and read as real signal.
  • windowSize must be an integer >= 1; otherwise RangeError, naming the
    value received.
  • Pure: the input array is not mutated.

Two implementation notes

  • Non-integer window sizes are rejected, not just < 1. A fractional
    window is undefined for index arithmetic, and NaN would otherwise sail
    through every comparison and silently produce an all-NaN series. This
    matches the existing toWindowCapacity in the same file, which already
    requires Number.isInteger.
  • Each window is summed directly rather than via a running total. The
    subtract-the-outgoing-value accumulator is the usual O(n) trick, but it
    drifts over a long float series. Windows here are small and bounded, so the
    exact form costs nothing worth having.

A NaN sample propagates only through the windows containing it, and the
series recovers afterwards — covered by a test.

Validation

  • npx vitest run test/feeTrend.movingAverage.test.ts — 16/16 passing.
    Covers padding width, window of 1, length preservation, window larger than
    the series, empty input, input immutability, realistic stroop values, NaN
    propagation, and every rejected windowSize (0, negatives, 2.5, NaN,
    Infinity).
  • npx tsc --noEmit reports 210 errors on this branch and 210 on unmodified
    main
    — identical, with none in src/fees/trend.ts. That baseline is
    pre-existing and unrelated to this change.

trend.ts exposed raw samples and percentiles but no smoothed series. Adds a
pure SMA returning the same length as the input, NaN-padded for the first
windowSize - 1 entries so callers can plot it against the samples without
re-aligning indices.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add moving average calculation for fee trend analysis in fees/trend.ts

1 participant