Re: cov matrix of sample means, variances and covariances
- To: mathgroup at smc.vnet.net
- Subject: [mg15222] Re: [mg15200] cov matrix of sample means, variances and covariances
- From: Jurgen Tischer <jtischer at col2.telecom.com.co>
- Date: Wed, 23 Dec 1998 01:04:00 -0500
- Organization: Universidad del Valle
- References: <199812220901.EAA29056@smc.vnet.net.>
- Sender: owner-wri-mathgroup at wolfram.com
Albert,
change
dmean = Table[Table[Sum[dmat[[i,j]], {i, subj}], {j, l}]/subj,{k, subj}]
w = T[dmat - dmean] . (dmat - dmean)/subj
to
dmean =Plus@@dmat/subj;
w =With[{m1=#-dmean&/@dmat}, T[m1] . m1/subj]
Jurgen
Albert Maydeu-Olivares wrote:
>
> 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)
> ----------------------------------------------------------------------------
- References:
- cov matrix of sample means, variances and covariances
- From: Albert Maydeu-Olivares <amaydeu@nil.fut.es>
- cov matrix of sample means, variances and covariances