Iván Castro - 4 months ago 6x

Python Question

Given these two arrays:

`E = [[16.461, 17.015, 14.676],`

[15.775, 18.188, 14.459],

[14.489, 18.449, 14.756],

[14.171, 19.699, 14.406],

[14.933, 20.644, 13.839],

[16.233, 20.352, 13.555],

[16.984, 21.297, 12.994],

[16.683, 19.056, 13.875],

[17.918, 18.439, 13.718],

[17.734, 17.239, 14.207]]

S = [[0.213, 0.660, 1.287],

[0.250, 2.016, 1.509],

[0.016, 2.995, 0.619],

[0.142, 4.189, 1.194],

[0.451, 4.493, 2.459],

[0.681, 3.485, 3.329],

[0.990, 3.787, 4.592],

[0.579, 2.170, 2.844],

[0.747, 0.934, 3.454],

[0.520, 0.074, 2.491]]

The problem states that I should get the 3x3 covariance matrix (C) between S and E using the following formula:

`C = (1/(n-1))[S'E - (1/10)S'i i'E]`

Here n is 10, and i is an n x 1 column vector consisting of only ones. S' and i' are the transpose of matrix S and column vector i, respectively.

So far, I can't get C because I don't understand the meaning of i (and i') and its implementation in the formula. Using numpy, so far I do:

`import numpy as np`

tS = numpy.array(S).T

C = (1.0/9.0)*(np.dot(tS, E)-((1.0/10.0)*np.dot(tS, E))) #Here is where I lack the i and i' implementation.

I will really appreciate your help to understand and implement i and i' in the formula. The output should be:

`C= [[0.2782, 0.2139, -0.1601],`

[-1.4028, 1.9619, -0.2744],

[1.0443, 0.9712, -0.6610]]

Answer

It looks like the only part you're missing is making `i`

:

```
>>> i = np.ones((N, 1))
>>> i
array([[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.]])
```

After that, we get

```
>>> C = (1.0/(N-1)) * (S.T.dot(E) - (1.0/N) * S.T.dot(i) * i.T.dot(E))
>>> C
array([[ 0.27842301, 0.21388842, -0.16011839],
[-1.4017267 , 1.96193373, -0.27441417],
[ 1.04532836, 0.97120807, -0.66095656]])
```

Note that this doesn't *quite* produce the array you expected, which is more obvious if you round it, but maybe there are some minor typos in your data?

```
>>> C.round(4)
array([[ 0.2784, 0.2139, -0.1601],
[-1.4017, 1.9619, -0.2744],
[ 1.0453, 0.9712, -0.661 ]])
```

Source (Stackoverflow)

Comments