Skip to content

Reject nonfinite weights before weighted sampling - #3848

Merged
quaquel merged 4 commits into
mesa:mainfrom
SahilKumar75:fix/nonfinite_weights
Sep 17, 2026
Merged

quaquel merged 4 commits into
mesa:mainfrom
SahilKumar75:fix/nonfinite_weights

Conversation

@SahilKumar75

Copy link
Copy Markdown
Contributor

Note

Use this template for bug fixes only. For enhancements or new features, use the feature template and get maintainer approval in an issue or discussion before opening a PR.

Pre PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Reject NaN and infinite weights before weighted sampling chooses an algorithm. Without this validation, sampling without replacement can silently select agents using invalid probability weights.

Bug / Issue

Fixes #3847.

AgentSet.select_random() currently accepts NaN and positive infinity when replace=False, although the replacement path rejects them. This makes the result depend on invalid generated keys instead of a valid probability distribution.

Implementation

Validate that every resolved weight is finite before checking negativity or total weight. The validation applies consistently to both replacement modes.

Testing

Added parameterized regression coverage for NaN and positive infinity. All 41 AgentSet tests pass. Ruff formatting passes. Ruff lint passes apart from two existing UP038 findings in agentset.py that are also present on current main.

Additional Notes

This validation is separate from PR #3845, which addresses zero weight sampling capacity.

@github-actions github-actions Bot added the testing Release notes label label Sep 10, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.8% [+0.1%, +1.4%] 🔵 +0.5% [+0.2%, +0.8%]
BoltzmannWealth large 🔵 +3.2% [+2.7%, +3.8%] 🔵 +1.1% [+0.2%, +2.1%]
Schelling small 🔵 +1.7% [+1.1%, +2.3%] 🔵 -0.3% [-1.2%, +0.6%]
Schelling large 🔵 +0.4% [-0.0%, +0.9%] 🟢 -3.8% [-4.2%, -3.2%]
WolfSheep small 🔵 +1.4% [+0.7%, +2.1%] 🔵 -1.8% [-5.5%, +1.8%]
WolfSheep large 🔵 +1.3% [+0.2%, +2.6%] 🔵 +1.0% [-0.2%, +2.0%]
SugarscapeG1mt small 🔵 +1.5% [+1.0%, +2.0%] 🔵 -0.9% [-2.7%, +0.8%]
SugarscapeG1mt large 🔵 +0.6% [-0.2%, +1.4%] 🔵 -0.6% [-2.9%, +1.7%]
BoidFlockers small 🔵 -1.2% [-1.7%, -0.7%] 🔵 +1.0% [+0.6%, +1.4%]
BoidFlockers large 🔵 +0.4% [+0.1%, +0.8%] 🔵 -0.0% [-0.3%, +0.2%]
MultiLevelAllianceModel small 🔵 -1.2% [-2.3%, +0.1%] 🔵 -0.5% [-0.9%, -0.0%]
MultiLevelAllianceModel large 🔵 +0.1% [-1.7%, +2.4%] 🔵 -0.1% [-1.6%, +1.6%]

Comment thread mesa/agentset.py Outdated
"Expected str, Callable, Sequence[float], or None."
)

if any(not np.isfinite(x) for x in w):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why use numpy here on each individual number. Might it not be faster (too be tested) to cast the weights to a numpy array and vectorize this check? Or alternatively compare with inf directly using python itself instead of numpy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Switched to math.isfinite. Timed all three over 100 runs on a list of 100k floats: numpy called per element 8.2s, vectorized np.isfinite(np.asarray(w)).all() 0.34s, math.isfinite 0.15s. Since w is already a plain list, building the array costs more than the vectorized check saves, so plain Python wins at every size I tried. Pushed in b9ccd50.

@quaquel quaquel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

minor question/change. Looks fine otherwise.

@quaquel
quaquel merged commit 20841b1 into mesa:main Sep 17, 2026
11 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Weighted sampling without replacement accepts nonfinite weights

2 participants