From 63b149186db3ede6eedfa68ec43e602f9dc4f752 Mon Sep 17 00:00:00 2001 From: xile611 Date: Fri, 18 Sep 2026 11:25:08 +0800 Subject: [PATCH 1/2] fix: preserve LOWESS nearest-neighbor selection --- .../vutils/__tests__/lowess-neighbors.test.ts | 101 ++++++++++++++++++ .../vutils/src/common/regression-lowess.ts | 49 ++++++--- 2 files changed, 138 insertions(+), 12 deletions(-) create mode 100644 packages/vutils/__tests__/lowess-neighbors.test.ts diff --git a/packages/vutils/__tests__/lowess-neighbors.test.ts b/packages/vutils/__tests__/lowess-neighbors.test.ts new file mode 100644 index 0000000..f24a442 --- /dev/null +++ b/packages/vutils/__tests__/lowess-neighbors.test.ts @@ -0,0 +1,101 @@ +import { regressionLowess } from '../src/common/regression-lowess'; + +type Point = { x: number; y: number }; + +// A direct distance-ranked reference, independent of the production window search. +function referencePredict(data: Point[], x0: number, span: number, degree: 0 | 1) { + if (!data.length) { + return 0; + } + const count = Math.min(data.length, Math.max(2, Math.floor(span * data.length))); + const nearest = data + .map((p, index) => ({ ...p, index, distance: Math.abs(p.x - x0) })) + .sort((a, b) => a.distance - b.distance || a.index - b.index) + .slice(0, count); + const radius = nearest[nearest.length - 1].distance; + let sw = 0; + let sx = 0; + let sy = 0; + let sxx = 0; + let sxy = 0; + nearest.forEach(p => { + const u = radius === 0 ? 0 : p.distance / radius; + const w = u >= 1 ? 0 : Math.pow(1 - u * u * u, 3); + sw += w; + sx += w * p.x; + sy += w * p.y; + sxx += w * p.x * p.x; + sxy += w * p.x * p.y; + }); + if (sw === 0) { + return nearest[0].y; + } + if (degree === 0) { + return sy / sw; + } + const meanX = sx / sw; + const meanY = sy / sw; + const denominator = sxx - sx * meanX; + const slope = Math.abs(denominator) < 1e-12 ? 0 : (sxy - sx * meanY) / denominator; + return meanY + slope * (x0 - meanX); +} + +const cases: { name: string; data: Point[] }[] = [ + { + name: 'a dense cluster separated from an outlier', + data: [0, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8].map(x => ({ x, y: x * x })) + }, + { + name: 'clusters with different densities', + data: [0, 0.1, 0.2, 0.3, 3, 10, 20, 20.1, 20.2, 50].map(x => ({ x, y: Math.sin(x) })) + }, + { + name: 'unsorted repeated x values', + data: [2, 1, 10, 1, 1, 7, 1, 10, 50].map((x, i) => ({ x, y: Math.sin(x) + i })) + }, + { + name: 'two points in reverse x order', + data: [ + { x: 2, y: 20 }, + { x: 0, y: 5 } + ] + }, + { name: 'coincident points', data: [1, 2, 3, 4, 5, 6, 7].map(y => ({ x: 1, y })) }, + { name: 'one point', data: [{ x: 4, y: 9 }] }, + { name: 'no points', data: [] } +]; + +describe.each([0, 1] as const)('LOWESS nearest neighbors with degree=%i', degree => { + test.each(cases)('matches distance-ranked neighbors for $name', ({ data }) => { + for (const span of [0.3, 0.6, 1]) { + const model = regressionLowess(data, undefined, undefined, { span, degree, iterations: 0 }); + const bounds = data.length ? [Math.min(...data.map(p => p.x)) - 1, Math.max(...data.map(p => p.x)) + 1] : []; + for (const x0 of [...bounds, 0, 0.15, 1, 2.5, 10.05, 12, 20]) { + expect(Math.abs((model.predict(x0) as number) - referencePredict(data, x0, span, degree))).toBeLessThan(1e-8); + } + const grid = model.evaluateGrid(11); + expect(grid).toHaveLength(data.length ? 11 : 0); + for (const p of grid) { + expect(Math.abs(p.y - referencePredict(data, p.x, span, degree))).toBeLessThan(1e-8); + } + } + }); +}); + +test.each([ + [ + { x: 2, y: 20 }, + { x: 0, y: 5 } + ], + [ + { x: 0, y: 5 }, + { x: 2, y: 20 } + ] +])('uses original input order when equally near points have zero weight: %j, %j', (first, second) => { + expect(regressionLowess([first, second]).predict(1)).toBe(first.y); +}); + +test('keeps the first coincident points when the neighborhood radius is zero', () => { + const data = [1, 2, 3, 4, 5, 6, 7].map(y => ({ x: 1, y })); + expect(regressionLowess(data).predict(1)).toBe(1.5); +}); diff --git a/packages/vutils/src/common/regression-lowess.ts b/packages/vutils/src/common/regression-lowess.ts index 5d3e30f..3ece82d 100644 --- a/packages/vutils/src/common/regression-lowess.ts +++ b/packages/vutils/src/common/regression-lowess.ts @@ -1,6 +1,12 @@ import { visitPoints } from './regression-linear'; import { computeLinearCIComponents, invNorm, stdErrorsAt } from './regression-utils'; +interface LowessPoint { + x: number; + y: number; + index: number; +} + function tricube(u: number) { const uu = Math.abs(u); if (uu >= 1) { @@ -13,13 +19,13 @@ function tricube(u: number) { /** * Stratified sampling to reduce data size while preserving distribution */ -function stratifiedSample(sortedData: { x: number; y: number }[], maxSamples: number) { +function stratifiedSample(sortedData: LowessPoint[], maxSamples: number) { const n = sortedData.length; if (n <= maxSamples) { return sortedData; } - const sampled: { x: number; y: number }[] = []; + const sampled: LowessPoint[] = []; // More aggressive sampling - use exact step size const step = n / maxSamples; @@ -54,9 +60,9 @@ export function regressionLowess( const maxSamples = options.maxSamples || 1000; // Collect and sort data by x - const rawPoints: { x: number; y: number }[] = []; - visitPoints(data, x, y, (dx, dy) => { - rawPoints.push({ x: dx, y: dy }); + const rawPoints: LowessPoint[] = []; + visitPoints(data, x, y, (dx, dy, index) => { + rawPoints.push({ x: dx, y: dy, index }); }); rawPoints.sort((a, b) => a.x - b.x); @@ -93,12 +99,20 @@ export function regressionLowess( } } - const m = Math.max(2, Math.min(n, Math.floor(span * n))); + const m = Math.min(n, Math.max(2, Math.floor(span * n))); - // Calculate range around insertion point - const start = Math.max(0, left - Math.floor(m / 2)); - const end = Math.min(n, start + m); - const actualStart = Math.max(0, end - m); + // Expand by distance: equal numbers on either side need not be the nearest points. + let lo = left - 1; + let hi = left; + for (let count = 0; count < m; count++) { + if (lo >= 0 && (hi >= n || x0 - ptsX[lo] <= ptsX[hi] - x0)) { + lo--; + } else { + hi++; + } + } + const actualStart = lo + 1; + const end = hi; // Find max distance and compute weights in single pass let maxDist = 0; @@ -127,8 +141,19 @@ export function regressionLowess( } if (sumw === 0) { - // fallback to nearest y - const nearestIdx = left < n ? left : n - 1; + // Fall back to the nearest y, preserving input order for equal distances. + let nearestIdx = 0; + let nearestDistance = Math.abs(ptsX[0] - x0); + for (let i = 1; i < n; i++) { + const distance = Math.abs(ptsX[i] - x0); + if ( + distance < nearestDistance || + (distance === nearestDistance && sampledPoints[i].index < sampledPoints[nearestIdx].index) + ) { + nearestIdx = i; + nearestDistance = distance; + } + } return ptsY[nearestIdx]; } From f9a29f93697df46e6165be00811da6b21078a56d Mon Sep 17 00:00:00 2001 From: xile611 Date: Fri, 18 Sep 2026 11:25:54 +0800 Subject: [PATCH 2/2] test: preserve LOWESS scatter regression output --- .../2026-09-18-lowess-nearest-neighbors.md | 366 +++++ .../vutils/__tests__/data/lowess-scatter.ts | 1325 +++++++++++++++++ .../__tests__/lowess-compatibility.test.ts | 27 + 3 files changed, 1718 insertions(+) create mode 100644 docs/superpowers/plans/2026-09-18-lowess-nearest-neighbors.md create mode 100644 packages/vutils/__tests__/data/lowess-scatter.ts create mode 100644 packages/vutils/__tests__/lowess-compatibility.test.ts diff --git a/docs/superpowers/plans/2026-09-18-lowess-nearest-neighbors.md b/docs/superpowers/plans/2026-09-18-lowess-nearest-neighbors.md new file mode 100644 index 0000000..e99a6c8 --- /dev/null +++ b/docs/superpowers/plans/2026-09-18-lowess-nearest-neighbors.md @@ -0,0 +1,366 @@ +# LOWESS Nearest Neighbors Fix Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** 恢复 LOWESS 按实际距离选择最近邻的语义,消除散点回归线用例的数值差异,同时保留已有性能优化。 + +**Architecture:** 保留数据预排序和二分定位,从插入点用左右双指针按距离扩展,选择最近的 m 个样本。加权拟合、稳健迭代和置信区间继续复用当前实现;不新增公开 API。用独立的直接计算参考函数和优化前的固定输出分别验证算法与真实用例。 + +**Tech Stack:** TypeScript 4.9.5、Jest 29.5、ts-jest、Rush 5.164.0、pnpm 10.7.0;Node.js >=20.19.6。 + +## Global Constraints + +- 计划制定阶段未执行实现;用户随后授权执行修复。执行范围包含本地修改、安装锁定依赖、验证与本地提交,不包含推送、发版或修改 bugserver 基准图。 +- 执行时只修改 LOWESS 邻域选择及其直接相关的边界处理,新增确定性测试;不重构其他回归算法。 +- 保留 maxSamples=1000、span=0.3、degree=1 和已有稳健迭代默认策略;本例 n=406,不触发抽样和 n>500 时关闭默认迭代的分支。 +- 不承诺大数据默认配置恢复到优化前结果:抽样及迭代策略属于另一项行为变化,不在本次修复范围内。 +- 不引入运行时依赖,不手改版本号或自动生成的历史 changelog。 +- 不在预测时对所有距离排序。常规单点预测保持 O(log n + m),其中 n 是抽样后的样本数;全零权重的罕见回退允许 O(n) 扫描。 +- 最近点等距且需要回退时,沿用旧版的输入顺序优先规则。边界等距点的 tricube 权重为零,不要求改变其累加顺序以追求浮点逐位一致。 +- 新测试不使用 Math.random;旧版对照的绝对误差阈值为 1e-8。 +- 上述执行技能当前未安装;本次按已确认计划在当前任务直接执行,没有创建子任务。 + +## 已确认的依据和方案选择 + +- 引入提交:b0766a02eafa6ed3f40778224343f320d1def84f,2025-12-05,v1.0.21 起包含。 +- 问题位置:packages/vutils/src/common/regression-lowess.ts 的 start/end/actualStart 窗口计算。 +- 用例:https://bugserver.cn.goofy.app/case?product=chartspace4&fileid=68f8b0ca43ec0700a96ec5ad +- 页面原始数据 406 条,坐标序列校验值 718450318,N=floor(406/4)=101,m=floor(406*0.3)=121。 +- x=12 时,旧邻域范围 [9,18]、半径 6;当前范围 [0,18]、半径 12。 +- 当前与旧版曲线最大差值 1.2197504464103162;仅替换窗口选择的实验最大误差 5.8832938520936295e-12。 +- 诊断材料位于 /Users/bytedance/.codex/visualizations/2026/09/18/01a0b27e-d458-7ea0-85ba-6754954cf853/lowess-diagnosis/,其中 results.json 保存 data、grids.old、cis.old,compare.cjs 保存复现方法。 + +选用“排序 + 二分 + 双指针”。整体回退 b0766a0 会一起移除抽样等优化;恢复每次距离排序会重新引入 O(n log n) 的预测成本。这两种方案均扩大改动或牺牲已有性能。 + +## 文件职责 + +| 文件 | 操作 | 职责 | +| --- | --- | --- | +| packages/vutils/src/common/regression-lowess.ts | 修改 | 最近邻窗口、样本数边界、等距回退 | +| packages/vutils/__tests__/lowess-neighbors.test.ts | 新增 | 非均匀数据和边界的独立数值验证 | +| packages/vutils/__tests__/data/lowess-scatter.ts | 新增 | 固定 406 条坐标及旧版曲线、置信区间基准 | +| packages/vutils/__tests__/lowess-compatibility.test.ts | 新增 | 原用例的端到端数值回归 | +| packages/vutils/__tests__/lowess-accuracy.test.ts | 复用 | 已有精度覆盖 | +| packages/vutils/__tests__/lowess-performance.test.ts | 复用 | 已有性能覆盖,不放宽阈值 | + +## Task 1: 修复最近邻选择,加入确定性算法测试 + +**Files:** 修改 regression-lowess.ts;新增 lowess-neighbors.test.ts,完整路径见上表。 + +**Interfaces:** 消费既有 regressionLowess(data, x?, y?, options?);保持 predict、evaluate、evaluateGrid、confidenceInterval 的签名及返回结构。 + +- [x] **Step 1: 准备执行环境** + +先检查工作区变化;在执行修复时创建 codex/fix-lowess-nearest-neighbors 分支,若同名分支已存在则检查并复用,避免覆盖其他工作。当前 VUtil 未安装包内依赖;从仓库根目录使用锁文件安装,禁止为此重写锁文件: + +```bash +node common/scripts/install-run-rush.js install +``` + +- [x] **Step 2: 写独立最近邻参考函数及失败测试** + +在 lowess-neighbors.test.ts 写入以下代码。参考实现仅用于小数据测试,直接按距离排序;生产实现不得调用它。 + +```ts +import { regressionLowess } from '../src/common/regression-lowess'; + +type Point = { x: number; y: number }; + +function referencePredict(data: Point[], x0: number, span: number, degree: 0 | 1) { + if (!data.length) { + return 0; + } + const count = Math.min(data.length, Math.max(2, Math.floor(span * data.length))); + const nearest = data + .map((p, index) => ({ ...p, index, distance: Math.abs(p.x - x0) })) + .sort((a, b) => a.distance - b.distance || a.index - b.index) + .slice(0, count); + const radius = nearest[nearest.length - 1].distance; + let sw = 0; + let sx = 0; + let sy = 0; + let sxx = 0; + let sxy = 0; + nearest.forEach(p => { + const u = radius === 0 ? 0 : p.distance / radius; + const w = u >= 1 ? 0 : Math.pow(1 - u * u * u, 3); + sw += w; + sx += w * p.x; + sy += w * p.y; + sxx += w * p.x * p.x; + sxy += w * p.x * p.y; + }); + if (sw === 0) { + return nearest[0].y; + } + if (degree === 0) { + return sy / sw; + } + const meanX = sx / sw; + const meanY = sy / sw; + const denominator = sxx - sx * meanX; + const slope = Math.abs(denominator) < 1e-12 ? 0 : (sxy - sx * meanY) / denominator; + return meanY + slope * (x0 - meanX); +} + +const cases: Point[][] = [ + [0, 100, 101, 102, 103, 104, 105, 106, 107, 108].map(x => ({ x, y: x * x })), + [0, 0.1, 0.2, 0.3, 3, 10, 20, 20.1, 20.2, 50].map(x => ({ x, y: Math.sin(x) })), + [{ x: 2, y: 20 }, { x: 0, y: 5 }], + [1, 2, 3, 4, 5, 6, 7].map(y => ({ x: 1, y })), + [{ x: 4, y: 9 }], + [] +]; + +describe('LOWESS nearest neighbors', () => { + test.each([0, 1] as const)('matches distance-ranked neighbors for degree=%i', degree => { + for (const data of cases) { + for (const span of [0.3, 0.6, 1]) { + const model = regressionLowess(data, undefined, undefined, { span, degree, iterations: 0 }); + for (const x0 of [-4, 0, 0.15, 1, 2.5, 12, 20, 100.5, 130]) { + expect(Math.abs((model.predict(x0) as number) - referencePredict(data, x0, span, degree))) + .toBeLessThan(1e-8); + } + for (const p of model.evaluateGrid(11)) { + expect(Math.abs(p.y - referencePredict(data, p.x, span, degree))).toBeLessThan(1e-8); + } + } + } + }); + + test('uses original input order when equally near points have zero weight', () => { + const model = regressionLowess([{ x: 2, y: 20 }, { x: 0, y: 5 }]); + expect(model.predict(1)).toBe(20); + }); + + test('keeps the first coincident points when the neighborhood radius is zero', () => { + const data = [1, 2, 3, 4, 5, 6, 7].map(y => ({ x: 1, y })); + expect(regressionLowess(data).predict(1)).toBe(1.5); + }); +}); +``` + +- [x] **Step 3: 确认新测试在未修复源码上失败** + +以下命令均在 packages/vutils 执行: + +```bash +node ../../common/scripts/install-run-rushx.js test --runInBand --runTestsByPath __tests__/lowess-neighbors.test.ts +``` + +预期:非均匀数据的数值断言失败。若出现依赖或编译错误,先排除环境错误,不能将其视为成功复现。 + +- [x] **Step 4: 按实际距离扩展窗口** + +在 predictSingle 中,保留 lower_bound 二分查找,以如下代码替换 m、start、end、actualStart 计算。其余距离、权重与拟合计算继续使用 actualStart/end。 + +```ts +const m = Math.min(n, Math.max(2, Math.floor(span * n))); +let lo = left - 1; +let hi = left; +for (let count = 0; count < m; count++) { + if (lo >= 0 && (hi >= n || x0 - ptsX[lo] <= ptsX[hi] - x0)) { + lo--; + } else { + hi++; + } +} +const actualStart = lo + 1; +const end = hi; +``` + +说明:每次消耗距离更近的一侧,窗口最终恰有 m 个样本;n=0 的已有提前返回继续保留。m 的外层 n 上限保护单点输入。距离相等时选左侧,仅影响零权重边界的取舍;当距离半径为零时,lower_bound 从重复 x 的第一项开始向右取样,保留旧版的输入顺序。 + +- [x] **Step 5: 保证全零权重回退的等距规则** + +使用内部类型保留原始顺序;相应更新 rawPoints、stratifiedSample 入参与 sampled 数组类型: + +```ts +interface LowessPoint { + x: number; + y: number; + index: number; +} + +// rawPoints 的采集: +const rawPoints: LowessPoint[] = []; +visitPoints(data, x, y, (dx, dy, index) => { + rawPoints.push({ x: dx, y: dy, index }); +}); +``` + +将 sumw===0 的插入点回退替换为以下逻辑。它仅在权重全零时执行;正常拟合无需额外全表扫描: + +```ts +if (sumw === 0) { + let nearestIdx = 0; + let nearestDistance = Math.abs(ptsX[0] - x0); + for (let i = 1; i < n; i++) { + const distance = Math.abs(ptsX[i] - x0); + if ( + distance < nearestDistance || + (distance === nearestDistance && sampledPoints[i].index < sampledPoints[nearestIdx].index) + ) { + nearestIdx = i; + nearestDistance = distance; + } + } + return ptsY[nearestIdx]; +} +``` + +不在本次处理非法 span、NaN 查询、evaluateGrid(1) 等已有独立问题。 + +- [x] **Step 6: 验证测试通过并形成第一项可审查改动** + +重跑 Step 3。检查生产路径没有距离排序;公开类型未变化;抽样和有效迭代数分支未变化。执行阶段按仓库流程提交,建议消息:`fix: preserve LOWESS nearest-neighbor selection`。提交应同时包含生产修复和本任务测试。 + +## Task 2: 固化真实用例的曲线及置信区间回归测试 + +**Files:** 新增 data/lowess-scatter.ts 和 lowess-compatibility.test.ts,完整路径见上表。 + +**Interfaces:** 测试消费 Task 1 保持不变的 API;fixture 导出 lowessScatterData、expectedGrid、expectedCI,分别为坐标数组、101 个旧版网格点和 101 个旧版置信区间结果。 + +- [x] **Step 1: 将已经验证的原始数据和旧版结果固化入库** + +从仓库根目录执行以下一次性转换。自动化测试本身不能依赖临时目录、邻接 VChart 仓库、网络或 git 历史: + +```python +import json +from pathlib import Path + +source = Path('/Users/bytedance/.codex/visualizations/2026/09/18/01a0b27e-d458-7ea0-85ba-6754954cf853/lowess-diagnosis/results.json') +r = json.loads(source.read_text()) +assert len(r['data']) == 406 +assert len(r['grids']['old']) == len(r['cis']['old']) == 101 +pairs = [[p['x'], p['y']] for p in r['data']] +h = 2166136261 +for c in json.dumps(pairs, separators=(',', ':')): + h = ((h ^ ord(c)) * 16777619) & 0xffffffff +assert h == 718450318 +out = Path('packages/vutils/__tests__/data/lowess-scatter.ts') +out.parent.mkdir(parents=True, exist_ok=True) +text = '// Baseline: b0766a0^ LOWESS, default options, evaluateGrid(101) / confidenceInterval(101).\n' +text += '// Dataset: scatter regression example; 406 (milesPerGallon, horsepower) pairs.\n' +for name, value in [('lowessScatterData', r['data']), ('expectedGrid', r['grids']['old']), ('expectedCI', r['cis']['old'])]: + text += f'export const {name} = ' + json.dumps(value, separators=(',', ':')) + ';\n' +out.write_text(text) +``` + +这些是已计算的优化前结果,不能以修复后函数输出重新生成基准。fixture 只保存公开示例坐标,不保存汽车名称或内部页面配置。 + +- [x] **Step 2: 写真实用例断言** + +```ts +import { regressionLowess } from '../src/common/regression-lowess'; +import { lowessScatterData, expectedGrid, expectedCI } from './data/lowess-scatter'; + +function expectNear(actual: number, expected: number) { + expect(Number.isFinite(actual)).toBe(true); + expect(Math.abs(actual - expected)).toBeLessThan(1e-8); +} + +test('preserves the scatter LOWESS curve and confidence intervals', () => { + expect(lowessScatterData).toHaveLength(406); + const model = regressionLowess(lowessScatterData); + const grid = model.evaluateGrid(101); + const ci = model.confidenceInterval(101); + expect(grid).toHaveLength(expectedGrid.length); + expect(ci).toHaveLength(expectedCI.length); + grid.forEach((point, i) => { + expectNear(point.x, expectedGrid[i].x); + expectNear(point.y, expectedGrid[i].y); + }); + const keys = ['x', 'mean', 'lower', 'upper', 'predLower', 'predUpper'] as const; + ci.forEach((point, i) => { + keys.forEach(key => expectNear(point[key], expectedCI[i][key])); + }); + const predicted = model.predict(expectedCI.map(point => point.x)) as number[]; + predicted.forEach((value, i) => expectNear(value, expectedCI[i].mean)); +}); +``` + +注意:旧版和当前实现均只有 evaluateGrid 使用稳健迭代,predict/CI 不使用;因此 predict 对比 CI.mean,不能错误地要求 predict 与默认 evaluateGrid 一致。本次不改变这项既有行为。 + +- [x] **Step 3: 验证能阻止原问题复发** + +从 packages/vutils 运行: + +```bash +node ../../common/scripts/install-run-rushx.js test --runInBand --runTestsByPath __tests__/lowess-neighbors.test.ts __tests__/lowess-compatibility.test.ts __tests__/lowess-accuracy.test.ts __tests__/regression.test.ts +``` + +预期全部通过。再在临时副本中仅替换回原 start/end/actualStart 计算,运行 compatibility 测试,预期数值失败;不要覆盖工作区文件。执行阶段将 fixture 与兼容性测试作为独立可审查提交,建议消息:`test: preserve LOWESS scatter regression output`。 + +## Task 3: 性能、包级检查与原用例验收 + +**Files:** 复用现有性能测试和包级配置;不计划新增性能框架、调整 CI 或放宽现有阈值。 + +**Interfaces:** 对已修复的 regressionLowess 运行现有调用;输出测试结果、性能记录和图像验收结果。 + +- [x] **Step 1: 运行已有性能测试** + +在 packages/vutils 执行: + +```bash +RUN_PERF_TESTS=true node ../../common/scripts/install-run-rushx.js test --runInBand --runTestsByPath __tests__/lowess-performance.test.ts +``` + +验收使用已有阈值:1K <100ms、5K <150ms、10K <100ms、20K <200ms;maxSamples=500 <100ms、maxSamples=2000 <200ms。记录实际耗时。若失败,先区分环境波动与修复引入的额外成本,不通过放宽阈值使测试变绿。 + +- [x] **Step 2: 完成包级验证** + +在 packages/vutils 执行;不自动修复全包格式: + +```bash +CI=true node ../../common/scripts/install-run-rushx.js test --runInBand +node ../../common/scripts/install-run-rushx.js compile +node node_modules/eslint/bin/eslint.js src/common/regression-lowess.ts __tests__/lowess-neighbors.test.ts __tests__/lowess-compatibility.test.ts __tests__/data/lowess-scatter.ts +node ../../common/scripts/install-run-rushx.js build +``` + +若遇到既有随机测试或编译配置失败,先用未改源码的相同命令确认基线,明确区分已有问题,不扩大本次修复范围。 + +- [ ] **Step 3: 用实际消费链路复验截图** + +在可加载本地修复构建的 VChart/ChartSpace 测试环境中运行同一份 406 条数据和三条回归线配置。沿用同一渲染版本、画布尺寸和截图环境,比较旧算法与修复后的输出;确认蓝色 LOWESS 曲线及置信带恢复,红色多项式和绿色线性回归保持一致。不得为消除 diff 执行 Set To Standard。 + +如果执行环境不能注入本地构建,先交付完成的代码、数值和性能验证,并明确“bugserver 图像复验待修复构建可用”,不能声称线上截图已经恢复。图像验收不通过时应首先比较传入数据与 101 个网格点,再检查渲染环境差异。 + +- [x] **Step 4: 汇总交付结果** + +在修复说明中记录:错误的固定下标窗口、实际距离最近邻修复、原用例最大误差、新增测试和性能结果。按当前发布流程作为 patch 修复;后续若要求 PR,标题可用 `fix: preserve LOWESS nearest-neighbor selection`,不在本计划执行期间直接发布版本或改线上基准。 + +## 完成标准 + +- 非均匀分布的失败测试在修复前失败、修复后通过。 +- 空数据、单点、重复 x、域外预测和等距回退有确定性断言。 +- 原用例 101 个曲线点及所有 CI 数值与优化前绝对误差 <1e-8。 +- 已有 LOWESS 精度和性能测试通过,包级检查结果明确。 +- 生产预测不恢复每次全量距离排序;公开 API、抽样及迭代默认策略不变。 +- 原消费环境图像复验完成,或明确留下依赖修复构建的验收项;不重置基准图。 + +## 计划自检 + +已核对:根因、目标、文件职责、函数签名、测试基准来源、重复 x 和等距语义、性能门槛、发布边界。没有将数值验证等同于浏览器像素验证,也没有将大数据默认行为变化包含在本次修复中。 + + +## 执行结果(2026-09-18) + +- 分支:`codex/fix-lowess-nearest-neighbors`。算法修复与最近邻测试已提交为 `63b1491`。 +- 修复前:新增 18 项测试中 10 项失败,包含真实用例的数值差异;修复后全部通过。 +- 新增独立直接距离排序参考、非均匀分布、乱序重复 x、等距回退、单点及空数据测试。合成数据调整了坐标尺度和外推范围以避免把浮点消减误差混入邻域正确性断言,没有放宽 `1e-8` 阈值。 +- 固化 406 条公开示例坐标,校验值为 `718450318`,并保存优化前 101 个曲线点和置信区间结果。 +- 全包 Jest:44 个测试套件、197 项测试通过;6 项性能测试在 CI 模式下跳过后另行显式运行并全部通过。 +- 独立性能测试整项耗时:1K 4 ms、5K 4 ms、10K 5 ms、20K 7 ms;自定义 maxSamples 对照项合计 36 ms。原始计时阈值全部满足。 +- 修改文件的 ESLint 通过;CJS、ES、UMD 和压缩 UMD 构建成功。 +- 对实际 CJS 构建再计算:曲线最大误差 `5.8832938520936295e-12`,CI 全部字段最大误差 `5.3717030823463574e-12`。 +- 临时副本中仅恢复错误的固定窗口,真实用例回归测试重新失败,首个差值 `0.024079300530246428`;工作区源码保持修复状态。 +- 原始 `compile` 命令报 TS6059:`rootDir=src` 与 `include=__tests__` 冲突。用修改前 HEAD 的临时归档确认同样失败。未修改项目配置;以下覆盖命令通过: + +```bash +node node_modules/typescript/bin/tsc --noEmit --rootDir . --composite false --incremental false --types node,jest +``` + +- 锁文件、公开 API、抽样和稳健迭代默认策略均未变更。 +- 未完成的外部验收:bugserver 图像复验需要可加载此 VUtil 构建的 ChartSpace 测试版本。本地没有已构建的对应 ChartSpace/VChart-extension 消费环境,未修改线上用例或重置基准图。数值与构建验证已完成,不能据此声称线上截图已复验。 diff --git a/packages/vutils/__tests__/data/lowess-scatter.ts b/packages/vutils/__tests__/data/lowess-scatter.ts new file mode 100644 index 0000000..a1e9452 --- /dev/null +++ b/packages/vutils/__tests__/data/lowess-scatter.ts @@ -0,0 +1,1325 @@ +// Baseline: b0766a0^ LOWESS, default options, evaluateGrid(101) / confidenceInterval(101). +// Dataset: scatter regression example; 406 (milesPerGallon, horsepower) pairs. +export const lowessScatterData = [ + { x: 18, y: 130 }, + { x: 15, y: 165 }, + { x: 18, y: 150 }, + { x: 16, y: 150 }, + { x: 17, y: 140 }, + { x: 15, y: 198 }, + { x: 14, y: 220 }, + { x: 14, y: 215 }, + { x: 14, y: 225 }, + { x: 15, y: 190 }, + { x: 0, y: 115 }, + { x: 0, y: 165 }, + { x: 0, y: 153 }, + { x: 0, y: 175 }, + { x: 0, y: 175 }, + { x: 15, y: 170 }, + { x: 14, y: 160 }, + { x: 0, y: 140 }, + { x: 15, y: 150 }, + { x: 14, y: 225 }, + { x: 24, y: 95 }, + { x: 22, y: 95 }, + { x: 18, y: 97 }, + { x: 21, y: 85 }, + { x: 27, y: 88 }, + { x: 26, y: 46 }, + { x: 25, y: 87 }, + { x: 24, y: 90 }, + { x: 25, y: 95 }, + { x: 26, y: 113 }, + { x: 21, y: 90 }, + { x: 10, y: 215 }, + { x: 10, y: 200 }, + { x: 11, y: 210 }, + { x: 9, y: 193 }, + { x: 27, y: 88 }, + { x: 28, y: 90 }, + { x: 25, y: 95 }, + { x: 25, y: 0 }, + { x: 0, y: 48 }, + { x: 19, y: 100 }, + { x: 16, y: 105 }, + { x: 17, y: 100 }, + { x: 19, y: 88 }, + { x: 18, y: 100 }, + { x: 14, y: 165 }, + { x: 14, y: 175 }, + { x: 14, y: 153 }, + { x: 14, y: 150 }, + { x: 12, y: 180 }, + { x: 13, y: 170 }, + { x: 13, y: 175 }, + { x: 18, y: 110 }, + { x: 22, y: 72 }, + { x: 19, y: 100 }, + { x: 18, y: 88 }, + { x: 23, y: 86 }, + { x: 28, y: 90 }, + { x: 30, y: 70 }, + { x: 30, y: 76 }, + { x: 31, y: 65 }, + { x: 35, y: 69 }, + { x: 27, y: 60 }, + { x: 26, y: 70 }, + { x: 24, y: 95 }, + { x: 25, y: 80 }, + { x: 23, y: 54 }, + { x: 20, y: 90 }, + { x: 21, y: 86 }, + { x: 13, y: 165 }, + { x: 14, y: 175 }, + { x: 15, y: 150 }, + { x: 14, y: 153 }, + { x: 17, y: 150 }, + { x: 11, y: 208 }, + { x: 13, y: 155 }, + { x: 12, y: 160 }, + { x: 13, y: 190 }, + { x: 19, y: 97 }, + { x: 15, y: 150 }, + { x: 13, y: 130 }, + { x: 13, y: 140 }, + { x: 14, y: 150 }, + { x: 18, y: 112 }, + { x: 22, y: 76 }, + { x: 21, y: 87 }, + { x: 26, y: 69 }, + { x: 22, y: 86 }, + { x: 28, y: 92 }, + { x: 23, y: 97 }, + { x: 28, y: 80 }, + { x: 27, y: 88 }, + { x: 13, y: 175 }, + { x: 14, y: 150 }, + { x: 13, y: 145 }, + { x: 14, y: 137 }, + { x: 15, y: 150 }, + { x: 12, y: 198 }, + { x: 13, y: 150 }, + { x: 13, y: 158 }, + { x: 14, y: 150 }, + { x: 13, y: 215 }, + { x: 12, y: 225 }, + { x: 13, y: 175 }, + { x: 18, y: 105 }, + { x: 16, y: 100 }, + { x: 18, y: 100 }, + { x: 18, y: 88 }, + { x: 23, y: 95 }, + { x: 26, y: 46 }, + { x: 11, y: 150 }, + { x: 12, y: 167 }, + { x: 13, y: 170 }, + { x: 12, y: 180 }, + { x: 18, y: 100 }, + { x: 20, y: 88 }, + { x: 21, y: 72 }, + { x: 22, y: 94 }, + { x: 18, y: 90 }, + { x: 19, y: 85 }, + { x: 21, y: 107 }, + { x: 26, y: 90 }, + { x: 15, y: 145 }, + { x: 16, y: 230 }, + { x: 29, y: 49 }, + { x: 24, y: 75 }, + { x: 20, y: 91 }, + { x: 19, y: 112 }, + { x: 15, y: 150 }, + { x: 24, y: 110 }, + { x: 20, y: 122 }, + { x: 11, y: 180 }, + { x: 20, y: 95 }, + { x: 21, y: 0 }, + { x: 19, y: 100 }, + { x: 15, y: 100 }, + { x: 31, y: 67 }, + { x: 26, y: 80 }, + { x: 32, y: 65 }, + { x: 25, y: 75 }, + { x: 16, y: 100 }, + { x: 16, y: 110 }, + { x: 18, y: 105 }, + { x: 16, y: 140 }, + { x: 13, y: 150 }, + { x: 14, y: 150 }, + { x: 14, y: 140 }, + { x: 14, y: 150 }, + { x: 29, y: 83 }, + { x: 26, y: 67 }, + { x: 26, y: 78 }, + { x: 31, y: 52 }, + { x: 32, y: 61 }, + { x: 28, y: 75 }, + { x: 24, y: 75 }, + { x: 26, y: 75 }, + { x: 24, y: 97 }, + { x: 26, y: 93 }, + { x: 31, y: 67 }, + { x: 19, y: 95 }, + { x: 18, y: 105 }, + { x: 15, y: 72 }, + { x: 15, y: 72 }, + { x: 16, y: 170 }, + { x: 15, y: 145 }, + { x: 16, y: 150 }, + { x: 14, y: 148 }, + { x: 17, y: 110 }, + { x: 16, y: 105 }, + { x: 15, y: 110 }, + { x: 18, y: 95 }, + { x: 21, y: 110 }, + { x: 20, y: 110 }, + { x: 13, y: 129 }, + { x: 29, y: 75 }, + { x: 23, y: 83 }, + { x: 20, y: 100 }, + { x: 23, y: 78 }, + { x: 24, y: 96 }, + { x: 25, y: 71 }, + { x: 24, y: 97 }, + { x: 18, y: 97 }, + { x: 29, y: 70 }, + { x: 19, y: 90 }, + { x: 23, y: 95 }, + { x: 23, y: 88 }, + { x: 22, y: 98 }, + { x: 25, y: 115 }, + { x: 33, y: 53 }, + { x: 28, y: 86 }, + { x: 25, y: 81 }, + { x: 25, y: 92 }, + { x: 26, y: 79 }, + { x: 27, y: 83 }, + { x: 17.5, y: 140 }, + { x: 16, y: 150 }, + { x: 15.5, y: 120 }, + { x: 14.5, y: 152 }, + { x: 22, y: 100 }, + { x: 22, y: 105 }, + { x: 24, y: 81 }, + { x: 22.5, y: 90 }, + { x: 29, y: 52 }, + { x: 24.5, y: 60 }, + { x: 29, y: 70 }, + { x: 33, y: 53 }, + { x: 20, y: 100 }, + { x: 18, y: 78 }, + { x: 18.5, y: 110 }, + { x: 17.5, y: 95 }, + { x: 29.5, y: 71 }, + { x: 32, y: 70 }, + { x: 28, y: 75 }, + { x: 26.5, y: 72 }, + { x: 20, y: 102 }, + { x: 13, y: 150 }, + { x: 19, y: 88 }, + { x: 19, y: 108 }, + { x: 16.5, y: 120 }, + { x: 16.5, y: 180 }, + { x: 13, y: 145 }, + { x: 13, y: 130 }, + { x: 13, y: 150 }, + { x: 31.5, y: 68 }, + { x: 30, y: 80 }, + { x: 36, y: 58 }, + { x: 25.5, y: 96 }, + { x: 33.5, y: 70 }, + { x: 17.5, y: 145 }, + { x: 17, y: 110 }, + { x: 15.5, y: 145 }, + { x: 15, y: 130 }, + { x: 17.5, y: 110 }, + { x: 20.5, y: 105 }, + { x: 19, y: 100 }, + { x: 18.5, y: 98 }, + { x: 16, y: 180 }, + { x: 15.5, y: 170 }, + { x: 15.5, y: 190 }, + { x: 16, y: 149 }, + { x: 29, y: 78 }, + { x: 24.5, y: 88 }, + { x: 26, y: 75 }, + { x: 25.5, y: 89 }, + { x: 30.5, y: 63 }, + { x: 33.5, y: 83 }, + { x: 30, y: 67 }, + { x: 30.5, y: 78 }, + { x: 22, y: 97 }, + { x: 21.5, y: 110 }, + { x: 21.5, y: 110 }, + { x: 43.1, y: 48 }, + { x: 36.1, y: 66 }, + { x: 32.8, y: 52 }, + { x: 39.4, y: 70 }, + { x: 36.1, y: 60 }, + { x: 19.9, y: 110 }, + { x: 19.4, y: 140 }, + { x: 20.2, y: 139 }, + { x: 19.2, y: 105 }, + { x: 20.5, y: 95 }, + { x: 20.2, y: 85 }, + { x: 25.1, y: 88 }, + { x: 20.5, y: 100 }, + { x: 19.4, y: 90 }, + { x: 20.6, y: 105 }, + { x: 20.8, y: 85 }, + { x: 18.6, y: 110 }, + { x: 18.1, y: 120 }, + { x: 19.2, y: 145 }, + { x: 17.7, y: 165 }, + { x: 18.1, y: 139 }, + { x: 17.5, y: 140 }, + { x: 30, y: 68 }, + { x: 27.5, y: 95 }, + { x: 27.2, y: 97 }, + { x: 30.9, y: 75 }, + { x: 21.1, y: 95 }, + { x: 23.2, y: 105 }, + { x: 23.8, y: 85 }, + { x: 23.9, y: 97 }, + { x: 20.3, y: 103 }, + { x: 17, y: 125 }, + { x: 21.6, y: 115 }, + { x: 16.2, y: 133 }, + { x: 31.5, y: 71 }, + { x: 29.5, y: 68 }, + { x: 21.5, y: 115 }, + { x: 19.8, y: 85 }, + { x: 22.3, y: 88 }, + { x: 20.2, y: 90 }, + { x: 20.6, y: 110 }, + { x: 17, y: 130 }, + { x: 17.6, y: 129 }, + { x: 16.5, y: 138 }, + { x: 18.2, y: 135 }, + { x: 16.9, y: 155 }, + { x: 15.5, y: 142 }, + { x: 19.2, y: 125 }, + { x: 18.5, y: 150 }, + { x: 31.9, y: 71 }, + { x: 34.1, y: 65 }, + { x: 35.7, y: 80 }, + { x: 27.4, y: 80 }, + { x: 25.4, y: 77 }, + { x: 23, y: 125 }, + { x: 27.2, y: 71 }, + { x: 23.9, y: 90 }, + { x: 34.2, y: 70 }, + { x: 34.5, y: 70 }, + { x: 31.8, y: 65 }, + { x: 37.3, y: 69 }, + { x: 28.4, y: 90 }, + { x: 28.8, y: 115 }, + { x: 26.8, y: 115 }, + { x: 33.5, y: 90 }, + { x: 41.5, y: 76 }, + { x: 38.1, y: 60 }, + { x: 32.1, y: 70 }, + { x: 37.2, y: 65 }, + { x: 28, y: 90 }, + { x: 26.4, y: 88 }, + { x: 24.3, y: 90 }, + { x: 19.1, y: 90 }, + { x: 34.3, y: 78 }, + { x: 29.8, y: 90 }, + { x: 31.3, y: 75 }, + { x: 37, y: 92 }, + { x: 32.2, y: 75 }, + { x: 46.6, y: 65 }, + { x: 27.9, y: 105 }, + { x: 40.8, y: 65 }, + { x: 44.3, y: 48 }, + { x: 43.4, y: 48 }, + { x: 36.4, y: 67 }, + { x: 30, y: 67 }, + { x: 44.6, y: 67 }, + { x: 40.9, y: 0 }, + { x: 33.8, y: 67 }, + { x: 29.8, y: 62 }, + { x: 32.7, y: 132 }, + { x: 23.7, y: 100 }, + { x: 35, y: 88 }, + { x: 23.6, y: 0 }, + { x: 32.4, y: 72 }, + { x: 27.2, y: 84 }, + { x: 26.6, y: 84 }, + { x: 25.8, y: 92 }, + { x: 23.5, y: 110 }, + { x: 30, y: 84 }, + { x: 39.1, y: 58 }, + { x: 39, y: 64 }, + { x: 35.1, y: 60 }, + { x: 32.3, y: 67 }, + { x: 37, y: 65 }, + { x: 37.7, y: 62 }, + { x: 34.1, y: 68 }, + { x: 34.7, y: 63 }, + { x: 34.4, y: 65 }, + { x: 29.9, y: 65 }, + { x: 33, y: 74 }, + { x: 34.5, y: 0 }, + { x: 33.7, y: 75 }, + { x: 32.4, y: 75 }, + { x: 32.9, y: 100 }, + { x: 31.6, y: 74 }, + { x: 28.1, y: 80 }, + { x: 0, y: 110 }, + { x: 30.7, y: 76 }, + { x: 25.4, y: 116 }, + { x: 24.2, y: 120 }, + { x: 22.4, y: 110 }, + { x: 26.6, y: 105 }, + { x: 20.2, y: 88 }, + { x: 17.6, y: 85 }, + { x: 28, y: 88 }, + { x: 27, y: 88 }, + { x: 34, y: 88 }, + { x: 31, y: 85 }, + { x: 29, y: 84 }, + { x: 27, y: 90 }, + { x: 24, y: 92 }, + { x: 23, y: 0 }, + { x: 36, y: 74 }, + { x: 37, y: 68 }, + { x: 31, y: 68 }, + { x: 38, y: 63 }, + { x: 36, y: 70 }, + { x: 36, y: 88 }, + { x: 36, y: 75 }, + { x: 34, y: 70 }, + { x: 38, y: 67 }, + { x: 32, y: 67 }, + { x: 38, y: 67 }, + { x: 25, y: 110 }, + { x: 38, y: 85 }, + { x: 26, y: 92 }, + { x: 22, y: 112 }, + { x: 32, y: 96 }, + { x: 36, y: 84 }, + { x: 27, y: 90 }, + { x: 27, y: 86 }, + { x: 44, y: 52 }, + { x: 32, y: 84 }, + { x: 28, y: 79 }, + { x: 31, y: 82 } +]; + +export const expectedGrid = [ + { x: 0, y: 160.50659199621273 }, + { x: 0.466, y: 160.94270026114776 }, + { x: 0.932, y: 161.36264994942022 }, + { x: 1.3980000000000001, y: 161.76748277994994 }, + { x: 1.864, y: 162.15904556303983 }, + { x: 2.33, y: 162.54027918817118 }, + { x: 2.7960000000000003, y: 162.91564205189616 }, + { x: 3.262, y: 163.29175618276182 }, + { x: 3.728, y: 163.67843540070237 }, + { x: 4.194, y: 164.09039404580946 }, + { x: 4.66, y: 164.5502207146875 }, + { x: 5.126, y: 165.0938216976506 }, + { x: 5.5920000000000005, y: 165.7809704421064 }, + { x: 6.058000000000001, y: 166.7171380911526 }, + { x: 6.524, y: 168.10216929898965 }, + { x: 6.99, y: 170.34770030236677 }, + { x: 7.456, y: 174.3760040760574 }, + { x: 7.922000000000001, y: 182.2791676368961 }, + { x: 8.388, y: 196.56488888782047 }, + { x: 8.854000000000001, y: 205.52979934486882 }, + { x: 9.32, y: 200.91136487307472 }, + { x: 9.786000000000001, y: 195.97209628451992 }, + { x: 10.252, y: 191.0929693431466 }, + { x: 10.718, y: 186.28780210011973 }, + { x: 11.184000000000001, y: 181.55927071823305 }, + { x: 11.65, y: 176.89965554637234 }, + { x: 12.116000000000001, y: 172.2835640008871 }, + { x: 12.582, y: 167.66427378450243 }, + { x: 13.048, y: 162.97075741103689 }, + { x: 13.514000000000001, y: 158.22221852387048 }, + { x: 13.98, y: 153.78820594388907 }, + { x: 14.446000000000002, y: 149.8152888122305 }, + { x: 14.912, y: 145.70110260143542 }, + { x: 15.378, y: 141.4629675844993 }, + { x: 15.844000000000001, y: 137.08401689945958 }, + { x: 16.310000000000002, y: 131.73059446721498 }, + { x: 16.776, y: 126.52718635392284 }, + { x: 17.242, y: 121.01950043864039 }, + { x: 17.708000000000002, y: 115.81761587530343 }, + { x: 18.174, y: 110.90131914464186 }, + { x: 18.64, y: 107.10387792501399 }, + { x: 19.106, y: 103.63973323982688 }, + { x: 19.572000000000003, y: 101.86891243164328 }, + { x: 20.038, y: 100.03421789901918 }, + { x: 20.504, y: 98.59461997587309 }, + { x: 20.970000000000002, y: 97.44223682511652 }, + { x: 21.436, y: 96.5638138235315 }, + { x: 21.902, y: 95.68910699647085 }, + { x: 22.368000000000002, y: 94.85065221359693 }, + { x: 22.834, y: 93.86559828478762 }, + { x: 23.3, y: 92.78492098127305 }, + { x: 23.766000000000002, y: 91.54246519670939 }, + { x: 24.232000000000003, y: 90.32260071073563 }, + { x: 24.698, y: 89.14067605083252 }, + { x: 25.164, y: 88.22030406193058 }, + { x: 25.630000000000003, y: 87.40335871583454 }, + { x: 26.096, y: 86.38984563294906 }, + { x: 26.562, y: 85.13872688199034 }, + { x: 27.028000000000002, y: 83.79797552426481 }, + { x: 27.494, y: 82.246739525109 }, + { x: 27.96, y: 80.92674062089691 }, + { x: 28.426000000000002, y: 79.50401884345041 }, + { x: 28.892000000000003, y: 78.27331459750332 }, + { x: 29.358, y: 77.03563065868627 }, + { x: 29.824, y: 75.86729397486579 }, + { x: 30.290000000000003, y: 74.76228573892578 }, + { x: 30.756, y: 73.82337035985928 }, + { x: 31.222, y: 73.09012495192292 }, + { x: 31.688000000000002, y: 72.7036711163131 }, + { x: 32.154, y: 72.17484164924186 }, + { x: 32.620000000000005, y: 71.72458770607582 }, + { x: 33.086, y: 71.54223714753593 }, + { x: 33.552, y: 71.28182667839371 }, + { x: 34.018, y: 70.97123571821533 }, + { x: 34.484, y: 70.64702306027013 }, + { x: 34.95, y: 70.34169835227283 }, + { x: 35.416000000000004, y: 70.05861532857185 }, + { x: 35.882000000000005, y: 69.7387092279914 }, + { x: 36.348, y: 69.22779422963339 }, + { x: 36.814, y: 68.57118172563226 }, + { x: 37.28, y: 67.9249360255973 }, + { x: 37.746, y: 67.22471145916501 }, + { x: 38.212, y: 66.5719050988772 }, + { x: 38.678000000000004, y: 65.96239810711401 }, + { x: 39.144000000000005, y: 65.38307698694538 }, + { x: 39.61, y: 64.82212359263369 }, + { x: 40.076, y: 64.27099945825407 }, + { x: 40.542, y: 63.723694572279534 }, + { x: 41.008, y: 63.17612768007998 }, + { x: 41.474000000000004, y: 62.62550058035388 }, + { x: 41.940000000000005, y: 62.069751343064404 }, + { x: 42.406, y: 61.5069167136404 }, + { x: 42.872, y: 60.93540803926646 }, + { x: 43.338, y: 60.354061311875284 }, + { x: 43.804, y: 59.76253619025937 }, + { x: 44.27, y: 59.16164329212949 }, + { x: 44.736000000000004, y: 58.55284606592195 }, + { x: 45.202000000000005, y: 57.93809803959992 }, + { x: 45.668, y: 57.318981928652626 }, + { x: 46.134, y: 56.69690781740423 }, + { x: 46.6, y: 56.073165108329306 } +]; + +export const expectedCI = [ + { + x: 0, + mean: 140.09445349344628, + lower: 133.759959414904, + upper: 146.42894757198857, + predLower: 95.97635407424448, + predUpper: 184.21255291264808 + }, + { + x: 0.466, + mean: 141.32733597469277, + lower: 135.10577839837717, + upper: 147.54889355100838, + predLower: 97.22531039981813, + predUpper: 185.42936154956743 + }, + { + x: 0.932, + mean: 142.5449143431299, + lower: 136.43601087034116, + upper: 148.65381781591864, + predLower: 98.45864003046725, + predUpper: 186.63118865579256 + }, + { + x: 1.3980000000000001, + mean: 143.7479341695275, + lower: 137.7513864857008, + upper: 149.7444818533542, + predLower: 99.67708819108167, + predUpper: 187.8187801479733 + }, + { + x: 1.864, + mean: 144.93783339274336, + lower: 139.05332609568939, + upper: 150.82234068979733, + predLower: 100.8820924812507, + predUpper: 188.993574304236 + }, + { + x: 2.33, + mean: 146.11700371890495, + lower: 140.3442030421214, + upper: 151.88980439568851, + predLower: 102.0760442744814, + predUpper: 190.1579631633285 + }, + { + x: 2.7960000000000003, + mean: 147.28917685132384, + lower: 141.62772927152756, + upper: 152.95062443112013, + predLower: 103.26267494814888, + predUpper: 191.3156787544988 + }, + { + x: 3.262, + mean: 148.46001837240007, + lower: 142.90954908919468, + upper: 154.01048765560546, + predLower: 104.4476497654358, + predUpper: 192.47238697936433 + }, + { + x: 3.728, + mean: 149.6380796799177, + lower: 144.19819095413027, + upper: 155.07796840570512, + predLower: 105.6395198116625, + predUpper: 193.6366395481729 + }, + { + x: 4.194, + mean: 150.8363919178551, + lower: 145.50666125353362, + upper: 156.1661225821766, + predLower: 106.85131592513122, + predUpper: 194.821467910579 + }, + { + x: 4.66, + mean: 152.07526320848964, + lower: 146.85524136183176, + upper: 157.29528505514753, + predLower: 108.1033459292639, + predUpper: 196.0471804877154 + }, + { + x: 5.126, + mean: 153.3874509397144, + lower: 148.27665973628223, + upper: 158.49824214314657, + predLower: 109.42836691995159, + predUpper: 197.3465349594772 + }, + { + x: 5.5920000000000005, + mean: 154.8283171762387, + lower: 149.8262471156847, + upper: 159.8303872367927, + predLower: 110.8817406767868, + predUpper: 198.77489367569058 + }, + { + x: 6.058000000000001, + mean: 156.4972230162609, + lower: 151.60333064143097, + upper: 161.39111539109084, + predLower: 112.5628280197675, + predUpper: 200.4316180127543 + }, + { + x: 6.524, + mean: 158.58650331258556, + lower: 153.80020831726154, + upper: 163.3727983079096, + predLower: 114.66396353044465, + predUpper: 202.50904309472648 + }, + { + x: 6.99, + mean: 161.5047237264655, + lower: 156.82540577314944, + upper: 166.18404167978156, + predLower: 117.59371260579425, + predUpper: 205.41573484713675 + }, + { + x: 7.456, + mean: 166.2162040364665, + lower: 161.64319925248634, + upper: 170.78920882044667, + predLower: 122.31639476711098, + predUpper: 210.11601330582204 + }, + { + x: 7.922000000000001, + mean: 175.1580804702822, + lower: 170.6906775871182, + upper: 179.62548335344619, + predLower: 131.26914599185142, + predUpper: 219.04701494871296 + }, + { + x: 8.388, + mean: 192.5906566221435, + lower: 188.2280927192516, + upper: 196.95322052503542, + predLower: 148.71226963107122, + predUpper: 236.4690436132158 + }, + { + x: 8.854000000000001, + mean: 206.27528820650207, + lower: 202.01674401708166, + upper: 210.53383239592247, + predLower: 162.40712116313526, + predUpper: 250.14345524986888 + }, + { + x: 9.32, + mean: 202.24716892567784, + lower: 198.09176365786172, + upper: 206.40257419349396, + predLower: 158.38889406139162, + predUpper: 246.10544378996406 + }, + { + x: 9.786000000000001, + mean: 197.7355417450799, + lower: 193.68232736801335, + upper: 201.78875612214645, + predLower: 153.88683106941755, + predUpper: 241.58425242074225 + }, + { + x: 10.252, + mean: 193.25853858120885, + lower: 189.30649352159213, + upper: 197.21058364082558, + predLower: 149.41906388904667, + predUpper: 237.09801327337104 + }, + { + x: 10.718, + mean: 188.82405764669448, + lower: 184.9720798378098, + upper: 192.67603545557918, + predLower: 144.9934905254305, + predUpper: 232.65462476795847 + }, + { + x: 11.184000000000001, + mean: 184.4306623294997, + lower: 180.67756155185515, + upper: 188.18376310714427, + predLower: 140.60867416626544, + predUpper: 228.25265049273398 + }, + { + x: 11.65, + mean: 180.0717543901033, + lower: 176.4162438410432, + upper: 183.7272649391634, + predLower: 136.25801637899798, + predUpper: 223.88549240120864 + }, + { + x: 12.116000000000001, + mean: 175.73181136819846, + lower: 172.1724983977588, + upper: 179.29112433863813, + predLower: 131.92599451754464, + predUpper: 219.5376282188523 + }, + { + x: 12.582, + mean: 171.3725772153067, + lower: 167.90795316803874, + upper: 174.83720126257467, + predLower: 127.57435235492687, + predUpper: 215.17080207568654 + }, + { + x: 13.048, + mean: 166.91295158568622, + lower: 163.5413806941889, + upper: 170.28452247718354, + predLower: 123.12198937419925, + predUpper: 210.7039137971732 + }, + { + x: 13.514000000000001, + mean: 162.31658135508818, + lower: 159.03628864193072, + upper: 165.59687406824563, + predLower: 118.53255228722507, + predUpper: 206.10061042295126 + }, + { + x: 13.98, + mean: 157.87747574164575, + lower: 154.68653390392564, + upper: 161.06841757936587, + predLower: 114.10005015558372, + predUpper: 201.6549013277078 + }, + { + x: 14.446000000000002, + mean: 153.8042065428404, + lower: 150.70052181987828, + upper: 156.90789126580253, + predLower: 110.03305462755452, + predUpper: 197.5753584581263 + }, + { + x: 14.912, + mean: 149.68237525077177, + lower: 146.6636723128361, + upper: 152.70107818870744, + predLower: 105.91716705340352, + predUpper: 193.44758344814002 + }, + { + x: 15.378, + mean: 145.30892075173074, + lower: 142.37272669895228, + upper: 148.2451148045092, + predLower: 101.54932618497247, + predUpper: 189.068515318489 + }, + { + x: 15.844000000000001, + mean: 140.3312284783127, + lower: 137.47485610697194, + upper: 143.18760084965348, + predLower: 96.57691732780725, + predUpper: 184.08553962881817 + }, + { + x: 16.310000000000002, + mean: 134.3900021241016, + lower: 131.61053270493846, + upper: 137.16947154326476, + predLower: 90.6406440558562, + predUpper: 178.13936019234703 + }, + { + x: 16.776, + mean: 128.74647824206693, + lower: 126.04074416413455, + upper: 131.45221231999932, + predLower: 85.00174280988067, + predUpper: 172.49121367425317 + }, + { + x: 17.242, + mean: 123.65196481337188, + lower: 121.01653257766118, + upper: 126.28739704908257, + predLower: 79.91152146627593, + predUpper: 167.39240816046782 + }, + { + x: 17.708000000000002, + mean: 118.83569245681446, + lower: 116.26684665418661, + upper: 121.4045382594423, + predLower: 75.09921054652398, + predUpper: 162.57217436710494 + }, + { + x: 18.174, + mean: 113.74912944818641, + lower: 111.24285851980467, + upper: 116.25540037656815, + predLower: 70.01627823656361, + predUpper: 157.4819806598092 + }, + { + x: 18.64, + mean: 109.27677619515899, + lower: 106.82876093880513, + upper: 111.72479145151284, + predLower: 65.54722486168623, + predUpper: 153.00632752863174 + }, + { + x: 19.106, + mean: 105.08174859363334, + lower: 102.68735452741362, + upper: 107.47614265985305, + predLower: 61.355166242895756, + predUpper: 148.8083309443709 + }, + { + x: 19.572000000000003, + mean: 102.53489358658916, + lower: 100.18916838668356, + upper: 104.88061878649476, + predLower: 58.810949255765586, + predUpper: 146.25883791741273 + }, + { + x: 20.038, + mean: 100.04947558588303, + lower: 97.7471528451029, + upper: 102.35179832666316, + predLower: 56.32783825224466, + predUpper: 143.77111291952139 + }, + { + x: 20.504, + mean: 97.77820165877178, + lower: 95.51371213305708, + upper: 100.04269118448647, + predLower: 54.05854024718764, + predUpper: 141.49786307035592 + }, + { + x: 20.970000000000002, + mean: 95.5628876233653, + lower: 93.33037891306944, + upper: 97.79539633366116, + predLower: 51.84487101381365, + predUpper: 139.28090423291695 + }, + { + x: 21.436, + mean: 93.62891338687618, + lower: 91.42227862073582, + upper: 95.83554815301653, + predLower: 49.91221042196097, + predUpper: 137.3456163517914 + }, + { + x: 21.902, + mean: 92.24537467282973, + lower: 90.05829022982144, + upper: 94.43245911583801, + predLower: 48.52965416530136, + predUpper: 135.9610951803581 + }, + { + x: 22.368000000000002, + mean: 90.69604004038793, + lower: 88.52201169405123, + upper: 92.87006838672463, + predLower: 46.9809707806674, + predUpper: 134.41110930010845 + }, + { + x: 22.834, + mean: 89.48288408640215, + lower: 87.31530025627718, + upper: 91.65046791652712, + predLower: 45.7681348501077, + predUpper: 133.19763332269662 + }, + { + x: 23.3, + mean: 88.23739027276046, + lower: 86.06958041239908, + upper: 90.40520013312185, + predLower: 44.52262982823593, + predUpper: 131.952150717285 + }, + { + x: 23.766000000000002, + mean: 87.10561913723745, + lower: 84.93091478012175, + upper: 89.28032349435315, + predLower: 43.39051625308144, + predUpper: 130.82072202139346 + }, + { + x: 24.232000000000003, + mean: 86.11821234551172, + lower: 83.9300080563392, + upper: 88.30641663468424, + predLower: 42.40243579810677, + predUpper: 129.83398889291666 + }, + { + x: 24.698, + mean: 85.23674417267169, + lower: 83.02855566134275, + upper: 87.44493268400063, + predLower: 41.51996275371254, + predUpper: 128.95352559163084 + }, + { + x: 25.164, + mean: 84.90516165560983, + lower: 82.67067860306851, + upper: 87.13964470815115, + predLower: 41.1870441796299, + predUpper: 128.62327913158975 + }, + { + x: 25.630000000000003, + mean: 84.77731202230456, + lower: 82.51044368851436, + upper: 87.04418035609476, + predLower: 41.057527334199925, + predUpper: 128.49709671040918 + }, + { + x: 26.096, + mean: 84.44151458321417, + lower: 82.13642692902309, + upper: 86.74660223740526, + predLower: 40.71973156576403, + predUpper: 128.16329760066432 + }, + { + x: 26.562, + mean: 83.73676507707611, + lower: 81.38790883107366, + upper: 86.08562132307857, + predLower: 40.01265265845895, + predUpper: 127.46087749569328 + }, + { + x: 27.028000000000002, + mean: 82.84972064041472, + lower: 80.45185038633875, + upper: 85.24759089449068, + predLower: 39.12294780171931, + predUpper: 126.57649347911013 + }, + { + x: 27.494, + mean: 81.54841870730654, + lower: 79.09660359060277, + upper: 84.00023382401031, + predLower: 37.818654490037034, + predUpper: 125.27818292457604 + }, + { + x: 27.96, + mean: 80.55444404519534, + lower: 78.04407106539155, + upper: 83.06481702499913, + predLower: 36.82135755876934, + predUpper: 124.28753053162134 + }, + { + x: 28.426000000000002, + mean: 79.23411525402756, + lower: 76.66088631958004, + upper: 81.80734418847507, + predLower: 35.497375683266625, + predUpper: 122.97085482478849 + }, + { + x: 28.892000000000003, + mean: 78.25136648652209, + lower: 75.61129047993744, + upper: 80.89144249310674, + predLower: 34.51064309913363, + predUpper: 121.99208987391054 + }, + { + x: 29.358, + mean: 77.2971886986043, + lower: 74.58656976335591, + upper: 80.00780763385269, + predLower: 33.55215085265411, + predUpper: 121.0422265445545 + }, + { + x: 29.824, + mean: 76.5064907913059, + lower: 73.72191394305275, + upper: 79.29106763955903, + predLower: 32.75680794268042, + predUpper: 120.25617363993138 + }, + { + x: 30.290000000000003, + mean: 75.6211402845101, + lower: 72.75945529911344, + upper: 78.48282526990677, + predLower: 31.866481994367646, + predUpper: 119.37579857465256 + }, + { + x: 30.756, + mean: 74.6693154727071, + lower: 71.72761983794487, + upper: 77.61101110746932, + predLower: 30.90935141491716, + predUpper: 118.42927953049704 + }, + { + x: 31.222, + mean: 73.73363618140652, + lower: 70.70925773623291, + upper: 76.75801462658013, + predLower: 29.968036149976378, + predUpper: 117.49923621283668 + }, + { + x: 31.688000000000002, + mean: 73.06723025992105, + lower: 69.95770999598975, + upper: 76.17675052385235, + predLower: 29.29566417640872, + predUpper: 116.83879634343337 + }, + { + x: 32.154, + mean: 72.3833793896555, + lower: 69.18645476056504, + upper: 75.58030401874596, + predLower: 28.60551731056841, + predUpper: 116.16124146874259 + }, + { + x: 32.620000000000005, + mean: 71.82098219165707, + lower: 68.53457116798884, + upper: 75.10739321532529, + predLower: 28.03649431583539, + predUpper: 115.60547006747873 + }, + { + x: 33.086, + mean: 71.59356218222769, + lower: 68.21574820437041, + upper: 74.97137616008496, + predLower: 27.802118858211806, + predUpper: 115.38500550624357 + }, + { + x: 33.552, + mean: 71.335081652605, + lower: 67.86409956656004, + upper: 74.80606373864995, + predLower: 27.536353385985976, + predUpper: 115.13380991922402 + }, + { + x: 34.018, + mean: 71.0243629757031, + lower: 67.45858598682688, + upper: 74.59013996457932, + predLower: 27.21802043645559, + predUpper: 114.83070551495061 + }, + { + x: 34.484, + mean: 70.58929415772968, + lower: 66.92722180081613, + upper: 74.25136651464324, + predLower: 26.77500818752634, + predUpper: 114.40358012793303 + }, + { + x: 34.95, + mean: 69.95883722530105, + lower: 66.19908432381153, + upper: 73.71859012679057, + predLower: 26.136278844807727, + predUpper: 113.78139560579439 + }, + { + x: 35.416000000000004, + mean: 69.22226496259478, + lower: 65.36355153278889, + upper: 73.08097839240067, + predLower: 25.39110537874562, + predUpper: 113.05342454644394 + }, + { + x: 35.882000000000005, + mean: 68.45743318593908, + lower: 64.49857523110073, + upper: 72.41629114077743, + predLower: 24.617343799190856, + predUpper: 112.29752257268731 + }, + { + x: 36.348, + mean: 67.53451623107605, + lower: 63.47441736529378, + upper: 71.59461509685833, + predLower: 23.68516864264079, + predUpper: 111.38386381951132 + }, + { + x: 36.814, + mean: 66.59681114739386, + lower: 62.43445498610934, + upper: 70.7591673086784, + predLower: 22.73787716644911, + predUpper: 110.45574512833862 + }, + { + x: 37.28, + mean: 65.72489337582422, + lower: 61.45933663057312, + upper: 69.99045012107533, + predLower: 21.856045026700173, + predUpper: 109.59374172494827 + }, + { + x: 37.746, + mean: 64.89307735559865, + lower: 60.523443572242414, + upper: 69.26271113895488, + predLower: 21.013986884941268, + predUpper: 108.77216782625604 + }, + { + x: 38.212, + mean: 64.14372046563685, + lower: 59.66919434833054, + upper: 68.61824658294314, + predLower: 20.254060349546364, + predUpper: 108.03338058172733 + }, + { + x: 38.678000000000004, + mean: 63.44806123688458, + lower: 58.86788350361031, + upper: 68.02823897015884, + predLower: 19.547504188028483, + predUpper: 107.34861828574067 + }, + { + x: 39.144000000000005, + mean: 62.779767456314794, + lower: 58.09323017571799, + upper: 67.4663047369116, + predLower: 18.86798643101463, + predUpper: 106.69154848161496 + }, + { + x: 39.61, + mean: 62.121046314567764, + lower: 57.32748867784501, + upper: 66.91460395129052, + predLower: 18.197714519859034, + predUpper: 106.0443781092765 + }, + { + x: 40.076, + mean: 61.460760569538785, + lower: 56.55956505508932, + upper: 66.36195608398825, + predLower: 17.525551470203126, + predUpper: 105.39596966887444 + }, + { + x: 40.542, + mean: 60.79194199753929, + lower: 55.782530889908664, + upper: 65.80135310516992, + predLower: 16.844529323108276, + predUpper: 104.7393546719703 + }, + { + x: 41.008, + mean: 60.11033617264598, + lower: 54.9921684008976, + upper: 65.22850394439436, + predLower: 16.150393924375784, + predUpper: 104.07027842091617 + }, + { + x: 41.474000000000004, + mean: 59.41438538157457, + lower: 54.18695364558876, + upper: 64.64181711756038, + predLower: 15.441587839390849, + predUpper: 103.38718292375829 + }, + { + x: 41.940000000000005, + mean: 58.70535720914797, + lower: 53.368185365452426, + upper: 64.0425290528435, + predLower: 14.719378938560155, + predUpper: 102.69133547973578 + }, + { + x: 42.406, + mean: 57.98432409079858, + lower: 52.536964772376635, + upper: 63.431683409220526, + predLower: 13.984839949782952, + predUpper: 101.98380823181421 + }, + { + x: 42.872, + mean: 57.25234697274276, + lower: 51.69437941985281, + upper: 62.810314525632705, + predLower: 13.239032118593627, + predUpper: 101.26566182689189 + }, + { + x: 43.338, + mean: 56.510621020788705, + lower: 50.84164910217121, + upper: 62.1795929394062, + predLower: 12.483150916936943, + predUpper: 100.53809112464046 + }, + { + x: 43.804, + mean: 55.76075597251689, + lower: 49.9804063784796, + upper: 61.54110556655418, + predLower: 11.71880639531522, + predUpper: 99.80270554971855 + }, + { + x: 44.27, + mean: 55.0049914560893, + lower: 49.11291204686395, + upper: 60.89707086531465, + predLower: 10.948238501563651, + predUpper: 99.06174441061495 + }, + { + x: 44.736000000000004, + mean: 54.24577082632284, + lower: 48.241629120794485, + upper: 60.249912531851194, + predLower: 10.173890916889064, + predUpper: 98.31765073575662 + }, + { + x: 45.202000000000005, + mean: 53.485459860120926, + lower: 47.36894165156228, + upper: 59.60197806867957, + predLower: 9.398129751266339, + predUpper: 97.57278996897551 + }, + { + x: 45.668, + mean: 52.72576454674652, + lower: 46.49657263355977, + upper: 58.954956459933264, + predLower: 8.62266133367546, + predUpper: 96.82886775981757 + }, + { + x: 46.134, + mean: 51.96817562604234, + lower: 45.62602864673715, + upper: 58.31032260534753, + predLower: 7.848976750285338, + predUpper: 96.08737450179933 + }, + { + x: 46.6, + mean: 51.2140413192692, + lower: 44.758672682001, + upper: 57.6694099565374, + predLower: 7.078424575254928, + predUpper: 95.34965806328347 + } +]; diff --git a/packages/vutils/__tests__/lowess-compatibility.test.ts b/packages/vutils/__tests__/lowess-compatibility.test.ts new file mode 100644 index 0000000..a7562fb --- /dev/null +++ b/packages/vutils/__tests__/lowess-compatibility.test.ts @@ -0,0 +1,27 @@ +import { regressionLowess } from '../src/common/regression-lowess'; +import { lowessScatterData, expectedGrid, expectedCI } from './data/lowess-scatter'; + +function expectNear(actual: number, expected: number) { + expect(Number.isFinite(actual)).toBe(true); + expect(Math.abs(actual - expected)).toBeLessThan(1e-8); +} + +test('preserves the scatter LOWESS curve and confidence intervals', () => { + expect(lowessScatterData).toHaveLength(406); + const model = regressionLowess(lowessScatterData); + const grid = model.evaluateGrid(101); + const ci = model.confidenceInterval(101); + expect(grid).toHaveLength(101); + expect(ci).toHaveLength(101); + grid.forEach((point, i) => { + expectNear(point.x, expectedGrid[i].x); + expectNear(point.y, expectedGrid[i].y); + }); + const keys = ['x', 'mean', 'lower', 'upper', 'predLower', 'predUpper'] as const; + ci.forEach((point, i) => { + keys.forEach(key => expectNear(point[key], expectedCI[i][key])); + }); + // predict and CI use the non-robust fit; evaluateGrid uses robust iterations. + const predicted = model.predict(expectedCI.map(point => point.x)) as number[]; + predicted.forEach((value, i) => expectNear(value, expectedCI[i].mean)); +});