随机游走的自相关函数的3D图

时间:2019-02-03 06:01:53

标签: r

Sample ACF plot

我想为随机游走模型生成自相关函数的3D图,说:

[rho(s,t)= min {s,t} / sqrt(st)]。

rm(list=ls())
s <- seq(1, 50, length= 25)
t <- seq(1, 50, length= 25)
rwacf <- function(s,t) {
  if (s<=t) {
    s/sqrt(s*t)  
  }
  else {
    t/sqrt(s*t) 
  }

}

z <- outer(s, t, rwacf)
persp(s, t, z,
      main="Perspective Plot of a ACF",
      zlab = "ACF",
      theta = 120, phi = 25,
      col = "blue", shade = 0.5)

但是,没有成功...

1 个答案:

答案 0 :(得分:1)

也许可以这样尝试:

z <- outer(s, t, function(x, y) pmin(x, y) / sqrt(x * y)) 

persp(s, t, z,
      main   = "Perspective Plot of a ACF",
      zlab   = "ACF",
      theta  = 120, 
      phi    = 30,
      col    = "skyblue",
      ltheta = 120,
      shade  = 0.5
      )

perspplot

您的rwacf()函数的问题在于,它始终使用具有第一最小值的向量,因此,该方法仅在s == tall(s < t) | all(t < s)时有效:

set.seed(54502029)

(s <- runif(10))

# [1] 0.73046419 0.85405022 0.49474445 0.68018823 0.55472058 0.76662928 0.08549485 0.90509036
# [9] 0.38289108 0.26295411

(t <- runif(10))

# [1] 0.749837531 0.165230584 0.007726242 0.027883945 0.416567829 0.946018690 0.645163628
# [8] 0.014774420 0.284255235 0.949773405

# first element of s is <= t

s[1] <= t[1]

# TRUE

# function returns s

identical(s, if(s <= t) s)

# [1] TRUE
# Warning message:
#   In if (s <= t) s :
#   the condition has length > 1 and only the first element will be used

# but in other positions ! s <= t

s <= t

# [1]  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE

# you cant use min, as it is suggested above, because it always
# returns single number (the same) - the minimum for all s and t 
# togehter:

min(s, t) %in% s

# FALSE

min(s, t) %in% t

# TRUE

# you should use pmin(), or 'parallel minimum', which finds min for 
# each sequential pair of elements of s and t:

ifelse(pmin(s, t) %in% s, 's', 't')

# [1] "s" "t" "t" "t" "t" "s" "s" "t" "t" "s"

# which is exactly what you need