How to sum a specific number of observations through an entire dataset

Hi!
Im totally new to Matlab, but not new to programming. Im in the process of create a buy/sell signal on a collection of logarithmic returns on equity. To create the signal I need to sum up the log returns for n periods (from 2 to 15) and if the sum is positive=buy (vice versa). So my question is how can I do it? Have tried to search for an answer but havent found one. (Been introduced to symsum etc, but didnt manage to get it to work)
to give an example, this is 15 of my log returns;
  • -0,003486069
  • -0,00047011
  • -0,00163133
  • -0,000500125
  • -0,007125325
  • 0,000619808
  • 0,015115188
  • -0,000310048
  • 0,00494774
  • -0,010040235
  • 0,001798382
  • 0,006518707
  • -0,014322073
  • 0,000809672
  • 0,004320653
Since there is 15 returns, using n=10, I should end up with 5 different sums for the last 5 periods.
Any help is much appreciated!

 Accepted Answer

Use cumsum. If your "log returns" are called 'LR', do this:
c = cumsum(LR);
s = c(n+1:15)-c(1:15-n);
For n = 10 that would give s the five sums, 2 to 11, 3 to 12, 4 to 13, 5 to 14, and 6 to 15.
Roger Stafford

3 Comments

% sum all power value with freq. range from LF criteria
lf_data = pow_list(freq_list >= 0.04 & freq_list < 0.15);
sum_lf_data = sum(lf_data);
C = [C char(10) 'sum all power value with freq, range from 0.04 to 0.15 for LF criteria...'];
% sum all power value with freq. range from HF criteria
hf_data = pow_list(freq_list >= 0.15 & freq_list <= 0.4);
sum_hf_data = sum(hf_data);
C = [C char(10) 'sum all power value with freq, range from 0.15 to 0.4 for LF criteria...'];
% ratio LF/HF
ratio_lf_hf = sum_lf_data / sum_hf_data;
C = [C char(10) 'ratio LF/HF...'];
Sir I got this error :
Unrecognized function or variable 'freq_list'.
Error in ekstraksi (line 4)
lf_data = pow_list(freq_list >= 0.04 & freq_list < 0.15);
Please help me I'm a newbie.
Why do you think 'freq_list' should exist? It's not there (yet). You need to define it somehow, like read in data from a file or something.
how sir? btw i have a data frquency and power from csv file. and what should i do with the range i made (freq_list)? i want to sum the data that is in range only (low frequency, high frequency, and ratio)
please help me with the coding. i am a new for matlab.

Sign in to comment.

More Answers (1)

To get all 6 sums, if you have 15 elements and a sliding window of 10 elements and you want the sum of each window, use conv():
m = [...
-0.003486069
-0.00047011
-0.00163133
-0.000500125
-0.007125325
0.000619808
0.015115188
-0.000310048
0.00494774
-0.010040235
0.001798382
0.006518707
-0.014322073
0.000809672
0.004320653]
numElementsToSum = 10; % Change to whatever you want
mSum = conv(m, ones(numElementsToSum, 1), 'valid')
Results:
mSum =
-0.002880506
0.002403945
0.009392762
-0.003297981
-0.001988184
0.009457794
For n = 10 that would give s the six sums, 1 to 10, 2 to 11, 3 to 12, 4 to 13, 5 to 14, and 6 to 15.

Categories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!