Hello All, I have a covariance matrix, generated by read.table, and cov: co<-cov(read.table("c:/r.x")) X Y Z X 0.0012517684 0.0002765438 0.0007887114 Y 0.0002765438 0.0002570286 0.0002117336 Z 0.0007887114 0.0002117336 0.0009168750 And a weight vector generated by w<- read.table("c:/r.weights") X Y Z 1 0.5818416 0.2158531 0.2023053 I want to compute the product of the matrix and vectors termwise to generate a 3x3 matrix, where m[i,j]=w[i]*co[i,j]*w[j]. 0.000423773 7.47216E-08 4.41255E-08 7.47216E-08 1.96566E-11 4.29229E-11 4.41255E-08 4.29229E-11 4.11045E-11 Is this possible without writing explicit loops? Thank you, Dan Stanger Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 [[alternative HTML version deleted]]
Abhijit Dasgupta
2008-May-14 19:21 UTC
[R] Newbie question about vector matrix multiplication
Won't diag(w)%*%co%*%diag(w) do it? Dan Stanger wrote:> Hello All, > > I have a covariance matrix, generated by read.table, and cov: > > co<-cov(read.table("c:/r.x")) > > X Y Z > > X 0.0012517684 0.0002765438 0.0007887114 > > Y 0.0002765438 0.0002570286 0.0002117336 > > Z 0.0007887114 0.0002117336 0.0009168750 > > > > And a weight vector generated by > > w<- read.table("c:/r.weights") > > X Y Z > > 1 0.5818416 0.2158531 0.2023053 > > > > I want to compute the product of the matrix and vectors termwise to > generate a 3x3 matrix, where m[i,j]=w[i]*co[i,j]*w[j]. > > 0.000423773 7.47216E-08 4.41255E-08 > > 7.47216E-08 1.96566E-11 4.29229E-11 > > 4.41255E-08 4.29229E-11 4.11045E-11 > > > > Is this possible without writing explicit loops? > > Thank you, > > Dan Stanger > > Eaton Vance Management > 200 State Street > Boston, MA 02109 > 617 598 8261 > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
Dimitris Rizopoulos
2008-May-14 19:31 UTC
[R] Newbie question about vector matrix multiplication
try this: V <- var(matrix(rnorm(100*3), 100, 3)) w <- c(0.5, 0.3, 0.2) V * (w %o% w) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting Dan Stanger <DStanger at eatonvance.com>:> Hello All, > > I have a covariance matrix, generated by read.table, and cov: > > co<-cov(read.table("c:/r.x")) > > X Y Z > > X 0.0012517684 0.0002765438 0.0007887114 > > Y 0.0002765438 0.0002570286 0.0002117336 > > Z 0.0007887114 0.0002117336 0.0009168750 > > > > And a weight vector generated by > > w<- read.table("c:/r.weights") > > X Y Z > > 1 0.5818416 0.2158531 0.2023053 > > > > I want to compute the product of the matrix and vectors termwise to > generate a 3x3 matrix, where m[i,j]=w[i]*co[i,j]*w[j]. > > 0.000423773 7.47216E-08 4.41255E-08 > > 7.47216E-08 1.96566E-11 4.29229E-11 > > 4.41255E-08 4.29229E-11 4.11045E-11 > > > > Is this possible without writing explicit loops? > > Thank you, > > Dan Stanger > > Eaton Vance Management > 200 State Street > Boston, MA 02109 > 617 598 8261 > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hi Phil, That solved the problem. Thanks, Dan Stanger Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 -----Original Message----- From: Phil Spector [mailto:spector at stat.Berkeley.EDU] Sent: Wednesday, May 14, 2008 3:42 PM To: Dan Stanger Subject: RE: [R] Newbie question about vector matrix multiplication Dan - Most likely it's due to the fact that you used read.table for your weight vector, and it gave you a one row, three column data frame (as it should). Your weights are in the first row of that, so ww = as.numeric(w[1,]) will extract them as a vector, and outer(ww,ww,'*") * X should get what you want. - Phil On Wed, 14 May 2008, Dan Stanger wrote:> Hi Phil, > outer(w,w,"*") > Error in as.vector(X) %*% t(as.vector(Y)) : > requires numeric matrix/vector arguments > > There must be something about the object I am creating when I read my > weight vector in, which causes it to be treated improperly. > My file is: > \tX\tY\tZ > 1\t0.581841567\t0.215853099\t0.202305334 > where tabs have been replaced by \t for readability. > > Dan Stanger > Eaton Vance Management > 200 State Street > Boston, MA 02109 > 617 598 8261 > > -----Original Message----- > From: Phil Spector [mailto:spector at stat.Berkeley.EDU] > Sent: Wednesday, May 14, 2008 3:27 PM > To: Dan Stanger > Subject: Re: [R] Newbie question about vector matrix multiplication > > Dan - > One possible way to do what you describe is > > outer(w,w,"*") * co > > - Phil Spector > Statistical Computing Facility > Department of Statistics > UC Berkeley > spector at stat.berkeley.edu > > > > On Wed, 14 May 2008, Dan Stanger wrote: > >> Hello All, >> >> I have a covariance matrix, generated by read.table, and cov: >> >> co<-cov(read.table("c:/r.x")) >> >> X Y Z >> >> X 0.0012517684 0.0002765438 0.0007887114 >> >> Y 0.0002765438 0.0002570286 0.0002117336 >> >> Z 0.0007887114 0.0002117336 0.0009168750 >> >> >> >> And a weight vector generated by >> >> w<- read.table("c:/r.weights") >> >> X Y Z >> >> 1 0.5818416 0.2158531 0.2023053 >> >> >> >> I want to compute the product of the matrix and vectors termwise to >> generate a 3x3 matrix, where m[i,j]=w[i]*co[i,j]*w[j]. >> >> 0.000423773 7.47216E-08 4.41255E-08 >> >> 7.47216E-08 1.96566E-11 4.29229E-11 >> >> 4.41255E-08 4.29229E-11 4.11045E-11 >> >> >> >> Is this possible without writing explicit loops? >> >> Thank you, >> >> Dan Stanger >> >> Eaton Vance Management >> 200 State Street >> Boston, MA 02109 >> 617 598 8261 >> >> >> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at 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. >> >
Jeremiah Rounds
2008-May-14 19:53 UTC
[R] Newbie question about vector matrix multiplication
> Date: Wed, 14 May 2008 15:18:32 -0400 > From: DStanger at eatonvance.com > To: r-help at r-project.org > Subject: [R] Newbie question about vector matrix multiplication > > Hello All, > > I have a covariance matrix, generated by read.table, and cov: > > co<-cov(read.table("c:/r.x")) > > X Y Z > > X 0.0012517684 0.0002765438 0.0007887114 > > Y 0.0002765438 0.0002570286 0.0002117336 > > Z 0.0007887114 0.0002117336 0.0009168750 > > > > And a weight vector generated by > > w<- read.table("c:/r.weights") > > X Y Z > > 1 0.5818416 0.2158531 0.2023053 > > > > I want to compute the product of the matrix and vectors termwise to > generate a 3x3 matrix, where m[i,j]=w[i]*co[i,j]*w[j]. > > 0.000423773 7.47216E-08 4.41255E-08 > > 7.47216E-08 1.96566E-11 4.29229E-11 > > 4.41255E-08 4.29229E-11 4.11045E-11First off your example matrix does not seem to represent the equation you wrote down. For example m[1,3] should be m[1,3] = 0.5818416 * 0.0007887114* 0.2023053 = .000928. I apologize if that represents something incorrect on part. However if I am correct then I believe what you seek is the line below: m = w %*% t(w)*co To get there btw picture moving the weights together then picture multiplying two equal sized matrixes together by each coefficient.> > > > Is this possible without writing explicit loops? > > Thank you, > > Dan Stanger > > Eaton Vance Management > 200 State Street > Boston, MA 02109 > 617 598 8261 > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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._________________________________________________________________ esh_messenger_052008