I have the following problem. For performance reasons I use numpy.tensordot
and have thus my values stored in tensors and vectors.
One of my calculations look like this:
<w_j>
is the expectancy value of w_j
and <sigma_i>
the expectancy value of sigma_i
. (Perhaps I should now have called is sigma, because it has nothing to do with standart deviation) Now for further calculations I also need the variance. To the get Variance I need to calculate:
Now when I implemented the first formula into python with numpy.tensordot
I was really happy when it worked because this is quite abstract and I am not used to tensors. The code does look like this:
erc = numpy.tensordot(numpy.tensordot(re, ewp, axes=1), ewp, axes=1)
Now this works and my problem is to write down the correct form for the second formula. One of my attempts was:
serc = numpy.tensordot(numpy.tensordot(numpy.tensordot(numpy.tensordot
(numpy.tensordot(re, re, axes=1), ewp, axes=1), ewp, axes=1)
, ewp, axes=1), ewp, axes=1)
But this does give me a scalar instead of a vector. Another try was:
serc = numpy.einsum('m, m', numpy.einsum('lm, l -> m',
numpy.einsum('klm, k -> lm', numpy.einsum('jklm, j -> klm',
numpy.einsum('ijk, ilm -> jklm', re, re), ewp), ewp), ewp), ewp)
The vectors have lenght l
and the dimension of the tensor is l * l * l
. I hope my problem is understandable and thank you in advance!
Edit: The first formula can in python also written down like: erc2 = numpy.einsum('ik, k -> i', numpy.einsum('ijk, k -> ij', re, ewp), ewp)