%% Price a Callable Bond Using a CIR Interest-Rate Tree
%  Create a RateSpec using the intenvset function.

Rates = [0.025; 0.032; 0.037; 0.042]; 
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 = length(EndDates); 
Alpha = 0.03; 
Theta = 0.02;  
Sigma = 0.1;
Maturity = '01-Jan-2018'; 
CIRTimeSpec = cirtimespec(ValuationDate, Maturity, NumPeriods); 
CIRVolSpec = cirvolspec(Sigma, Alpha, Theta); 
CIRT = cirtree(CIRVolSpec, RateSpec, CIRTimeSpec)

% Define the bond with embedded option instrument.

BondSettlement = 'Jan-1-2017';
BondMaturity   = 'Jan-1-2020'; 
CouponRate = 0.035;
Period = 1;
OptSpec = 'put'; 
Strike = 100;  
ExerciseDates = {'Jan-1-2018' '01-Jan-2019'}; 

% Price the bond.

[Price,PriceTree] = optembndbycir(CIRT,CouponRate,BondSettlement,BondMaturity,OptSpec,...
Strike,ExerciseDates,'AmericanOpt',1,'Period',1)

% Copyright 2012 The MathWorks, Inc.