econ-ark / HARK

Heterogenous Agents Resources & toolKit
Apache License 2.0
335 stars 198 forks source link

Deprecate interpolation implementation and move to interpolation.py #424

Closed MridulS closed 4 months ago

MridulS commented 5 years ago

Investigate merge of API of https://github.com/EconForge/interpolation.py with HARK

sbenthall commented 5 years ago

I see; in the meeting this morning I didn't realize it was a standalone repo.

I agree it makes sense to swap this in.

It does not depend on dolo maturity.

sbenthall commented 5 years ago

Some interesting discussion of interpolation.py here: https://github.com/econ-ark/DARKolo/issues/5#issuecomment-552254988

llorracc commented 4 years ago

Need to find a way to map from our syntax to the EconForge syntax.

MridulS commented 4 years ago

@alanlujan91

mnwhite commented 4 years ago

We can use off the shelf interpolators to crunch numbers, but we will need the classes to have the distance_criteria attribute defined.

On Thu, Jun 11, 2020 at 11:10 AM Mridul Seth notifications@github.com wrote:

@alanlujan91 https://github.com/alanlujan91

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/econ-ark/HARK/issues/424#issuecomment-642724974, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADKRAFJXKGI5PWRFZANTKETRWDXVLANCNFSM4JHJUN3A .

llorracc commented 4 years ago

You can get an overview of the Mathematica InterpolatingFunction object here.

It has a very rich set of functionalities. For our purposes, the most important is that, once it has been created, an InterpolatingFunction object can be used without any knowledge of the internals.

SciPy allows creation of a ScipySplineObject: ScipySplineObject = scipy.interpolate.InterpolatedUnivariateSpline( DataPointsDomain, DataPointsRange, k = 1, )

but it has the defect of always being a purely numerical object.

SymPy has something closer to the Mathematica version:

interpolating_spline creates an analytical function that can be differentiated, integrated, etc (like the Mathematica InterpolatingFunction object).

My goal is that, once the object has been created, a user of it does not need to know whether it was constructed using SciPy, SymPy, Interpolation.py, or some other (future, better) tool.

If the user tries to do something (like differentiate the object using SymPy syntax) that cannot be done (for example, this will not work on a SymPy interpolating object), the result should just be a (fatal) error.

The way to accomplish this is probably to adopt the structure and syntax of a SymPy interpolating_spline object, and then when the user of the object asks for an evaluation of a set of points, it will feed that input to whatever lower-level interpolating object(s) it may have inside it. If it has more than one such object in it, there should be a canonical order for which the first existing instance is used. (There should be an option for the user explicitly to specify which kind of object to use if more than one exists).

Concretely, this should just require that when the object is asked for the results of an interpolation, it knows how to translate its own input into the form that is required for each of the potential interpolators.

This will require some thought, mainly about how to specify the universally required inputs (at a minimum, a vector of the values of the input) and some universally available outputs (e.g., there should be a default distance_criteria attribute, per Matt's message.

sbenthall commented 4 years ago

Thinking about the distance_criteria issue...

My understanding is that currently, HARK solution functions need the distance_criteria because it feeds into the distance method, which is used to determine the convergence of the solver. I.e, here: https://github.com/econ-ark/HARK/blob/c756c9a214b523aeafb5123657a1adac40c953d6/HARK/core.py#L737

As far as I know, while every HARKObject has a distance function, and other things besides solution functions are HARKObjects (like AgentTypes), the distance criteria for these are not actually used anywhere.

While we could write a wrapper that introduces distance_criteria to any other sort of object constructed from a different library, it's possible that the HARKObject distance function is more general than is needed. There may be a more direct way to compute distances between interpolated functions specifically, which would be adequate for the solvers.

For example, if an interpolated function has a vector of (x,y) pairs, then the distance between spline(x_1, y_1) and spline(x_2,y_2) could be proportional to the mean squared error of the two functions when applied to some set of points (such as the union of x_1 and x_2).

MridulS commented 4 years ago

derivatives are missing in interpolation.py (maybe this should be an issue there too) which are a blocker for swapping in interpolation.py in HARK.

A basic swap in would look something like this

from interpolation import interp
class LinearInterp:
    def __init__(self, x_list, y_list):
        self.x_list = np.array(x_list) if _check_flatten(1, x_list) else np.array(x_list).flatten()
        self.y_list = np.array(y_list) if _check_flatten(1, y_list) else np.array(y_list).flatten()

    def __call__(self, z):
        return interp(self.x_list, self.y_list, z)
llorracc commented 4 years ago

@MridulS

The way your post is written it sounds like the second part is a solution to the first. But, to be clear, the second part is what we would do if we want to IGNORE the missing differentiation, right?

MridulS commented 4 years ago

The way your post is written it sounds like the second part is a solution to the first. But, to be clear, the second part is what we would do if we want to IGNORE the missing differentiation, right?

Oh no no, the new code will NOT work currently (ideally it should have) as there are places in HARK which expects derivatives to be implemented for LinearInterp but it's not implemented yet in interpolation.py. I just saw that they have mentioned in the "Missing but available soon" section of README that they are planning on implementing derivatives too.

llorracc commented 4 years ago

Glad to hear that derivatives are on their "To-Do" list.

PS. Does "automatic differentiation" only work on polynomials?

albop commented 4 years ago

Derivatives are now available in interpolation.py (doc here https://www.econforge.org/interpolation.py/splines/)

albop commented 4 years ago

Derivatives are now available in interpolation.py (doc here https://www.econforge.org/interpolation.py/splines/)

sbenthall commented 2 years ago

@gms158

alanlujan91 commented 1 year ago

Closed by #1151 and #1157? @Mv77

Mv77 commented 1 year ago

Depends on the meaning of Deprecate. We still use the hark interpolators. The econforge wrappers exist and some of us use them, but they are far from being the mode across hark I think.

mnwhite commented 4 months ago

Closing this, because I don't think we actually intend to fully replace HARK interpolators with EconForge ones, just provide the option.