How to make vectors A1, A2,... A127. Rolling time window.

21 ビュー (過去 30 日間)
Markus
Markus 2015 年 2 月 11 日
回答済み: Walter Roberson 2024 年 11 月 18 日 22:02
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.

回答 (2 件)

TED MOSBY
TED MOSBY 2024 年 11 月 14 日 4:33
編集済み: TED MOSBY 2024 年 11 月 18 日 19:52
Hi @Markus,
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!

Walter Roberson
Walter Roberson 2024 年 11 月 18 日 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.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by