# How can I get a variable to be recognized as an array when computing expectation for multivariate distributions

The following expression works correctly:

Expectation[
x^4, {x, y, z} \[Distributed]
MultinormalDistribution[{0, 0, 0}, IdentityMatrix[3]]]


But when I replace {x,y,z} by a vector v:

ClearAll[v];
Expectation[v[[1]]^4,
v \[Distributed]
MultinormalDistribution[{0, 0, 0}, IdentityMatrix[3]]]


I get the following error message:

Part::partd: Part specification v[[1]] is longer than depth of object.


Is there a way to use a vector variable in such an expression ?

ClearAll[v, vv];
v = Array[vv, 3];
Expectation[v[[1]]^4,  v \[Distributed]
MultinormalDistribution[{0, 0, 0}, IdentityMatrix[3]]]


3

Or

Expectation[vv[1]^4,
v \[Distributed]  MultinormalDistribution[{0, 0, 0}, IdentityMatrix[3]]]


3

You wrote:

But when I replace {x,y,z} by a vector v:

ClearAll[v]; Expectation[v[[1]]^4, v \[Distributed]
MultinormalDistribution[{0, 0, 0}, IdentityMatrix[3]]]

I get the following error message:....

The essence of why this didn't work is that, in the above syntax, you actually didn't replace {x,y,z} by a vector v, because you did not define v as a vector. If we do define v as a vector, it works:

ClearAll[v];
v = {x, y, z};
Expectation[v[[1]]^4,
v \[Distributed]
MultinormalDistribution[{0, 0, 0}, IdentityMatrix[3]]]


3

In particular, the reason you got that specific error message is because you were asking Mathematica to return the first indexed part of an undefined variable, and an undefined variable doesn't have any indexed parts. However, if you define v as, say, the vector {x, y, z} (an object consisting of three indexed parts), then when you ask Mathematica to return v[[1]], it will return the first indexed part of v, which is x.