Search code examples
python-3.xeigenvector

How do I extract the weights of eigenVectors relating to a particular realized 'move' vector


I have a time-series of historical data (interest rates), where I have managed to extract the eigenValues and eigenVectors.

I am trying to generate/infer some kind of relationship between the interest rates and another financial data-series. Given I have the eigenVectors, how can I extract the weights of a particular day (or point) of that move?

For example, my code below extracts the first 3 pca vectors from a data-set called data_.

eigVal, eigVec = np.linalg.eig((data_.diff(-1)*100).cov())
if np.sign(eigVec[:,0][5]) == -1: #swtich such that eigVec are positiv for PCA1
    eigVec = -1 * eigVec
plt.plot(eigVec[:,0])
plt.plot(eigVec[:,1])
plt.plot(eigVec[:,2])
plt.legend(['PCA1', 'PCA2', 'PCA3'])

Pca Vectors

How can I infer for the array example below, what weights to assign to PCA1, PCA2 and PCA3 to replicate the array move?

this array is what I am trying to get.

array([ 0.  ,  0.  , -0.3 ,  1.9 ,  1.7 ,  1.3 ,  1.4 ,  1.3 ,  1.3 ,
        1.36,  1.3 ])

this is eigenVector 2

array([-0.16562563, -0.26507723, -0.34871106, -0.47812195, -0.44021972,
       -0.25093085, -0.16544612,  0.00289244,  0.17209837,  0.28198136,
        0.3985353 ])

this is eigenVector 1

array([0.01448251, 0.03349128, 0.0559946 , 0.1639687 , 0.24343858,
       0.32155586, 0.36727468, 0.38627769, 0.41226496, 0.41694435,
       0.42199623])

Solution

  • use the code below, where y_ = the array you are trying to hit eigVec[:,0] = is the 1st PCA vector you extracted from using linalg.eig()....

    a1, a2, a3 are the weights of PCA1, PCA2 and PCA3.

    a1, _, _, _ = np.linalg.lstsq(eigVec[:,0][:,np.newaxis], y_)
    a2, _, _, _ = np.linalg.lstsq(eigVec[:,1][:,np.newaxis], y_)
    a3, _, _, _ = np.linalg.lstsq(eigVec[:,2][:,np.newaxis], y_)
    
    mv_pca = a1 * eigVec[:,0] + a2 * eigVec[:,1] + a3* eigVec[:,2]