#
#
#
#
#
#
模拟用的函数:
- # 需要已经安装了MASS包
- gaussprocess <- function(from = 0,
- to = 1,
- K = function(s, t) {min(s, t)},
- start = NULL,
- m = 1000) {
- # Simulates a Gaussian process with a given kernel
- #
- # args:
- # from: numeric for the starting location of the sequence
- # to: numeric for the ending location of the sequence
- # K: a function that corresponds to the kernel (covariance function) of
- # the process; must give numeric outputs, and if this won't produce a
- # positive semi-definite matrix, it could fail; default is a Wiener
- # process
- # start: numeric for the starting position of the process; if NULL, could
- # be randomly generated with the rest
- # m: positive integer for the number of points in the process to simulate
- #
- # return:
- # A data.frame with variables "t" for the time index and "xt" for the value
- # of the process
-
- t <- seq(from = from, to = to, length.out = m)
- Sigma <- sapply(t, function(s1) {
- sapply(t, function(s2) {
- K(s1, s2)
- })
- })
-
- path <- MASS::mvrnorm(mu = rep(0, times = m), Sigma = Sigma)
- if (!is.null(start)) {
- path <- path - path[1] + start # Must always start at "start"
- }
-
- return(data.frame("t" = t, "xt" = path))
- }
模拟“维纳过程”:
- library(tidyverse)
- gaussprocess() %>%
- ggplot(aes(t, xt)) +
- geom+line() +
- theme_bw()
模拟“高斯过程”:
- gaussprocess(K = function(s, t) {exp(-16 * (s - t) ^ 2)}) %>%
- ggplot(aes(t, xt)) +
- geom_line() +
- theme_bw()
有兴趣的同学可以看看相关书籍: