コード例 #1
0
ファイル: fraud.hpp プロジェクト: hmm34/fraud
// Calculates the correlation coefficient for two lists of numbers in an array
// in O(5n)
double correlation_coefficient(double a[], double b[], int n) {
  double arithmetic_meanA = arithmetic_mean(a, n);                  // O(n)
  double arithmetic_meanB = arithmetic_mean(b, n);                  // O(n)
  double stddevA = standard_deviation(a, n);  // O(2n)
  double stddevB = standard_deviation(b, n);  // O(2n)
  double total = 0;
  for (int i = 0; i < n; ++i) {               // O(n)
    total += ((a[i] - arithmetic_meanA) / stddevA) * ((b[i] - arithmetic_meanB) / stddevB);
  }
  return total / (double) (n - 1);
}
コード例 #2
0
ファイル: fraud.hpp プロジェクト: hmm34/fraud
Value_type<I> correlation_coefficient(I firstA, I lastA,
                                      I firstB, I lastB, T id) {
  typedef Value_type<I> R;
  R arithmetic_meanA = arithmetic_mean(firstA, lastA, id);
  R arithmetic_meanB = arithmetic_mean(firstB, lastB, id);
  R stddevA = standard_deviation(firstA, lastA, id);
  R stddevB = standard_deviation(firstB, lastB, id);
  R count = R(id);
  R total = R(id);
  while (firstA != lastA) {
    total += ((*firstA - arithmetic_meanA) / stddevA) * ((*firstB - arithmetic_meanB) / stddevB);
    ++firstA;
    ++firstB;
    ++count;
  }
  return total / (count - R(1));
}
コード例 #3
0
ファイル: fraud.hpp プロジェクト: hmm34/fraud
// Calculates the variance in a list of numbers in an array in O(2n)
double variance(double a[], int n) {
  double m = arithmetic_mean(a, n);         // O(n)
  double sumSquares = 0;
  for (int i = 0; i < n; ++i) {  // O(n)
    double diff = a[i] - m;
    sumSquares += (diff * diff);
  }
  return sumSquares / (double) (n - 1);
}
コード例 #4
0
ファイル: fraud.hpp プロジェクト: hmm34/fraud
Value_type<I> variance(I first, I last, T id) {
  typedef Value_type<I> R;
  R count = R(id);
  R var = R(id);
  R m = arithmetic_mean(first, last, id);
  while (first != last) {
    R diff = (*first) - m;
    var += (diff * diff);
    ++count;
    ++first;
  }
  return var / (count - R(1));
}
コード例 #5
0
ファイル: stats.hpp プロジェクト: zoujiaqing/origin
 inline S
 generalized_mean(I first, I last, T p) {
   // Check for well known cases.
   if (p == -1)
     return harmonic_mean(first, last);
   if (p == 0)
     return geometric_mean(first, last);
   if (p == 1)
     return arithmetic_mean(first, last);
   if (p == 2)
     return quadratic_mean(first, last);
   if (p == 3)
     return cubic_mean(first, last);
   
   // Either handle the infinity case or compute the generalized
   // value.
   if (is_infinity(p))
     return generalized_mean_inf(first, last, p);
   else
     return generalized_mean_non_inf(first, last, p);
 }