Correlation matrix in NumPy with NaN's

correlation, matrix, numpy, python

Solution

leewangzhong (in the comment) is correct, Pearson's r is not defined for constant timeseries, as their standard deviation is zero. Thanks!

Problem

A have a `n x m` matrix in which row `i` represents the timeseries of the variable `V_i`. I would like to compute the `n x n` correlation matrix `M`, where `M_{i,j}` contains the correlation coefficient (Pearson's r) between `V_i` and `V_j`. However, when I try the following in numpy: ``` numpy.corrcoef(numpy.matrix('5 6 7; 1 1 1')) ``` I get the following output: ``` array([[ 1., nan], [ nan, nan]]) ``` It seems that `numpy.corrcoef` doesn't like unit vectors, because if I change the second row to `7 6 5`, I get the expected result: ``` array([[ 1., -1.], [ -1., 1.]]) ``` What is the reason for this kind of behavior of `numpy.corrcoef`?

Original source