Bizarre bug with `Statistics.varm()`
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
Start by running the minimal Julia reproduction in the issue and inspect the Statistics.varm() entry point. Compare its repeated results with var() for the shown 3D array and dimensions, then add a regression test that verifies stable, correct variance results across repeated calls.
Written by the indexing model from the issue text.
Description
This is a very strange bug that took me forever to isolate because it seems to be affected by changes in code that should have no affect on it. I came across it when testing a set of wrappers for the built-in array reduction functions (actually, I realized var() has a mean argument so varm() is pretty much useless to me, but I've spent too long struggling with this not to follow up with a bug report).
Here's a minimal example that I've come up with:
using Statistics
using Random
# Random 3D array
Random.seed!(1)
a = randn((4, 5, 3))
# @show a
dims = 2
for i in 1:2
println("\n\n########## $i ##########\n")
@show a
println()
# Calculate the correct mean
m = mean(a; dims=dims)
m2 = dropdims(m; dims=dims)
@show m2
println()
# varm() with correct mean
@show varm(a, m2; dims=dims)
println()
# var()
@show var(a; dims=dims)
println()
end
And the output:
########## 1 ##########
a = [0.297288 -0.839027 0.529966 0.458791 -0.693654; 0.382396 0.311111 0.431422 -0.522337 -1.77383; -0.597634 2.29509 0.583708 0.408396 0.120766; -0.0104452 -2.26709 0.963272 -0.0504512 -0.757633]
[-1.72976 -0.0633746 -1.22006 -0.066768 0.378599; 0.795949 1.33694 -0.0531773 1.22246 -0.645597; 0.670062 -0.0731486 -0.165136 0.567695 -0.664646; 0.550852 -0.745464 -2.11537 0.763499 -1.80303]
[-1.28905 1.73517 -0.850056 -0.0556471 -2.0073; -0.33527 1.29992 1.12941 0.75939 -0.568859; 0.0704676 0.206364 -1.34853 -0.0302669 -1.14963; 0.341794 -1.00886 1.24029 -0.361083 1.88062]
m2 = [-0.0493272 -0.540271 -0.493378; -0.234248 0.531316 0.456917; 0.562065 0.0669652 -0.450319; -0.424469 -0.669902 0.418553]
varm(a, m2; dims=dims) = [0.438172; 0.893525; 1.14177; 1.43584]
[1.1581; 1.08375; 0.311409; 2.29195]
[2.35685; 0.994507; 0.797163; 1.58502]
var(a; dims=dims) = [0.438172; 0.893525; 1.14177; 1.43584]
[0.793235; 0.730876; 0.305804; 1.73098]
[2.05257; 0.73354; 0.543679; 1.36603]
########## 2 ##########
a = [0.297288 -0.839027 0.529966 0.458791 -0.693654; 0.382396 0.311111 0.431422 -0.522337 -1.77383; -0.597634 2.29509 0.583708 0.408396 0.120766; -0.0104452 -2.26709 0.963272 -0.0504512 -0.757633]
[-1.72976 -0.0633746 -1.22006 -0.066768 0.378599; 0.795949 1.33694 -0.0531773 1.22246 -0.645597; 0.670062 -0.0731486 -0.165136 0.567695 -0.664646; 0.550852 -0.745464 -2.11537 0.763499 -1.80303]
[-1.28905 1.73517 -0.850056 -0.0556471 -2.0073; -0.33527 1.29992 1.12941 0.75939 -0.568859; 0.0704676 0.206364 -1.34853 -0.0302669 -1.14963; 0.341794 -1.00886 1.24029 -0.361083 1.88062]
m2 = [-0.0493272 -0.540271 -0.493378; -0.234248 0.531316 0.456917; 0.562065 0.0669652 -0.450319; -0.424469 -0.669902 0.418553]
varm(a, m2; dims=dims) = [0.438172; 0.893525; 1.14177; 1.43584]
[1.1581; 1.08375; 0.311409; 2.29195]
[34.0631; 35.6667; 32.8363; 20.5391]
var(a; dims=dims) = [0.438172; 0.893525; 1.14177; 1.43584]
[0.793235; 0.730876; 0.305804; 1.73098]
[2.05257; 0.73354; 0.543679; 1.36603]
First notice that the return value of varm() is different from var(). At first I thought I was using it incorrectly, but then also notice that the last row of the returned matrix is different between the two loops, even though nothing has changed. I wondered if a was somehow getting modified, but you can see the values of it and m2 don't change.
Also, notice that in the 2nd iteration the magnitude of the values is way off. There's no way they should be > 20. I first caught this when looping through all valid combinations of dims for a 3D array obtained from randn() (so var ~= 1), and I was seeing values of 1e8 or more. They're usually more consistent in the first few elements of the output and diverge towards the end.
The bug seems to be deterministic from one run of the script to another, but I would also see it happen or not happen based on how I iterated over the possible values of dims. Initially it would happen on a specific iteration each time, but if I tried testing just that value it disappeared.
I see the same thing on two different machines, running different operating systems and different versions of Julia. Here's the output of versioninfo() for both:
Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
OS: macOS (x86_64-apple-darwin14.5.0)
CPU: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Julia Version 1.0.2
Commit d789231e99 (2018-11-08 20:11 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Xeon(R) CPU E5-2650 0 @ 2.00GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-6.0.0 (ORCJIT, sandybridge)
- Dominant language
- Julia
- Stars
- 93
- Forks
- 51
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
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.
More from JuliaStats/Statistics.jl
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
JuliaStats/Statistics.jl#221 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
JuliaStats/Statistics.jl#219 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
JuliaStats/Statistics.jl#207 · 1 comment ·
-
Difficulty 3/5 Half a day Newbie friendliness 55/100
JuliaStats/Statistics.jl#220 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
JuliaStats/Statistics.jl#215 · 1 reaction ·
All issues in JuliaStats/Statistics.jl
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
JuliaParallel/Hwloc.jl#93 ·
-
Affine transformations of `JohnsonSU` could return a `JohnsonSU`Possibly taken @devmotion claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
JuliaStats/Distributions.jl#2117 ·
Maintainers usually reply within 2 days
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
TomerAberbach/profiler-md#970 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
grame-cncm/faust#1409 ·
Maintainers usually reply within 1 day