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

Fix dynamic rate step bracketing interval #7

Merged
merged 2 commits into from
Aug 21, 2024

Conversation

matias-martini
Copy link
Member

@matias-martini matias-martini commented Aug 21, 2024

Context

Hey there! 👋

In this PR, there was a precision issue we were having in the find_bracketing_interval function when searching for bracketing intervals at very large rates. Originally, the max rate was capped at 10.0, but we've now increased that to 1E300. The fixed step size approach wasn't cutting it, leading to floating-point precision problems as the rate got larger.

To address this, I introduced a dynamic step size that adjusts as the rate increases. This allows us to explore higher values effectively (all the way up to 1E300!) without running into precision issues. The new approach uses a logarithmic-like step, ensuring better coverage and stability across the entire range.

Changelog

  • Fixed: The find_bracketing_interval function now supports extremely high rates, improving precision for very high XIRRs (over 1E300%). Dynamic step sizing ensures accurate results even at extreme rates.

QA

Now the extremely good investment converges in the specs:

  it 'calculates xirr for an extremely good and unrealistic investment' do
    cashflows = [
      [-1000, Date.new(1985, 1, 1)],
      [6000, Date.new(1985, 1, 2)]
    ]

    result = FastXirr.calculate(cashflows: cashflows)
    expect(result).to be_within(1e-6).of(1.0597571969623571e+284)
  end

@matias-martini matias-martini force-pushed the fix/dynamic-rate-step-bracketing-interval branch from ede2412 to 46ac946 Compare August 21, 2024 21:57
@matias-martini matias-martini force-pushed the fix/dynamic-rate-step-bracketing-interval branch from 46ac946 to 9825b62 Compare August 21, 2024 21:59
@matias-martini matias-martini self-assigned this Aug 21, 2024
Copy link
Collaborator

@jecastro1 jecastro1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@matias-martini matias-martini merged commit d99b8f0 into main Aug 21, 2024
3 checks passed
@matias-martini matias-martini deleted the fix/dynamic-rate-step-bracketing-interval branch August 21, 2024 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants