3D-график для автокорреляционной функции случайного блуждания - PullRequest
0 голосов
/ 03 февраля 2019

Sample ACF plot

Я хочу сгенерировать трехмерный график функции автокорреляции для модели Random Walk, скажем:

[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 голосов
/ 03 февраля 2019

Возможно, попробуйте это так:

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 == t или all(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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...