-
Notifications
You must be signed in to change notification settings - Fork 0
/
RW-model.Rmd
146 lines (115 loc) · 3.35 KB
/
RW-model.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
---
title: "Ngme2 random walk model"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Ngme2 random walk model}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
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 \mathbf{W} = \boldsymbol{\epsilon},$$
where $K$ 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 $n$:
1. When $\epsilon$ follows the NIG distribution:
$$
\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:
$$
\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 $\Delta w_i := w_{i+1} - w_{i}$, $\Delta x_i := x_{i+1} - x_{i}$.
The operator matrix $K$ of dimension $(n-1 \times n)$ is
$$
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 $K$ of dimension $(n-1 \times n-1)$ is
$$
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:
$$
\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:
$$
\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 $\Delta^2 w_i := w_{i+2} - 2w_{i+1} - w_{i}$, $\Delta x_i := x_{i+1} - x_{i}$.
The operator matrix $K$ of dimension $(n-2 \times n)$ is
$$
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 $K$ of dimension $(n-2 \times n-2)$ is
$$
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.
```{r rw-usage}
library(ngme2)
set.seed(16)
m1 <- f(rexp(5), model="rw1", noise = noise_normal())
m1$operator$K
m2 <- f(rnorm(6), model="rw2", cyclic = TRUE)
m2$operator$K
```
## Simulation
Doing the simulation is simple, just pass the corresponding model into `simulate` function.
```{r rw-simulate}
simulate(m1)
```