Skip to content

Instantly share code, notes, and snippets.

@WilsonMongwe
Created September 23, 2017 05:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WilsonMongwe/735d8193dda99d703320b63115df2eed to your computer and use it in GitHub Desktop.
Save WilsonMongwe/735d8193dda99d703320b63115df2eed to your computer and use it in GitHub Desktop.
A Random Forest Test For Jumps
# Load packages ----
library(shiny)
library(quantmod)
library(randomForest)
### The simulation functions
simulateJump=function(mu_,ss_,lambda_,mu2_,sigma_,TotalTime,delta,compare,size)
{
Sn=0
times <- c(0)
while(Sn <= TotalTime)
{
n <- length(times)
u <- runif(1)
expon <- -log(u)/lambda_
Sn <- times[n]+expon
times <- c(times, Sn)
}
times=times[-length(times)] #the last time is beyond TotalTime, so i delete from the vector times
indicator=seq(from=0,to=0,length=TotalTime) # if there are jumps or not between two times
jumpSize=seq(from=0,to=0,length=TotalTime) # stores the size of the jump
delta=1/252
t=(0:(length=(TotalTime)-1))
for(m in 2:length(times))
{
for(k in 2: length(t))
{
if( t[k-1]<=times[m] && times[m]<=t[k])
{
indicator[k]=1
if(compare)
{
u = runif(1,0,1)
signA=1
if(u<=0.5)
{ signA=-1}
jumpSize[k]=ss_*size*signA
}
else
{
jumpSize[k]=rnorm(1,mean=mu2_,sd=sigma_)
}
}
}
}
stock=seq(from=0,to=0,length=TotalTime)
stock[1]=10
for(i in 2:TotalTime)
{
stock[i]=stock[i-1]+(mu_-0.5*ss_^2)*delta+ss_*sqrt(delta)*rnorm(1)+jumpSize[i]*indicator[i]
}
final=cbind(stock)
return(final)
}
simulateGBM=function(mu_,ss_,TotalTime,delta)
{
stock=seq(from=0,to=0,length=TotalTime)
stock[1]=10
for(i in 2:TotalTime)
{
stock[i]=stock[i-1]+(mu_-0.5*ss_^2)*delta+ss_*sqrt(delta)*rnorm(1)
}
final=cbind(stock)
return(final)
}
### Generate the traning data
trainingData<-function(sampleSize)
{
trainingdata=matrix(0,nrow=sampleSize,ncol=9)
for(i in 1:sampleSize)
{
mu=runif(1,-1,1)
s=runif(1,0.01,0.7)
lam=runif(1,1,252)/252
mu_jump=runif(1,-1,1)
s_jump=runif(1,0.01,0.5)
delta=1/252
TotalTime=252*5
u = runif(1,0,1)
if(u<=0.5)
{
data=simulateJump(mu,s,lam,mu_jump,s_jump,TotalTime,delta,FALSE,1)
X= data[,1]#X is already log form
jump=1
}
else
{
data=simulateGBM(mu,s,TotalTime,delta)
X= data[,1]#X is already log form
jump=0
}
FirstMoment= mean(diff(X)^1)
SecondMoment= mean((diff(X)-FirstMoment)^2)
Skewness=mean((diff(X)-FirstMoment)^3)/SecondMoment^(3/2)
Kurtosis = (3-mean((diff(X)-FirstMoment)^4)/SecondMoment^2)/10
FirthMoment= mean((diff(X)-FirstMoment)^5)
SixthMoment= mean((diff(X)-FirstMoment)^6)
SeventhMoment= mean((diff(X)-FirstMoment)^7)
EigthMoment= mean((diff(X)-FirstMoment)^8)
#Column bind the data into one variable
trainingdata[i,]= cbind(FirstMoment,SecondMoment,Skewness,Kurtosis,
FirthMoment,SixthMoment,SeventhMoment,EigthMoment,jump)
cat("Iteration Number:\t",i, "\n")
}
colnames(trainingdata) <- c("FirstMoment", "SecondMoment","Skewness","Kurtosis",
"FirthMoment", "SixthMoment","SeventhMoment","EigthMoment","jump")
trainingdata
}
### Beging the Server Function
server <- function(input, output) {
data=trainingData(50)
randomForestModel <- randomForest(as.factor(data[,9]) ~ ., data=data, importance=TRUE,
proximity=TRUE)
dataInput <- reactive({
getSymbols(input$symb, src = "google",
from = input$dates[1],
to = input$dates[2],
auto.assign = FALSE)
})
output$plotLevels <- renderPlot({
X=dataInput()
if(input$use_log=="YES")
{ X=log(X[,4])}
else
{
X=(X[,4])
}
SharePrice=tail((X),-1)
plot(SharePrice,main="Share Price Level",col="blue")
})
output$plotReturns <- renderPlot({
X=dataInput()
X=log(X[,4])
logReturns=tail(diff(X),-1)
plot(logReturns,main="Log Returns",col="green")
})
output$JumpTest <- renderText(
{
X=dataInput()
X=X[,4]
Y=tail(diff(X),-1)
length(Y)
FirstMoment= mean(Y^1)
SecondMoment= mean((Y-FirstMoment)^2)
Skewness=mean((Y-FirstMoment)^3)/SecondMoment^(3/2)
Kurtosis = (3-mean((Y-FirstMoment)^4)/SecondMoment^2)/10
FirthMoment= mean((Y-FirstMoment)^5)
SixthMoment= mean((Y-FirstMoment)^6)
SeventhMoment= mean((Y-FirstMoment)^7)
EigthMoment= mean((Y-FirstMoment)^8)
jump=0
#Column bind the data into one variable
test= cbind(FirstMoment,SecondMoment,Skewness,Kurtosis,
FirthMoment,SixthMoment,SeventhMoment,EigthMoment,(jump))
colnames(test) <- c("FirstMoment", "SecondMoment","Skewness","Kurtosis",
"FirthMoment", "SixthMoment","SeventhMoment","EigthMoment","jump")
pred <- predict(randomForestModel, test)
if(pred==1)
{" Jump Test Result: This share has Jumps"}
else
{
" Jump Test Result: This share has NO Jumps"
}
})
}
library(shiny)
shinyUI( fluidPage(
titlePanel("Random Forest Test For Jumps"),
fluidRow(
column(12, helpText("Select the share that you want to test for Jumps (Exchange:Symbol).Note that the share price information will be obtained from Google Finance."))),
fluidRow(
column(4, textInput("symb", "Share Symbol", "JSE:MTN")),
column(4, dateRangeInput("dates",
"Date Range",
start = "2015-01-01",
end = as.character(Sys.Date()))),
column(4, selectInput("use_log",
label = "Use Log Scale?",
choices = list("YES", "NO"),
selected = "YES")
)
),
fluidRow(
column(4, textOutput("JumpTest"))
),
fluidRow(
# textOutput("JumpTest"),
plotOutput("plotLevels"),
plotOutput("plotReturns")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment