Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculate_correlation_matrix #14

Closed
rebornwwp opened this issue Mar 26, 2017 · 1 comment
Closed

calculate_correlation_matrix #14

rebornwwp opened this issue Mar 26, 2017 · 1 comment

Comments

@rebornwwp
Copy link

I use the calculate_correlation_matrix, and the result is greater than 1. is there any wrong?

it is convariance_matrix = (1 / (n_sample - 1)) * (X - X.mean(0)).T.dot(Y - Y.mean(0))?
is there some meaningful thing when the relation is greater than 1?

every function called has been tested. I think all are right.
x = np.array(np.random.random((10, 4))) print(calculate_variance(x)) print(calculate_std_dev(x)) print(calculate_convariance_matrix(x)) print(calculate_correlation_matrix(x))

the result is

[ 0.06257289 0.0854199 0.08613444 0.04435951]

[ 0.25014574 0.29226683 0.2934867 0.21061698]

[[ 0.06952543 -0.00037733 0.00416579 0.01641681]
[-0.00037733 0.094911 -0.02781782 0.0075379 ]
[ 0.00416579 -0.02781782 0.09570494 0.02909376]
[ 0.01641681 0.0075379 0.02909376 0.04928835]]

[[ 1.11111111 -0.00516115 0.05674344 0.31160352]
[-0.00516115 1.11111111 -0.32430615 0.1224553 ]
[ 0.05674344 -0.32430615 1.11111111 0.47067173]
[ 0.31160352 0.1224553 0.47067173 1.11111111]]

@eriklindernoren
Copy link
Owner

eriklindernoren commented Mar 26, 2017

Yes, the diagonal values in the correlation matrix should be 1. I have compared my methods to the corresponding numpy implementations, and the scaling factor in calculating the correlation matrix should have been 1/N instead of 1/(N-1) (used in calculating the covariance matrix).

The latest commit fixed this.

> import numpy as np
> from data_operation import *
> x = np.array(np.random.random((10, 4)))
> calculate_correlation_matrix(x)
array([[ 1. , 0.18390596, 0.34851226, 0.83499889],
[ 0.18390596, 1. , 0.27027579, 0.44432891],
[ 0.34851226, 0.27027579, 1. , 0.52136316],
[ 0.83499889, 0.44432891, 0.52136316, 1. ]])
> np.corrcoef(x, rowvar=0)
array([[ 1. , 0.18390596, 0.34851226, 0.83499889],
[ 0.18390596, 1. , 0.27027579, 0.44432891],
[ 0.34851226, 0.27027579, 1. , 0.52136316],
[ 0.83499889, 0.44432891, 0.52136316, 1. ]])
> calculate_covariance_matrix(x)
array([[ 0.11678785, 0.019298 , 0.0229638 , 0.07239497],
[ 0.019298 , 0.09428342, 0.01600117, 0.03461353],
[ 0.0229638 , 0.01600117, 0.03717523, 0.02550297],
[ 0.07239497, 0.03461353, 0.02550297, 0.06436463]])
> np.cov(x, rowvar=0)
array([[ 0.11678785, 0.019298 , 0.0229638 , 0.07239497],
[ 0.019298 , 0.09428342, 0.01600117, 0.03461353],
[ 0.0229638 , 0.01600117, 0.03717523, 0.02550297],
[ 0.07239497, 0.03461353, 0.02550297, 0.06436463]])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants