%% Price an Interest-Rate Swap Using a CIR Interest-Rate Tree
% Define an interest-rate swap with a fixed receiving leg and a floating paying leg. Payments are made once a year and the notional principal amount is $100.

Basis = 0; 
Principal = 100;
LegRate = [0.06 20]; % [CouponRate Spread] 
LegType = [1 0]; % [Fixed Float] 
LegReset = [1 1]; % Payments once per year

% Create a RateSpec using the intenvset function.

Rates = [0.035; 0.042147; 0.047345; 0.052707]; 
Dates = {'Jan-1-2017'; 'Jan-1-2018'; 'Jan-1-2019'; 'Jan-1-2020'; 'Jan-1-2021'}; 
ValuationDate = 'Jan-1-2017'; 
EndDates = Dates(2:end)'; 
Compounding = 1; 
RateSpec = intenvset('ValuationDate', ValuationDate, 'StartDates', ValuationDate, 'EndDates',EndDates,'Rates', Rates, 'Compounding', Compounding); 

% Create a CIR tree.

NumPeriods = 5;  
Alpha = 0.03; 
Theta = 0.02;  
Sigma = 0.1;   
Settle = '01-Jan-2017'; 
Maturity = '01-Jan-2022'; 
CIRTimeSpec = cirtimespec(ValuationDate, Maturity, NumPeriods); 
CIRVolSpec = cirvolspec(Sigma, Alpha, Theta); 

CIRT = cirtree(CIRVolSpec, RateSpec, CIRTimeSpec)

% Price the interest-rate swap.

[Price,PriceTree] = swapbycir(CIRT,LegRate,Settle,Maturity,'LegReset',LegReset,'Basis',3,'Principal',100,'LegType',LegType) 

% Copyright 2012 The MathWorks, Inc.