我有一个名为BondData
的表,其中包含以下内容:
Settlement Maturity Price Coupon
8/27/2016 1/12/2017 106.901 9.250
8/27/2019 1/27/2017 104.79 7.000
8/28/2016 3/30/2017 106.144 7.500
8/28/2016 4/27/2017 105.847 7.000
8/29/2016 9/4/2017 110.779 9.125
对于此表中的每一天,我将执行某项任务,即为变量分配多个值并执行必要的计算。逻辑如下:
do while Settlement is the same
m_settle=current_row_settlement_value
m_maturity=current_row_maturity_value
and so on...
my_computation_here...
end
就像我希望遍历我的结算日期并执行任务一样,只要日期相同。
编辑:为了澄清我的问题,我正在使用Nelson-Siegel和Svensson模型实现Yield Curve拟合。到目前为止,这是我的代码:
function NS_SV_Models()
load bondsdata
BondData=table(Settlement,Maturity,Price,Coupon);
BondData.Settlement = categorical(BondData.Settlement);
Settlements = categories(BondData.Settlement); % get all unique Settlement
for k = 1:numel(Settlements)
rows = BondData.Settlement==Settlements(k);
Bonds.Settle = Settlements(k); % current_row_settlement_value
Bonds.Maturity = BondData.Maturity(rows); % current_row_maturity_value
Bonds.Prices=BondData.Price(rows);
Bonds.Coupon=BondData.Coupon(rows);
Settle = Bonds.Settle;
Maturity = Bonds.Maturity;
CleanPrice = Bonds.Prices;
CouponRate = Bonds.Coupon;
Instruments = [Settle Maturity CleanPrice CouponRate];
Yield = bndyield(CleanPrice,CouponRate,Settle,Maturity);
NSModel = IRFunctionCurve.fitNelsonSiegel('Zero',Settlements(k),Instruments);
SVModel = IRFunctionCurve.fitSvensson('Zero',Settlements(k),Instruments);
NSModel.Parameters
SVModel.Parameters
end
end
同样,我的主要目标是每天获取每个模型的参数(beta0,beta1,beta2等)。我在Instruments = [Settle Maturity CleanPrice CouponRate];
收到错误,因为Settle只包含一条记录(2016年8月27日),因为这个日期有两行,所以假设有两条记录。另外,我注意到Maturity,CleanPrice和CouponRate包含所有记录。它们应该只包含每天的相应数据。
希望我现在能让我的问题更加清晰。顺便说一下,我正在使用MATLAB R2015a。
答案 0 :(得分:0)
使用分类数组。这是你的功能(没有它的标题,我不能运行的所有行都被评论过):
BondData = table(datetime(Settlement),datetime(Maturity),Price,Coupon,...
'VariableNames',{'Settlement','Maturity','Price','Coupon'});
BondData.Settlement = categorical(BondData.Settlement);
Settlements = categories(BondData.Settlement); % get all unique Settlement
for k = 1:numel(Settlements)
rows = BondData.Settlement==Settlements(k);
Settle = BondData.Settlement(rows); % current_row_settlement_value
Mature = BondData.Maturity(rows); % current_row_maturity_value
CleanPrice = BondData.Price(rows);
CouponRate = BondData.Coupon(rows);
Instruments = [datenum(char(Settle)) datenum(char(Mature))...
CleanPrice CouponRate];
% Yield = bndyield(CleanPrice,CouponRate,Settle,Mature);
%
% NSModel = IRFunctionCurve.fitNelsonSiegel('Zero',Settlements(k),Instruments);
% SVModel = IRFunctionCurve.fitSvensson('Zero',Settlements(k),Instruments);
%
% NSModel.Parameters
% SVModel.Parameters
end
请记住以下内容:
Instruments = [Settle Maturity CleanPrice CouponRate];
Bond
中没有必要使用它(例如Settle = Bonds.Settle;
)。datetime
对象和字符串或数字之间进行转换。例如,在上面的代码中:datenum(char(Settle))
。我不知道您需要传递给以下函数的输入类型。