Loop with Time series

4 visualizzazioni (ultimi 30 giorni)
Giorgio
Giorgio il 26 Set 2023
Risposto: Voss il 26 Set 2023
Dear all, I'm currently trying to build a Global minimum Variance Portfolio that changes over time. I have a timetable of several daily returns of 8 assets, and I want lo calculate the global minimum variance portfolio for that given month. I tried with imposing 21 as the number of days in a month, however this is not optimal, since months also have 22 and 23 days. Is it possible to create a loop where I link the groups to the belonging month?
Brief example: I have 5208 daily returns, assuming that every month has 21 days the output should be a 248x8 matrix
Thank you all

Risposte (1)

Voss
Voss il 26 Set 2023
You can use the month and year functions, along with findgroups, to group the timetable by year and month. Then splitapply to perform some function (e.g., your method to "calculate the global minimum variance portfolio") on each month's data.
% construct a timetable like yours, containing (random)
% daily returns of 8 assets over 5208 days:
n_days = 5208;
n_assets = 8;
temp = num2cell(randn(n_days,n_assets),1);
t = timetable(datetime(now()-(n_days-1:-1:0).', ...
'ConvertFrom','datenum','Format','dd-MMM-yyyy'),temp{:})
t = 5208×8 timetable
Time Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 ___________ _________ _________ _________ ________ ________ ________ ________ _________ 24-Jun-2009 -0.8481 0.71243 0.5571 -1.0396 1.4287 0.060597 1.9732 0.73235 25-Jun-2009 0.17632 -0.099614 -1.3244 1.597 1.2128 -2.7768 1.1392 -0.031456 26-Jun-2009 -1.1902 -2.3053 -0.1268 0.47073 0.54342 0.93671 -0.77996 1.9441 27-Jun-2009 -1.3209 1.4919 -0.058169 -3.1413 0.29271 0.67738 -0.41053 -1.9958 28-Jun-2009 1.1728 -0.7338 1.5726 -0.1833 0.46258 1.1811 -0.14591 -2.6423 29-Jun-2009 -0.61775 -0.67265 -1.3583 -1.0131 -1.8862 0.86179 0.77355 -0.77497 30-Jun-2009 1.6884 -0.072801 0.5692 1.3678 -0.89934 0.09098 1.1243 -2.0821 01-Jul-2009 2.0786 0.26348 0.58298 0.14293 1.0838 -0.68792 -1.3042 1.1386 02-Jul-2009 -1.0093 0.76961 0.3776 0.89128 -0.82922 -0.67901 0.53186 -0.81903 03-Jul-2009 -1.1324 0.24465 -1.2842 0.052565 2.3662 1.7737 1.5313 1.4701 04-Jul-2009 1.6285 0.20871 0.75007 0.14514 -0.52201 1.8578 0.45132 0.66033 05-Jul-2009 -0.076449 0.59771 -0.11854 -1.9007 -0.10417 -0.24515 -0.79338 0.47538 06-Jul-2009 -0.34889 0.76339 -0.058678 -0.8654 -0.6213 0.84136 0.61626 0.54069 07-Jul-2009 0.48528 0.42656 -2.5592 0.026154 0.19026 0.30093 2.256 -2.0029 08-Jul-2009 0.67997 0.98076 -1.0043 1.4066 0.11917 -0.53833 2.1094 -0.13361 09-Jul-2009 -0.12264 -1.0469 0.53019 1.3641 1.6656 0.15018 2.2481 -1.1163
% group returns by year and month
mm = month(t.Time);
yy = year(t.Time);
g = findgroups(yy,mm);
% apply a function on each group (i.e., each month+year)
% the function get_max_return() here calculates the maximum
% value amongst all the returns of that group
tC = splitapply(@(varargin)get_max_return(varargin{:}),t,g)
tC = 172×1
1.9732 2.3662 2.3034 2.7011 3.2285 3.0647 2.9996 3.3741 2.8999 2.8206
function out = get_max_return(varargin)
out = max([varargin{:}],[],'all');
end

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by