模拟带有漂移的随机游走(使用 for 循环)

Simulating a Random Walk with a drift (using a for loop)

我正在尝试使用循环构建具有漂移的随机游走过程的模拟,但是,我无法获得任何输出,而是出现长度错误(要替换的项目数不是替换长度的倍数),我无法完全理解,因为我提供的长度会随着任何数量的值(N)而变化。我应该能够提供特定值,然后模拟随机游走。这是我的代码:

random_walk <- function(prices){
  

  prices <- as.vector(prices)
  
 
  ln_prices <- log(prices)
  
  
  N <- length(prices)
  
 
  phi0 <- (ln_prices[N] - ln_prices[1]) / N
  
  
  sigma <- sd(ln_prices) / sqrt(ln_prices)
  
 
  shock <- rnorm(ln_prices, 0, sigma)
  
 
  rw1 <- c(ln_prices[1])
  
 for (i in 2:N){
  # I calculate the rw value for day t:
  # rw <- drift + shock + rw of yesterday
  rw1 <- rw1 + phi0 + shock

 }
  
}```

你需要return这个函数之外的东西,你可以使用return或者在最后一行调用你想要的东西的名字

random_walk <- function(prices){
  
  
  prices <- as.vector(prices)
  
  
  ln_prices <- log(prices)
  
  
  N <- length(prices)
  
  
  phi0 <- (ln_prices[N] - ln_prices[1]) / N
  
  
  sigma <- sd(ln_prices) / sqrt(ln_prices)
  
  
  shock <- rnorm(ln_prices, 0, sigma)
  
  
  rw1 <- c(ln_prices[1])
  
  for (i in 2:N){
    # I calculate the rw value for day t:
    # rw <- drift + shock + rw of yesterday
    rw1 <- rw1 + phi0 + shock
    
  }
  return(rw1)
}  

price <- c(10,11,9,10.6,10.2,9.8,8.5,8,8.8,11)  

random_walk(prices = price)
#>  [1] 2.5747813 2.2403036 1.8345087 2.9714599 1.4440819 0.8269357 2.0922631
#>  [8] 2.0563724 1.7999183 3.1020998

reprex package (v2.0.0)

于 2021-06-03 创建 会话信息
sessionInfo()
#> R version 4.1.0 (2021-05-18)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 21390)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252   
#> [3] LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C                      
#> [5] LC_TIME=Portuguese_Brazil.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] ps_1.6.0          digest_0.6.27     withr_2.4.2       magrittr_2.0.1   
#>  [5] reprex_2.0.0      evaluate_0.14     highr_0.9         stringi_1.6.2    
#>  [9] rlang_0.4.11      cli_2.5.0         rstudioapi_0.13   fs_1.5.0         
#> [13] rmarkdown_2.8     tools_4.1.0       stringr_1.4.0     glue_1.4.2       
#> [17] xfun_0.23         yaml_2.2.1        compiler_4.1.0    htmltools_0.5.1.1
#> [21] knitr_1.33

让我试着重构一下你的代码,这样它实际上 returns 一些东西:


random_walk <- function(prices){

  #  prices <- as.vector(prices) # You don't need this since you're already passing a vector
  
  ln_prices <- log(prices)
  
  N <- length(prices)
    
  phi0 <- (ln_prices[N] - ln_prices[1]) / N
    
  sigma <- sd(ln_prices) / sqrt(ln_prices)
    
  shock <- rnorm(ln_prices, 0, sigma)
    
  rw1 <- ln_prices[1]
  
  # This for loop is also unnecessary. You're basically adding phi0 + shock N-1 times
  #for (i in 2:N){
  #  # I calculate the rw value for day t:
  #  # rw <- drift + shock + rw of yesterday
  #  rw1 <- rw1 + phi0 + shock
  #  
  #}
  rw.N <- rw1 + (N-1) * (phi0 + shock)
  
  rw.N # Call rw.N to actually return anything  
}