Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better heuristic tolerance for @test_approx_eq? #19318

Closed
chenych11 opened this issue Nov 14, 2016 · 3 comments
Closed

A better heuristic tolerance for @test_approx_eq? #19318

chenych11 opened this issue Nov 14, 2016 · 3 comments
Labels
testsystem The unit testing framework and Test stdlib

Comments

@chenych11
Copy link

chenych11 commented Nov 14, 2016

The @test_approx_eq macro does not work as expected with arrays. For example

using Base.Test

a = rand(Float32, 1000)
sigmoid(x) = 1.0 ./ (1.0 + exp(-x))
expa = exp(a)
siga = sigmoid(a)

@test_approx_eq(expa, siga)            # PASS
@test_approx_eq_eps(expa, siga, 1e-4)  # FAIL

Both tests (indicated by the last two lines) should fail, because exp(x) and sigmoid(x) are different.
Currently, this macro defined as follows.

macro test_approx_eq_eps(a, b, c)
    :(test_approx_eq($(esc(a)), $(esc(b)), $(esc(c)), $(string(a)), $(string(b))))
end

The function called by this macro is defined like this

test_approx_eq(va, vb, astr, bstr) = test_approx_eq(va, vb, 1E4*length(linearindices(va))*max(array_eps(va), array_eps(vb)), astr, bstr)

I think the primary reason for this issue is the length dependent tolerance heuristic: Eps = 1E4*length(linearindices(va))*max(array_eps(va), array_eps(vb))
Currently, the overload function test_approx_eq(va, vb, Eps, astr, bstr) computes the difference of two arrays va and vb using the img-norm, which is defined as the maximum absolute value of va - vb. The img-norm, which differs from the 1-norm or 2-norm, does not sum over the dimensions. So, this heuristic, which is dependent on the length of the arrays, is problematic when the length of the arrays are large.

By the way, I don't know if there are some justification to use the magic constant 1E4 as a factor of the heuristic tolerance.

The source code can be found at base/test.jl.

(commit: 72725a1)

@stevengj
Copy link
Member

stevengj commented Nov 14, 2016

@test_approx_eq should be deprecated in favor of isapprox, which can be called via @test expa ≈ siga for the default (√ɛ) tolerance. (This test correctly fails on your example above.)

@stevengj stevengj added the testsystem The unit testing framework and Test stdlib label Nov 14, 2016
@yuyichao
Copy link
Contributor

So dup of #4615?

@stevengj
Copy link
Member

Yes, I think so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testsystem The unit testing framework and Test stdlib
Projects
None yet
Development

No branches or pull requests

3 participants