Comments (9)
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.
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.
from interpolation.py.
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.
@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.
@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.
from interpolation.py.
from interpolation.py.
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)
- Is the dependency on tempita necessary? HOT 7
- `eval_splines` gives error `TypeError: 'module' object is not callable` HOT 2
- "interp" not working with python 3.11 HOT 2
- Errors with multidimensional outputs HOT 4
- eval_spline() doesn't recognize argument `k=` HOT 5
- literal type error when eval_spline() called without some keyword arguments HOT 4
- New version HOT 5
- Numba warnings HOT 8
- eval_cubic fails when array to interpolate on is non-writable
- After 2.2.0 eval_linear needs C-order and dtype FLOAT64 HOT 3
- guvectorized wrapping of eval_linear() appears broken HOT 2
- DOC: reorganize
- 1D interpolation with eval_linear HOT 5
- Documentation link broken HOT 1
- Import jitclass error HOT 1
- interpolating wind data on the satellilte track (in same lon lat time)
- Error when running example code in the documentation HOT 3
- `curses` requirement? HOT 6
- Python 3.10 support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from interpolation.py.