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

interp in NumPy/SciPy #189

Closed
oyamad opened this issue Sep 16, 2015 · 5 comments
Closed

interp in NumPy/SciPy #189

oyamad opened this issue Sep 16, 2015 · 5 comments
Labels

Comments

@oyamad
Copy link
Member

oyamad commented Sep 16, 2015

This is a minor issue.
In the whole library, interp is imported as from scipy, and one of the lectures states "Linear interpolation is performed by SciPy’s interp function", but it is actually a function from NumPy (numpy.interp).

Importing from scipy is fine because the __init__.py in SciPy has from numpy import *. The problem is that if the user looks into SciPy for the documentation (as I did), it is not found there, but in NumPy.

@mmcky
Copy link
Contributor

mmcky commented Mar 14, 2016

Thanks @oyamad - you are correct - this should be fixed. The alternative would be to use interp1d which is imported from scipy.

@mmcky mmcky added the bug label Mar 14, 2016
@lbui01
Copy link
Contributor

lbui01 commented Mar 8, 2017

Hi @oyamad - I have opened a PR to change the scipy.interp to be directly used from numpy. See PR jstac/QuantEcon.lectures#90

This also needs to be updated in the solutions_py.ipynb. See issue jstac/QuantEcon.lectures#92.

We should be able to close this issue soon.

Also note:

I ran a speed test for np.interp and scipy.interpolate.interp1d and found that np.interp was significantly faster. 8.31ms for interp and 38.4ms for interp1d (including initializing the function). If anyone is interested, see this gist: https://gist.github.com/lbui01/1e7f2bffeae8296a31e49727bb710ef4

@jstac
Copy link
Contributor

jstac commented Mar 8, 2017

@lbui01 Thanks, nice work.

I guess the main difference is that interp returns an array whereas interp1d returns a callable object.

Having a callable object (e.g., a function) is often more convenient. In fact you can see in this line

w_func = lambda x: interp(x, grid, w)

from the optimal growth lecture that I'm creating a callable using lambda syntax. If I'd known I might have just used interp1d1.

I looked at the relative performance as well, and, if you do want to make a callable, then interp is only slightly faster. See here. That kind of difference is negligible.

@jstac
Copy link
Contributor

jstac commented Mar 8, 2017

In summary, if we want to make a callable I suggest we use interp1d imported from SciPy, whereas if we only wish to make an array (i.e., evaluate once off) then we use interp, imported from NumPy.

@lbui01
Copy link
Contributor

lbui01 commented Mar 8, 2017

Thanks @jstac, I see why interp1d is useful.

@jstac jstac closed this as completed Nov 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants