WMTdata <- pdfetch_YAHOO("WMT",from = c("2019-01-01"),to = c("2023-01-03"), interval = '1d')
Walmart <- WMTdata[,4]
length(WMTdata)
## [1] 6048
tsWalmart <- ts(Walmart, start = c(2019,1),frequency=365)
plot(tsWalmart)
Según lo observado en la gráfica, esta serie de tiempo no es estacionaria ya que posee tendencia. Por lo tanto, se aplica una diferenciación con logaritmos.
l_Walmart<-diff(log(tsWalmart))
plot(l_Walmart)
tseries::adf.test(l_Walmart)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Warning in tseries::adf.test(l_Walmart): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: l_Walmart
## Dickey-Fuller = -9.7777, Lag order = 10, p-value = 0.01
## alternative hypothesis: stationary
Luego de realizar la diferenciación, se obtuvo que la serie de tiempo sí es estacionaria. Esto se confirma por medio de la prueba de Dickey-Fuller, ya que el p-value es 0.01 < 0.05
Delta <- 1/365
alpha <- mean(l_Walmart)/Delta
sigma <- sqrt(var(l_Walmart)/Delta)
mu <- alpha +0.5*sigma^2
x0<-tsWalmart[1]
x <- tsWalmart
gBm <- setModel(drift="mu*x", diffusion="sigma*x", xinit=x0)
## Warning in yuima.warn("Solution variable (lhs) not specified. Trying to use state variables."):
## YUIMA: Solution variable (lhs) not specified. Trying to use state variables.
mod <- setYuima(model=gBm, data=setData(tsWalmart, delta=Delta))
set.seed(123)
fit <- qmle(mod, start=list(mu=0, sigma=1),
lower=list(mu=0.1, sigma=0.1),
upper=list(mu=100, sigma=10))
summary(fit)
## Quasi-Maximum likelihood estimation
##
## Call:
## qmle(yuima = mod, start = list(mu = 0, sigma = 1), lower = list(mu = 0.1,
## sigma = 0.1), upper = list(mu = 100, sigma = 10))
##
## Coefficients:
## Estimate Std. Error
## sigma 0.2819980 0.006285613
## mu 0.1912724 0.169776570
##
## -2 log L: 4144.221
coef(fit)
## sigma mu
## 0.2819980 0.1912724
sigma
## WMT.close
## WMT.close 0.2819388
mu
## WMT.close
## WMT.close 0.1912898
gbm_vec <- function(nsim = 10000, t = 25, mu = 0, sigma = 0.1, S0 = 100, dt = 1./365) {
# matrix of random draws - one for each day for each simulation
epsilon <- matrix(rnorm(t*nsim), ncol = nsim, nrow = t)
# get GBM and convert to price paths
gbm <- exp((mu - sigma * sigma / 2) * dt + sigma * epsilon * sqrt(dt))
gbm <- apply(rbind(rep(S0, nsim), gbm), 2, cumprod)
return(gbm)
}
gBm
##
## Diffusion process
## Number of equations: 1
## Number of Wiener noises: 1
## Parametric model with 2 parameters
valores_simulados <- simulate(gBm, true.parameter = list(mu=mu, sigma=sigma))
## Warning in yuima.warn("'delta' (re)defined."):
## YUIMA: 'delta' (re)defined.
plot(valores_simulados)
nsim <- 1000
t <- 366
mu <- 1.517177
sigma <- 0.531907
S0 <- 32.72
dt = 1/365
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()
D$x[which.max(D$y)]
## [1] 71.38717
WMTdata2 <- pdfetch_YAHOO("WMT",from = c("2019-01-01"),to = c("2023-03-03"), interval = '1d')
Walmart2 <- WMTdata2[,4]
View(Walmart2)