-
Notifications
You must be signed in to change notification settings - Fork 87
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
Fix machine error negative values on m2 #238
base: master
Are you sure you want to change the base?
Conversation
src/jarque_bera.jl
Outdated
@@ -63,7 +63,7 @@ function JarqueBeraTest(y::AbstractVector{T}) where T<:Real | |||
m4r += yi^4 / n | |||
end | |||
# compute central moments (http://mathworld.wolfram.com/CentralMoment.html) | |||
m2 = -m1r^2 + m2r | |||
m2 = abs(-m1r^2 + m2r) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what the (dis)advantages of this approach are compared with
m2 = abs(-m1r^2 + m2r) | |
m2 = max(-m1r^2 + m2r, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just about to pull a request for that option! It is better. At least mathematically more pleasant. Unless abs
is faster than max
... but probably not.
And now I am thinking that in the example that fails the second moment is not exactly zero which means that abs
in that scenario is more precise than max
that would set the second moment to exactly zero.
Now I think I should revert back to abs
again! Okay, I'll sleep on it.
Fix machine error negative values on m2. Fix NaN when m2 equals 0. Improve performance.
Turns out that neither The new change should improve performance since it calculates moments recursively replacing powers per products. In fact this approach prevents entirely negative numbers in the example that previously failed. To deal with zeros in m2 the calculation stops and returns a default for zero JarqueBeraTest result. |
avoid last operation machine error problems
Fix negative machine error values
Have you tried only replacing |
Also, could you add a test that fails without this PR? |
Maybe |
According to BTW, apparently precision will be better for |
I found an issue with this test when m2 takes negative values due to machine error precision, see below:
x = [0.09999999999999981, 0.09999999999999978, 0.09999999999999978, 0.09999999999999978, 0.09999999999999977, 0.09999999999999983, 0.09999999999999981, 0.0999999999999998, 0.09999999999999981, 0.09999999999999978];
JarqueBeraTest(x)
ERROR: DomainError with -1.734723475976807e-18:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.