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

Errors with multidimensional outputs #104

Open
Mv77 opened this issue Mar 20, 2023 · 4 comments
Open

Errors with multidimensional outputs #104

Mv77 opened this issue Mar 20, 2023 · 4 comments

Comments

@Mv77
Copy link
Contributor

Mv77 commented Mar 20, 2023

Hi @albop,

I have been working on enhancing our HARK wrapper of interpolation to work with functions of multiple outputs, $f:\mathbb{R}^n\rightarrow \mathbb{R}^m, m>1$. I have run into a strange behavior where calling eval_linear with parameters representing a function of multiple outputs can return interpolations of a single output. Say, I am approximating [g(x), h(x)] and it might only return g(x).

I created the following script that reproduces the error

from interpolation.splines import CGrid, eval_linear
from interpolation.splines import extrap_options as xto
import numpy as np

# Create grids
x = np.linspace(0, 10, 11)
y = np.linspace(0, 10, 11)
grid = CGrid(x, y)

# Make two linear functions
f1 = lambda x, y: 2 * x + y
f2 = lambda x, y: 3 * x + 2 * y

# Values for the two interpolated functions
x_tiled, y_tiled = np.meshgrid(x, y, indexing='ij')

z1 = f1(x_tiled, y_tiled)
z2 = f2(x_tiled, y_tiled)
z1z2 = np.stack([z1,z2], axis=-1)

# Evaluate always on the same points
npoints = 300
eval_points = np.column_stack([np.linspace(0, 10, npoints)]*2)

for i in range(1000):

    print(i)

    # Evaluate 1d interpolator
    if True:
        z1_eval = eval_linear(
            grid,
            z1,
            eval_points,
            xto.LINEAR,
        )
        assert(z1_eval.shape == (npoints,))
    
    # Evaluate 2d interpolator
    z1z2_eval = eval_linear(
        grid,
        z1z2,
        eval_points,
        xto.LINEAR,
    )
    assert(z1z2_eval.shape == (npoints,2))
    
    print('ok')

The script interpolates a 2-d function a thousand times and raises an error if the output is not of the expected shape.

Here is the output I get

...
ok
163
ok
164
ok
165
Traceback (most recent call last):
  File "/home/mvg/Dropbox/bug_example.py", line 49, in <module>
    assert(z1z2_eval.shape == (npoints,2))
AssertionError

There are various strange things about the behavior of this script:

  • It does not fail immediately, or consistently. If I run the script 3 times, it might fail on iterations 130, 140, and 107 respectively, even though none of the inputs are stochastic or iteration-dependent.
  • It fails only if one does both 1-d and 2-d interpolation. If I set the if True: below # Evaluate 1d interpolator to if False: the script completes the 1000 iterations without issues.
  • It fails only when extrapolation options are given. If I comment out the xto.LINEAR lines, the script completes the 1000 iterations without issues.

This makes me think that what is going on is some bug in the overloading of functions with different inputs, but that only explains part of the strange behaviors above.

Do you know what might be going on? I'd be happy to help fix/test this issue.

@albop
Copy link
Member

albop commented Mar 20, 2023

Hmm, it doesn't look immediately obvious. I need to think a little bit. Will get back to it later this week.

albop added a commit that referenced this issue Jun 8, 2023
@albop
Copy link
Member

albop commented Jun 8, 2023

@Mv77, I have, I believe found the source of the error and fixed it in master. Do you want to check ?

@albop
Copy link
Member

albop commented Jun 8, 2023

Actually, I haven't. The bug is still there...

@Mv77
Copy link
Contributor Author

Mv77 commented Jun 9, 2023

Hey @albop , thanks for looking into it.

I ran the test anyway and yes, it looks like the error is still there.

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

No branches or pull requests

2 participants