Hi,
I am generating a lot of random matrices and want to study their statistical properties. Right now I am storing them in a 3-dimensional array, where the first two dimensions are the matrix sizes, let’s say square matrices of size D, and the third dimension is the number of realizations of random matrices. E.g.
D = 10
realizations = 20
matrices(D,D,realizations)
holds 20 10x10 matrices. Now I want to calculate some statistics for each entry of the random matrices, say the mean of each entry or the variance of each entry. I did this in two ways. First, by explicitly writing out the formulas for mean and variance and then using them for each entry, e.g.
mu = zeros(D,D)
for i in 1:realizations
mu .+= matrices[:,:,i]
end
mu ./= realizations
Second, converting the D x D x realizations array into a DxD matrix of vectors of length realizations. And then using the functions mean
and var
from Statistics
.
matrices_2(D,D)
mean.(matrices)
The array conversion hit performance really hard, because I did it naively in a loop.
How would achieve my goal, calculate some statistics for each entry of the random matrices, by using functions from Statistics
or any other package, and not have to allocate a new array?
EDIT: The answer for mean
and var
is the marked answer, while for higher moment estimations (and general estimations) see the post of @nilshg Statistics on random matrices - #7 by nilshg.