LAB6

library(pdfetch)

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.7
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(yuima)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: stats4
## Loading required package: expm
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## Attaching package: 'expm'
## The following object is masked from 'package:Matrix':
## 
##     expm
## Loading required package: cubature
## Loading required package: mvtnorm
## ########################################
## This is YUIMA Project package v.1.15.3
## Why don't you try yuimaGUI package?
## Visit: http://www.yuima-project.com
## ########################################
## 
## Attaching package: 'yuima'
## The following object is masked from 'package:tibble':
## 
##     char
## The following object is masked from 'package:stats':
## 
##     simulate
#obtenemos los datos desde el sitio de yahoo
NVCRdata <- pdfetch_YAHOO("TSLA",from = c("2019-01-01"),to = c("2020-01-01"), interval = '1d')
#Obtenemos la columna que es de nuestro interés
Novocure <- NVCRdata[,4]
length(Novocure)
## [1] 252
#Convertimos en una serie temporal
tsNovoCure <- ts(Novocure, start = c(2019,1),frequency=365)
#Graficamos
plot(tsNovoCure)

#Ver si estacionaria
#.-....
#Se calculan las diferencias de la serie de datos con logaritmo
l_NovoCure<-diff(log(tsNovoCure))
plot(l_NovoCure)

#Calculo parámetros iniciales manera 1
Delta <- 1/365
alpha <- mean(l_NovoCure)/Delta
sigma <- sqrt(var(l_NovoCure)/Delta)
mu <- alpha +0.5*sigma^2
x0<-tsNovoCure[1]



#Calculo parámetros iniciales manera 2
x <- tsNovoCure
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(tsNovoCure, 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.5884456 0.02631672
## mu    0.6087305 0.70960370
## 
## -2 log L: 963.6664
#comparación
coef(fit)
##     sigma        mu 
## 0.5884456 0.6087305
sigma
##            TSLA.close
## TSLA.close  0.5904736
mu
##            TSLA.close
## TSLA.close  0.6095831
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)

#PROBAR CON FORMA 1 Y FORMA 2

nsim <- 1000
t <- 366
mu <- 0.6095831
sigma <- 0.5904736
S0 <- 62.023998
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[259, ]) %>%
  ggplot(aes(x = price)) +
  geom_histogram(aes(y = ..density..), binwidth = 0.1) +
  geom_density() +
  ggtitle('terminal price distribution')

D <- gbm[259, ] %>%
  density()



D$x[which.max(D$y)] #resultado del modelo
## [1] 66.32121
NVCRdata2 <- pdfetch_YAHOO("TSLA",from = c("2019-01-01"),to = c("2020-01-10"), interval = '1d')
Novocure2 <- NVCRdata2[,4]
View(Novocure2)