%% Calculate the Option-Adjusted Spread of a 30-Year Fixed-Rate Mortgage Pool
% Calculate the option-adjusted spread of a 30-year, fixed-rate mortgage pool with about 28-year weighted average maturity left, given assumptions of 0, 50, and 100 PSA prepayments. First, create the bonds matrix:

Bonds = [datenum('11/21/2002')  0        100  0  2  1;
         datenum('02/20/2003')  0        100  0  2  1;
         datenum('07/31/2004')  0.03     100  2  3  1;
         datenum('08/15/2007')  0.035    100  2  3  1;
         datenum('08/15/2012')  0.04875  100  2  3  1;
         datenum('02/15/2031')  0.05375  100  2  3  1];

%
% Choose a settlement date.

Settle = datenum('20-Aug-2002');

%
% Assume the following clean prices for the bonds:

Prices =  [ 98.97467;
            98.58044;
           100.10534;
            98.18054;
           101.38136;
            99.25411];

%
% Use the following formula to compute spot compounding for the bonds:

SpotCompounding = 2*ones(size(Prices));

%
% Compute the zero curve.

[ZeroRatesP, CurveDatesP] = zbtprice(Bonds, Prices, Settle);
ZeroCurve = [CurveDatesP, ZeroRatesP, SpotCompounding]

%
% Assign the following parameters:

Price         = 95;
Maturity      = datenum('02-Jan-2030');
IssueDate     = datenum('02-Jan-2000');
GrossRate     = 0.08125;
CouponRate    = 0.075;
Delay         = 14;
Interpolation = 1;
PrepaySpeed   = [0 50 100];

%
% Compute the yield, and from the yield, compute the option-adjusted spread.

[mbsyld, beyld] = mbsyield(Price, Settle, ...
Maturity, IssueDate, GrossRate, CouponRate, Delay, PrepaySpeed);

OAS = mbsyield2oas(ZeroCurve, mbsyld, Settle, ...
Maturity, IssueDate, GrossRate, CouponRate, Delay, ... 
Interpolation, PrepaySpeed)

% Copyright 2015 The MathWorks, Inc.