-
Notifications
You must be signed in to change notification settings - Fork 1
/
class_jump_diffusion.R
263 lines (182 loc) · 7.49 KB
/
class_jump_diffusion.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
source("required_libraries.R")
#
# constructor of the class jumpDiffusion----------------------------------------
#
jumpDiffusion <- function(drift, jumpIntensity){
# drift parameter
b = drift
# jump intensity
lamda = jumpIntensity
# simulation of the process on the the interval [0,T]
T = 1
# time step in grid
timeStep = 10000
#
numbersInTimestep = 4
# fixed time grid
t = seq(0, T, by = 1/timeStep)
# diffusion volatility
sigma = 15
# simulate n independent centered gaussian rv G_i
G = rnorm(length(t), mean = 0, sd = (1/timeStep)*sigma^2 )
# compound Poisson #############################################################
# simulate a r.v. N from Poisson distribution with parameter lamda*T.
# N gives the total number of jumps on the interval [0, T]
N = rpois(1,lamda*T)
# simulate N independent rv uniformely distributed on the interval [0, T]
# these variables correspond to the jump times
U = round(sort(runif(N, min = 0, max = T)), digits=numbersInTimestep)
# initialization of the vector that should contain values of the compound Poisson
# process at the given set of time points
X = rep(0, length(t))
# simulate jump sizes: N independent rv Y_i from a specified law
Y = rnorm(N, mean = 0, sd = 0.5)
# fill vector X, i.e., calculate trajectory of the compound Poisson process
for (i in (1:length(t)) ) {
# the trajectory is given by
term1 = sum((U<t[i])*Y)
X[i] = term1
}
################################################################################
# initialization of the vector that should contain values of the jumpDiffusion
# process at the given set of time points
jumpDiffusion = rep(0, length(t))
# initialization of the vector that should contain values of the gaussian
# component of the process at the given set of time points
GupToi = rep(0, length(t))
# fill vector jumpDiffusion with values
for (i in (1:length(t)) ){
# gaussian component
count = 0
for (k in 1:i){
count = count + G[k]
}
GupToi[i] = count
jumpDiffusion[i] = b*t[i] + GupToi[i] + X[i]
}
jumpDiffusionAllData <- list(time = t, jump_diffusion = jumpDiffusion,
gaussian_component = GupToi,
jump_component = X,
drift = b,
jump_intensity = lamda)
class(jumpDiffusionAllData) <- "jumpDiffusion"
return(jumpDiffusionAllData)
}
# object of the class jumpDiffusion
instance1 = jumpDiffusion(0,20)
#
# plot method for the class jumpDiffusion ------------------------------------
#
plot.jumpDiffusion <- function(instance_jumpDiffusion){
# properties of the process to be plotted
process = instance_jumpDiffusion$jump_diffusion
time = instance_jumpDiffusion$time
dfPlot <- data.frame(time, process)
# construct name of the file to save the plot (based on the given instance)
parameter1 <- instance_jumpDiffusion$drift
parameter2 <- instance_jumpDiffusion$jump_intensity
name_of_plot <-paste0("process","JumpDiffusion","Drift", as.character(parameter1),
"JumpIntensity", as.character(parameter2), ".pdf")
ggplot(dfPlot, aes(time, process)) + geom_point(size=0.3) +
xlab("time") +
ylab("") +
ggtitle("jump diffusion process ")
ggsave(name_of_plot, width = 6, height =4)
}
plot(instance1)
#
# density of the log returns in exponential Levy model -------------------------
#
# reserve the name of the function, and use UseMethod command to tell R to
# search for the correct function
returnsDensity <- function(someClass) {
UseMethod("returnsDensity", someClass)
}
returnsDensity.jumpDiffusion <- function(instance_jumpDiffusion){
# log returns of the process
log_returns = diff(instance_jumpDiffusion$jump_diffusion, lag=1)
# construct name of the file to save the plot (based on the given instance)
parameter1 <- instance_jumpDiffusion$drift
parameter2 <- instance_jumpDiffusion$jump_intensity
name_of_plot <-paste0("retDen","JumpDiffusion","Drift", as.character(parameter1),
"JumpIntensity", as.character(parameter2), ".pdf")
# density of the log returns
pdf(name_of_plot, width = 6, height = 4)
plot(density(log_returns,kernel="gaussian"), xlim=c(-0.1,0.1),
col = "blue3", main = "density of log returns", xlab = "", ylab = "")
grid()
dev.off()
}
#
returnsDensity(instance1)
#
# plot of the log returns ------------------------------------------------------
#
# reserve the name of the function, and use UseMethod command to tell R to
# search for the correct function
returnsPlot <- function(someClass) {
UseMethod("returnsPlot", someClass)
}
returnsPlot.jumpDiffusion <- function(instance_jumpDiffusion){
# log returns of the process
log_returns = diff(instance_jumpDiffusion$jump_diffusion, lag=1)
# construct name of the file to save the plot (based on the given instance)
parameter1 <- instance_jumpDiffusion$drift
parameter2 <- instance_jumpDiffusion$jump_intensity
name_of_plot <-paste0("retPlot","JumpDiffusion","Drift", as.character(parameter1),
"JumpIntensity", as.character(parameter2), ".pdf")
# plot of log returns
pdf(name_of_plot, width = 6, height = 4)
plot(log_returns, type = "l", xlab = "", ylab = "", main = "log returns")
grid()
dev.off()
}
returnsPlot(instance1)
#
# plot of gaussian component ---------------------------------------------------
#
# reserve the name of the function, and use UseMethod command to tell R to
# search for the correct function
gaussianComponentPlot <- function(someClass) {
UseMethod("gaussianComponentPlot", someClass)
}
gaussianComponentPlot.jumpDiffusion <- function(instance_jumpDiffusion){
process = instance_jumpDiffusion$gaussian_component
time = instance_jumpDiffusion$time
dfPlot <- data.frame(time, process)
# construct name of the file to save the plot (based on the given instance)
parameter1 <- instance_jumpDiffusion$drift
parameter2 <- instance_jumpDiffusion$jump_intensity
name_of_plot <-paste0("Gaus","JumpDiffusion","Drift", as.character(parameter1),
"JumpIntensity", as.character(parameter2), ".pdf")
ggplot(dfPlot, aes(time, process)) + geom_line(size=0.3) +
xlab("time") +
ylab("") +
ggtitle("gaussian component")
ggsave(name_of_plot, width = 6, height = 4)
}
gaussianComponentPlot(instance1)
#
# plot of the jump component ---------------------------------------------------
#
# reserve the name of the function, and use UseMethod command to tell R to
# search for the correct function
jumpComponentPlot <- function(someClass) {
UseMethod("jumpComponentPlot", someClass)
}
jumpComponentPlot.jumpDiffusion <- function(instance_jumpDiffusion){
process = instance_jumpDiffusion$jump_component
time = instance_jumpDiffusion$time
dfPlot <- data.frame(time, process)
# construct name of the file to save the plot (based on the given instance)
parameter1 <- instance_jumpDiffusion$drift
parameter2 <- instance_jumpDiffusion$jump_intensity
name_of_plot <-paste0("Jump","JumpDiffusion","Drift", as.character(parameter1),
"JumpIntensity", as.character(parameter2), ".pdf")
ggplot(dfPlot, aes(time, process)) + geom_point(size=0.7) +
xlab("time") +
ylab("") +
ggtitle("jump component")
ggsave(name_of_plot, width = 6, height = 4)
}
jumpComponentPlot(instance1)