Skip to content

Stabilize piecewise-constant weight normalization - #4294

Closed
FlorianPfaff wants to merge 17 commits into
mainfrom
agent/stabilize-piecewise-constant-weights-20260713
Closed

Stabilize piecewise-constant weight normalization#4294
FlorianPfaff wants to merge 17 commits into
mainfrom
agent/stabilize-piecewise-constant-weights-20260713

Conversation

@FlorianPfaff

Copy link
Copy Markdown
Owner

Summary

  • scale finite nonnegative piecewise-constant weights by their maximum before normalization
  • preserve relative interval mass while avoiding overflow in the raw mean and the mean * 2π denominator
  • add a backend-aware regression using the active floating-point dtype's maximum finite value

Bug

PiecewiseConstantDistribution normalized constructor weights with

mean_weight = mean(w)
self.w = w / (mean_weight * 2.0 * pi)

Valid individually finite weights near the dtype maximum can overflow while computing the mean or while multiplying the mean by . The resulting infinite denominator collapses every stored density weight to zero. For example, [finfo.max, finfo.max / 2] should retain a 2:1 mass ratio, but the current implementation produces [0, 0].

Fix

Divide all weights by their maximum finite value first. The scaled values lie in [0, 1], so their mean and subsequent normalization remain finite while the relative weights are unchanged.

Validation

  • isolated numerical reproduction: the previous formula yields [0, 0] for [finfo.max, finfo.max / 2]
  • the scale-first formula yields densities [2/(3π), 1/(3π)] and integrates to one
  • regression derives the maximum finite value from the active backend dtype and checks the expected normalized density
  • modified source and regression modules pass Python bytecode compilation
  • branch is two commits ahead of current main, zero behind; final diff is 8 production-line changes plus one focused 23-line regression

The full multi-backend test matrix is delegated to GitHub Actions.

@github-actions

github-actions Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

MegaLinter analysis: Success

Descriptor Linter Files Fixed Errors Warnings Elapsed time
✅ COPYPASTE jscpd yes no no 22.61s
✅ JSON prettier 7 0 0 0 1.31s
✅ JSON v8r 7 0 0 4.69s
✅ MARKDOWN markdownlint 68 0 0 0 1.9s
✅ MARKDOWN markdown-table-formatter 68 0 0 0 0.87s
✅ PYTHON black 1490 182 0 0 96.91s
✅ PYTHON isort 1490 328 0 0 2.69s
✅ REPOSITORY betterleaks yes no no 2.44s
✅ REPOSITORY checkov yes no no 54.4s
✅ REPOSITORY gitleaks yes no no 15.19s
✅ REPOSITORY git_diff yes no no 0.14s
✅ REPOSITORY secretlint yes no no 68.86s
✅ REPOSITORY syft yes no no 5.93s
✅ REPOSITORY trivy-sbom yes no no 6.28s
✅ REPOSITORY trufflehog yes no no 32.31s
✅ YAML prettier 11 0 0 0 0.75s
✅ YAML v8r 11 0 0 11.44s
✅ YAML yamllint 11 0 0 0.57s

Notices

📣 MegaLinter 9.5.0 is out! Discover the new features and security recommendations in the release announcement. (Skip this info by defining SECURITY_SUGGESTIONS: false)

See detailed reports in MegaLinter artifacts

Your project could benefit from a custom flavor, which would allow you to run only the linters you need, and thus improve runtime performances. (Skip this info by defining FLAVOR_SUGGESTIONS: false)

  • Documentation: Custom Flavors
  • Command: npx mega-linter-runner@9.6.0 --custom-flavor-setup --custom-flavor-linters PYTHON_BLACK,PYTHON_ISORT,COPYPASTE_JSCPD,JSON_V8R,JSON_PRETTIER,MARKDOWN_MARKDOWNLINT,MARKDOWN_MARKDOWN_TABLE_FORMATTER,REPOSITORY_CHECKOV,REPOSITORY_GIT_DIFF,REPOSITORY_GITLEAKS,REPOSITORY_BETTERLEAKS,REPOSITORY_SECRETLINT,REPOSITORY_SYFT,REPOSITORY_TRIVY_SBOM,REPOSITORY_TRUFFLEHOG,YAML_PRETTIER,YAML_YAMLLINT,YAML_V8R

MegaLinter is graciously provided by OX Security
Show us your support by starring ⭐ the repository

@FlorianPfaff
FlorianPfaff force-pushed the agent/stabilize-piecewise-constant-weights-20260713 branch from 261a4c5 to 340843f Compare July 13, 2026 16:30

Copy link
Copy Markdown
Owner Author

Closing as duplicate of #4295. The latter carries the same piecewise-constant normalization repair on the preferred branch.

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.

1 participant