Skip to content

Commit 11e0078

Browse files
first commit
0 parents  commit 11e0078

6 files changed

+879
-0
lines changed

NIC_Lykhnenko_Roman.Rproj

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX

class_compound_poisson.R

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
source("required_libraries.R")
2+
3+
#
4+
# constructor of the class compoundPoisson -------------------------------------
5+
#
6+
compoundPoisson <-function(drift, jumpIntensity){
7+
8+
# drift parameter
9+
b = drift
10+
11+
# jump parameter
12+
lamda = jumpIntensity
13+
14+
# simulation of the process on the the interval [0,T]
15+
T = 1
16+
17+
# at these points value of the process is due to be calculated
18+
t = seq(0, T, by = 0.00001)
19+
20+
# simulate a r.v. N from Poisson distribution with parameter lamda*T.
21+
# N gives the total number of jumps on the interval [0, T]
22+
N = rpois(1,lamda*T)
23+
24+
# simulate N independent rv uniformly distributed on the interval [0, T]
25+
# these variables correspond to the jump times
26+
U = sort(runif(N, min = 0, max = T))
27+
28+
# initialization of the vector that should contain values of the stochastic
29+
# process at the given set of time points
30+
X = rep(0, length(t))
31+
32+
# values of the process without adding NAs at the times of jumps
33+
X_withoutNAs = rep(0, length(t))
34+
35+
# simulate jump sizes: N independent rv Y_i from a specified law
36+
Y = rnorm(N, mean = 0, sd = 1)
37+
38+
# fill vector X, i.e., calculate trajectory of the process
39+
for (i in (1:length(t)) ) {
40+
41+
# the trajectory is given by
42+
term1 = sum((U<t[i])*Y)
43+
X[i] = b*t[i] + term1
44+
45+
}
46+
47+
compoundPoissonProcess = list(processValues = X, time = t, drift = b,
48+
jumpIntensity = lamda)
49+
50+
class(compoundPoissonProcess) <- "compoundPoisson"
51+
compoundPoissonProcess
52+
}
53+
54+
# object of the class compoundPoisson
55+
instance1 = compoundPoisson(0,20)
56+
57+
#
58+
# plot method for the class compoundPoisson ------------------------------------
59+
#
60+
plot.compoundPoisson <- function(instance_compoundPoisson){
61+
62+
# properties of the process to be plotted
63+
process = instance_compoundPoisson$processValues
64+
time = instance_compoundPoisson$time
65+
dfPlot <- data.frame(time, process)
66+
67+
# construct name of the file to save the plot (based on the given instance)
68+
parameter1 <- instance_compoundPoisson$drift
69+
parameter2 <- instance_compoundPoisson$jumpIntensity
70+
name_of_plot <-paste0("process","CompoundPoisson","Drift", as.character(parameter1),
71+
"JumpIntensity", as.character(parameter2), ".pdf")
72+
73+
ggplot(dfPlot, aes(time, process)) + geom_point(size=1) +
74+
xlab("time") +
75+
ylab("") +
76+
ggtitle("compound Poisson process ")
77+
ggsave(name_of_plot, width = 6, height =4)
78+
}
79+
80+
plot(instance1)
81+
82+
#
83+
# density of the log returns in exponential Levy model -------------------------
84+
#
85+
86+
# reserve the name of the function, and use UseMethod command to tell R to
87+
# search for the correct function
88+
returnsDensity <- function(someClass) {
89+
90+
UseMethod("returnsDensity", someClass)
91+
92+
}
93+
94+
returnsDensity.compoundPoisson <- function(instance_compoundPoisson){
95+
96+
# log returns of the process
97+
log_returns = diff(instance_compoundPoisson$processValues, lag=1)
98+
99+
100+
# construct name of the file to save the plot (based on the given instance)
101+
parameter1 <- instance_compoundPoisson$drift
102+
parameter2 <- instance_compoundPoisson$jumpIntensity
103+
name_of_plot <-paste0("retDen","CompoundPoisson","Drift", as.character(parameter1),
104+
"JumpIntensity", as.character(parameter2), ".pdf")
105+
106+
# density of the log returns
107+
pdf(name_of_plot, width = 6, height =4)
108+
plot(density(log_returns,kernel="gaussian"), xlim=c(-0.05,0.05),
109+
col = "blue3", main = "", xlab = "log returns", ylab = "Density")
110+
grid()
111+
dev.off()
112+
}
113+
114+
#
115+
returnsDensity(instance1)
116+
117+
#
118+
# plot of the log returns ------------------------------------------------------
119+
#
120+
121+
# reserve the name of the function, and use UseMethod command to tell R to
122+
# search for the correct function
123+
returnsPlot <- function(someClass) {
124+
125+
UseMethod("returnsPlot", someClass)
126+
127+
}
128+
129+
returnsPlot.compoundPoisson <- function(instance_compoundPoisson){
130+
131+
# log returns of the process
132+
log_returns = diff(instance_compoundPoisson$processValues, lag=1)
133+
134+
# construct name of the file to save the plot (based on the given instance)
135+
parameter1 <- instance_compoundPoisson$drift
136+
parameter2 <- instance_compoundPoisson$jumpIntensity
137+
name_of_plot <-paste0("retPlot","CompoundPoisson","Drift", as.character(parameter1),
138+
"JumpIntensity", as.character(parameter2), ".pdf")
139+
140+
# density of the log returns
141+
# plot of log returns
142+
pdf(name_of_plot, width = 6, height =4) # plot_name: variable from global environment
143+
plot(log_returns, type = "l", xlab = "", ylab = "log returns", main = "")
144+
grid()
145+
dev.off()
146+
}
147+
148+
returnsPlot(instance1)
149+
150+
151+

0 commit comments

Comments
 (0)