Optimizing for speed. Moving skewness finder. Cumulative sum proving to be bottleneck.
Show older comments
Hello,
I have written the below provided function to calculate the moving window skewness of a time series.
To find out where the bottleneck is I used tic/toc and it seems the part where the cumulative sum (T2) is calculated takes the most amount of time.
The fraction of time taken (for each section) depends on N, the size of the time series. This can be found out using the attached script (Trial.m). For N = 1000000 the cumsum takes around 83% of the time.
The concern:
How can I speed up this code? Or has it reached its limit?
function sk_timeseries = moving_skewness(x, window_size, window_step)
% T1 start
X1 = x;
X2 = X1 .* x;
X3 = X2 .* x;
% T1 end
% T2 start
S1 = cumsum([0, X1]);
S2 = cumsum([0, X2]);
S3 = cumsum([0, X3]);
% T2 end
% T3 start
% Get the indices at which to calculate skewness values
window_ends_idx = window_size: window_step: length(x);
% Preallocate array for storing skewness time series
sk_timeseries = zeros(1, length(window_ends_idx));
N = window_size;
% T3 end
% T4 start
% Generate skewness time series
for k = 1: length(window_ends_idx)
j = window_ends_idx(k) + 1;
i = j - window_size;
% Calculate M1_ji
M1_ji = (S1(j) - S1(i)) / N;
% Calculate necessary central moments
M2_ji = (S2(j) - S2(i)) - window_size * M1_ji * M1_ji;
M3_ji = (S3(j) - S3(i)) - 3 * M1_ji * (S2(j) - S2(i)) + 2 * N * M1_ji * M1_ji * M1_ji;
% Calculate skewness
sk_timeseries(k) = sqrt(N) * M3_ji / (M2_ji^1.5);
end
% T4 end
end
2 Comments
The concatenation with 0 takes as much as the cumsum
N = 1000000;
X1 = rand(1,N);
tic
X1 = [0 X1];
toc
tic
S1 = cumsum(X1);
toc
function sk_timeseries = moving_skewness(x, window_size, window_step)
% T1 start
X1 = [0 x];
...
end
would at least remove two of those catenation operations...and
N = 1000000;
X1 = rand(1,N);
tic
X1 = [0 X1];
toc
X1 = rand(1,N+1);
tic
X1(1)=0;
toc
would eliminate needing either if could generate x with the extra position to begin with. Clearing the one location is much cheaper than needing to allocate the new array [] requires.
Of course, if generating the initial array is just adding a [0 ...] somewhere else it doesn't help unless it can be moved out of the function and only done once't.
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB Support Packages 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!