Skip to content

[Fix] Center both operands in paddle.linalg.cov - #80046

Open
yiwenmu1 wants to merge 1 commit into
PaddlePaddle:developfrom
yiwenmu1:fix/cov-center-both-operands
Open

yiwenmu1 wants to merge 1 commit into
PaddlePaddle:developfrom
yiwenmu1:fix/cov-center-both-operands

Conversation

@yiwenmu1

@yiwenmu1 yiwenmu1 commented Dec 1, 2026

Copy link
Copy Markdown

PR Category

User Experience

PR Types

Bug fixes

Description

For the float64 input [1000000000, 1000000001, 1000000003], paddle.linalg.cov returned -57.27131152153015 on macOS arm64 CPU. Subtracting the first timestamp gives [0, 1, 3], whose sample variance is 7/3.

The right operand of the covariance matrix product retained the original offset. A rounding error in the mean could therefore dominate the covariance. Center both operands and apply the observation weights to the centered right operand. The example now gives 2.3333333333333357.

The regression cases compare against NumPy on offset-free samples. They cover scalar variance and the full covariance matrix, including both floating-point types, observation layouts, normalization settings and weight types.

Tested through the public APIs on the Paddle 3.3.1 CPU runtime with this PR's Python cov implementation. The original function body matches develop at 865a2a62f662. Before the fix, 35 of 37 new subcases failed. After the fix, all 22 test methods in test_cov.py and all 9 in test_corr.py passed. All applicable repository pre-commit hooks passed on the changed files.

是否引起精度变化

是。大偏移输入在矩阵乘法前两侧均减去均值,减少浮点消去误差。

@Paddle-Review-Bot

Paddle-Review-Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Paddle-Bot Review Board (review完成)

序号 位置 优先级 规则来源 状态

可维护性建议(总分 93.4 / 100,只给建议,不影响评审结论)

序号 位置 维度 状态
1 新增测试类命名与文件约定不一致 约定偏离 💡
2 期望值计算未复用 numpy_cov 辅助函数 约定偏离 💡
各维度得分(仅评本 PR 改动,点击展开)
86 命名质量
87 底层优雅度
88 约定偏离
1 2
90 逻辑清晰度
92 设计一致性
92 中层优雅度
93 AI 生成债务
93 高层优雅度
93 测试策略
94 契约一致性
95 抽象适配度
95 API 表面一致性
95 错误处理一致性
95 初始化耦合
95 类型安全
96 跨模块架构
96 包组织结构
100 依赖健康度
100 未完成迁移
— 鉴权一致性

Powered by Nyanpasu claude with DeepSeek-V4.1-Flash 默认推理级别, please check the suggestions carefully.

self.weightshape = [10]


class TestCovLargeOffset(unittest.TestCase):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 可维护性建议(约定偏离,不阻塞合入):同文件其余 10 个测试类均使用 Cov_Test / Cov_TestN / Cov_Test_ZeroSize 前缀(第 34、194、251-333、342 行),此新增类使用 TestCovLargeOffset,与既有命名约定不一致,且插在 Cov_Test2 与 Cov_Test3 之间打断了 Cov_TestN 的排列。建议改名为 Cov_Test_LargeOffset(或 Cov_Test10)并放到 Cov_Test2 之后,便于按名字定位 cov 用例。

if aw is None
else paddle.to_tensor(aw, dtype=dtype),
).numpy()
expected = np.cov(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 可维护性建议(约定偏离,不阻塞合入):本文件已在第 24 行定义 numpy_cov(...) 辅助函数并统一处理 ddof=int(ddof),其余用例均通过它计算期望值;此处内联调用 np.cov(..., ddof=int(ddof), ...) 重复了同样的转换。建议改为 expected = numpy_cov(offsets, ddof=ddof, fweights=fw, aweights=aw),与同文件用例保持一致。

This branch has not been deployed

No deployments
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.

2 participants