Skip to content

Replace np.sum(a * b) with a @ b for better performance and accuracy #542

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

Merged
merged 7 commits into from
Aug 16, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 14, 2025

This PR replaces instances of np.sum(a * b) with the more efficient a @ b operator throughout the lecture series, following best practices for NumPy operations.

Changes Made

Educational Enhancement

lectures/linear_algebra.md: Added side-by-side comparison showing both methods:

np.sum(x * y)          # Inner product of x and y, method 1
x @ y                  # Inner product of x and y, method 2 (preferred)

Explains that @ operator is preferred for performance and numerical accuracy.

Direct Replacements

  • lectures/career.md: Expected value calculations in probability distributions
  • lectures/lake_model.md: Expected value computations in economic models
  • lectures/mccall_model_with_separation.md: JAX-based dot products
  • lectures/mix_model.md: Posterior mean calculations
  • lectures/kalman.md: Squared error computations optimized to dot products
  • lectures/rand_resp.md: Mean squared error calculations

Benefits

  • Performance: Uses optimized BLAS libraries with fused multiply-add operations
  • Accuracy: Reduces rounding errors compared to separate multiply and sum steps
  • Best Practices: Follows NumPy documentation recommendations and PEP 465
  • Educational Value: Demonstrates modern NumPy usage patterns

Examples

Before:

self.F_mean = np.sum(self.θ * self.F_probs)
e1[t-1] = np.sum((x[:, t] - kn.x_hat.flatten())**2)
d_new = jnp.sum(jnp.maximum(v, u(c) + β * d) * q)

After:

self.F_mean = self.θ @ self.F_probs
diff1 = x[:, t] - kn.x_hat.flatten()
e1[t-1] = diff1 @ diff1
d_new = jnp.maximum(v, u(c) + β * d) @ q

All changes preserve mathematical equivalence while improving performance through optimized linear algebra operations.

Fixes #463.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] np.sum(a * b) versus a @ b Replace np.sum(a * b) with a @ b for better performance and accuracy Aug 14, 2025
@Copilot Copilot AI requested a review from mmcky August 14, 2025 07:03
Copilot finished work on behalf of mmcky August 14, 2025 07:03
Copy link

github-actions bot commented Aug 14, 2025

@github-actions github-actions bot temporarily deployed to pull request August 14, 2025 07:33 Inactive
@github-actions github-actions bot temporarily deployed to pull request August 14, 2025 07:34 Inactive
@github-actions github-actions bot temporarily deployed to pull request August 14, 2025 07:34 Inactive
@github-actions github-actions bot temporarily deployed to pull request August 14, 2025 07:34 Inactive
Copy link
Member

@HumphreyYang HumphreyYang left a comment

Choose a reason for hiding this comment

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

Hi @mmcky,

I had a look over this PR. I think it introduces a bug to lake_model.

Other than that, other changes are correct to me.

@HumphreyYang HumphreyYang marked this pull request as ready for review August 16, 2025 00:20
@HumphreyYang
Copy link
Member

Hi @mmcky,

I incorprated John's comments. I will merge this once it is built.

Please let me know if you have further comments or changes you want to make!

@mmcky mmcky self-requested a review August 16, 2025 00:30
@mmcky mmcky added the ready label Aug 16, 2025
@github-actions github-actions bot temporarily deployed to pull request August 16, 2025 00:45 Inactive
@github-actions github-actions bot temporarily deployed to pull request August 16, 2025 00:46 Inactive
@HumphreyYang HumphreyYang merged commit cf28a4c into main Aug 16, 2025
7 checks passed
@HumphreyYang HumphreyYang deleted the copilot/fix-463 branch August 16, 2025 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

np.sum(a * b) versus a @ b
4 participants