nardew / talipp

talipp - incremental technical analysis library for python
https://nardew.github.io/talipp
MIT License
365 stars 58 forks source link

Uses rtol instead of places in tests #148

Open femtotrader opened 2 months ago

femtotrader commented 2 months ago

Hello,

Test with assertAlmostEqual uses places which is close to absolute tolerance (atol) 10**-places is atol relative tolerance (rtol) would probably be a better idea.

See https://github.com/nardew/talipp/pull/147/files#diff-356e9ecd00b59db50b3531a4b1143455b87bc4d213b491b642d19db6bff50ec9R51

Kind regards

nardew commented 1 month ago

I am not sure I understand, could you please elaborate a bit more? What exactly is the problem with current version?

femtotrader commented 1 month ago

The use of relative tolerance (rtol) instead of absolute tolerance (places) in unit tests, especially for floating-point comparisons, is often preferred for several reasons:

  1. Scale Independence:

rtol is scale-independent, meaning it works well across a wide range of magnitudes. It allows for a consistent level of precision regardless of the size of the numbers being compared.

  1. Numerical Stability:

Floating-point arithmetic can introduce small errors due to how computers represent and operate on decimal numbers. These errors can accumulate, especially in complex calculations. rtol accounts for these small discrepancies more reliably than absolute tolerance.

  1. Meaningful Comparisons:

For very large or very small numbers, an absolute tolerance might be too strict or too lenient, respectively. rtol ensures the comparison is meaningful relative to the magnitude of the numbers.

  1. Handling Different Units:

When dealing with calculations involving different units or scales, rtol provides a more consistent way to compare results.

  1. Compatibility with Scientific Computing:

In scientific and engineering applications, relative error is often more important than absolute error.

  1. Flexibility:

rtol allows for setting a percentage difference that's acceptable, which can be more intuitive in many cases.

Example: Consider comparing 1.0 and 1.000001:

With places=6 (absolute tolerance), this would fail. With rtol=1e-5 (relative tolerance), this would pass, as the relative difference is about 1e-6.

Now compare 1,000,000 and 1,000,001:

With places=6, this would pass, which might not be desirable for such large numbers. With rtol=1e-5, this would still pass, maintaining consistent precision.

In practice, a combination of both relative (rtol) and absolute (atol) tolerance is often used for robust comparisons, especially in libraries like NumPy's isclose() function or pytest's approx().

(claude.ai answer)