Skip to content

Errors with multidimensional outputs #104

Open
@Mv77

Description

@Mv77

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions