-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.R
More file actions
92 lines (48 loc) · 1.51 KB
/
main.R
File metadata and controls
92 lines (48 loc) · 1.51 KB
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
rm(list = ls()) # clear work space
######## load packages
library(ggplot2) # sweet plots
library(reshape2) # melt function
######## source functions
source("functions/simulate_GBM.R")
######## initialize
x0 <- 100
mu <- 0.02
sigma <- 0.5
n_sim <- 10
end_time <- 1
dt <- 1 / 200
time_vector <- seq(0, end_time, by = dt)
######## read stock price data
sp_100_data <- read.table("data/sp_100_jan_feb_2018.csv",
header = TRUE,
sep = ";",
dec = ",",
stringsAsFactors = FALSE)
######## plot closing price
plot(1:dim(sp_100_data)[1], sp_100_data$Close, type = 'l')
######## simulate paths from GBM
simulated_paths <- simulate_GBM(x0 = x0,
mu = mu,
sigma = sigma,
n_sim = n_sim,
time_vector = time_vector)
######### plot simulated paths
#### R plot
plot(time_vector, simulated_paths[, 1], type = "l")
#### ggplot
data_plot <- data.frame('time' = time_vector, "GBM" = simulated_paths)
data_plot <- melt(data_plot, id = c('time'))
plot_gbm <- ggplot(data_plot, aes(time, value)) +
geom_line(aes(colour = variable)) +
theme(legend.position = "none")
#### save ggplot
ggsave(filename = "plots/gbm_paths.png",
plot = plot_gbm)
#### save simulated paths
write.table(
simulated_paths,
"results/simulated_paths.csv",
sep = ";",
dec = ",",
row.names = FALSE
)