feat(analytics): rolling beta & market-factor exposure report (#352) - #417
Merged
Abidoyesimze merged 1 commit intoSep 5, 2026
Merged
Conversation
|
@BernardOnuh Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #352
Summary
Connects the previously-unfed
betainmetrics.tsto the DeFi-yield "market factor": a rolling beta & market-factor exposure report that measures how much of a portfolio's yield movement is explained by the canonical market factor versus idiosyncratic protocol selection, computed on a rolling window so exposure can be seen changing over time.What changed
src/analytics/benchmark.ts(new) — the single canonical definition of "the market":buildMarketFactorSeriesproduces a daily benchmark return series on the aligned grid. Equal-weighted by default, with a pluggable TVL-weighted alternative that falls back to equal (flagged) when no TVL is available. Reusesbacktest.buildDailyRateSeriesforward-fill so it stays index-aligned with every other series.src/analytics/attribution.ts— refactored to importbuildMarketFactorSeriesinstead of re-deriving the market inline (one definition, not two). A golden test pins attribution's benchmark return so a future benchmark change can never silently alter an attribution number.src/analytics/factorExposure.ts(new) — pure, zero-I/O core:rollingBeta(per-window OLS →{ beta, alpha, rSquared, sampleCount },nullon under-sample/zero-variance) +factorDecomposition(full-window{ beta, alpha(annualized), rSquared, idiosyncraticVolShare }). Never NaN/Infinity.src/analytics/factorExposureService.ts(new) — DB glue: reads the user'sYieldSnapshotvalue buckets + the benchmark universe'sProtocolRatehistory, intersects both on a shared daily grid (never zero-fill), caps retention at 90d, returns a deterministicinputHash.src/routes/analytics.ts—GET /api/v1/analytics/factor-exposure?window=&rollingWindow=&weighting=(owner-scoped viareq.auth.userId). EnforcesrollingWindow < window(400 otherwise); fewer than 2 rolling windows returns the summary only with a caveat; every response ships the fixed yield-co-movement caveat.docs/PERFORMANCE_ATTRIBUTION.md;docs/openapi.yamlpath (+ validated with@redocly/cli).Acceptance criteria coverage
buildMarketFactorSeries; attribution imports it with a golden testrollingBetareturns per-window{ beta, alpha, rSquared, sampleCount },nullon under-sample/zero-variancefactorDecompositionreturns full-window beta/alpha/R²/idiosyncratic-vol-shareGET /analytics/factor-exposureowner-scoped, retention-bounded,rollingWindow < windowenforced, mandatory caveatinputHashVerification
npm run typecheck,npm run lint,npm run format:check,npm run validate:spec, and the full suite (124 suites / 1672 tests) all pass.