detrend using cubic splines
22 views (last 30 days)
Show older comments
Venkatessh
on 8 Feb 2013
Commented: Bruno Luong
on 17 Oct 2018
How can I detrend a time series using cubic spline interpolation? I would like to get this done over for eg., 0.2 year bins.
0 Comments
Accepted Answer
Shashank Prasanna
on 8 Feb 2013
Venkatessh, detrending is just a process of removing long term deterministic patterns or in short trends. For example your data may look like it is exponentially increasing, but with variation. You are more interested in the variation because the exponential increase renders the data non-stationary.
It is as simple as fitting your data to the best possible curve and subtracting it from your data.
Simple example:
Generate data and get a spline fitted trend:
x = 0:100;
y = 100*randn(1,length(x))+x.^2;
xx = 0:10:100;
yy = spline(x,y,xx);
Substract the trend from your original data and plot the detrended variation sequence.
ytrend = interp1(xx,yy,x,'spline');
subplot(2,1,1)
plot(x,y,'.',x,ytrend);
subplot(2,1,2)
plot(x,y-ytrend)
18 Comments
Shashank Prasanna
on 8 Feb 2013
Honestly, I don't know if there is a definition written on stone somewhere east of here, but that is atleast the accepted convention. All the following methods fit into the non-parametric model framework: Splines, Neural Networks, SVM (and other kernel estimators), regression trees etc
Anyway this made for a nice discussion. I wish this topic wasn't embedded in another post, would loved to have see more responses.
Image Analyst
on 8 Feb 2013
I'm not sure what "I need to be very careful in discarding the data. " means. Of course any type of fitting, regression, or detrending is going to discard data. Even though the data may go into creating the fit, eventually the data is going to be discarded and replaced by the fitted/estimated/smoothed values. If you didn't want to do that, you'd just keep your original data. So you are going to discard data and replace it - the only issue to decide is how much to smooth the data.
More Answers (2)
Matt J
on 8 Feb 2013
Edited: Matt J
on 8 Feb 2013
Here's an example of how to do a cubic spline regression using interpMatrix. You would have to choose how finely you wanted to space the control points, which would affect the stiffness of the spline fit. In the example, the control points occur every 9 samples. After obtaining the 'trend' you would of course subtract it off the original time series to detrend it.
s = @(t) cos(2*pi*t).*exp(-abs(2*t))+ 2; %timeseries to fit
cubicBspline = @(t) (t>-1 & t<1).*(2/3 - t.^2 +abs(t).^3/2) +...
(abs(t)>=1 & abs(t)<2).*((2-abs(t)).^3/6);
tCtrlPts=linspace(-1.2, 1.2,9); %CtrlPts sample locations on t-axis
dtCtrlPts=tCtrlPts(2)-tCtrlPts(1);
tFine=linspace(-1.2, 1.2,81); %Fine sample locations on t-axis
dtFine=tFine(2)-tFine(1);
timeseries=s(tFine(:));
%create regression matrix
SampRatio=round(dtCtrlPts/dtFine); %Sampling ratio
kernel=cubicBspline(-2:1/SampRatio:2 );
nCtrlPts=length(tCtrlPts);
A=interpMatrix(kernel, 'max', nCtrlPts, SampRatio, 'mirror');
%%Do the fit!!!
trend = A*(A\timeseries);
plot(tFine,timeseries,tFine,trend,'*-');
legend('Time Series','Fitted Trend')
2 Comments
Image Analyst
on 8 Feb 2013
Because a spline is an interpolation rather than a regression, and so it goes through all the points, I don't see how it could detrend. Why not use detrend() or sgolay()?
2 Comments
See Also
Categories
Find more on Spline Construction in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!