0

Let's say, I have the following code.

import numpy as np
import pandas as pd

x = pd.DataFrame(np.random.randn(100, 3)).rolling(window=10, center=True).cov()

For each index, I have a 3x3 matrix. I would like to calculate eigenvalues and then some function of those eigenvalues. Or, perhaps, I might want to compute some function of eigenvalues and eigenvectors. The point is that if I take x.loc[0] then I have no problem to compute anything from that matrix. How do I do it in a rolling fashion for all matrices?

Thanks!

0

1 Answer 1

1

You can use the analogous eigenvector/eigenvalue methods in spicy.sparse.linalg.

import numpy as np
import pandas as pd
from scipy import linalg as LA

x = pd.DataFrame(np.random.randn(100, 3)).rolling(window=10, center=True).cov()
for i in range(len(x)):
    try:
        e_vals,e_vec = LA.eig(x.loc[i])
        print(e_vals,e_vec)
    except:
        continue

If there are no NaN values present then you need not use the try and except instead go for only for loop.

Sign up to request clarification or add additional context in comments.

1 Comment

What I don't understand is why I can calculate a covariance matrix for each "window x 3" window but I cannot calculate a more general function. Of course, I can do another loop after that but it seems wasteful. Why go over the matrix twice?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.