For loop with moving window
3 ビュー (過去 30 日間)
古いコメントを表示
Hi
I want to create a for-loop that calculates the weights of portfolios using a moving window for the period I am investigating. The moving window should move one day at a time and there are 1000 days in the window.
For example, compute the optimal weights on day1001 based on observations for period 1-1000days. Then you move on to day 1002, and re-calculate the weights, based on observations for period 2-1001days, etc.
I have a matrix of returns (Rets) that is 3740x6. The first column has the dates and the rest of the columns have daily returns for five different asset classes.
Thanks!
0 件のコメント
回答 (4 件)
Image Analyst
2014 年 7 月 31 日
Simply use conv()
kernel = [zeros(1,999), ones(1,1000)]; % Look backward 1000 elements.
output = conv(observations, kernel, 'valid'); % Get sliding means
5 件のコメント
Evan
2014 年 7 月 31 日
編集済み: Evan
2014 年 7 月 31 日
Googling "convolution" may help you to get an overall idea of the operation before getting into the MATLAB implementation of it. You might find the wikipedia entry helpful, although I often find that I drown in notation when trying to read through mathematics wiki articles.
Basically, conv() will slide the kernel over the vector, progressing one element at a time, and multiply then sum corresponding values that fall within the window. It "pads" the array with zeros in order to start with the first element. Here's an example:
A = [1 2 3 4 5];
B = [1 1];
conv(A,B)
ans =
1 3 5 7 9 5
So here's the progression:
[ 0 1 2 3 4 5 0]
[ 1 1 ]
1*0 + 1*1 = 1;
__________________
[ 0 1 2 3 4 5 0 ]
[ 1 1 ]
1*1 + 1*2 = 3
__________________
[ 0 1 2 3 4 5 0 ]
[ 1 1 ]
1*2 + 1*3 = 5
__________________
[ 0 1 2 3 4 5 0]
[ 1 1 ]
1*3 + 1*4 = 7
__________________
[ 0 1 2 3 4 5 0]
[ 1 1 ]
1*4 + 1*5 = 9
__________________
[ 0 1 2 3 4 5 0]
[ 1 1 ]
1*5 + 1*0 = 5
Image Analyst
2014 年 7 月 31 日
編集済み: Image Analyst
2014 年 7 月 31 日
Convolution basically gives you a weighted sum in a sliding window. It would give you the average value (price?) of your signal over the past 1000 days. I'm a scientist, not a financial person, so perhaps you don't want that - I don't know. If you do want a weighted sum in a sliding window (like the 20 day or 50 day moving stock price average) then convolution is for you, though there may be some functions in the Financial Toolbox that are better suited, like with more options and using the terminology financial people are familiar with.
There is also a conv2() function that deals with 2D arrays. If you just wanted to sum up going down columns, then the kernel would be a column vector. If you wanted to average across columns, then the kernel would be a row vector.
Ahmet Cecen
2014 年 7 月 30 日
編集済み: Ahmet Cecen
2014 年 7 月 30 日
If I understand you correctly, you have an indexing problem. Try an indexing scheme like this:
for i=1:N
Weights(i)=function(Rets(i:(i+1000),2));
end
Arijit Ghosh
2017 年 3 月 9 日
Hello I need to design a sliding window of time t=5 sec. to slide all over the signal without overlap and thereby perform CWT from each window. Can anyone help please..
0 件のコメント
Alejandra Pena-Ordieres
2024 年 7 月 29 日
Hello,
You might want to consider using the backtesting workflow available in the Financial Toolbox, Backtest Investment Strategies Using Financial Toolbox. In your case, you'd need to convert your data into a timetable and define your optimization strategy as a backtestStrategy with RebalanceFrequency=1 and a LookbackWindow = [1000 1000].
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Portfolio Optimization and Asset Allocation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!