Borrar filtros
Borrar filtros

gridded data interpolant interp1

8 visualizaciones (últimos 30 días)
DIMITRY
DIMITRY el 23 de Feb. de 2016
Editada: John D'Errico el 23 de Feb. de 2016
Hi world,
A very basic question if you could help. I have a one dimension interp1(X,Y,Z,'spline','extrap') where X is a 39x1 size (date vector) Y is a 39x1x7000 size (rate vector) and Z is a 1020x1 size (weekly date vector) the reason for this is simply to interpolate semestrial rates to weekly rates. It is working fine as far as X is stricly monotonic increasing so in some case I have an error message
Error using griddedInterpolant The grid vectors are not strictly monotonic increasing
Can you help guys?
Regards,
  3 comentarios
DIMITRY
DIMITRY el 23 de Feb. de 2016
the X vector is created with the matlab function X = daysadd(valuationdate,round(360*[1:30]),Basis); where the basis is 2 it is working fine but if I change the basis number to what I really need to specify it is giving monotonic increasing array error
dpb
dpb el 23 de Feb. de 2016
Again, we need to see an example of the dates created that cause the problem; describing something other than what you show doesn't help much.
If you create the X vector, then what does
sum(diff(X))
show? If it is >0, then the supposition of there being identical values as postulated above is true and you'll have to fixup that to do interpolation with the builtin Matlab methods.

Iniciar sesión para comentar.

Respuestas (1)

John D'Errico
John D'Errico el 23 de Feb. de 2016
Editada: John D'Errico el 23 de Feb. de 2016
There are several standard solutions, depending on the problem and your goals.
Interpolation takes the form y=f(x). So, for any given value x, there must be a unique value for y. So the first question is, does the data take that form? If for example, your data takes the form of points that lie around a circle, then the function will be multi-valued. For any x, there will be two values of y. In that case, interpolation requires a tool that can interpolate along a path in two dimensions, but you cannot get out a single value of y for a given x. Tools that can do this are cscvn, or my own interparc, from the FEX.
Next, we might have a problem wher the x points are simply unsorted, but there is a simple, single-valued functional relationship. For example,
x = rand(1,50);
y = sin(3*x);
Were I to plot those points in the order they were generated, they would NOT represent a nice single valued function.
plot(x,y,'-ro')
Interpolation using a tool like interp1 will fail. It will complain that x must lie in a monotonic sequence. The simple solution here is to sort x and y, so that x is now monotonic.
[xs, tags] = sort(x);
ys = y(tags);
plot(xs,ys,'-ro')
Voila! The result is NOW an interpolable set of points.
The next common problem is what to do when you have replicates. Replicate points are an issue, because for a single value of x, you have two different values of y. BAD DATA! Again, this must cause interpolation to fail.
Even if the replicate values for y are identical (so in theory there is no problem) interp1 will still get upset. It checks that you have a monotonic sequence for x, and if that fails, then it stops there.
The classic fix is to average the values of y at any given x where there are replicate x values. You can do that using my consolidator tool, also found on the file exchange.
Another way around the replicate problem is to joggle the x values. (dpb suggested that in his comment.) This is a common solution, with one caveat. A random joggle is acceptable, IF the points are now in a monotonic sequence, AND if you are not using a spline model. Applying a spline interpolant to a joggled sequence will be a terrible thing, because the spline will be ill-conditioned in a variety of ways. You MAY get a wildly oscillatory spline. For example...
x = [0:.1:1, 1:.1:2];
y = [0:.1:1, 2:.1:3];
I've created a vector x that has the point at x=1 replicated, and a jump at x==1. See what happens when I try to use interp1.
xi = 0:.01:2;
yi = interp1(x,y,xi,'spline');
Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.
Error in interp1 (line 161)
F = griddedInterpolant(X,V,method);
Lets fix this by jogging that replicated point just slightly up.
x(11:12)
ans =
1 1
xjog = x;
xjog(12) = xjog(12) + eps*1000;
yi = interp1(xjog,y,xi,'spline');
No error this time, but the result was clearly crapola anyway.
We can see what happens with the averaging approach.
[xcon,ycon] = consolidator(x',y',@mean);
[xcon,ycon]
ans =
0 0
0.1 0.1
0.2 0.2
0.3 0.3
0.4 0.4
0.5 0.5
0.6 0.6
0.7 0.7
0.8 0.8
0.9 0.9
1 1.5
1.1 2.1
1.2 2.2
1.3 2.3
1.4 2.4
1.5 2.5
1.6 2.6
1.7 2.7
1.8 2.8
1.9 2.9
2 3
You can see that it replaced the replicated x with a single point, with the average value for y at that point. Now, interpolate and plot.
yicon = interp1(xcon,ycon,xi,'spline');
plot(xcon,ycon,'bo',xi,yicon,'r-')
A better interpolaant for this data is pchip.
yiconp = interp1(xcon,ycon,xi,'pchip');
plot(xcon,ycon,'bo',xi,yiconp,'r-')
So you can fix the problem. But you need to understand what makes most sense for you and your data.

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by