[18701] in s-news-athena
[S] Summary - how to generate a symmetrix matrix?
daemon@ATHENA.MIT.EDU (Robert Hillman)
Thu Jul 22 06:27:38 1999
Message-Id: <000201bed42b$bdfe6280$1eb5883e@oemcomputer>
Reply-To: "Robert Hillman" <R.J.T.Hillman@city.ac.uk>
From: "Robert Hillman" <R.J.T.Hillman@city.ac.uk>
To: <s-news@wubios.wustl.edu>
Date: Thu, 22 Jul 1999 08:47:33 +0100
Mime-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
THANKS! I got over 25 replies to a question I was seriously worried might
have generated a few 'Read the manual!' type replies.
Not all the suggestions could do what I wanted. This was entirely my own
fault. My original email was somewhat ambiguous. Many people did however
realise what I required, probably recognising the problem from their own
work. To restate the problem clearly
Given a vector x[i], i=1,....n
I wanted to generate symmetric matrix G of size (n*n) such that entry
G[i,j]=G[j,i] contains x[ abs(i-j) ]
and that the diagonal is zeros
For those people so inclined, in my application, the vector x[i] represents
semi-variogram estimates at lags i, and the matrix G I needed to construct
quickly is used in an ordinary kriging equation to produce predictions. I'm
comparing kriging with other more standard time series methods to predict
time series out-of-sample. Incidentally if anyone knows of other work on
this I'd be grateful if they could let me know. (Besides Cressie's).
I can't summarise every suggestion, but I'll give you a selection of the
ones that I have tested can do this task, and some brief timing comparions,
based on a 50 by 50 matrix. I APPRECIATE not everyone wrote code with speed
in mind beyond the improvement offered by moving away from loops. So don't
take these time comparisons seriously. Sorry if your suggestion isn't here,
it doesn't mean I didn't like it!
1) Peter Holzer
> now <- proc.time()
> n <- 50
> gamma <- 1:50
> gammamat <- matrix(ncol = n + 1, nrow = n + 1)
> x <- c(0, gamma)
# vector corresponding to first column of the matrix
> for(i in 1:(n + 1)) {
# fill the matrix by columns
gammamat[, i] <- x
x <- c(gamma[i], x[ - (n + 1)])
# "shift" the column vector one down
}
> cat(proc.time() - now)
0.110016
2) S.D Byers
> now <- proc.time()
> gamma <- 1:50
> gammamat <- matrix(0, nrow = 50, ncol = 50)
> gammamat[row(gammamat) != col(gammamat)] <- gamma[abs(
col(gammamat) - row(gammamat))]
> cat(proc.time() - now)
0.0999756
3) Claude Nadeau
> now <- proc.time()
> gamma <- 1:50
> n <- 50
> gammamat <- matrix(c(0, gamma)[abs(outer(1:n, 1:n, "-")
) + 1], nrow = n)
> cat(proc.time() - now)
0.0799561
4) Christian Keller
> now <- proc.time()
> n <- 50
> gamma <- 1:50
> ii <- matrix(NA, n, n)
> gammamat <- abs(col(ii) - row(ii))
> gammamat[gammamat > 0] <- gamma[gammamat]
> cat(proc.time() - now)
0.0499878
5) Rolf Turner
suggested the function foo()
> now <- proc.time()
> foo <- function(g)
{
n <- length(g)
i <- 1:n
ii <- outer(i, i, function(x, y)
{
abs(x - y)
}
)
matrix(c(0, g)[ii + 1], n, n)
}
> gammamat <- foo(1:50)
> cat(proc.time() - now)
0.0699463
( compiling foo() then running it on this problem cut the execution time to
0.029 )
6) Andrew Sinclair
> now <- proc.time()
> gamma <- 1:50
> n <- 50
> absij.plus1 <- abs(row(matrix(NA, n, n)) - col(matrix(
NA, n, n))) + 1
> match.idx <- match(absij.plus1, 1:(n + 1))
> gammamat <- matrix(c(0, gamma)[match.idx], n, n)
> cat(proc.time() - now)
0.0800781
7) Bill Venables
> now <- proc.time()
> gamma <- 1:50
> n <- 50
> index <- as.vector(abs(outer(1:n, 1:n, "-"))) + 1
> zgamma <- c(0, gamma)
> gammamat <- matrix(zgamma[index], n, n)
> cat(proc.time() - now)
0.0900879
8) Axel Scheffner
> now <- proc.time()
> gamma <- 1:50
> n <- 50
> gammamat <- matrix(c(0, gamma)[1 + abs(outer(1:length(
gamma), 1:length(gamma), "-"))], nc = length(
gamma))
> cat(proc.time() - now)
0.0800781
Personally, I'm attracted to 2 and 3, though probably more for aesthetic
reasons.
Once again, thanks very much to all of you,
in the context of my specific problem this has speeded things up
immensely....
Cheers
Robert
Robert J T Hillman
Research Fellow
Financial Econometrics Research Centre
City University Business School
The Barbican
Frobisher Crescent
LONDON
EC2Y 8HB
tel: +44 (0) 171 477 8734 Direct Line
tel: +44 (0) 171 477 8611 Secretary
fax:+44 (0) 171 477 8881
Home page:http://www.city.ac.uk/cubs/ferc
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news