Given a vector t. How can I fill a matrix like
t[1] t[1] t[1] ... t[1]
t[1] t[2] t[2] ... t[2]
t[1] t[2] t[3] ... t[3]
... ... ... ... ...
t[1] t[2] t[3] ... t[n]
that corresponds to a covariance matrix for Brownian motion at times t?
Here's one way.
t <- 11:15
m <- vapply(seq_along(t), function(i) c(t[seq_len(i)], rep(t[i], length(t)-i)), numeric(length(t)))
m
# [,1] [,2] [,3] [,4] [,5]
#[1,] 11 11 11 11 11
#[2,] 11 12 12 12 12
#[3,] 11 12 13 13 13
#[4,] 11 12 13 14 14
#[5,] 11 12 13 14 15
You could use sapply
too - a bit shorter but also a bit slower (you don't specify what the function returns):
m <- sapply(seq_along(t), function(i) c(t[seq_len(i)], rep(t[i], length(t)-i)))
The value of the (i,j) coefficient is min(i,j):
m <- matrix( NA, nr=5, nc=5 ) # Empty matrix with the right size
m <- pmin( col(m), row(m) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With