GithubHelp home page GithubHelp logo

Comments (9)

aniecon avatar aniecon commented on July 28, 2024

Hi Mateo, I tried this as well. And it gave me the correct first derivative only when the grid was set to x = np.linspace(1.5,4.5,4).

from interpolation.py.

llorracc avatar llorracc commented on July 28, 2024

Pablo,

Some context for this: We've embarked on a goal of wiring your interpolation.py into the core of HARK, but can't do that until we figure out this issue. It sure looks like a bug to me; or, if not a bug, then probably there is some place where the documentation should be improved. Is there someone on your team who can look at this, maybe with collaboration/help from @Mv77 and/or @alanlujan91

from interpolation.py.

albop avatar albop commented on July 28, 2024

from interpolation.py.

albop avatar albop commented on July 28, 2024

Indeed, there was a bug with the evaluation of derivatives for linear interpolators.
I have pushed a fix in master. It should in principle work for uniform and nonuniform grids.

@Mv77 or @aniecon : can you check it works as you expect ? Would you submit a test file ? In principle, it should work for regular and irregular grids, linear and cubic splines. When we are sure it works we can add it to the doc.

NB: a grid is represented as a tuple of 1d grid. A 1d grid can itself be regular, it which case it is given by (min, max, n) or irregular in which case it must be a numpy vector of increasing coordintates. In your example, there is a hidden bug: UCGrid should not accept x as argument. Instead, you should do UCGrid( (0.,10.,11) ) which leads to faster interpolation. And to construct nonuniform grid CGrid( x ) (then x can be either a vector or a min,max,n, tuple). I have pushed another little change so that CGrid, and UCGrid actually do some type check (in the long run my intention was to use python type system for that but so far it will do).

NB2: to debug I used the call code = (get_code_spline(1,1,False,True,allocate=True,extrap_mode="linear", orders=((0,), (1,), (2,)))) to see what code had been generated and noticed 1.0*δ_0 instead of 1.0/δ_0 in the derivative of the basis functions.

from interpolation.py.

Mv77 avatar Mv77 commented on July 28, 2024

@albop thank you for the fix and for the tips.

I'd be happy to create a test file. I'll submit a PR in the next few days and confirm that it works.

from interpolation.py.

Mv77 avatar Mv77 commented on July 28, 2024

@albop I'm already working on tests and should have a PR soon. It's looking good. Thank you!

Just a question: I am using order=1 interpolators. First order derivatives look right. Second order derivatives are always coming out as 0. Just wanted to check that this is expected behavior (second derivative of a linear function), and that the interpolators are not expected to get a 2nd order derivative from some other numerical approach.

from interpolation.py.

llorracc avatar llorracc commented on July 28, 2024

from interpolation.py.

albop avatar albop commented on July 28, 2024

from interpolation.py.

llorracc avatar llorracc commented on July 28, 2024

So linear splines are differentiable everywhere from the right,
but not differentiable from the left at the knots points. This is actually
the expected mathematical result

That's surprising to me. It seems as though it should either be defined both from right and from left with slopes of the corresponding segments, or be undefined from either direction because it is undefined exactly at the knot.

If you do not specify the direction you are coming from, I guess it returns the derivative from the right? To prevent code from breaking when people use it without thinking about the knots point?

In practice, because actual kink points can be so ornery, it has occurred to me to handle this by adding two points to the grid symmetrically slightly to the left and slightly to the right, and connecting them with the unique quadratic that matches the (well-defined) slopes at the left and right. This would yield a function that was differentiable everywhere, and that would preserve the concavity (or convexity) of the function; and with the disadvantage that the quadratic would not yield exactly the right answer exactly at the kink point.

from interpolation.py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.