-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.r
executable file
·144 lines (106 loc) · 4.19 KB
/
static.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
#!/usr/bin/env Rscript
path = getwd()
source(paste(path, "/install.r", sep=""))
# set.seed(2)
calc_abs_il <- function(P, P1, Pa, Pb) {
range_factor <- sqrt(Pb / Pa)
px <- P1 / sqrt(Pb * Pa)
CE <- (sqrt(range_factor) / (sqrt(range_factor) - 1))
a1 <- (sqrt(range_factor) - px) / (px + 1)
a2 <- (CE) * (((2 * sqrt(px)) / (px + 1)) -1)
a3 <- ((px * (sqrt(range_factor) - 1))) / (px + 1)
IL <- 0
if (range_factor <= 5) {
if ((Pb / P) < px) {
IL <- a1
} else if ((Pa / P) > px) {
IL <- a3
} else {
IL <- a2
}
} else {
IL <- ((2 * sqrt(px)) / (px + 1)) - 1
}
return(list(r = range_factor, IL = IL, expected_price = P1))
}
get_expected_prices <- function(P0, mu) {
static_data <- matrix(ncol = 3, nrow = 2)
static_data[1, 1] <- mu
static_data[1, 2] <- (P0 * exp(mu * 168))
static_data[1, 3] <- (P0 * exp(mu * 720))
static_data[2, 1] <- 2 * mu
static_data[2, 2] <- (P0 * exp(2 * mu * 168))
static_data[2, 3] <- (P0 * exp(2 * mu * 720))
return(list(data = static_data))
}
bs_option <- function(S0, K, sigma, r, T, flag) {
# Calculate the option value using the Black-Scholes formula
d1 <- (log(S0/K) + (r + sigma^2/2) * T) / (sigma * sqrt(T))
d2 <- d1 - sigma * sqrt(T)
if (flag == "Call") {
option_value <- S0 * pnorm(d1) - K * exp(-r * T) * pnorm(d2)
} else if (flag == "Put") {
option_value <- K * exp(-r * T) * pnorm(-d2) - S0 * pnorm(-d1)
} else {
stop("Invalid flag parameter. Must be 'Call' or 'Put'.")
}
# Return the option value
return(option_value)
}
# Adapted from https://github.com/Victorletzelter/brownian_motion
# Define a function to calculate the price at time t
price_at_t <- function(n_paths, P0, mu, sigma, T) {
Bo = 0
t = seq(0, T, by = 0.1)
gbms = matrix(0, nrow = n_paths, ncol = length(t))
for (i in 1:n_paths) {
# Simulation of increments
B.acc = rnorm(t)
# Simulation of a trajectory > we have an MB B.sim
B.sim = Bo + cumsum(B.acc)
# Simulation of the price
gbm = P0 * exp((mu - sigma^2 / 2) * t + sigma * B.sim)
# expectation vs prediction:
pred = P0 * exp(mu * t)
gbms[i, ] = gbm
}
return(list(gbms = gbms, pred = pred, IL_v = c()))
}
# Define a function to simulate stock prices using the Heston model with a drift term for the volatility process
price_at_t_heston <- function(n_paths, P0, mu, sigma, T, v0, kappa, theta, sigma_v, rho) {
dt <- 0.1
t = seq(0, T, by = dt)
dW1 <- matrix(rnorm(n_paths * length(t)), nrow = n_paths, ncol = length(t))
dW2 <- matrix(rnorm(n_paths * length(t)), nrow = n_paths, ncol = length(t))
dZ <- matrix(rnorm(n_paths * length(t)), nrow = n_paths, ncol = length(t))
W1 <- apply(dW1, 1, cumsum)
W2 <- apply(dW2, 1, cumsum)
# Simulate the stock price paths
stock_price_simulations <- matrix(0, nrow = n_paths, ncol = length(t) + 1)
stock_price_simulations[, 1] <- P0
# Simulate the volatility paths
volatility_simulations <- matrix(0, nrow = n_paths, ncol = length(t) + 1)
volatility_simulations[, 1] <- v0
pred <- numeric(length(t))
for (i in 1:n_paths) {
for (j in 2:(length(t) + 1)) {
v <- volatility_simulations[i, j - 1]
dZv <- sigma * (theta - v) * dt + sigma_v * sqrt(v * dt) * dZ[i, j - 1]
v <- max(0, v + kappa * (theta - v) * dt + sigma_v * sqrt(v * dt) * dW1[i, j - 1] + dZv)
volatility_simulations[i, j] <- v
stock_price_simulations[i, j] <- stock_price_simulations[i, j - 1] * exp((mu - v / 2) * dt + sqrt(v * dt) * (rho * dW1[i, j - 1] + sqrt(1 - rho^2) * dW2[i, j - 1]))
# Expected price
pred[j - 1] <- P0 * exp(mu * t[j - 1])
}
}
return(list(gbms = stock_price_simulations, pred = pred ))
}
# result <- price_at_t_heston(n_paths = 10, P0 = 1000, mu = 0.1, sigma = 0.2, T = 1, v0 = 0.04, kappa = 1.5, theta = 0.04, sigma_v = 0.3, rho = -0.5)
# tail(result$gbms[, ncol(result$gbms)])
# Random number Box–Muller transform
random_bm <- function(mu, sigma) {
u <- runif(1, min=0, max=1)
v <- runif(1, min=0, max=1)
mag <- sigma * sqrt(-2 * log(u))
return (mag * cos(2 * pi * v) + mu)
}