[BUG] mx.var / mx.std wrong for offset data
Open
Nobody has claimed this yet.
bug
low priority
- Dominant language
- C++
- Stars
- 28.5k
- Forks
- 2.3k
- Avg merge
- 3d 8h
- Merged PRs (30d)
- 62
Description
☑️ I understand it is strictly prohibited to use AI to write issues.
Describe the bug
For data with a large mean relative to its spread, MLX variance is wrong by hundreds of percent while NumPy is exact:
x = 1e6 + 10 * noise (10,000 float32 samples, true var = 100)
mx.var(x) = 589.69 (+489%)
np.var(x) = 97.53
mx.std(x) = 24.28 (want ~10)
mx.mean(x) off by ~22 (mean = 1e6, rel err 2e-5)
To Reproduce
Include code snippet
import mlx.core as mx, numpy as np
mx.set_default_device(mx.cpu)
rng = np.random.RandomState(0)
noise = rng.standard_normal(10_000).astype(np.float32)
x = (1e6 + noise * 10).astype(np.float32)
print(float(mx.var(mx.array(x)))) # 589.686 (true ~100)
print(float(np.var(x))) # 97.527
print(abs(float(mx.mean(mx.array(x))) - 1e6)) # ~22 <- the poison
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the Python reproduction with mx.var, mx.std, and mx.mean on the generated offset data, comparing each result with NumPy. Trace the MLX implementations behind these entry points and verify that the corrected results remain accurate for large-mean, small-spread float32 inputs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- data, machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100