Skip to content

Instantly share code, notes, and snippets.

@WilsonMongwe
Last active October 6, 2015 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WilsonMongwe/ccae31085ba02a4ca2c7 to your computer and use it in GitHub Desktop.
Save WilsonMongwe/ccae31085ba02a4ca2c7 to your computer and use it in GitHub Desktop.
#### Author: Wilson Mongwe
#### Date: 05/10/2015
#### Website: www.wilsonmongwe.co.za
#### Title: Implementing Lee Test
################################### Volatility estimation ########################
sigmaf=function(W,k)
{
result=W*0
for(i in (k-1):length(W))
{
result[i]=0
for(j in (i-k+4):(i-1))
{
result[i]=result[i]+abs((W[j]-W[j-1]))*abs((W[j-1]-W[j-2]))
}
}
return (sqrt(result/(k-2)))
}
muF=function(W,k)
{
result=W*0
for(i in (k-1):length(W))
{
result[i]=0
for(j in (i-k+3):(i-1))
{
result[i]=result[i]+((W[j]-W[j-1]))
}
}
return ((result/(k-1)))
}
################## Calculation ##########################
leeTest=function(X,k,delta)
{
jumpTimes=matrix(0,nrow=1,ncol=length(X))
delta= delta
k=k
S=X
sig=sigmaf(S,k)
drift=muF(S,k)
L=S*0
n=length(S)
c=sqrt(2/pi)
c_n = (2*log(n))^0.5/c-(log(pi)+log(log(n)))/(2*c*(2*log(n))^0.5)
s_n = 1/(c*(2*log(n))^0.5)
alpha=0.05/(length(X)-k+1)
critical= -log(-log(1-alpha))*s_n+c_n
for(i in (k-1):length(S))
{
L[i]= (S[i]-S[i-1]-drift[i])/sig[i]
}
jumpTimes[1,k:length(jumpTimes)] = seq(from=1,to=1,length=length(L[k:length(jumpTimes)]))*
(abs(L[k:length(jumpTimes)])>=critical)
return(jumpTimes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment