cubic smoothing splines -凯发k8网页登录
this example shows how to use the csaps
and spaps
commands from curve fitting toolbox™ to construct cubic smoothing splines.
the csaps command
the command csaps
provides the smoothing spline. this is a cubic spline that more or less follows the presumed underlying trend in noisy data. a smoothing parameter, to be chosen by you, determines just how closely the smoothing spline follows the given data. here is the basic information, an abbreviated version of the documentation:
csaps cubic smoothing spline.
values = csaps(x, y, p, xx)
returns the values at xx of the cubic smoothing spline for the
given data (x,y) and depending on the smoothing parameter p, chosen
from the interval [0 .. 1]. this smoothing spline f minimizes
p * sum_i w(i)(y(i) - f(x(i)))^2 (1-p) * integral (d^2 f)^2
example: noisy data from a cubic polynomial
here are some trial runs. we start with data from a simple cubic, q(x) := x^3
, contaminate the values with some noise, and choose the value of the smoothing parameter to be .5. then plot the resulting smoothed values, along with the underlying cubic, and the contaminated data.
xi = (0:.05:1); q = @(x) x.^3; yi = q(xi); randomstream = randstream.create( 'mcg16807', 'seed', 23 ); ybad = yi .3*(rand(randomstream, size(xi))-.5); p = .5; xxi = (0:100)/100; ys = csaps(xi,ybad,p,xxi); plot(xi,yi,':',xi,ybad,'x',xxi,ys,'r-') title('clean data, noisy data, smoothed values') legend( 'exact', 'noisy', 'smoothed', 'location', 'northwest' )
the smoothing is way overdone here. by choosing the smoothing parameter p
closer to 1, we obtain a smoothing spline closer to the given data. we try p = .6, .7, .8, .9, 1
, and plot the resulting smoothing splines.
yy = zeros(5,length(xxi)); p = [.6 .7 .8 .9 1]; for j=1:5 yy(j,:) = csaps(xi,ybad,p(j),xxi); end hold on plot(xxi,yy); hold off title('smoothing splines for various values of the smoothing parameter') legend({'exact','noisy','p = 0.5','p = 0.6','p = 0.7','p = 0.8', ... 'p = 0.9', 'p = 1.0'}, 'location', 'northwest' )
we see that the smoothing spline can be very sensitive to the choice of the smoothing parameter. even for p
= 0.9, the smoothing spline is still far from the underlying trend, while for p
= 1, we get the interpolant to the (noisy) data.
in fact, the formulation used by csapi
(p.235ff of a practical guide to splines) is very sensitive to scaling of the independent variable. a simple analysis of the equations used shows that the sensitive range for p
is around 1/(1 epsilon)
, with epsilon := h^3/16
, and h
the average difference between neighboring sites. specifically, you would expect a close following of the data when p = 1/(1 epsilon/100)
and some satisfactory smoothing when p = 1/(1 epsilon*100)
.
the plot below shows the smoothing spline for values of p
near this magic number 1/(1 epsilon)
. for this case, it is more informative to look at 1-p
since the magic number, 1/(1 epsilon)
, is very close to 1.
epsilon = ((xi(end)-xi(1))/(numel(xi)-1))^3/16; 1 - 1/(1 epsilon)
ans = 7.8124e-06
plot(xi,yi,':',xi,ybad,'x') hold on labels = cell(1,5); for j=1:5 p = 1/(1 epsilon*10^(j-3)); yy(j,:) = csaps(xi,ybad,p,xxi); labels{j} = ['1-p= ',num2str(1-p)]; end plot(xxi,yy) title('smoothing splines for smoothing parameter near its ''magic'' value') legend( [{'exact', 'noisy'}, labels], 'location', 'northwest' ) hold off
in this example, the smoothing spline is very sensitive to variation of the smoothing parameter near the magic number. the one farthest from 1 seems the best choice from these, but you may prefer the one beyond that.
p = 1/(1 epsilon*10^3); yy = csaps(xi,ybad,p,xxi); hold on plot( xxi, yy, 'y', 'linewidth', 2 ) title( sprintf( 'the smoothing spline for 1-p = %s is added, in yellow', num2str(1-p) ) ) hold off
you can also supply csaps
with error weights, to pay more attention to some data points than others. also, if you do not supply the evaluation sites xx
, then csaps
returns the ppform of the smoothing spline.
finally, csaps
can also handle vector-valued data and even multivariate, gridded data.
the spaps command
the cubic smoothing spline provided by the command spaps
differs from the one constructed in csaps
only in the way it is selected. here is an abbreviated version of the documentation for spaps
:
spaps smoothing spline.
[sp,values] = spaps(x,y,tol) returns the b-form and, if asked,
the values at x, of a cubic smoothing spline f for the given
data (x(i),y(:,i)), i=1,2, ..., n.
the smoothing spline f minimizes the roughness measure
f(d^2 f) := integral ( d^2 f(t) )^2 dt on x(1) < t < x(n)
over all functions f for which the error measure
e(f) := sum_j { w(j)*( y(:,j) - f(x(j)) )^2 : j=1,...,n }
is no bigger than the given tol. here, d^m f denotes the m-th
derivative of f. the weights w are chosen so that e(f) is the
composite trapezoid rule approximation for f(y-f).
f is constructed as the unique minimizer of
rho*e(f) f(d^2 f),
with the smoothing parameter rho so chosen so that e(f) equals
tol. hence, fn2fm(sp,'pp') should be (up to roundoff) the same
as the output from cpaps(x,y,rho/(1 rho)).
tolerance vs. smoothing parameter
it may be easier to supply a suitable tolerance for spaps
than the smoothing parameter p
required by csaps
. in our earlier example, we added uniformly-distributed random noise from the interval 0.3*[-0.5 .. 0.5]
. hence, we can estimate a reasonable value for tol
as the value of the error measure e
at such noise.
tol = sum((.3*(rand(randomstream, size(yi))-.5)).^2);
this plot shows the resulting smoothing spline constructed by spaps
. note that the error weights are specified to be uniform, which is their default value in csaps
.
[sp,ys,rho] = spaps(xi,ybad,tol,ones(size(xi))); plot(xi,yi,':',xi,ybad,'x',xi,ys,'r-') title( sprintf( 'clean data, noisy data, smoothed values (1-p = %s )', num2str(1/(1 rho)) ) ); legend( {'exact','noisy','smoothed'}, 'location', 'northwest' )
the figure title shows the value of p
you would use in csaps
to obtain exactly this smoothing spline for these data.
here, in addition, is the smoothing spline provided by csaps
when not given a smoothing parameter. in this case csaps
chooses the parameter by a certain ad hoc procedure that attempts to locate the region where the smoothing spline is most sensitive to the smoothing parameter (similar to the earlier discussion).
hold on plot(xxi,fnval(csaps(xi,ybad),xxi),'-') title('clean data, noisy data, smoothed values') legend({'exact' 'noisy' 'spaps, specified tolerance' ... 'csaps, default smoothing parameter'}, 'location', 'northwest' ) hold off
csaps vs. spaps
the csaps
and spaps
commands differ in the way in which you specify a particular smoothing spline, via a smoothing parameter vs. a tolerance. another difference is that spaps
can provide a linear or a quintic smoothing spline, in addition to the cubic smoothing spline.
the quintic smoothing spline is better than the cubic smoothing spline in the situation when you would like the second derivative to move as little as possible.