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

Add Halley's method via descent API #404

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

tansongchen
Copy link

@tansongchen tansongchen commented Apr 8, 2024

Add Halley's method by computing Hessian-vector-vector product with TaylorDiff.jl in $O(n)$ time. Note that this isn't structured as something like "HessianCache", but instead structured as a descent method, which can be viewed as "adding some correction term to the Newton's method". This is mainly because Hessian, either implicitly or explicitly, isn't likely to be used elsewhere.

Tests haven't been added because TaylorDiff.jl should be added to an extension. Once that is done, it is straightforward to add tests.

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

end
b = cache.b
# compute the hessian-vector-vector product
hvvp = derivative(x -> cache.f(x, cache.p), u, δu, 2)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
hvvp = derivative(x -> cache.f(x, cache.p), u, δu, 2)
hvvp = derivative(Base.Fix2(cache.f, cache.p), u, δu, 2)

Copy link
Member

Choose a reason for hiding this comment

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

This will also need a case for inplace cache.f

Copy link
Author

Choose a reason for hiding this comment

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

Got it. TaylorDiff.jl haven't got a in-place mode yet, but will do this week

Comment on lines 4 to 5
Compute the descent direction as ``J δu = -fu``. For non-square Jacobian problems, this is
commonly referred to as the Gauss-Newton Descent.
Copy link
Member

Choose a reason for hiding this comment

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

Old docs

Comment on lines 4 to 5

An experimental Halley's method implementation.
Copy link
Member

Choose a reason for hiding this comment

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

Ig you will change this later. But keeping it marked so that it doesn't fall through.

Copy link

codecov bot commented Apr 9, 2024

Codecov Report

Attention: Patch coverage is 0% with 44 lines in your changes are missing coverage. Please review.

Project coverage is 51.76%. Comparing base (411f7d3) to head (d815785).

Current head d815785 differs from pull request most recent head 83be985

Please upload reports for the commit 83be985 to get more accurate results.

Files Patch % Lines
src/descent/halley.jl 0.00% 41 Missing ⚠️
src/algorithms/halley.jl 0.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master     #404       +/-   ##
===========================================
- Coverage   86.46%   51.76%   -34.71%     
===========================================
  Files          47       49        +2     
  Lines        2904     2954       +50     
===========================================
- Hits         2511     1529      -982     
- Misses        393     1425     +1032     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tansongchen
Copy link
Author

tansongchen commented Apr 26, 2024

@avik-pal Support for in-place f and tests are added. Let me know if you have any questions!

I think there isn't an eqivalent for Fix2 that fixes the third argument, which is needed by evaluating the derivatives given p. Is it ok to use a closure?

@avik-pal
Copy link
Member

I think there isn't an eqivalent for Fix2 that fixes the third argument, which is needed by evaluating the derivatives given p. Is it ok to use a closure?

Yes mark it as @closure and do it

@avik-pal avik-pal linked an issue Apr 30, 2024 that may be closed by this pull request
src/descent/halley.jl Outdated Show resolved Hide resolved
Comment on lines 1 to 16
@testitem "Halley method" begin
f(u, p) = u .* u .- p
f!(fu, u, p) = fu .= u .* u .- p
u0 = [1.0, 1.0]
p = 2.0

# out-of-place
prob1 = NonlinearProblem(f, u0, p)
sol1 = solve(prob1, Halley())
@test sol1.u ≈ [sqrt(2.0), sqrt(2.0)]

# in-place
prob2 = NonlinearProblem(f!, u0, p)
sol2 = solve(prob2, Halley())
@test sol2.u ≈ [sqrt(2.0), sqrt(2.0)]
end
Copy link
Member

Choose a reason for hiding this comment

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

Let's add a bit more comprehensive tests here similar to SimpleNonlinearSolve

Copy link
Author

Choose a reason for hiding this comment

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

Sure, will do

@tansongchen
Copy link
Author

I am trying to add this as a block in 23_test_problems_tests.jl so I can make use of these problems. Do you know how to run a @testitem independently? With the vscode built-in test runner, I am getting the following error:

image

@avik-pal
Copy link
Member

avik-pal commented May 2, 2024

Yeah ReTestItems doesn't yet work with VSCode Test Runner. In a julia REPL activate the test using TestEnv then do

using ReTestItems

ReTestItems.runtests(<path to file or test directory>; name="<name of the testitem>")

@tansongchen
Copy link
Author

Ok I added it to this file near NewtonRaphson. Some cases haven't work mainly due to limitations of TaylorDiff.jl

@tansongchen
Copy link
Author

@avik-pal Do you have an out-of-place version of 23 problems? When testing with SimpleHallely, I hit

12:48:34 | START (1/1) test item "SimpleHalley" at test/core/23_test_problems_tests.jl:53
┌ Error: ErrorException("SimpleHalley currently only supports out-of-place nonlinear problems")
└ @ Main.var"##RobustnessTesting#247" ~/Public/SimpleNonlinearSolve.jl/test/core/23_test_problems_tests.jl:28

@avik-pal
Copy link
Member

Don't the tests in simplenonlinearsolve use the oop version?

@tansongchen
Copy link
Author

So currently, the tests at https://github.com/SciML/SimpleNonlinearSolve.jl/blob/main/test/core/23_test_problems_tests.jl doesn't include Halley's method, and when I tried to add Halley in the same way as Newton, I get the error above.

And the file at https://github.com/SciML/DiffEqProblemLibrary.jl/blob/master/lib/NonlinearProblemLibrary/src/NonlinearProblemLibrary.jl doesn't have out-of-place f's

@tansongchen
Copy link
Author

Ok so I manually ran [1, 5, 8, 15, 16] against SimpleHalley, and 1, 5, 15, 16 also failed. 8 is due to an error on TaylorDiff.jl and has been fixed. I need to release a new version of TaylorDiff to make the CIs pass though.

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 this pull request may close these issues.

Jacobian and Hessian Free Halley's Method
2 participants