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 Cholesky decomposition to nucal._linear_fit #987

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion hera_cal/nucal.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,10 +761,11 @@
# Assert that the method is valid
assert solver in [
"lu_solve",
"cho_solve",
"solve",
"pinv",
"lstsq",
], "method must be one of {}".format(["lu_solve", "solve", "pinv", "lstsq"])
], "method must be one of {}".format(["lu_solve", "cho_solve", "solve", "pinv", "lstsq"])

# Assert that the regularization tolerance is non-negative
assert alpha >= 0.0, "alpha must be non-negative."
Expand All @@ -789,6 +790,19 @@
# Save info
cached_output = {'LU': L}

elif solver == "cho_solve":
# Factor XTX using scipy.linalg.cho_factor
if "c_and_lower" in cached_input:
c_and_lower = cached_input.get('c_and_lower')

Check warning on line 796 in hera_cal/nucal.py

View check run for this annotation

Codecov / codecov/patch

hera_cal/nucal.py#L796

Added line #L796 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

Might be good to add a test that involves caching.

else:
c_and_lower = linalg.cho_factor(XTX)

# Solve the linear system of equations using scipy.linalg.cho_solve
beta = linalg.cho_solve(c_and_lower, Xy)

# Save info
cached_output = {'c_and_lower': c_and_lower}

elif solver == "solve":
# Solve the linear system of equations using np.linalg.solve
beta = np.linalg.solve(XTX, Xy)
Expand Down
2 changes: 2 additions & 0 deletions hera_cal/tests/test_nucal.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,14 @@ def test_linear_fit():
b2, _ = nucal._linear_fit(XTX, Xy, solver='solve')
b3, _ = nucal._linear_fit(XTX, Xy, solver='lstsq')
b4, cached_input = nucal._linear_fit(XTX, Xy, solver='pinv')
b5, _ = nucal._linear_fit(XTX, Xy, solver='cho_solve')
assert cached_input.get('XTXinv') is not None

# Show that all modes give the same result
np.testing.assert_allclose(b1, b2, atol=1e-6)
np.testing.assert_allclose(b1, b3, atol=1e-6)
np.testing.assert_allclose(b1, b4, atol=1e-6)
np.testing.assert_allclose(b1, b5, atol=1e-6)

# Test that the fit is correct
model = np.dot(X, b4)
Expand Down
Loading