In this vignette, we will briefly introduce the random walk model in ngme2.

Description

In ngme2, we currently support random walk model of the first-order and the second order.

Remeber in ngme2, all the models has the same form: K𝐖=𝛜,K \mathbf{W} = \boldsymbol{\epsilon}, where KK is the operator matrix, ϵ\epsilon is the random noise (maybe non-Gaussian), 𝐖\mathbf{W} is the random vector we want to model.

The first-order random walk (RW(1))

The first-order random walk is constructed assuming independent increments given data 𝐱\mathbf{x} (use as location, sorted) of length nn:

  1. When ϵ\epsilon follows the NIG distribution: Δwi=μ+μVi+σViZ,n=1,,n1ViIG(ν,ν(Δxi)2),ZiN(0,1), \begin{align} \Delta w_i &= -\mu + \mu V_i + \sigma \sqrt{V_i} Z , \; n=1, \dots, n-1 \\ V_i &\sim IG(\nu, \nu (\Delta x_i)^2), \\ Z_i &\sim N(0, 1), \end{align}

  2. When ϵ\epsilon follows the normal distribution: Δwi=ϵi,n=1,,n1ϵiN(0,σ2Δxi), \begin{align} \Delta w_i &= \epsilon_i , \; n=1, \dots, n-1 \\ \epsilon_i &\sim N(0, \sigma^2 \Delta x_i), \\ \end{align}

where Δwi:=wi+1wi\Delta w_i := w_{i+1} - w_{i}, Δxi:=xi+1xi\Delta x_i := x_{i+1} - x_{i}.

The operator matrix KK of dimension (n1×n)(n-1 \times n) is

K=[111111]. K = \begin{bmatrix} -1 & 1 \\ & -1 & 1 \\ & & \ddots & \ddots \\ & & & -1 & 1 \end{bmatrix}.

We also provide the special case of circular random walk, which the 1st element and n-th element is connected. In the circular RW(1) case, the operator matrix KK of dimension (n1×n1)(n-1 \times n-1) is

K=[111111]. K = \begin{bmatrix} -1 & 1 \\ & \ddots & \ddots \\ & & -1 & 1 \\ 1 & & & -1 \end{bmatrix}.

The second-order random walk (RW(2))

Similarily, the second-order random walk is constructed assuming the second order difference is independent:

  1. When ϵ\epsilon follows the NIG distribution: Δ2wi=μ+μVi+σViZ,n=1,,n2ViIG(ν,ν(Δxi)2),ZiN(0,1), \begin{align} \Delta^2 w_i &= -\mu + \mu V_i + \sigma \sqrt{V_i} Z , \; n=1, \dots, n-2 \\ V_i &\sim IG(\nu, \nu (\Delta x_i)^2), \\ Z_i &\sim N(0, 1), \end{align}

  2. When ϵ\epsilon follows the normal distribution: Δ2wi=ϵi,n=1,,n2ϵiN(0,σ2Δxi), \begin{align} \Delta^2 w_i &= \epsilon_i , \; n=1, \dots, n-2 \\ \epsilon_i &\sim N(0, \sigma^2 \Delta x_i), \\ \end{align}

where Δ2wi:=wi+22wi+1wi\Delta^2 w_i := w_{i+2} - 2w_{i+1} - w_{i}, Δxi:=xi+1xi\Delta x_i := x_{i+1} - x_{i}.

The operator matrix KK of dimension (n2×n)(n-2 \times n) is

K=[121121121]. K = \begin{bmatrix} 1 & -2 & 1 \\ & 1 & -2 & 1 \\ & & \ddots & \ddots & \ddots \\ & & & 1 & -2 & 1 \end{bmatrix}.

In the circular RW(2) case, the operator matrix KK of dimension (n2×n2)(n-2 \times n-2) is

K=[121121112211]. K = \begin{bmatrix} 1 & -2 & 1 \\ & 1 & -2 & 1 \\ & & \ddots & \ddots & \ddots \\ 1 & & & 1 & -2 \\ -2 & 1 & & & 1 \end{bmatrix}.

Usage

Use the f(model = "rw1") or f(model = "rw2") (in formula) to specify a random walk model.

library(ngme2)
#> This is ngme2 of version 0.6.0
#> - See our homepage: https://davidbolin.github.io/ngme2 for more details.
set.seed(16)
m1 <- f(rexp(5), model="rw1", noise = noise_normal())
m1$operator$K
#> 5 x 5 sparse Matrix of class "dgCMatrix"
#>                   
#> [1,]  1  .  .  . .
#> [2,] -1  1  .  . .
#> [3,]  . -1  1  . .
#> [4,]  .  . -1  1 .
#> [5,]  .  .  . -1 1

m2 <- f(rnorm(6), model="rw2", cyclic = TRUE)
m2$operator$K
#> 6 x 6 sparse Matrix of class "dgCMatrix"
#>                       
#> [1,]  1 -2  1  .  .  .
#> [2,]  .  1 -2  1  .  .
#> [3,]  .  .  1 -2  1  .
#> [4,]  .  .  .  1 -2  1
#> [5,]  1  .  .  .  1 -2
#> [6,] -2  1  .  .  .  1

Simulation

Doing the simulation is simple, just pass the corresponding model into simulate function.

simulate(m1)
#>         sim_1
#> 1 -0.37366877
#> 2 -0.45827890
#> 3 -1.61594885
#> 4 -0.78243995
#> 5 -0.02372775