deps/kb_meme/meme-5.0.1/src/regress.c
/******************************************************************************
*	pearson_correlation
*	
*	Returns the sample Pearson correlation coefficient of two sets of points	
*	and computes an estimate of its significance (that it is large, one-tailed) 
*	using the Fisher transform.  Also computes the regression line
*	and its mean-squared error unless those variables are null.
*
******************************************************************************/
pearson_correlation
Returns the sample Pearson correlation coefficient of two sets of points
and computes an estimate of its significance (that it is large, one-tailed)
using the Fisher transform.  Also computes the regression line
and its mean-squared error unless those variables are null.
pearson_correlation Returns the sample Pearson correlation coefficient of two sets of points and computes an estimate of its significance ( that it is large one - tailed ) using the Fisher transform . Also computes the regression line and its mean - squared error unless those variables are null .
double pearson_correlation(
  int n,			
  double *x,			
  double *y,			
  double *m,			
  double *b,			
  double *mse,			
  double *log_pv 		
)
{
  int i;
  double sx=0, sy=0, sxy=0, sxx=0, syy=0; 
  double r=0;		
  double z=0;		
  for (i=0; i<n; i++) {
    sx += x[i];
    sy += y[i];
    sxy += x[i]*y[i];
    sxx += x[i]*x[i];
    syy += y[i]*y[i];
  }
  if (m != NULL && b != NULL) {
    double denom = n*sxx - sx*sx;
    if (denom != 0) {
      *m = (n*sxy - sx*sy) / denom;
      *b = (sy - *m*sx) / n;
    } else {
      *m = 0;
      *b = 0;
    }
    if (mse != NULL) {
      for (i=0; i<n; i++) {
	double err = y[i] - (*m*x[i] + *b);
	*mse += err * err;
      }
      *mse = sqrt(*mse);
      *mse /= n;
    }
  }
  double denom = (n*sxx - sx*sx) * (n*syy - sy*sy);
  r = denom == 0 ? 0 : (n*sxy - sx*sy) / sqrt(denom);
  if (n < 3) {
    *log_pv = 0;		
  } else if (r < 1) {
    z = 0.5*sqrt(n-3)*log((1+r)/(1-r));
    *log_pv = log(0.5) + log_erfc(z/sqrt(2));	
  } else {
    *log_pv = -BIG;		
  }
  return r;
}
double pearson_correlation ( int n , double * x , double * y , double * m , double * b , double * mse , double * log_pv ) { int i ; double sx = 0 , sy = 0 , sxy = 0 , sxx = 0 , syy = 0 ; double r = 0 ; double z = 0 ; for ( i = 0 ; i < n ; i ++ ) { sx += x [ i ] ; sy += y [ i ] ; sxy += x [ i ] * y [ i ] ; sxx += x [ i ] * x [ i ] ; syy += y [ i ] * y [ i ] ; } if ( m != NULL && b != NULL ) { double denom = n * sxx - sx * sx ; if ( denom != 0 ) { * m = ( n * sxy - sx * sy ) / denom ; * b = ( sy - * m * sx ) / n ; } else { * m = 0 ; * b = 0 ; } if ( mse != NULL ) { for ( i = 0 ; i < n ; i ++ ) { double err = y [ i ] - ( * m * x [ i ] + * b ) ; * mse += err * err ; } * mse = sqrt ( * mse ) ; * mse /= n ; } } double denom = ( n * sxx - sx * sx ) * ( n * syy - sy * sy ) ; r = denom == 0 ? 0 : ( n * sxy - sx * sy ) / sqrt ( denom ) ; if ( n < 3 ) { * log_pv = 0 ; } else if ( r < 1 ) { z = 0.5 * sqrt ( n - 3 ) * log ( ( 1 + r ) / ( 1 - r ) ) ; * log_pv = log ( 0.5 ) + log_erfc ( z / sqrt ( 2 ) ) ; } else { * log_pv = - BIG ; } return r ; }
pearson_correlation
Returns the sample Pearson correlation coefficient of two sets of points
and computes an estimate of its significance (that it is large, one-tailed)
using the Fisher transform.
pearson_correlation Returns the sample Pearson correlation coefficient of two sets of points and computes an estimate of its significance ( that it is large one - tailed ) using the Fisher transform .
/* number of points */ /* x values */ /* y values */ /* slope */ /* intercept */ /* mean-squared error */ /* log of Fisher transform p-value */ // correlation // Fisher transform of r // Get regression line. // Get mean-squared error. // Pearson sample correlation coefficient // Estimate the signficance. // Can't estimate on fewer than 3 points. // Right tail of normal. // p-value is 0