-
Notifications
You must be signed in to change notification settings - Fork 1
/
class_stable_process.R
179 lines (97 loc) · 3.51 KB
/
class_stable_process.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
source("required_libraries.R")
#
# constructor of the class alphaStable ----------------------------------------
#
alphaStable <- function(alpha){
# time step in grid
timeStep = 10000
# fixed time grid
t = seq(0, T, by = 1/timeStep)
# number of time points
n = length(t)
# n independent uniformely distributed rv
gamma = runif(n, min = -pi/2, max = pi/2)
# n independent standart exponential rv
W = rexp(n, rate = 1)
deltaX = (1/timeStep)^(1/alpha)*sin(alpha)*gamma/((cos(gamma))^(1/alpha))*
(cos((1-alpha)*gamma)/W)^((1-alpha)/alpha)
# initialization of the vector that should contain values of
# process at the given set of time points
X = rep(0, length(t))
for (i in 1:length(t)){
sumDeltaX = 0
for (j in 1:i){
sumDeltaX = sumDeltaX + deltaX[j]
}
X[i] = sumDeltaX
}
alpha_stable_data <- list(time=t, process=X, alpha=alpha)
class(alpha_stable_data) <- "alphaStable"
return(alpha_stable_data)
}
# object of the class alphaStable
instance1 = alphaStable(1)
#
# plot method for the class alphaStable ----------------------------------------
#
plot.alphaStable <- function(instance_alphaStable){
# properties of the process to be plotted
process = instance_alphaStable$process
time = instance_alphaStable$time
dfPlot <- data.frame(time, process)
# construct name of the file to save the plot (based on the given instance)
parameter <- instance_alphaStable$alpha
name_of_plot <-paste0("process", "Stable", as.character(parameter), ".pdf")
# plotting procedure
ggplot(dfPlot, aes(time, process)) + geom_line(size=0.3) +
xlab("time") +
ylab("") +
ggtitle("alpha stable 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.alphaStable <- function(instance_alphaStable){
# log returns of the process
log_returns = diff(instance_alphaStable$process, lag=1)
# construct name of the file to save the plot (based on the given instance)
parameter <- instance_alphaStable$alpha
name_of_plot <-paste0("retDen","Stable", as.character(parameter), ".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 = "", xlab = "log returns", ylab = "Density")
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.alphaStable <- function(instance_alphaStable){
# log returns of the process
log_returns = diff(instance_alphaStable$process, lag=1)
# construct name of the file to save the plot (based on the given instance)
parameter <- instance_alphaStable$alpha
name_of_plot <-paste0("retPlot","Stable", as.character(parameter), ".pdf")
# plot of log returns
pdf(name_of_plot, width = 6, height =4)
plot(log_returns, type = "l", xlab = "", ylab = "log returns",
main = "")
grid()
dev.off()
}
returnsPlot(instance1)