어떤 행렬의 열들 사이의 공분산을 계산하기 위해 사용할 수 있는 함수는
numpy.cov()
pandas데이터프레임.cov()
가 있다.
그러나 numpy.cov()의 경우 두 객체들 사이의 공분산을 계산하고 그 이상의 차원에서는 엉뚱한 값들을 반환한다.
>>> x
Open High Low Volume
2017-12-05 0.493023 0.648262 0.624396 -0.220359
2017-12-06 0.737391 1.452395 1.166491 0.200745
2017-12-07 1.877774 1.256265 0.499297 0.316586
2017-12-08 0.065379 -0.450067 -0.480642 0.635944
2017-12-11 -0.260445 -0.548132 -0.084496 -0.518430
2017-12-12 -0.138261 -0.626584 -0.105346 -1.072204
2017-12-13 -0.117897 -0.705036 -0.188745 -0.986260
2017-12-14 -0.525176 -0.116646 -0.147046 -0.465978
2017-12-15 0.594843 1.668138 1.249890 1.623176
2017-12-18 1.144670 0.785553 0.853744 -0.322070
2017-12-19 0.615207 0.177549 0.978842 -1.352655
2017-12-20 1.042850 1.177813 1.041392 -0.172654
2017-12-21 0.289383 0.334453 0.999692 -1.654738
2017-12-22 0.004287 -0.391228 -1.710780 2.694075
2017-12-26 -2.032111 -2.097560 -1.794179 -0.207932
2017-12-27 -1.543376 -1.332653 -1.669080 0.903655
2017-12-28 -1.909927 -1.469944 -1.502282 -0.178807
>>> ind.cov()
Open High Low Volume
Open 1.128767 1.034342 0.937355 -0.011133
High 1.034342 1.183175 1.034811 0.115844
Low 0.937355 1.034811 1.176485 -0.408230
Volume -0.011133 0.115844 -0.408230 1.165753
>>> np.cov(x.iloc[:,0], x.iloc[:,1])
Out[101]:
array([[ 1.12876671, 1.03434229],
[ 1.03434229, 1.18317514]])
그러나 위 객체 x를 한번에 전달할 경우 이상한 결과를 반환합니다.
>>> np.cov(np.array(ind))
array([[ 1.68246268e-01, 2.05004449e-01, 1.52065103e-01,
-2.03718707e-01, 4.10165985e-02, 1.48847333e-01,
1.18723362e-01, 5.37039745e-02, -6.22683960e-02,
2.42177363e-01, 3.88604631e-01, 2.57479167e-01,
4.53461118e-01, -7.14587480e-01, -3.55736684e-01,
-4.86439604e-01, -2.79320294e-01],
[ 2.05004449e-01, 2.97031729e-01, 1.14245017e-01,
-2.78161416e-01, 2.14653937e-02, 1.25719977e-01,
8.25312257e-02, 9.80441073e-02, 2.59998167e-02,
2.42325728e-01, 4.03081003e-01, 3.04161921e-01,
5.20283579e-01, -8.50051153e-01, -4.08953290e-01,
-5.33662952e-01, -2.77036140e-01],
[ 1.52065103e-01, 1.14245017e-01, 5.17762819e-01,
-8.49246271e-02, 3.28664601e-03, 1.59751623e-01,
1.53117702e-01, -3.81687057e-02, -2.40400279e-01,
3.43175863e-01, 3.41690092e-01, 2.84154220e-01,
3.23217459e-01, -3.57868100e-01, -4.52525849e-01,
-5.07897566e-01, -4.14047915e-01],
....
numpy.cov()
pandas데이터프레임.cov()
가 있다.
그러나 numpy.cov()의 경우 두 객체들 사이의 공분산을 계산하고 그 이상의 차원에서는 엉뚱한 값들을 반환한다.
>>> x
Open High Low Volume
2017-12-05 0.493023 0.648262 0.624396 -0.220359
2017-12-06 0.737391 1.452395 1.166491 0.200745
2017-12-07 1.877774 1.256265 0.499297 0.316586
2017-12-08 0.065379 -0.450067 -0.480642 0.635944
2017-12-11 -0.260445 -0.548132 -0.084496 -0.518430
2017-12-12 -0.138261 -0.626584 -0.105346 -1.072204
2017-12-13 -0.117897 -0.705036 -0.188745 -0.986260
2017-12-14 -0.525176 -0.116646 -0.147046 -0.465978
2017-12-15 0.594843 1.668138 1.249890 1.623176
2017-12-18 1.144670 0.785553 0.853744 -0.322070
2017-12-19 0.615207 0.177549 0.978842 -1.352655
2017-12-20 1.042850 1.177813 1.041392 -0.172654
2017-12-21 0.289383 0.334453 0.999692 -1.654738
2017-12-22 0.004287 -0.391228 -1.710780 2.694075
2017-12-26 -2.032111 -2.097560 -1.794179 -0.207932
2017-12-27 -1.543376 -1.332653 -1.669080 0.903655
2017-12-28 -1.909927 -1.469944 -1.502282 -0.178807
>>> ind.cov()
Open High Low Volume
Open 1.128767 1.034342 0.937355 -0.011133
High 1.034342 1.183175 1.034811 0.115844
Low 0.937355 1.034811 1.176485 -0.408230
Volume -0.011133 0.115844 -0.408230 1.165753
>>> np.cov(x.iloc[:,0], x.iloc[:,1])
Out[101]:
array([[ 1.12876671, 1.03434229],
[ 1.03434229, 1.18317514]])
그러나 위 객체 x를 한번에 전달할 경우 이상한 결과를 반환합니다.
>>> np.cov(np.array(ind))
array([[ 1.68246268e-01, 2.05004449e-01, 1.52065103e-01,
-2.03718707e-01, 4.10165985e-02, 1.48847333e-01,
1.18723362e-01, 5.37039745e-02, -6.22683960e-02,
2.42177363e-01, 3.88604631e-01, 2.57479167e-01,
4.53461118e-01, -7.14587480e-01, -3.55736684e-01,
-4.86439604e-01, -2.79320294e-01],
[ 2.05004449e-01, 2.97031729e-01, 1.14245017e-01,
-2.78161416e-01, 2.14653937e-02, 1.25719977e-01,
8.25312257e-02, 9.80441073e-02, 2.59998167e-02,
2.42325728e-01, 4.03081003e-01, 3.04161921e-01,
5.20283579e-01, -8.50051153e-01, -4.08953290e-01,
-5.33662952e-01, -2.77036140e-01],
[ 1.52065103e-01, 1.14245017e-01, 5.17762819e-01,
-8.49246271e-02, 3.28664601e-03, 1.59751623e-01,
1.53117702e-01, -3.81687057e-02, -2.40400279e-01,
3.43175863e-01, 3.41690092e-01, 2.84154220e-01,
3.23217459e-01, -3.57868100e-01, -4.52525849e-01,
-5.07897566e-01, -4.14047915e-01],
....
댓글
댓글 쓰기