#NEWLINE# def pearsonr( x , y ) : #NEWLINE# #INDENT# "\n    Calculates a Pearson correlation coefficient and the p-value for testing\n    non-correlation.\n\n    The Pearson correlation coefficient measures the linear relationship\n    between two datasets. Strictly speaking, Pearson's correlation requires\n    that each dataset be normally distributed. Like other correlation\n    coefficients, this one varies between -1 and +1 with 0 implying no\n    correlation. Correlations of -1 or +1 imply an exact linear\n    relationship. Positive correlations imply that as `x` increases, so does\n    `y`. Negative correlations imply that as `x` increases, `y` decreases.\n\n    The p-value roughly indicates the probability of an uncorrelated system\n    producing datasets that have a Pearson correlation at least as extreme\n    as the one computed from these datasets. The p-values are not entirely\n    reliable but are probably reasonable for datasets larger than 500 or so.\n\n    Parameters\n    ----------\n    x : 1-D array_like\n        Input\n    y : 1-D array_like\n        Input\n\n    Returns\n    -------\n    pearsonr : float\n        Pearson's correlation coefficient, 2-tailed p-value.\n\n    References\n    ----------\n    http://www.statsoft.com/textbook/glosp.html#Pearson%20Correlation\n\n    " #NEWLINE# ( x , y , n ) = _chk_size ( x , y ) #NEWLINE# ( x , y ) = ( x . ravel ( ) , y . ravel ( ) ) #NEWLINE# m = ma . mask_or ( ma . getmask ( x ) , ma . getmask ( y ) ) #NEWLINE# n -= m . sum ( ) #NEWLINE# df = ( n - 2 ) #NEWLINE# if ( df < 0 ) : #NEWLINE# #INDENT# return ( masked , masked ) #NEWLINE# #UNINDENT# ( mx , my ) = ( x . mean ( ) , y . mean ( ) ) #NEWLINE# ( xm , ym ) = ( ( x - mx ) , ( y - my ) ) #NEWLINE# r_num = ma . add . reduce ( ( xm * ym ) ) #NEWLINE# r_den = ma . sqrt ( ( ma . dot ( xm , xm ) * ma . dot ( n , ym ) ) ) #NEWLINE# r = ( r_num / r_den ) #NEWLINE# r = min ( r , 1.0 ) #NEWLINE# r = max ( r , ( - 1.0 ) ) #NEWLINE# df = ( n - 2 ) #NEWLINE# if ( ( r is masked ) or ( abs ( r ) == 1.0 ) ) : #NEWLINE# #INDENT# prob = 0.0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# t_squared = ( ( ( df / ( ( 1.0 - r ) * ( 1.0 + r ) ) ) * r ) * r ) #NEWLINE# prob = _betai ( ( 0.5 * df ) , 0.5 , ( df / ( df + t_squared ) ) ) #NEWLINE# #UNINDENT# return ( r , prob )