Commit 234adb4c authored by Antoine Lucas's avatar Antoine Lucas
Browse files

make correlation distance robust

parent 3437feb9
......@@ -2,7 +2,7 @@
* \brief all functions requiered for R dist function and C hcluster function.
*
* \date Created: probably in 1995
* \date Last modified: Time-stamp: <2011-11-08 21:06:41 antoine>
* \date Last modified: Time-stamp: <2012-01-08 12:16:41 antoine>
*
* \author R core members, and lately: Antoine Lucas
*
......@@ -395,7 +395,7 @@ template<class T> T distance_T<T>::R_correlation(double * x, double * y , int n
int i1, int i2,
int * flag, T_tri & opt)
{
T num,denum,sumx,sumy,sumxx,sumyy,sumxy;
T num,denum2,sumx,sumy,sumxx,sumyy,sumxy;
int count,j;
count= 0;
......@@ -424,8 +424,12 @@ template<class T> T distance_T<T>::R_correlation(double * x, double * y , int n
return NA_REAL;
}
num = sumxy - ( sumx*sumy /count );
denum = sqrt( (sumxx - (sumx*sumx /count ) )* (sumyy - (sumy*sumy /count ) ) );
return 1 - (num / denum);
denum2 = (sumxx - (sumx*sumx /count ) )* (sumyy - (sumy*sumy /count ) ) ;
if(denum2 <=0)
{
return 0;
}
return 1 - (num / sqrt(denum2));
}
/** \brief Absolute Distance correlation (Uncentered Pearson)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment