How to make vectors A1, A2,... A127. Rolling time window.
21 views (last 30 days)
Show older comments
hey guys,
my problem is the following: i have a column vector (276x1)of S&P500 returns (276 monthly returns in total) and i need so to say 127 time windows (so 127 column vectors) with always a time frame of 150 months. So what i mean: The total time window is from Jan92 - Dec14, now i need a vector1 = Jan92 - Jun04; vector2 = Feb92 - Jul04, ...., vector127 = Jul02 - Dec14.
m = 150
returns=zeros(1,150)'
for k=1:127
returns(k)=SP500(k:m+k-1,1) %SP500 is the column vector with all monthly returns
end
That doesnt work. Can anyone help me?
Thx in advance.
0 Comments
Answers (2)
TED MOSBY
on 14 Nov 2024 at 4:33
Edited: TED MOSBY
on 18 Nov 2024 at 19:52
It looks like you're trying to create a series of 127 time windows, each containing 150 monthly returns, based on the 276 monthly returns available in your S&P 500 return data (SP500). If you want each time window to shift by 1 month, starting from January 1992 to December 2014 you can simply loop over the indices from 1 to 127, and for each iteration, extract the corresponding 150 months from SP500. Have a look at an example code below:
% Assume SP500 is a column vector of size (276x1)
SP500 = randn(276, 1); % Example data, replace with your actual data
% Pre-allocate a matrix to store the 127 time windows
returns = zeros(150, 127); % Each column is a 150-month window
% Loop to create 127 time windows
for k = 1:127
% Extract the 150 months for the k-th window
returns(:, k) = SP500(k:k+149);
end
Hope this helps!
0 Comments
Walter Roberson
on 18 Nov 2024 at 22:02
If you have the Signal Processing Toolbox, you might as well use buffer()
I always think that buffer() should be moved into MATLAB itself instead of the toolbox.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements 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!