%% Simulate the Price of a Bond Using a Hull-White One-Factor Model Until the Bond's Maturity
% Define the zero curve data.

Settle = datenum('4-Apr-2016');
ZeroTimes = [3/12 6/12 1 5 7 10 20 30]';
ZeroRates = [0.033 0.034 0.035 0.040 0.042 0.044 0.048 0.0475]';
ZeroDates = datemnth(Settle,ZeroTimes*12);
RateSpec = intenvset('StartDates', Settle,'EndDates', ZeroDates, 'Rates', ZeroRates)

%
% Define the bond parameters.

Maturity = datemnth(Settle,12*5);
CouponRate = 0;

%
% Define the Hull-White parameters.

alpha = .1;
sigma = .01;
HW1F = HullWhite1F(RateSpec,alpha,sigma)

%
% Define the simulation parameters.

nTrials = 100;
nPeriods = 12*5;
deltaTime = 1/12;
SimZeroCurvePaths = simTermStructs(HW1F, nPeriods,'nTrials',nTrials,'deltaTime',deltaTime);
SimDates = datemnth(Settle,1:nPeriods);

%
% Preallocate and initialize for the simulation.

SimBondPrice = zeros(nPeriods+1,nTrials);
SimBondPrice(1,:,:) = bondbyzero(RateSpec,CouponRate,Settle,Maturity);
SimBondPrice(end,:,:) = 100;

%
% Compute the bond values for each simulation date and path, note that you can vectorize over the trial dimension.

for periodidx=1:nPeriods-1
    simRateSpec = intenvset('StartDate',SimDates(periodidx),'EndDates',...
        datemnth(SimDates(periodidx),ZeroTimes*12),'Rates',squeeze(SimZeroCurvePaths(periodidx+1,:,:)));
    SimBondPrice(periodidx+1,:) = bondbyzero(simRateSpec,CouponRate,SimDates(periodidx),Maturity);
end

plot([Settle SimDates],SimBondPrice)
datetick
ylabel('Bond Price')
xlabel('Simulation Dates')
title('Simulated Bond Price')

% Copyright 2012 The MathWorks, Inc.