lecture-python-advanced.myst icon indicating copy to clipboard operation
lecture-python-advanced.myst copied to clipboard

[amss] remove interpolation and replace np.interp

Open mmcky opened this issue 4 months ago • 11 comments

The interpolation package is used in the amss lecture as it provided numba jit compatible functions.

The np.interp function is now compatible with numba so we can update this lecture and remove a dependency.

Currently this lecture uses

from interpolation.splines import eval_linear, UCGrid, nodes

hopefully we can find alternatives in numpy as a quick short term fix.

In the medium term we will update this lecture to use jax

mmcky avatar Aug 06 '25 00:08 mmcky

@HumphreyYang are you familiar with these lectures?

We need to replace these interpolation functions with numpy alternatives (for now). Are there any easy replacements?

mmcky avatar Aug 06 '25 01:08 mmcky

Hi @mmcky, as far as I know there is no simple alternative for UCGrid for multi-dimensional interpolation in numpy.

There is one in scipy: https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.RegularGridInterpolator.html

Would you like pure numpy implementation?

HumphreyYang avatar Aug 06 '25 01:08 HumphreyYang

Thanks @HumphreyYang

Also just FYI I am doing some testing of Copilot.

I assigned it to fix this issue and here is what it is coming up with so far

https://github.com/QuantEcon/lecture-python-advanced.myst/pull/228

mmcky avatar Aug 06 '25 02:08 mmcky

Hi @mmcky, as far as I know there is no simple alternative for UCGrid for multi-dimensional interpolation in numpy.

There is one in scipy: https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.RegularGridInterpolator.html

Would you like pure numpy implementation?

I would just need to check if this sits in numba jit or not.

mmcky avatar Aug 06 '25 02:08 mmcky

Hi @mmcky, I just had a try, no, RegularGridInterpolator is not in numba.

I think we might need to implement our own interpolator in numpy like what copilot is doing.

Let's see how it goes.

HumphreyYang avatar Aug 06 '25 02:08 HumphreyYang

Just noting that this is also in JAX: https://docs.jax.dev/en/latest/_autosummary/jax.scipy.interpolate.RegularGridInterpolator.html

Maybe someone could bite the bullet and just convert this whole lecture over to JAX...?

jstac avatar Aug 06 '25 07:08 jstac

Maybe someone could bite the bullet and just convert this whole lecture over to JAX...?

Many thanks @jstac, I can do that!

I think it would probably easier than writing a numba compatible function that replicates the figure as I did this afternoon : )

HumphreyYang avatar Aug 06 '25 07:08 HumphreyYang

I think it would probably easier than writing a numba compatible function that replicates the figure as I did this afternoon

Thanks for doing that @HumphreyYang ! I really appreciate the effort nontheless. What you did was natural and we could pause at that and open a separate issue if you like. I'll leave it up to you.

jstac avatar Aug 06 '25 07:08 jstac

Many thanks, @jstac. I think this issue and PR make a really nice case study on Copilot and AI that we can share with the team.

It’s my fault for getting a bit too involved in trying to beat the AI by replicating the graph : )

I’ll leave this issue to @mmcky and start rewriting!

HumphreyYang avatar Aug 06 '25 07:08 HumphreyYang

Just noting that this is also in JAX: https://docs.jax.dev/en/latest/_autosummary/jax.scipy.interpolate.RegularGridInterpolator.html

Maybe someone could bite the bullet and just convert this whole lecture over to JAX...?

thanks @jstac I will start cross checking some of these functions with jax docs

mmcky avatar Aug 06 '25 09:08 mmcky

I agree with the comments above on trying the JAX APIs. Also, the lectures here are in the advanced series so we can prefer JAX over numba even if some of the code looks complex with JAX.

kp992 avatar Aug 06 '25 23:08 kp992