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

ENH - More efficient B.dot and B.T.dot in Cox datafit #168

Merged
merged 18 commits into from Jun 20, 2023

Conversation

Badr-MOUFAD
Copy link
Collaborator

In Cox datafit, It is possible to perform the operations evolving the matrix B in linear time and without having to form explicitly the matrix.

Up to a sorting permutation of tm, the matrix B has a block triangular structure involving matrices with coordinates equal to 1.
Hence applying B to a vector (slightly) resembles performing a cumulative sum on the coordinates of v.

Advantage

  • reduce the memory footprint of Cox datafit from O(n²) --> O(n) as we no longer store B
  • Evaluate gradient and Hessian in O(n) instead of O(n²)

Benchmarks

A link to benchmark results to showcase the timing improvements compared to the main branch.


Riding "la trottinette 🛴 à la @agramfort"

@Badr-MOUFAD
Copy link
Collaborator Author

Need to merge #167 beforehand

@Badr-MOUFAD
Copy link
Collaborator Author

The test fails because the gradient and Hessian disagree with scipy check_grad at atol=1e-3.
I'm not that familiar with truncation and roundoff errors to blame numerical errors for that.

However, I pushed a debug_script that checks independently the matrix-vector operations. It reveals that both operations are the same (difference of 1e-11).

@mathurinm, any clues?

skglm/datafits/single_task.py Outdated Show resolved Hide resolved
skglm/datafits/single_task.py Outdated Show resolved Hide resolved
skglm/datafits/single_task.py Outdated Show resolved Hide resolved
skglm/datafits/single_task.py Outdated Show resolved Hide resolved
@@ -654,38 +658,53 @@ def initialize(self, X, y):
"""Initialize the datafit attributes."""
tm, s = y

tm_as_col = tm.reshape((-1, 1))
self.B = (tm >= tm_as_col).astype(X.dtype)
self.T_indices = np.argsort(tm)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice!

skglm/datafits/single_task.py Outdated Show resolved Hide resolved
@mathurinm mathurinm merged commit 189d21e into scikit-learn-contrib:main Jun 20, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants