%% Price a European Call and Put Option on a Bond Using a CIR Interest-Rate Tree
% Compute the price for a European call option on a 4% bond with a strike of 96. The exercise date for the option is Jan. 01, 2018. The settle date for the bond is Jan. 01, 2017, and the maturity date is Jan. 01, 2020.
% 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 = length(EndDates); 
Alpha = 0.03; 
Theta = 0.02;  
Sigma = 0.1;   
Settle = '01-Jan-2017'; 
Maturity = '01-Jan-2019'; 
CIRTimeSpec = cirtimespec(ValuationDate, Maturity, NumPeriods); 
CIRVolSpec = cirvolspec(Sigma, Alpha, Theta); 

CIRT = cirtree(CIRVolSpec, RateSpec, CIRTimeSpec)

% Price the 'Call' option.

[Price,PriceTree] = optbndbycir(CIRT,'Call',96,'01-Jan-2018',... 
0,0.04,'01-Jan-2017','01-Jan-2020')

% Price the 'Put' option.

[Price,PriceTree] = optbndbycir(CIRT,'Put',96,'01-Jan-2018',... 
0,0.04,'01-Jan-2017','01-Jan-2020')

% The PriceTree.ExTree output for the 'Call' and 'Put' option contains the exercise indicator arrays. Each element of the cell array is an array containing 1's where an option is exercised and 0's where it is not.
% Copyright 2012 The MathWorks, Inc.