cov matrix of sample means, variances and covariances
- To: mathgroup at smc.vnet.net
- Subject: [mg15200] cov matrix of sample means, variances and covariances
- From: Albert Maydeu-Olivares <amaydeu at nil.fut.es>
- Date: Tue, 22 Dec 1998 04:01:33 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Hi, Does anyone know how to efficiently compute the covariance matrix of the sample means, covariances, and variances of a subj x nvars data matrix? I enclose a notebook with some code I wrote with a 300 x 4 matrix of binary data. It's very slow. I would not like to use use it on a 1000 x 50 matrix!! In[1]:= T[matrix_List] := Transpose[matrix] L[matrix_List] := Length[matrix] Diag[matrix_List] := Table[If[i == j, matrix[[i,j]], 0], {i, L[matrix]}, {j, L[matrix]}] VecLow[matrix_List] := Flatten[MapIndexed[Take[#1, First[#2] - 1] & , matrix]] VecLowDiag[matrix_List] := Flatten[MapIndexed[Take[#1, First[#2]] & , matrix]] VecDiag[matrix_List] := Table[matrix[[i,i]], {i, Length[matrix]}] In[2]:= patterns = {{0, 0, 1, 1}, {0, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}}//N; obsfreq = {39, 10, 30, 51, 24, 12, 19, 20, 43, 52}; n = L[obsfreq]; data = Flatten[Table[Table[patterns[[i]], {obsfreq[[i]]}], {i, n}], 1]; subj = L[data]; nvars = L[T[data]]; l = nvars + Binomial[nvars, 2]; Timing[dmat = Table[dd = {data[[i]]}; mat = T[dd] . dd; dj = Flatten[{VecDiag[mat], VecLow[mat]}], {i, subj}]; dmean = Table[Table[Sum[dmat[[i,j]], {i, subj}], {j, l}]/subj, {k, subj}]; w = T[dmat - dmean] . (dmat - dmean)/subj; ] Out[2]= {23.95 Second,Null} N[w,3]//MatrixForm ---------------------------------------------------------------------------- Albert Maydeu-Olivares Tel. +34 93 4021079 ext. 3099 Faculty of Psychology Fax. +34 93 4021362 University of Barcelona E-Mail: amaydeu at tinet.fut.es Passeig de la Vall d'Hebron, 171. 08035 - Barcelona (Spain) ----------------------------------------------------------------------------
- Follow-Ups:
- Re: cov matrix of sample means, variances and covariances
- From: Jurgen Tischer <jtischer@col2.telecom.com.co>
- Re: cov matrix of sample means, variances and covariances