Feeds:
Posts

## CARR-MADAN 1999, MOODLEY 2005 FOR FFT HESTON/ZULF MODELS

Heston’s original model inverted a characteristic function numerically.  CarrMadan_OptionValuationUsingtheFastFourierTransform_1999 found a way to make FFT practical and Moodley 2005nimalinmoodley2005 provided a Matlab implementation that I adopted to speed up both Heston’s and my time-fractional Heston model.

```library(Quandl)
library(plyr)
library(rPython)

library(fOptions)
Quandl.api_key("CoHxdmas1Pz2t7Hk8maE")

timeStart outlier.threshold]thresh){
jumps[k]timeEnd || tau<timeStart){
next
}
S<-surface\$underlyingpx[rk]
X<-surface\$strike[rk]
callPrice<-callHestoncf(surface\$underlyingpx[rk],
surface\$strike[rk],
tau,
rate,
dividendrate,
v0=v0,
vT,
rho,
kappa,
sigma,
implVol=FALSE)
cp<-callPrice[[1]]
#mid0.20){
stars<-'***'
}
#print(sprintf("S=%.2f X=%.2f log(MN)=%.3f tau=%.3f mkt=%.2f heston=%.2f relerr=%.4f abserr=%.3f %s",S,X,log(S/X),tau,mktIV,modelIV,relerr,abserr,stars))
#print(paste('In Heston act mkt iv=',mktIV,' model=',modelIV))

#weight<-exp(-abs(log(S/x)))
weight0.0){
#stimeEnd || tau<timeStart){
next
}

callPrice<-callHestoncf(S,
X,
tau,
rate,
dividendrate,
v0,
vT,
rho,
kappa,
sigma,
implVol=FALSE)
cp<-callPrice[[1]]
#mid0.0){
stars<-''
relerr<-abs(mktIV-modelIV)/mktIV
abserr<-abs(mktIV-modelIV)
abserrors[rk]0.1){
stars<-'***'
}
print(sprintf("S=%.2f X=%.2f log(MN)=%.3f tau=%.3f mkt=%.2f heston=%.2f relerr=%.4f abserr=%.3f %s",S,X,log(S/X),tau,mktIV,modelIV,relerr,abserr,stars))
#print(paste('S=',S,' X=',X,' tau=',tau,' mkt=',mid,' model=',cp,' relerr=',abs(mid-cp)/mid))
}
}
return(abserrors)
}

calibrate.lmheston.model<-function(surface){
ratetimeEnd || tau<timeStart){
next
}
S<-surface\$underlyingpx[rk]
X<-surface\$strike[rk]
#print(c(S,X,tau,rate,dividendrate,v0,vT,rho,kappa,sigma,alpha))

callPrice<-callLMHestoncf(S,
X,
tau,
rate,
dividendrate,
v0,
vT,
rho,
kappa,
sigma,
alpha,
implVol=FALSE)
cp<-callPrice[[1]]
#mid0.20){
stars<-'***'
}

#print(sprintf("S=%.2f X=%.2f log(MN)=%.3f tau=%.3f mkt=%.2f heston=%.2f relerr=%.4f abserr=%.3f %s",S,X,log(S/X),tau,mktIV,modelIV,relerr,abserr,stars))

#weight<-exp(-abs(log(S/X)))
weight0.0){
#stimeEnd || tau<timeStart){
next
}

#callPrice<-lmheston.price(lmheston.params,secparams)
callPrice<-callLMHestoncf(S,
X,
tau,
rate,
dividendrate,
v0,
vT,
rho,
kappa,
sigma,
alpha,
implVol=FALSE)

cp0.0){
stars<-''
relerr<-abs(mktIV-modelIV)/mktIV
abserr<-abs(mktIV-modelIV)
abserrors[rk]0.2){
stars<-'***'
}
print(sprintf("S=%.2f X=%.2f log(MN)=%.3f tau=%.3f mkt=%.2f fracheston=%.2f relerr=%.4f abserr=%.3f %s",S,X,log(S/X),tau,mktIV,modelIV,relerr,abserr,stars))
#print(paste('S=',S,' X=',X,' tau=',tau,' mkt=',mid,' model=',cp,' relerr=',abs(mid-cp)/mid))
}
}
return(abserrors)

}

NDays6){
print(paste('Stock:',symbol))
} else {
return
}

if (1){
lmhparms<-calibrate.lmheston.model(Snew)
#print('Before Heston calibration')
hparms<-calibrate.heston.model(Snew)
#print('Completed Heston calibration')

#print(paste('Completed calibration for',symbol))
}

heston.errors<-compare.heston.versus.market(hparms,Snew)
lmheston.errors<-compare.lmheston.versus.market(lmhparms,Snew)
HestonErr<-append(HestonErr,c(mean(heston.errors)))
LMHestonErr<-append(LMHestonErr,c(mean(lmheston.errors)))
PValues<-append(PValues,t.test(heston.errors,lmheston.errors)\$p.value)
print(paste(symbol,dates[k],mean(heston.errors),mean(lmheston.errors),sep=","))
}

comp<-data.frame(dates[1:length(HestonErr)],HestonErr,LMHestonErr,PValues)
colnames(comp)<-c("Date","Heston","fracHeston","PValue")
#tt<-t.test(comp\$Heston,comp\$fracHeston)
print(paste(stock,dates[1],dates[NDays],mean(comp\$Heston),mean(comp\$fracHeston),mean(comp\$PValue),sep=","))

compdata.fn<-sprintf("compdata-%s.csv",stock)
write.csv(comp,file=compdata.fn)
#print(comp)
}

files0<-list.files(path="/home/user1/Dropbox/options/bystock/")
files<-sample(files0,1000)
#files<-c("IBM.csv")
for (f in files){
fs<-strsplit(f,"[.]")
stock<-fs[[1]][1]
possibleError<-tryCatch({
heston.vs.lmheston.analysis(stock)
},error=function(e)e)

if (inherits(possibleError,"error")){
next
}
}
```