La cantidad de observaciones totales es:
length(Novocure)
## [1] 252
tsNovoCure <- ts(Novocure, start = c(2019,1),frequency=365)
plot(tsNovoCure)

l_NovoCure<-diff(log(tsNovoCure))
plot(l_NovoCure)

Delta <- 1/365
alpha <- mean(l_NovoCure)/Delta
sigma <- sqrt(var(l_NovoCure)/Delta)
mu <- alpha +0.5*sigma^2
x0 <-tsNovoCure[1]
sigma
## RACE.close
## RACE.close 0.2855984
mu
## RACE.close
## RACE.close 0.792782
nsim <- 1000
t <- 252
mu <- 0.792782
sigma <- 0.2855984
S0 <- 32.72
dt = 1/365
gbm_vec <- function(nsim = 1000, t = 252, mu = 0.792782, sigma = 0.2855984, S0 = 32.72, dt = 1./365) {
epsilon <- matrix(rnorm(t*nsim), ncol = nsim, nrow = t)
gbm <- exp((mu - sigma * sigma / 2) * dt + sigma * epsilon * sqrt(dt))
gbm <- apply(rbind(rep(S0, nsim), gbm), 2, cumprod)
return(gbm)
}
gbm <- gbm_vec(nsim, t, mu, sigma, S0, dt)
gbm_df <- as.data.frame(gbm) %>%
mutate(ix = 1:nrow(gbm)) %>%
pivot_longer(-ix, names_to = 'sim', values_to = 'price')
gbm_df %>%
ggplot(aes(x=ix, y=price, color=sim)) +
geom_line() +
theme(legend.position = 'none')

data.frame(price = gbm[253, ]) %>%
ggplot(aes(x = price)) +
geom_histogram(aes(y = ..density..), binwidth = 0.1) +
geom_density() +
ggtitle('terminal price distribution')
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.

D <- gbm[253, ] %>%
density()
El Pronostico para el día numero 5 es el siguiente:
D$x[which.max(D$y)]
## [1] 57.18942