On Fri, 29 Aug 2008, Giovanni Petris wrote:
You can cut execution time by a factor 2 simply using the fact that the
double summation is symmetric in the indices j and k:
2 * sum(sapply(1:(m-1), function(k){sum(sapply((k-1):m,
function(j){x[k]*x[j]*dnorm((mu[j]+mu[k])/sqrt(sig[k]+sig[j]))/sqrt(sig[k]+sig[j])}))}))
+ sum(x^2*dnorm((2*mu)/sqrt(2*sig))/ sqrt(2*sig))
Best,
Giovanni Petris
Better still, vectorize kathie's code as:
foo <- function(j,k) {x[k]*x[j]*dnorm((mu[j]+mu[k])/
sqrt(sig[k]+sig[j]))/sqrt(sig[k]+sig[j])}
sum( outer(1:m, 1:m, foo ) )
To achieve Giovanni's savings in a vectorized framework use
jk <- combn( 1:m, 2 )
j.alone <- 1:m
2*sum( foo( jk[1,], jk[2,]) )+ sum( foo( j.alone, j.alone ) )
HTH,
Chuck
Date: Thu, 28 Aug 2008 21:47:38 -0700 (PDT)
From: kathie <[EMAIL PROTECTED]>
Sender: [EMAIL PROTECTED]
Precedence: list
Dear R users...
I made the R-code for this double summation computation
http://www.nabble.com/file/p19213599/doublesum.jpg
-------------------------------------------------
Here is my code..
sum(sapply(1:m, function(k){sum(sapply(1:m,
function(j){x[k]*x[j]*dnorm((mu[j]+mu[k])/sqrt(sig[k]+sig[j]))/sqrt(sig[k]+sig[j])}))}))
-------------------------------------------------
In fact, this is a part of optimization. I think if it is changed more
efficiently, then the running time could be shortened.
How could I change this to more efficiently? Any suggestion will be greatly
appreciated.
Kathryn Lord
--
View this message in context:
http://www.nabble.com/more-efficient-double-summation...-tp19213599p19213599.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
--
Giovanni Petris <[EMAIL PROTECTED]>
Associate Professor
Department of Mathematical Sciences
University of Arkansas - Fayetteville, AR 72701
Ph: (479) 575-6324, 575-8630 (fax)
http://definetti.uark.edu/~gpetris/
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:[EMAIL PROTECTED] UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.