Loop through data until date changes

I'm implementing Interest Rate Curve-fitting using Nelson-Siegel and Svensson models.I'm doing fine for a one-day data (sheet1) and got the needed values. In actual testing, I'm about to run a 5-year daily records which is very tedious if I tested it on a per-day/sheet basis.I created another sheet (sheet2) with 3 days of records as a sample. Kindly help me on how to implement my code wherein I need get each model's parameters on a per day basis (loop through settlement dates and compare if the same?). Thanks in advance.
Here's my code:
function NS_SVModels()
%Read data from Excel file and load in a matrix
[B Btext] = xlsread('mybonddata3.xls', 'sheet1', 'A2:D60');
%[B] contains Clean Price and Coupon rates
%[Btext] contains Settlement and Maturity dates
Bonds.Settle = datenum(Btext(:,1));
Bonds.Maturity = datenum(Btext(:,2));
Bonds.Prices = B(:,1);
Bonds.Coupon = B(:,2)./100;
%Get a single record from Settlement
%to be used in implementing the models
mySettle=datenum(Btext(1,1));
%Assign values
Settle = Bonds.Settle;
Maturity = Bonds.Maturity;
CleanPrice = Bonds.Prices;
CouponRate = Bonds.Coupon;
%Load to array the necessary values
Instruments = [Settle Maturity CleanPrice CouponRate];
%Get corresponding YTMs
Yield = bndyield(CleanPrice,CouponRate,Settle,Maturity);
%Implement the necessary models
NSModel = IRFunctionCurve.fitNelsonSiegel('Zero',mySettle,Instruments);
SVModel = IRFunctionCurve.fitSvensson('Zero',mySettle,Instruments);
SettleDate=datestr(Btext(1,1));
%Get the parameters for each model
fprintf('%s',SettleDate)
Nelson=NSModel.Parameters
Svensson=SVModel.Parameters
end

답변 (1개)

dpb
dpb 2016년 9월 5일

0 개 추천

Use unique to find the set of dates in Bonds.Settle then accumarray to process and accumulate results over those by set.

댓글 수: 4

Adie Malicsi
Adie Malicsi 2016년 9월 6일
Thanks for your reply, really appreciate it. Been reading about accumarray now. I hope you won't mind me asking perhaps a snippet on how can I possibly implement this on my code, please?
Adie Malicsi's "Answer" moved here:
After reading through with the accumarray function, I'm not sure if this is the right function to use in accomplishing what I wanted. Yes, I can now identify the unique Bonds.Settle dates but the major task now is to implement the main code for each unique date.From these codes below, I need to know what is the range I need to input for each parameters ...(datenum(Btext(?,1));)? Thanks.
Bonds.Settle = datenum(Btext(:,1));
Bonds.Maturity = datenum(Btext(:,2));
Bonds.Prices = B(:,1);
Bonds.Coupon = B(:,2)./100;
Stephen23
Stephen23 2016년 9월 6일
편집: Stephen23 2016년 9월 6일
@Adie Malicsi: have a look at the third output of unique: this gives you the indices that you require for accumarray. For example provide unique with the date vectors (just the first three columns) and use the 'rows' option. Then call accumarry with those indices and your data: either call an inbuilt function, your own function, or simply collect the data for each day into the cell of a cell array:
accumarray(sub,val,[],@(v){v})
dpb
dpb 2016년 9월 6일
"...perhaps a snippet on how can I possibly implement this ...?"
I guess all your functions must be in an econometrics toolbox of some sort; I don't recognize anything so don't know what the output of IRFunctionCurve.fitNelsonSiegel is going to be so don't know how to specifically write it, but basically Stephen's given the outline; the indices from unique indicate which element of the output array is associated with each of the unique dates; accumulate that in a cell array most likely is the answer unless there is a single value returned.

댓글을 달려면 로그인하십시오.

질문:

2016년 9월 5일

댓글:

dpb
2016년 9월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by