Skip to content

[Repo Assist] Add Stats.movingMedian and Stats.expandingMedian#661

Merged
dsyme merged 2 commits intomasterfrom
repo-assist/improve-moving-expanding-median-e038a30dda0f6d4b
Mar 19, 2026
Merged

[Repo Assist] Add Stats.movingMedian and Stats.expandingMedian#661
dsyme merged 2 commits intomasterfrom
repo-assist/improve-moving-expanding-median-e038a30dda0f6d4b

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 Repo Assist — filling a gap in the Stats moving/expanding window API.

Summary

The Stats module provides a comprehensive set of moving and expanding window statistics — count, sum, mean, variance, stdDev, skew, kurt, min, max — but was missing median. This PR adds:

  • Stats.movingMedian : int -> Series<'K,'V> -> Series<'K,float> — median over a rolling window
  • Stats.expandingMedian : Series<'K,'V> -> Series<'K,float> — cumulative median over an expanding window

Both functions follow the same conventions as the existing moving/expanding stats functions: they handle missing values, accept any numeric value type (via toFloat conversion), and skip non-numeric/NaN values within the window.

Implementation

A circular-buffer approach: for each window position the non-missing values are collected and sorted, and the median is computed. This is O(n·k log k) per series where k is the window size — straightforward and correct for typical use cases. No new dependencies are required.

For movingMedian, the first size-1 elements are Missing (incomplete window), consistent with movingMean, movingVariance, etc.

For expandingMedian, all positions are present (using 1-element through full window), consistent with expandingMean, expandingMin, etc.

Also removes a stale // TODO: still to do, possibly: median, percentile comment (both were already implemented).

Tests

Four new tests are added to tests/Deedle.Tests/Stats.fs, following the same pattern as existing moving/expanding tests:

  • Moving median works — sum of windowed medians matches oracle values for series with NaN, float, and int
  • Moving median - first size-1 values are missing — confirms missing value count for incomplete window head
  • Expanding median works — sum of cumulative medians matches oracle values

Test Status

✅ All 68 Stats tests pass (dotnet test tests/Deedle.Tests/Deedle.Tests.fsproj --filter "FullyQualifiedName~Stats")
dotnet build src/Deedle/Deedle.fsproj -c Release — 0 errors, 4 pre-existing warnings

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@30f2254f2a7a944da1224df45d181a3f8faefd0d

The Stats module covers all moving/expanding statistics (count, sum, mean,
variance, stdDev, skew, kurt, min, max) but was missing median. This adds
Stats.movingMedian and Stats.expandingMedian to fill the gap.

Implementation uses a simple circular-buffer O(n*k log k) approach: for each
window position the window contents are sorted and the median is computed.
This is straightforward and correct for typical window sizes.

Also removes a stale TODO comment ('still to do: median, percentile') since
both median and quantile/percentile are already implemented.

Co-authored-by: Copilot <[email protected]>
@dsyme dsyme marked this pull request as ready for review March 19, 2026 01:12
@dsyme dsyme merged commit 7d30d27 into master Mar 19, 2026
2 checks passed
@dsyme dsyme deleted the repo-assist/improve-moving-expanding-median-e038a30dda0f6d4b branch March 19, 2026 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant