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

Use np.full? #165

Closed
oyamad opened this issue Jul 5, 2021 · 9 comments · Fixed by #168 or #170
Closed

Use np.full? #165

oyamad opened this issue Jul 5, 2021 · 9 comments · Fixed by #168 or #170
Assignees

Comments

@oyamad
Copy link
Member

oyamad commented Jul 5, 2021

In several pieces of code, an array of a constant value is created by something like

Q = np.ones((num_nodes, num_nodes))
Q = Q * np.inf

This can be simplified with np.full:

Q = np.full((num_nodes, num_nodes), np.inf)
@mmcky
Copy link
Contributor

mmcky commented Jul 8, 2021

thanks @oyamad I am all for this.

@shlff would you have time to go through the lectures and make a PR?

@jstac
Copy link
Contributor

jstac commented Jul 9, 2021

Thanks @oyamad , I didn't know about np.full.

@shlff please go ahead when you have time. In this and other lectures, you can search for np.ones and see if it's being used in the pattern that @oyamad mentioned (creation of a constant vector).

@shlff
Copy link
Member

shlff commented Jul 9, 2021

Thanks @jstac , @oyamad and @mmcky .

I went through all lectures in lecture-python.myst and find similar patterns in the following lectures:

  • career: v = np.ones((cw.grid_size, cw.grid_size)) * 100
  • geom_series: f_2 = np.ones(T_max+1)*infinite_lease(g, r, x_0)
  • inventory_dynamics: X = np.ones(num_firms) * x_init
  • lqcontrol: income_r = np.ones(T-K) * s
  • markov_asset: n = 5 and P = 0.0125 * np.ones((n, n))
  • mccall_correlated: f_init = np.log(js.c) * np.ones(len(js.z_grid))
  • multi_hyper: Σ = np.ones((c, c)) * n * (N - n) / (N - 1) / N ** 2
  • multivariate_normal: μ_IQ = np.ones(n+1) * μθ and μb = np.ones(T) * 𝛼0
  • odu: e = np.ones((N, T+1)) and π = np.ones((N, T+1)) * 1e-3
  • short_path: Q = np.ones((num_nodes, num_nodes)) and Q = Q * np.inf (This one is exactly what @oyamad mentioned.)
  • time_series_with_matrices: b = np.ones(T) * 𝛼0 and b_steady = np.ones(T) * 𝛼0
  • wealth_dynamics: ψ_0 = np.ones(num_households) * wdy.y_mean and ψ_0 = np.ones(num_households) * wdy.y_mean and ψ_0 = np.ones(num_households) * wdy.y_mean

I am not really sure which one is better: Q = np.ones((num_nodes, num_nodes)) * np.inf or Q = np.full((num_nodes, num_nodes), np.inf).

But maybe we can implement some simple experiments to see compare the speed and stability (e.g., under numba) for these two cases?

@jstac
Copy link
Contributor

jstac commented Jul 9, 2021

Thanks @shlff .

A few of these do not fit the pattern @oyamad identified: using np.ones to create an array and then multiplying it by a constant to create a constant vector.

Can you please cut down this list to just those examples?

(Once we have a list fitting this pattern, we will change them all to use np.full, since dealing with this pattern is the purpose of that function.)

@shlff
Copy link
Member

shlff commented Jul 10, 2021

Thanks @jstac . Please find the updated list above.

Looking forward to your comments.

@jstac
Copy link
Contributor

jstac commented Jul 10, 2021

Thanks @shlff , good work. That list is correct so please proceed.

@shlff
Copy link
Member

shlff commented Jul 10, 2021

Thanks @jstac . In the next step I will create a PR to replace all patterns in that list with np.full, suggested by @oyamad .

@oyamad
Copy link
Member Author

oyamad commented Jul 15, 2021

There are a few more:

  • inventory_dynamics.md

  • lake_model.md

  • complex_and_trig.md

  • multivariate_normal.md

  • samuelson.md
    Use np.full_like.

  • markov_asset.md

    P += np.diag(0.95 - 0.0125 * np.ones(5))

    This does not seem to be very clever. It should be, for example,

    P[range(n), range(n)] += 1 - P.sum(1)

    or

    P[np.diag_indices(n)] += 1 - P.sum(1)

@shlff
Copy link
Member

shlff commented Jul 15, 2021

Thanks @oyamad . I will check the pattern through the lectures you mentioned above and create another PR to modify it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants