%% Compute OAS Using a CIR Interest-Rate Tree
% Create a RateSpec using the intenvset function.

ValuationDate = 'October-25-2018';
Rates = [0.0355; 0.0382; 0.0427; 0.0489];
StartDates = ValuationDate;
EndDates = datemnth(ValuationDate, 12:12:48)';
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-2023'; 
CIRTimeSpec = cirtimespec(ValuationDate, Maturity, NumPeriods); 
CIRVolSpec = cirvolspec(Sigma, Alpha, Theta); 

CIRT = cirtree(CIRVolSpec, RateSpec, CIRTimeSpec)

% Define the OAS instrument.

CouponRate = 0.045;
Settle = ValuationDate;
Maturity = '25-October-2019';
OptSpec = 'call';
Strike = 100;
ExerciseDates = {'25-October-2018','25-October-2019'};
Period = 1;
AmericanOpt = 0;
Price = 97;

% Compute the OAS.

[OAS,OAD] = oasbycir(CIRT,Price,CouponRate,Settle,Maturity,OptSpec,Strike,ExerciseDates,'Period',Period,'AmericanOpt',AmericanOpt)

% Copyright 2012 The MathWorks, Inc.