2016-09-07 23 views
0

我有這樣的表名爲BondData其中包含以下內容:如何通過表圈基於獨特的日期在MATLAB

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 

這就像我通過我的結算日期要循環,只要日期是一樣執行任務。

編輯:只是爲了澄清我的問題,我在執行收益率曲線使用納爾遜 - 西格爾和斯文森models.Here是我的代碼至今配件:

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),假設有兩條記錄,因爲此日期有兩行。另外,我注意到成熟度,CleanPrice和CouponRate包含所有記錄。他們應該只包含每天的相應數據。

希望我現在讓我的問題更清楚了。順便說一下,我正在使用MATLAB R2015a。

+0

你到目前爲止試過了什麼?你有你的代碼的最小,完整和可驗證的例子嗎? – bushmills

+0

我試圖得到這樣的獨特的結算日期,但我有一個關於如何實現循環負載債券的問題數據表 A =表(結算,到期日,價格,優惠券); B =唯一(A(:,1),'rows'); – aldrean

+0

你使用哪個MATLAB版本? – EBH

回答

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 

請記住以下幾點:

  1. 你不能Concat的不同類型的變量,你嘗試在:Instruments = [Settle Maturity CleanPrice CouponRate];
  2. 有沒有必要在結構Bond,你不使用它(例如Settle = Bonds.Settle;)。
  3. 使用相關函數在datetime對象和字符串或數字之間進行轉換。例如,在上面的代碼中:datenum(char(Settle))。我不知道你需要傳遞給以下函數的輸入是什麼類型。
+0

感謝您的迅速回復。因爲我完全是MATLAB的新手,請耐心等待。我在這段代碼中遇到了一個錯誤:Settlements = categories(datetime(BondData.Settlement),'mm-dd-yyyy'),它爲'datetime'類型的輸入參數給出了'Undefined function'類別'。 – aldrean

+0

您必須首先將所有的'BondData.Settlement'轉換爲'datetime'格式,然後將其轉換爲分類數組,並使用'categories'。在這裏看到我的編輯。 – EBH

+0

非常感謝EBH!我在MATLAB中是全新的,你在幫助我時非常慷慨。得到這段代碼正在運行。我印象深刻。 – aldrean