Serving the Quantitative Finance Community

 
User avatar
Shawnxiangyu
Topic Author
Posts: 0
Joined: November 6th, 2013, 10:14 am

Hull-White model in Matlab with calibration and Monte Carlo

November 11th, 2013, 8:58 am

Dear MrIsaksen,Thank you so much for sharing.I found two problems in your code:the first one and the fatal one : in the 'HWmodelMC_short.m' file, in line 149 :echo offfor i=1:nsteps, % Random vector dz (slow, but requires less memory) dz=randn(npaths,1)*sqrt(dt); % 1: Calculate increments dr=(theta-ar)dt+?dz dr(:,i)=theta(i)-a*r(:,i)*dt+sigma*dz(:,1);%dz(:,i); %(here is line 149) % 2: Cumulate r by: r(t+1)=r(t)+dr(t) r(:,i+1)=r(:,i)+dr(:,i);endecho onyou should have a bracket for the 'theta(i)-a*r(:,i)' part, isn't it?i.e. , it should be like this : dr(:,i)=(theta(i)-a*r(:,i))*dt+sigma*dz(:,1);%dz(:,i);the second problem(a tiny one): it seems that mtit (plot the main title for all the subplots) seems not to be a built-in function in matlab. i know there is such function on the mathworks forum, but you should mention that or stored this function in the zip-file.As to the theta function in the hull white model, there is matlab built-in function, actually matlab has offered a detailed example for the hull white model. You could use which +function name to find the location of the function and then see the matlab codeHow do you deal with the negative rates?Do you also have some code with the BK model?Kind RegardsYU
 
User avatar
Shawnxiangyu
Topic Author
Posts: 0
Joined: November 6th, 2013, 10:14 am

Hull-White model in Matlab with calibration and Monte Carlo

November 11th, 2013, 8:59 am

Dear MrIsaksen,Maybe, we could keep in touch because i am also working on these interest rate models!!! QuoteOriginally posted by: MrIsaksenHi!I am sharing my Matlab-implementation of a one-factor Hull-White term structure model. (I also posted it on quantcode, but thought you guys would like to see it as well.) I would love to hear your comments/suggestions. For example, I had problems implementing the theta-function in discrete time, so I decided to ignore the last term of the equation as suggested by Hull, chapter 30.3. Do you guys have any other advice?The code includes:- interpolation of zero rates (hence forward rates and discount factors)- calibration of parameters "a" and "sigma", using market data for interest rate caps and pricing functions in Matlab- Monte Carlo simulation of interest rate pathsSources:o Hull, Options Futures and Other Derivatives (8th ed.), chapter 30o Brigo & Mercurio, Interest Rate Models theory and practice, chapter 3.3
 
User avatar
Shawnxiangyu
Topic Author
Posts: 0
Joined: November 6th, 2013, 10:14 am

Hull-White model in Matlab with calibration and Monte Carlo

November 12th, 2013, 7:20 am

Forget about the toolbox, some function in the toolbox are so poorly written that it is so dangerrous to use them directly without checking. These codes in the financial toolbox seems coming out from some engineers with less financial markets knowledge. However, it offers a good reference. As to the level function, the matlab built-in function (level function for hull white one factor model) is as follows:function level = hw1LevelFun(t0,dt,FwdRates,Alpha,Sigma)% Compute the level function for the single factor Hull-White short rate% model.% Theta functiont = max(t0 + round(dt * 365),t0+1);theta = Ft(t) + Alpha * F(t) + (Sigma^2/(2*Alpha)) * (1 - exp(-2*Alpha*dt));% HW1 level is theta/alphalevel = theta / Alpha; function r = F(t) % Instantaneous forward rate r = FwdRates(t+1-t0); end function dr = Ft(t) % Derivative of the instantaneous forward rate w/ respect to time Rates = FwdRates(t-t0:t+2-t0); dr = (Rates(3) - Rates(2)) / (1/365); endendIs it reasonable to allow negative rates, especially given that the current interest rates is so low. When i use the calibrated alpha and sigma, and the current term sturcture as inputs for the hull white 1 factor model, it generates so many negative rates!!!Have you tried other models?Regards
 
User avatar
Darry1977
Posts: 1
Joined: June 16th, 2011, 6:53 am

Hull-White model in Matlab with calibration and Monte Carlo

November 14th, 2013, 7:47 pm

Hi,I don't think that it is the best idea to simulate Hull White model paths from the SDE.E.g. in Brigo-Mercurio you have formulas for conditional expectation E[r(t)|r(s)] and conditional variance Var[r(t)|r(s)]=Var[r(t-s)].Then you can simply simulate the HW model from the pathsr(t+1) = E[r(t+1)|r(t)] + sqrt(Var[r(1)]) * X(t+1) (where X is standard normal random variable)Then you don't have any numerical error with Theta (the only thing you need to do is to estimate the instantaneous forward rate), and you can simulate with any time step you want (does not need to be small).Good luck and regards
 
User avatar
Shawnxiangyu
Topic Author
Posts: 0
Joined: November 6th, 2013, 10:14 am

Hull-White model in Matlab with calibration and Monte Carlo

November 22nd, 2013, 2:46 pm

Dear Darry,Thanks for the information,but...If i simulate the short rate as you say, how could i ensure the current term structure is reflected in my model?Shoud i include the theta(t) function if i want to keep the term structure?How do you deal with the negative rates generated from the model?Kind RegardsQuoteOriginally posted by: Darry1977Hi,I don't think that it is the best idea to simulate Hull White model paths from the SDE.E.g. in Brigo-Mercurio you have formulas for conditional expectation E[r(t)|r(s)] and conditional variance Var[r(t)|r(s)]=Var[r(t-s)].Then you can simply simulate the HW model from the pathsr(t+1) = E[r(t+1)|r(t)] + sqrt(Var[r(1)]) * X(t+1) (where X is standard normal random variable)Then you don't have any numerical error with Theta (the only thing you need to do is to estimate the instantaneous forward rate), and you can simulate with any time step you want (does not need to be small).Good luck and regards
 
User avatar
Darry1977
Posts: 1
Joined: June 16th, 2011, 6:53 am

Hull-White model in Matlab with calibration and Monte Carlo

December 28th, 2013, 8:05 pm

Hi,The term structure is reflected in the expectation E[r(t+1)|r(t)] via the function "alfa" defined in Brigo-Mercurio. The funtion "alfa" is somehow equivalent to "theta", but easier to calculate. To compute it you need to derive the instantaneous forward rates from the term structure, which requires calculating derivative of discount rates (possible to obtain e.g. from some smooth interpolation of the term structure).The advantage of the simulation directly from the distribution is that you don't need any type of frequent simulation from SDE, but get directly the time steps you need (in fact you need to simulate from the bivariate normal distribution - one variable is for "r" and the second one is for [$]I(t_{n}) | r(t_{n-1}) = \int_{t_{n-1}}^{t_{n}} r(s) ds[$](the variable which has also normal distribution and which is needed to construct the discount factors). The only parameter that is not defined in Brigo-Mercurio was the covariance between r and I - I calculated it some time ago and it is[$]Cov(r(t_{n}),I(t_{n}) | r(t_{n-1})) = \frac{\sigma^{2}}{2a^{2}} (1-e^{-a(t_{n}-t_{n-1})})^{2}[$]Regarding the negative interest rates - I think there is not more to do than accept their existence or switching to another model (like CIR). If you calibrate the model to the swaptions / caps with low strikes (in the money), the effect of negative interest rates should be much smaller - but of course this depends on your pricing problem.Good luck