-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
202 lines (154 loc) · 5.5 KB
/
Copy pathREADME.Rmd
File metadata and controls
202 lines (154 loc) · 5.5 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
---
title: "Supplemental material"
output: github_document
---
Also see:
* My [Blogpost](https://strakaps.github.io/post/voffpe/) with a
one page introduction to fractional diffusion
* The pdf version of the article, available [here](/varyKern.pdf)
and on the [arXiv](https://arxiv.org/abs/1712.06767)
* The published paper at [Physica A](https://doi.org/10.1016/j.physa.2018.03.010)
## Simulation of trajectories for variable order fractional diffusion
The paper derives a "Variable Order Fractional Fokker-Planck Equation",
which has the strange property that a global change in the time-scale
(e.g. measure time in minutes instead of seconds) induces locally
different changes in the dynamics.
To check that the equation is consistent, we calculate (an approximation
of) the solution at two different time scales, $T_0 = 1$ and $T_0 = 10$.
## Helper functions
The inverse tail function of the waiting times:
```{r}
invTailPsi <- function(q, beta = 0.7, theta = 0, c=1){
(gamma(1-beta) * c * q)^(-1/beta)
}
```
Waiting times are simulated via [inverse transform sampling](https://en.wikipedia.org/wiki/Inverse_transform_sampling):
```{r}
drawT <- function(x) invTailPsi(q = runif(1), beta = beta(x), c = c)
```
Given drift and diffusivity functions $a(x)$ and $b(x)$, jumps are simulated
via
```{r}
drawJ <-
function(x) {
rnorm(
n = 1,
mean = b(x) / (timescale ^ beta(x) * c),
sd = sqrt(a(x) / (timescale ^ beta(x) * c))
)
}
```
Given a vector of times `endTimes` (e.g. `r 2^(-3:3)`), this function returns the location
of one CTRW trajectory at these times (first column) and the number of steps taken in the time interval $(0, \text{endTime})$ (second column).
```{r}
propagateCTRW <- function(startLocation, endTimes){
steps <- 0
endTimes <- sort(endTimes)
endLocations <- numeric(0)
endSteps <- numeric(0)
t <- 0
x <- startLocation
t <- t + drawT(x)
for (tEnd in endTimes){
while(t < tEnd){
x <- x + drawJ(x)
t <- t + drawT(x)
steps <- steps + 1
}
endLocations <- c(endLocations, x)
endSteps <- c(endSteps, steps)
}
return(cbind(endLocations, endSteps))
}
```
## Global parameters
```{r}
library(parallel)
library(plyr)
library(magrittr)
beta <- function(x) 0.525 + 0.45 * atan(x/0.2)/pi
a <- function(x) 2
b <- function(x) dnorm(x, 0, 0.2)
c <- 200
tEnd <- 2^(c(-3, 0, 3, 6))
# initialize the arrays storing the output of propagateCTRW
ensemble1 <- array(dim=c(0,length(tEnd),2))
ensemble2 <- array(dim=c(0,length(tEnd),2))
num_trajectories <- 10000
```
## At scale $T_0 = 1$
This block generates a number of trajectories and appends their values
to `ensemble1`. Run it multiple times if more trajectories are needed.
```{r, eval=FALSE}
timescale <- 1
startX <- 0
more <- mclapply(X = rep(startX, num_trajectories),
FUN = function(x) propagateCTRW(startLocation = startX,
endTimes = tEnd * timescale),
mc.cores = detectCores())
more <- laply(.data = more, .fun = function(slice) slice)
ensemble1 <- abind::abind(ensemble1, more, along=1)
if (mean(ensemble1[,1,2]) < 30) print("Careful: not enough steps. Increase c.")
```
## At scale $T_0 = 10$
Identical to the above block. Run multiple times if more trajectories needed.
```{r, eval=FALSE}
timescale <- 10
startX <- 0
more <- mclapply(X = rep(startX, num_trajectories),
FUN = function(x) propagateCTRW(startLocation = startX,
endTimes = tEnd * timescale),
mc.cores = detectCores())
more <- laply(.data = more, .fun = function(slice) slice)
ensemble2 <- abind::abind(ensemble2, more, along=1)
if (mean(ensemble2[, 1, 2]) < 30)
print("Careful: not enough steps. Increase c."
)
```
## Comparing the densities
`ensemble1` and `ensemble2` contain the positions of the walkers at
the times `r tEnd` and `r 10 * tEnd`. Apply the kernel density estimator
`density()` from the `stats` package and plot
1. the density of `ensemble1` and
2. the difference `ensemble2 - ensemble1`:
```{r plot-densities-ggplot2, eval=FALSE}
library(tidyverse)
from = -6
to = 8
n = 512
ensemble_to_matrix <- function(ensemble) {
d <- density(ensemble, from = from, to = to, n = n)
d$y
}
y1 <- apply(ensemble1[ , ,1], 2, ensemble_to_matrix) %>% as.data.frame()
names(y1) <- c("t = 1/8", "t = 1", "t = 8", "t = 64")
y2 <- apply(ensemble2[ , ,1], 2, ensemble_to_matrix) %>% as.data.frame()
names(y2) <- names(y1)
x <- density(ensemble1[,1,1], from = from, to = to, n = n)$x
density_df <- cbind(x, y1)
density_df$panel <- "density"
diff_df <- cbind(x, y2-y1)
diff_df$panel <- "difference"
p_dens <- rbind(density_df, diff_df) %>%
reshape2::melt(c("x", "panel")) %>%
ggplot(aes(x = x, y = value, colour = variable)) +
geom_line() + ylab("") +
facet_grid(panel ~ ., scales = "free")
```
```{r save-plots, eval=FALSE, include=FALSE}
library(ggplot2)
ggsave(filename = "densities.pdf", plot = p_dens)
ggsave(filename = "densities.png", plot = p_dens)
```

And this is what the coefficients look like:
```{r, include=FALSE, eval=FALSE}
ggsave(filename = "beta_b.pdf", plot = p_coef, width = 6, height = 3)
ggsave(filename = "beta_b.png", plot = p_coef, width = 6, height = 3)
```
```{r coefficients-plot, eval=FALSE}
p_coef <- tibble(x = x, b = b(x), a = a(x), beta = beta(x)) %>%
reshape2::melt("x") %>%
ggplot(aes(x = x, y = value, colour = variable)) + geom_line()
```
