Nash Vlasov
Nash Vlasov

Reputation: 67

Correlations between 2 columns for all columns

I have 2 arrays each of which has n columns so that two arrays are identical in shape structure. The issue i came across was that i couldn't calculate correlations between two columns for all columns. I need a correlation for W1[:, 0] and W2[:, 0] and another one for instance W1[:, 1] and W2[:, 1] and so on and so forth till the last column. How can one accomplish that ?

steps_per_year = 12
n_years = 1
n_scenarios = 5
n_steps = steps_per_year * n_years

W1 = np.random.normal(size = (n_steps, n_scenarios))
W2 = np.random.normal(size = (n_steps, n_scenarios))

Upvotes: 0

Views: 45

Answers (1)

abe
abe

Reputation: 987

Correlations are just normalized scalar products, so you can do something like this:

W1_copy = np.copy(W1)
W2_copy = np.copy(W2)
W1_copy -= np.mean(W1_copy, axis=0)
W1_copy /= np.linalg.norm(W1_copy, axis=0)
W2_copy -= np.mean(W2_copy, axis=0)
W2_copy /= np.linalg.norm(W2_copy, axis=0)
correlations = np.dot(np.transpose(W1_copy), W2_copy)

If I'm not mistaken, correlations[i, j] will hold the correlation of the ith column of W1 and jth column of W2

Upvotes: 1

Related Questions