NB: The differences are sometimes very small (order of 1e-15, but for other runs, they are way more significant)
Understand values differences between smooth and smoothdata functions
6 views (last 30 days)
Show older comments
Hi,
I'm working on rlowess smoothing and I was wondering why I observe output differences between smooth and smoothdata functions, despite the fact that they are supposed to be similar.
I wrote a small piece of code:
% Sample one-dimensional data
x = 1:100;
data = cos(2*pi*0.05*x+2*pi*rand) + 0.5*randn(1,100);
% Apply rlowess smoothing with smooth
smoothedData2 = smooth(data, 9, 'rlowess');
% Apply rlowess smoothing with smoothdata
smoothedData3 = smoothdata(data, 'rlowess', 9);
smoothedData2
smoothedData3
isequal(smoothedData2, smoothedData3)
% Plot the original and smoothed data
figure;
plot(x, data, 'o');
hold on;
plot(x, smoothedData2, '--');
plot(x, smoothedData3, ':');
legend('Original Data', 'Smoothed Data 2', 'Smoothed Data 3');
title('Comparison of Smoothing Methods');
% Calculate the differences
diff2_3 = smoothedData2(:) - smoothedData3(:);
% Display the differences
disp('Difference between smoothedData2 and smoothedData3:');
disp(diff2_3);
As you can see, the graph is similar for both cases, but the values are different.
I also tried to change the method to 'lowess', and the differences there are only on the first and last entries, so I'm guessing that the moving window isn't calculated similarly in both cases (however, I can't find a difference in the window calculation from the documentation here and here).
To sum up, my two questions are:
- Is there a difference in window calculation between smooth and smoothdata for 'lowess' method
- Apart from this difference, could there be another issue with the calculation of robust weights for example in 'rlowess' method.
Thank you!
Answers (1)
Adam Danz
on 29 Jan 2025
Edited: Adam Danz
on 29 Jan 2025
> Is there a difference in window calculation between smooth and smoothdata for 'lowess' method
These two functions compute the regression using completely different methods but they both produce the same result within limits of precision. The difference in the results is caused by limits to floating point representation.
As an analogy, here are two solutions for creating the same 5-element vector. However, isequal returns false when we compare the results!
method1 = [0.1 : 0.1 : 0.5]
method2 = [0.1 0.2 0.3 0.4 0.5]
isequal(method1, method2)
When we compare each value, we find that 0.3 in the first method is not represented in the same way as "0.3" in the second method.
method1 == method2
method1(3) - 0.3
That's just a simple vector but it's enough to show how two different solutions that arrive to the same output can differ because of the way floating values are represented.
More info
Here I use isapprox (R2024b) to compare the two results and I confirm that the max difference is indicative of floating point limitations.
% Sample one-dimensional data
x = 1:100;
data = cos(2*pi*0.05*x+2*pi*rand) + 0.5*randn(1,100);
% Apply rlowess smoothing with smooth
smoothedData2 = smooth(data, 9, 'lowess');
% Apply rlowess smoothing with smoothdata
smoothedData3 = smoothdata(data, 'lowess', 9);
isequal(smoothedData2(:), smoothedData3(:))
all(isapprox(smoothedData2(:), smoothedData3(:))) %R2024b
max(abs(smoothedData2(:) - smoothedData3(:)))
2 Comments
Adam Danz
on 29 Jan 2025
Thanks for the demo Ben. I'll try to find some time this afternoon to investigate.
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!