Skip to content

⚡ Optimize Logit and Probit Hessian calculations#49

Open
dot-comma-hyphen wants to merge 1 commit intomainfrom
optimize-logit-hessian-9108368597733373413
Open

⚡ Optimize Logit and Probit Hessian calculations#49
dot-comma-hyphen wants to merge 1 commit intomainfrom
optimize-logit-hessian-9108368597733373413

Conversation

@dot-comma-hyphen
Copy link
Copy Markdown
Collaborator

This PR optimizes the Logit and Probit regression implementations in the oaxaca_blinder crate.

💡 What

The optimization replaces high-level matrix operations that triggered full matrix clones and transpositions (like x.clone(), x.transpose()) with manual, memory-efficient calculations. It leverages the column-major layout of nalgebra's DMatrix to compute weighted Gram matrices (for the Hessian) and dot products (for the Gradient) directly from the underlying data slices.

🎯 Why

In each iteration of the Newton-Raphson optimization, the original code cloned the entire feature matrix $X$ and transposed it multiple times. For datasets with many observations ($N$) or predictors ($K$), this led to significant $O(N \cdot K)$ memory pressure and unnecessary CPU cycles spent on allocations and data movement.

📊 Measured Improvement

  • Memory: Eliminated multiple $O(N \cdot K)$ allocations per iteration.
  • CPU: Improved cache locality by accessing matrix data in column-major order and reduced overhead from high-level nalgebra operations that perform defensive copies.
  • Correctness: Verified through equivalence tests that the optimized manual calculations produce identical results to the original high-level implementations.

PR created automatically by Jules for task 9108368597733373413 started by @dot-comma-hyphen

Avoid full feature matrix clones and transpositions by computing
weighted Gram matrices and gradients manually using column-major
access patterns. This significantly reduces memory allocations
and improves cache locality during Newton-Raphson optimization.

Co-authored-by: dot-comma-hyphen <216583967+dot-comma-hyphen@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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.

1 participant