how can I convert fints to timetable?

3 views (last 30 days)
Jonghyeok Choi
Jonghyeok Choi on 29 Jul 2019
Answered: Pujitha Narra on 7 Oct 2019
Hello,
I have been trying to convert fints functionality used in an existing code to timetable functionality as a future release will not support fints anymore.
I have replaced fints functions in the first attachment to timetable functions in the second attachment.
%Allign data by settle date
%Clear memory/close windows
clear all
close all
%Define global variables
dlGlobal;
load clnMktDataAll;
%%% Calculate currSettle (applies to interest rates) - trading dates to be used in analysis %%%
Data=mktData.h15;
Data(sum(isnan(Data),2)>=2,:) = [];
nonMsngH15=Data;
Data=mktData.ofIndDN ;
Data(sum(isnan(Data),2)>=4,:) = [];
nonMsngOFdn=Data;
Data=mktData.ofIndTAP ;
Data(sum(isnan(Data),2)>=4,:) = [];
nonMsngOFtap=Data;
Data=mktData.libor;
Data(sum(isnan(Data),2)>=8,:) = [];
nonMsngLibor=Data;
tsH15=fints(nonMsngH15(:,1),nonMsngH15(:,2:end));
tsOFtap=fints(nonMsngOFtap(:,1),nonMsngOFtap(:,3:end));
tsOFdn=fints(nonMsngOFdn(:,1),nonMsngOFdn(:,2:end));
tsLibor=fints(nonMsngLibor(:,1),nonMsngLibor(:,2:end));
newfts=merge(tsOFdn, tsOFtap, tsH15, tsLibor, 'DateSetMethod', 'intersection');
currSettle=newfts.dates;
clear newfts
The "fints" fuction used in the code above have been replaced with "timetable" in the code below.
%Allign data by settle date
%Clear memory/close windows
clear all
close all
%Define global variables
dlGlobal;
load clnMktDataAll;
%%% Calculate currSettle (applies to interest rates) - trading dates to be used in analysis %%%
Data=mktData.h15;
Data(sum(isnan(Data),2)>=2,:) = [];
nonMsngH15=Data;
TnonMsngH15=array2table(nonMsngH15); %%%added to convert array to table
th15=datetime(TnonMsngH15.nonMsngH151,'ConvertFrom','datenum'); %%%added to convert 1st column to datetime vector
ttH15=table2timetable(TnonMsngH15(:,2:end),'RowTimes',th15); %%%added to convert table to timetable
Data=mktData.ofIndDN ;
Data(sum(isnan(Data),2)>=4,:) = [];
nonMsngOFdn=Data;
TnonMsngOFdn=array2table(nonMsngOFdn);
tOFdn=datetime(TnonMsngOFdn.nonMsngOFdn1,'ConvertFrom','datenum');
ttOFdn=table2timetable(TnonMsngOFdn(:,2:end),'RowTimes',tOFdn);
Data=mktData.ofIndTAP ;
Data(sum(isnan(Data),2)>=4,:) = [];
nonMsngOFtap=Data;
TnonMsngOFtap=array2table(nonMsngOFtap);
tOFtap=datetime(TnonMsngOFtap.nonMsngOFtap1,'ConvertFrom','datenum');
ttOFtap=table2timetable(TnonMsngOFtap(:,3:end),'RowTimes',tOFtap);
Data=mktData.libor;
Data(sum(isnan(Data),2)>=8,:) = [];
nonMsngLibor=Data;
TnonMsngLibor=array2table(nonMsngLibor);
tLibor=datetime(TnonMsngLibor.nonMsngLibor1,'ConvertFrom','datenum');
ttLibor=table2timetable(TnonMsngLibor(:,2:end),'RowTimes',tLibor);
newTT = synchronize(ttOFdn,ttOFtap,ttH15,ttLibor); %%%%%%union should be changed
%currSettle=newfts.dates;
currSettle=newTT.Time;
%clear newfts
clear newTT
However, I have encountered other problems in replacing the fints functionality with the timetable functionality.
First problem is that, I would like to change the sub function of fints, 'merge', to 'synchronize,' which occurs to me that it is an equivalent sub-function in timetable, and I would like to keep the 'intersection' option for the replaced timetables as well. However, it appears that there is no option like 'intersection' for 'synchronize.' So my questions is, is there any way I can combine the timetable data only if the dates for the different timetable data overlap?(Just like using merge with intersection option.)
Second problem comes from the different formats in fints and timetable. I figured out that fints uses the array format, while timetable uses the table format. The problems takes place when using the loop functions to generate hiscurveAlgn, histCapVolAlgn, histSwpVolAlgn, currSettle, SwpVolSettle, CapVolSettle, TimeToMat, and Tickers. The original program was written to work out with the array format (fints format) and it runs into errors as the earlier part of the program is now written in the table format, not array format. I was wondering if you could correct these loops so that they can work out with table format to generate aforementioned output.
%%% merge ofIndDNand ofIndTAP rates %%%
for k = 1:length(TimeToMat.ofIndDN)
tmp = num2str(TimeToMat.ofIndDN(k));
addnull=4-length(tmp);
tmp=[repmat('_', 1, addnull) tmp];
namesDN(k,:)= genvarname(tmp);
end
tsOFDN=fints(nonMsngOFdn(:,1),nonMsngOFdn(:,2:end), cellstr(namesDN));
for k = 2:length(TimeToMat.ofIndTAP) %Drop 1-year TAP as we use 12-month DN instead
tmp = num2str(TimeToMat.ofIndTAP(k));
addnull=6-length(tmp);
tmp=[repmat('_', 1, addnull) tmp];
namesTAP(k-1,:)= genvarname(tmp);
end
tsOFTAP=fints(nonMsngOFtap(:,1),nonMsngOFtap(:,3:end), cellstr(namesTAP)); %Drop 1-year TAP as we use 12-month DN instead
newftsDN = tsOFDN(datestr(currSettle));
newftsTAP = tsOFTAP(datestr(currSettle));
newftsOF=merge(newftsDN, newftsTAP, 'DateSetMethod', 'intersection','SortColumns',0);
mktData.ofInd=[newftsOF.dates fts2mat(newftsOF)];
TimeToMat.ofInd=[TimeToMat.ofIndDN TimeToMat.ofIndTAP(2:end)];
%%% Allign interest rates %%%
%initialize
tmpTypes=[origRateTypes {'EDHAlligned' 'EDMAlligned' 'EDUAlligned' 'EDZAlligned'}];
for idx=1:length(tmpTypes)
currtab=char(tmpTypes(idx));
s1= ['histCurveAlgn' '.' currtab];
s2= ['mktData' '.' currtab];
evalc([s1 '=NaN(size(currSettle,1), size(' s2 ',2)-1)']);
end
%build time series
for jdx=1:length(currSettle);
for idx=1:length(tmpTypes)
currtab=char(tmpTypes(idx));
s1= ['histCurveAlgn' '.' currtab];
s2= ['mktData' '.' currtab];
evalc(['tmpIdx=find(' s2 '(:,1)==currSettle(jdx))']);
if length(tmpIdx)>0
evalc([s1 '(jdx,:)=' s2 '((tmpIdx),2:end)']);
end
clear tmpIdx;
end
end
%%% Allign implied volatility quotes %%%
%Swaption Vols
Data=mktData.swaption7X;
Data(sum(isnan(Data),2)>4,:)=[];
SwpVolSettle=sortrows(Data(:,1));
%histSwpVolAlgn
for idx=1:length(swaptionTypes)
currtab=char(swaptionTypes(idx));
s1= ['histSwpVolAlgn' '.' currtab];
evalc([s1 '=NaN(length(SwpVolSettle),length(TimeToMat' '.' currtab '))' ]); %initialize
s2= ['mktData' '.' currtab];
for jdx=1:length(SwpVolSettle);
evalc(['tmpIdx=find(' s2 '(:,1)==SwpVolSettle(jdx))']);
if length(tmpIdx)>0
evalc([s1 '(jdx,:)=' s2 '((tmpIdx),2:end)']);
end
clear tmpIdx;
end
end
%Cap Vols
Data=mktData.strike1Vol;
Data(sum(isnan(Data),2)>4,:)=[];
CapVolSettle=sortrows(Data(:,1));
%histCapAlgn
for idx=1:length(capTypes)
currtab=char(capTypes(idx));
s1= ['histCapVolAlgn' '.' currtab];
evalc([s1 '=NaN(length(CapVolSettle),length(TimeToMat' '.' currtab '))' ]);
s2= ['mktData' '.' currtab];
for jdx=1:length(CapVolSettle);
evalc(['tmpIdx=find(' s2 '(:,1)==CapVolSettle(jdx))']);
if length(tmpIdx)>0
evalc([s1 '(jdx,:)=' s2 '((tmpIdx),2:end)']);
end
clear tmpIdx;
end
end
In summary, I would like to replace merge with synchronize but keep the intersection option.
And I would like the loops in the program to work out with the table format, which now only work out with the array format.
Thank you for your answer and time in advance.
PS. The Program was written by Alex Bogin on 10/17/18.

Answers (1)

Pujitha Narra
Pujitha Narra on 7 Oct 2019
Hi Jonghyeok Choi,
The answer for the first part of your question is,Yes. You can use 'synchronize' as follows
TT = synchronize(TT1,TT2,'intersection')
You can look at the following for further details:
For the second part of the question, I am assuming that you want to extract data from a timetable as an array(for each column). You can do it as follows:
After creating the time table, you can name each of the columns and access the data by using the names(this would return an array). You can refer to the examples in this link for the same:

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!