I have a dataset with bonds (of different maturities, coupon rates etc.) with daily close prices going back to the 2000s. I want to calculate the Svensson curve parameters for every day of this dataset, based on the bonds that were available for that day. I have written the code below that does that in a loop:
% calculating Svensson
dataset = readtable("IT_FixedHistorical.csv");
weekdays = unique(dataset{:,"Date"});
% initialise table with dummy parameters,
% not sure if this is necessary but not sure how to do it otherwise
tab1 = table(0,1,2,3,4,5,datetime(2024,1,1));
tab1.Properties.VariableNames=["params1","params2","params3","params4","params5","params6","params7"];
% loop for every unique day on the dataset
for idx = 1:numel(weekdays)
try
i = weekdays(idx);
% filter dataset based on date
matches = (dataset.Date == i);
data = dataset(matches,:);
Maturity = datenum(data{:,"Maturity"});
CouponRate = data{:,"CouponRate"};
InstrumentPeriod = data{:,"InstrumentPeriod"};
CleanPrice = data{:,"MidPrice"};
Settle = repmat(datenum(i),[height(data) 1]);
CurveSettle = datenum(i);
Redemption = repmat(1000,[height(data) 1]);
Instruments = [Settle Maturity CleanPrice CouponRate];
SvenssonModel = IRFunctionCurve.fitSvensson('Zero',CurveSettle,...
Instruments,'InstrumentPeriod',InstrumentPeriod);
%[Beta0,Beta1,Beta2,Beta3,tau1,tau2].
params = SvenssonModel.Parameters;
params(end+1)=datenum(i);
t1 = array2table(params);
t1.params7 = datetime(t1.params7,'ConvertFrom','datenum');
tab1 = [tab1;t1];
catch
% do nothing
end
end
writetable(tab1,'IT_Svensson.csv');
Is there any way to rewrite the loop so that it takes less time to run this script? I have considerably increased the dataset which means hours of running time.
Thank you!