Welcome to THETAWIKI. If you like to create or edit a page please make sure to login or register an account. All registered users please make sure to provide a valid email address.


From ThetaWiki
Jump to: navigation, search

model Heston %this model returns simulated stock prices with Heston variance %example input parameters: %S0 =100, Vt0 = 0.012, r = 0.0272, tht = 0.012 %kpp = 1.98937, epp = 0.15, rho_sv = -0.9, t = 1, dt = 0.01 import S0 "Initial stock price" import Vt0 "Initial variance" import r "Risk free interest rate" import tht "Long term variance" import kpp "Mean reversion rate for Heston variance" import epp "Volatility of Heston variance" import rho_sv "Instantaneous correlation of stock price and stock price variance" import t "Simulation time horizon" import dt "Discretization time step" export S "Heston stock prices, Drift Martingale Corrected" export Vt "Heston variance process"

S = S0;  %initial Heston stock price Vt = Vt0;  %initial Heston stochastic variance

%parameter values used for discretization, =0.5 uses central discretization gmm1 = 0.5; gmm2 = 0.5; %constant for the switching rule used in the QE scheme shi_crt = 1.5;

loop t/dt theta dt; %Andersen's paper, equation (17) m = tht + (Vt-tht)*exp(-kpp*dt); %Andersen's paper, equation (18) s2 = Vt*epp^2*exp(-kpp*dt)*(1-exp(-kpp*dt))/kpp + tht*epp^2*(1-exp(-kpp*dt))^2/(2*kpp); %Andersen's paper, equation (19) shi = s2/(m^2); %Andersen's paper, p19, where C0 - K0, C1 - K1, C2 - K2, C3 - sqrt(K3/gmm1) C0 = (-rho_sv*tht*kpp/epp) * dt; C1 = gmm1*dt*(kpp*rho_sv/epp-0.5) - rho_sv/epp; C2 = gmm2*dt*(kpp*rho_sv/epp-0.5) + rho_sv/epp; C3 = sqrt((1-rho_sv^2)*dt);

%Andersen's paper, p20, A - AVtdt AVtdt = C2 + 0.5*C3^2*gmm2; %Andersen's QE algorithm 3.2.4, p16 - 17 u = rand(); Z1 = norminv(u,0,1); if shi <= shi_crt %for sufficiently large value s of Vt %condition to be satisfied for QE martingale correction: AVtdt < (1/(2*a)) c4 = 2/shi; b2 = max(c4-1+sqrt(c4*(c4-1)),0); a = m/(1+b2); Vdt = a*(sqrt(b2)+Z1)^2; %Martingale drift correction, p22, K0_star -- C00 C00 = -AVtdt*b2*a/(1-2*AVtdt*a) + 0.5*log(1-2*AVtdt*a) - (C1+0.5*C3^2*gmm1)*Vt; else %for low values of Vt %condition to be satisfied for QE martingale correction: AVtdt < beta p = (shi-1)/(shi+1); bet = (1-p)/m; if (u <= p) Vdt = 0; else Vdt = log((1-p)/(1-u))/bet; end %Martingale drift correction, p22, K0_star -- C00 C00 = -log(p + bet*(1-p)/(bet-AVtdt)) - (C1+0.5*C3^2*gmm1)*Vt; end

Z0 = randn();

%simulated Heston stock prices %Andersen's paper, p19, equation (33), with drift corrected, K0 replaced by C00 S = S * exp(r*dt + C00 + C1*Vt + C2*Vdt + C3*sqrt(gmm1*Vt+gmm2*Vdt)*Z0);

%update Heston stochastic variance Vt = Vdt; end