Main Content

Remove Linear Trends from Timetable Data

This example shows how to remove a linear trend from daily closing stock prices in a timetable. If the data does have a trend, detrending forces the mean of the detrended data to zero and reduces overall variation.

Create Stock Data

Create a sample timetable containing daily closing stock prices. Use randomly sampled numbers from a normal distribution.

x = 0:300;
Time = days(x)';
dailyFluct = gallery("normaldata",size(x),2); 
closing = cumsum(dailyFluct) + 20 + x/100;
StockPrice = closing';
TT = timetable(Time,StockPrice)
TT=301×1 timetable
     Time      StockPrice
    _______    __________

    0 days       21.749  
    1 day        21.892  
    2 days       22.227  
    3 days       21.443  
    4 days       21.768  
    5 days       21.251  
    6 days       22.193  
    7 days       23.368  
    8 days       21.332  
    9 days       20.698  
    10 days      22.449  
    11 days      22.946  
    12 days      24.004  
    13 days      25.503  
    14 days      26.783  
    15 days      24.937  
      ⋮

Plot and label the stock price data.

plot(TT,"Time","StockPrice");
xlabel("Time (days)");
ylabel("Stock Price (dollars)");

Remove Trend

Apply detrend, which performs a linear fit to the stock prices and removes the trend. Specify to append the detrended data to the input timetable.

TT = detrend(TT,ReplaceValues=false);

Compute the trend line by subtracting the detrended data from the input data.

trend = TT.StockPrice - TT.StockPrice_detrended;
TT = addvars(TT,trend,NewVariableNames="Trend")
TT=301×3 timetable
     Time      StockPrice    StockPrice_detrended    Trend 
    _______    __________    ____________________    ______

    0 days       21.749             -3.7893          25.538
    1 day        21.892             -3.7397          25.631
    2 days       22.227             -3.4975          25.724
    3 days       21.443             -4.3742          25.817
    4 days       21.768             -4.1423           25.91
    5 days       21.251             -4.7525          26.003
    6 days       22.193             -3.9033          26.096
    7 days       23.368             -2.8216          26.189
    8 days       21.332             -4.9502          26.282
    9 days       20.698             -5.6776          26.375
    10 days      22.449             -4.0195          26.468
    11 days      22.946             -3.6157          26.561
    12 days      24.004             -2.6498          26.654
    13 days      25.503             -1.2442          26.747
    14 days      26.783           -0.056718           26.84
    15 days      24.937             -1.9958          26.933
      ⋮

Find the average closing price of the detrended data.

average_detrended = mean(TT.StockPrice_detrended)
average_detrended = -1.2464e-14

As expected, the detrended data has a mean very close to 0.

Visualize Detrended Data

Display the results by plotting the original daily closing stock prices, the trend line, the detrended data, and its mean.

plot(TT,"StockPrice")
hold on
plot(TT,"Trend")
plot(TT,"StockPrice_detrended")
yline(average_detrended)
legend("Input Data","Trend","Detrended Data", ...
    "Mean of Detrended Data","Location","northwest")
xlabel("Time (days)"); 
ylabel("Stock Price (dollars)");

See Also

Live Editor Tasks

Functions