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

1D Newton iteration count issue(s) #1008

Open
ryanelandt opened this issue Jul 31, 2023 · 1 comment · May be fixed by #1012
Open

1D Newton iteration count issue(s) #1008

ryanelandt opened this issue Jul 31, 2023 · 1 comment · May be fixed by #1012

Comments

@ryanelandt
Copy link
Contributor

The documentation for newton_raphson_iterate says:

uintmax_t& max_iter
    An optional maximum number of iterations to perform.
    On exit, this is updated to the actual number of iterations performed. 

There are two issues relating to max_iter:

  1. max_iter is copied to a variable count. After each Newton iteration, one is subtracted from count. Because of the do {...} while (count && ...) structure of the Newton iteration, if max_iter = 0, rollover occurs, making max_iter functionally infinite.
  2. If 0 < max_iter, running out of iterations exits the Newton loop as if the tolerance was met.

These two observations are demonstrated in the code below.

const auto fn = [](double x) { return std::make_pair(x * x - 3, 2 * x); };
std::uintmax_t iters;
std::cout << std::setprecision(16);

for (int count = 0; count < 10; count++) {
   iters = count;
   const double x0 = boost::math::tools::newton_raphson_iterate(fn, 10.0, 0.0, 100.0, 52, iters);
   std::cout << "max_iters: " << count << " -- used_iters: " << iters << " -- x: " << x0 << std::endl;
}
max_iters: 0 -- used_iters: 8 -- x: 1.732050807568877
max_iters: 1 -- used_iters: 1 -- x: 5.15
max_iters: 2 -- used_iters: 2 -- x: 2.86626213592233
max_iters: 3 -- used_iters: 3 -- x: 1.956460731776899
max_iters: 4 -- used_iters: 4 -- x: 1.74492093914502
max_iters: 5 -- used_iters: 5 -- x: 1.732098271119538
max_iters: 6 -- used_iters: 6 -- x: 1.732050808219183
max_iters: 7 -- used_iters: 7 -- x: 1.732050807568877
max_iters: 8 -- used_iters: 8 -- x: 1.732050807568877
max_iters: 9 -- used_iters: 8 -- x: 1.732050807568877
@ryanelandt
Copy link
Contributor Author

ryanelandt commented Jul 31, 2023

Because of the issue described above, the unit test

math/test/test_roots.cpp

Lines 657 to 659 in 50ef83a

// bug reports:
boost::math::skew_normal_distribution<> dist(2.0, 1.0, -2.5);
BOOST_CHECK(boost::math::isfinite(quantile(dist, 0.075)));
passes, even though the answer in produces in inaccurate due to running out of iterations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant