Issues with DieboldLi and failing to get the correct matrix to work and error message ctranspose

3 次查看(过去 30 天)
I have tried to replicate the codes in Matlab link:
I used different data set: Data_DieboldLi and the part I am having issues with is to turn my data inot suitable format for the code to work for me. The code keep returning the error message
"Error using ' (line 217)
Undefined function 'ctranspose' for input arguments of type 'timetable'. Use the ROWS2VARS function instead.
Error in block4test (line 46)
tmpCurveModel = DieboldLi.fitBetasFromYields(EOMDates(jdx),lambda_t*12,daysadd(EOMDates(jdx),30*TimeToMat),EstimationData(jdx,:)');"
I realise it has something to do with the matrix I am using but I failed to fix it. Please can any one help.
Here is what I did
clear;clc
%import Matlab dataset object
load Data_DieboldLi
EstimationData= DataTable;
%EstimationDataset = DataTable;
%EstimationData = double(Estimationdataset);
% Extract data for the last day of each month
%create the vector of months within the data sample period from 1972 to 2000
MonthYearMat = repmat((1972:2000)',1,12)';
%set end of the month dates in numeric format, exclude holidays
EOMDates = lbusdate(MonthYearMat(:),repmat((1:12)',29,1));
% Explicitly set the time factor lambda see Diebold Li (2006) Please fill
% the lambda
lambda_t = .06090;
% Construct a matrix of the factor loadings
% Tenors (monthly) associated with data
%3, 6, 9, 12, 15, 18, 21, 24, 30, 36, 48, 60, 72, 84, 96, 108 and 120
TimeToMat = [3 6 9 12 15 18 21 24 30 36 48 60 72 84 96 108 120 ]';
%DL (2006) Factor Loadings
X = [ones(size(TimeToMat)) (1 - exp(-lambda_t*TimeToMat))./(lambda_t*TimeToMat) ...
((1 - exp(-lambda_t*TimeToMat))./(lambda_t*TimeToMat) - exp(-lambda_t*TimeToMat))];
% Plot the factor loadings
plot(TimeToMat,X)
title('Factor Loadings for Diebold Li Model with time factor of .0609')
xlabel('Maturity (months)')
ylim([0 1.1])
legend({'Beta1','Beta2','Beta3'},'location','east')
% Preallocate the Betas
Beta = zeros(size(EstimationData,1),3);
% Loop through and fit each end of month yield curve
for jdx = 1:size(EstimationData,1)
tmpCurveModel = DieboldLi.fitBetasFromYields(EOMDates(jdx),lambda_t*12,daysadd(EOMDates(jdx),30*TimeToMat),EstimationData(jdx,:)');
Beta(jdx,:) = [tmpCurveModel.Beta1 tmpCurveModel.Beta2 tmpCurveModel.Beta3];
end

回答(1 个)

Narvik
Narvik 2023-8-24
Hi,
I recognize that you encountered a problem when utilizing MATLAB's 'ctranspose' function. The 'ctranspose' function does not operate on the table datatype, as stated in the error message.
The data stored in ‘EstimationData’ in the example documentation is of type ‘datasetwhereas the data stored in the variable as per your code is of the type ‘table’.
Please find the working code in which the table is converted to a dataset using the ‘table2dataset’ function.
load Data_DieboldLi
% Create the vector of months within the data sample period from 1972 to 2000
MonthYearMat = repmat((1972:2000)',1,12)';
% Set end of the month dates in numeric format, exclude holidays
EOMDates = lbusdate(MonthYearMat(:),repmat((1:12)',29,1));
% Removing the last 3 columns of the table ('CU', 'FEDFUNDS', 'PI')
% and converting the table to dataset as input for 'double' function.
EstimationDataset = table2dataset(DataTable(:, 1:end-3));
EstimationData = double(Estimationdataset);
% Explicitly set the time factor lambda
lambda_t = .0609;
% Construct a matrix of the factor loadings
% Tenors associated with data
TimeToMat = [3 6 9 12 15 18 21 24 30 36 48 60 72 84 96 108 120 ]';
X = [ones(size(TimeToMat)) (1 - exp(-lambda_t*TimeToMat))./(lambda_t*TimeToMat) ...
((1 - exp(-lambda_t*TimeToMat))./(lambda_t*TimeToMat) - exp(-lambda_t*TimeToMat))];
% Plot the factor loadings
plot(TimeToMat,X)
title('Factor Loadings for Diebold Li Model with time factor of .0609')
xlabel('Maturity (months)')
ylim([0 1.1])
legend({'Beta1','Beta2','Beta3'},'location','east')
% Preallocate the Betas
Beta = zeros(size(EstimationData,1),3);
% Loop through and fit each end of month yield curve
for jdx = 1:size(EstimationData,1)
tmpCurveModel = DieboldLi.fitBetasFromYields(EOMDates(jdx),lambda_t*12,daysadd(EOMDates(jdx),30*TimeToMat),EstimationData(jdx,:)');
Beta(jdx,:) = [tmpCurveModel.Beta1 tmpCurveModel.Beta2 tmpCurveModel.Beta3];
end
If you prefer working with 'table' datatype rather than 'dataset' datatype, another option is to use the 'rows2vars' function rather than the 'ctranspose' method.
Please refer to the documentation below for more information :
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by