Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brownian Motion and covariance matrix

Tags:

r

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?

like image 844
jmbarrios Avatar asked Feb 22 '23 12:02

jmbarrios


2 Answers

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)))
like image 112
Tommy Avatar answered Mar 02 '23 17:03

Tommy


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) )
like image 29
Vincent Zoonekynd Avatar answered Mar 02 '23 15:03

Vincent Zoonekynd