Brownian_bridge_path_sampler: Brownian Bridge path sampler (Algorithm 13 in ST329)

View source: R/RcppExports.R

Brownian_bridge_path_samplerR Documentation

Brownian Bridge path sampler (Algorithm 13 in ST329)

Description

Simulation of a path of a Brownian bridge at given times

Usage

Brownian_bridge_path_sampler(x, y, s, t, times)

Arguments

x

start value of Brownian bridge

y

end value of Brownian bridge

s

start time of Brownian bridge

t

end time of Brownian bridge

times

vector of real numbers to simulate Brownian bridge

Value

A list with the following components

full_path

Matrix of the simulated Brownian bridge path at all included time points, i.e. s, t and times. The times are sorted and duplicates are removed. The first row are the points of the Brownian bridge (named 'X') second row are corresponding times (named 'time')

simulated_path

Matrix of the simulated Brownian bridge path only at the specified times passed into the function, i.e. the times vector. The times are not sorted and duplicates are not removed. The first row are the points of the Brownian bridge (named 'X') second row are corresponding times (named 'time')

Examples

# simulating paths for time [0,1] and plotting them
start <- runif(1, -1, 1)
end <- runif(1, -1, 1)
path <- Brownian_bridge_path_sampler(x = start,
                                     y = end,
                                     s = 0,
                                     t = 1,
                                     times = seq(0, 1, 0.01))$full_path
plot(x = path['time',], y = path['X',], pch = 20, xlab = 'Time', ylab = 'X')
lines(x = path['time',], y = path['X',])

# notice that simulated_path only includes points that are included in times vector
# note that simulated_path does not remove duplicates passed into times
Brownian_bridge_path_sampler(x = 0,
                             y = 1,
                             s = 0,
                             t = 1,
                             times = c(0.1, 0.2, 0.4, 0.6, 0.6, 0.8, 0.1))

# comparing the simulated distribution of simulated points to the
# theoretical distribution of simulated points
# set variables
x <- 0.53
y <- 4.32
s <- 0.53
t <- 2.91
q <- 1.72
replicates <- 10000
paths <- list()
# repeatedly simulate Brownian bridge 
for (i in 1:replicates) {
  paths[[i]] <- Brownian_bridge_path_sampler(x = x,
                                             y = y,
                                             s = s,
                                             t = t,
                                             times = seq(s, t, 0.01))
}
# select the points at the specified time q
index <- which(seq(s, t, 0.01)==q)
simulated_points <- sapply(1:replicates, function(i) paths[[i]]$full_path['X', index])
# calculate the theoretical mean and standard deviation of the simulated points at time q
theoretical_mean <- x + (q-s)*(y-x)/(t-s)
theoretical_sd <- sqrt((t-q)*(q-s)/(t-s))
# plot distribution of the simulated points and the theoretical distribution
plot(density(simulated_points))
curve(dnorm(x, theoretical_mean, theoretical_sd), add = T, col = 'red')

rchan26/layeredBB documentation built on March 25, 2022, 3:44 a.m.