Skip to content

[calvo_machine_learn] Replace np.sum(a * b) with a @ b #214

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 2 commits into from
Aug 5, 2025
Merged
Changes from all commits
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
6 changes: 3 additions & 3 deletions lectures/calvo_machine_learn.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def compute_θ(μ, α=1):

# Compute the weighted sums for all t
weighted_sums = jnp.array(
[jnp.sum(λ_powers[:T-t] * μ[t:T]) for t in range(T)])
Copy link
Contributor

Choose a reason for hiding this comment

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

@HumphreyYang should we do this for jnp.sum as well as np.sum?

Copy link
Member

Choose a reason for hiding this comment

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

Many thanks @mmcky, I think it's fine because they behave similarly.

I will do another pass over every lecture once the preview PR is opened!

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks @HumphreyYang this is building now
#219

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll merge this in and then update #219

[(λ_powers[:T-t] @ μ[t:T]) for t in range(T)])

# Compute θ values except for the last element
θ = (1 - λ) * weighted_sums + λ**(T - jnp.arange(T)) * μbar
Expand Down Expand Up @@ -595,7 +595,7 @@ def compute_V(μ, β, c, α=1, u0=1, u1=0.5, u2=3):
t = np.arange(T)

# Compute sum except for the last element
V_sum = np.sum(β**t * (h0 + h1 * θ[:T] + h2 * θ[:T]**2 - 0.5 * c * μ[:T]**2))
V_sum = (β**t) @ (h0 + h1 * θ[:T] + h2 * θ[:T]**2 - 0.5 * c * μ[:T]**2)

# Compute the final term
V_final = (β**T / (1 - β)) * (h0 + h1 * μ[-1] + h2 * μ[-1]**2 - 0.5 * c * μ[-1]**2)
Expand Down Expand Up @@ -931,7 +931,7 @@ def compute_J(μ, β, c, α=1, u0=1, u1=0.5, u2=3):
(β**T/(1-β))])

θ = B @ μ
βθ_sum = jnp.sum((β_vec * h1) * θ)
βθ_sum = (β_vec * h1) @ θ
βθ_square_sum = β_vec * h2 * θ.T @ θ
βμ_square_sum = 0.5 * c * β_vec * μ.T @ μ

Expand Down
Loading