www.gusucode.com > fininst 案例源码程序 matlab代码 > fininst/SimulateTotalReturnofBondPortfolioUntilMaturityExample.m
%% Simulate the Total Return of a Bond Portfolio Until Maturity %% % Define the zero curve data. Settle = datenum('4-Apr-2016'); ZeroTimes = [3/12 6/12 1 5 7 10 20 30]'; ZeroRates = [-0.01 -0.009 -0.0075 -0.003 -0.002 -0.001 0.002 0.0075]'; ZeroDates = datemnth(Settle,ZeroTimes*12); RateSpec = intenvset('StartDates', Settle,'EndDates', ZeroDates, 'Rates', ZeroRates) %% % Define the bond parameters for the five bonds in the portfolio. Maturity = datemnth(Settle,12*5); % All bonds have the same maturity CouponRate = [0.035;0.04;0.02;0.015;0.042]; % Different coupon rates for the bonds nBonds = length(CouponRate); %% % Define the Hull-White parameters. alpha = .1; sigma = .01; HW1F = HullWhite1F(RateSpec,alpha,sigma) %% % Define the simulation parameters. nTrials = 1000; 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,nBonds,nTrials); SimBondPrice(1,:,:) = repmat(bondbyzero(RateSpec,CouponRate,Settle,Maturity)',[1 1 nTrials]); SimBondPrice(end,:,:) = 100; [BondCF,BondCFDates,~,CFlowFlags] = cfamounts(CouponRate,Settle,Maturity); BondCF(CFlowFlags == 4) = BondCF(CFlowFlags == 4) - 100; SimBondCF = zeros(nPeriods+1,nBonds,nTrials); %% % Compute bond values for each simulation date and path. Note that you can % vectorize over the trial dimension. for periodidx=1:nPeriods if periodidx < nPeriods 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 simidx = SimDates(periodidx) == BondCFDates; SimCF = zeros(1,nBonds); SimCF(any(simidx,2)) = BondCF(simidx); ReinvestRate = 1 + SimZeroCurvePaths(periodidx+1,1,:); SimBondCF(periodidx+1,:,:) = bsxfun(@times,bsxfun(@plus,SimBondCF(periodidx,:,:),SimCF),ReinvestRate); end %% % Compute the total return series. TotalCF = SimBondPrice + SimBondCF; %% % Assume the bond portfolio is equally weighted and plot the simulated bond portfolio % returns. TotalCF = squeeze(sum(TotalCF,2)); TotRetSeries = bsxfun(@rdivide,TotalCF(2:end,:),TotalCF(1,:)) - 1; plot(SimDates,TotRetSeries) datetick ylabel('Bond Portfolio Returns') xlabel('Simulation Dates') title('Simulated Bond Portfolio Returns')