home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 41 / IOPROG_41.ISO / soft / c++ / NUMCPP11.ZIP / correlat.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-09  |  3.8 KB  |  121 lines

  1. //===================================================================
  2. // correlat.hpp
  3. //
  4. // Version 1.1
  5. //
  6. // Written by:
  7. //   Brent Worden
  8. //   WordenWare
  9. //   email:  Brent@Worden.org
  10. //
  11. // Copyright (c) 1998-1999 WordenWare
  12. // 
  13. // Created:  August 28, 1998
  14. // Revised:  April 10, 1999
  15. //===================================================================
  16.  
  17. #ifndef _CORRELAT_HPP_
  18. #define _CORRELAT_HPP_
  19.  
  20. #include "descript.hpp"
  21. #include "ranking.hpp"
  22. #include "vector.hpp"
  23.  
  24. NUM_BEGIN
  25.  
  26. template<class Iter1, class Iter2>
  27. inline double covariance(Iter1 first1, Iter1 last1, Iter2 first2)
  28. //-------------------------------------------------------------------
  29. // Returns the covariance of the corresponding elements in
  30. // [first1, last1) and [first2, first2 + (last1 - first1) ).
  31. //-------------------------------------------------------------------
  32. {
  33.     return sumproduct(first1, last1, first2) / length(first1, last1);
  34. }
  35.  
  36. template<class Iter1, class Iter2>
  37. inline double correlation(Iter1 first1, Iter1 last1, Iter2 first2)
  38. //-------------------------------------------------------------------
  39. // Returns the Pearson's product moment correlation coefficient of
  40. // the corresponding elements in [first1, last1) and [first2,
  41. // first2 + (last1 - first1) ).
  42. //-------------------------------------------------------------------
  43. {
  44.     return sumproduct(first1, last1, first2) / sqrt(sumsquare(first1, last1) *
  45.         sumsquare(first2, first2 + (last1 - first1)));
  46. };
  47.  
  48. template<class Iter1, class Iter2>
  49. double kendall(Iter1 first1, Iter1 last1, Iter2 first2)
  50. //-------------------------------------------------------------------
  51. // Returns Kenall's Tau correlation coefficient of the corresponding
  52. // values in [first1, last1) and [first2, first2 + (last1 - first1)).
  53. //-------------------------------------------------------------------
  54. {
  55.     int nc, nd, ey, ex;
  56.     int i, j;
  57.     int n = length(first1, last1);
  58.     
  59.     nc = nd = ey = ex = 0;
  60.     
  61.     for(i = 0; i < n; ++i){
  62.         for(j = 0; j < i; ++j){
  63.             if(*(first1 + i) < *(first1 + j)){
  64.                 if(*(first2 + i) < *(first2 + j)){
  65.                     ++nc;
  66.                 } else if(*(first2 + i) > *(first2 + j)){
  67.                     ++nd;
  68.                 } else {
  69.                     ++ex;
  70.                 }
  71.             } else if(*(first1 + i) > *(first1 + j)){
  72.                 if(*(first2 + i) < *(first2 + j)){
  73.                     ++nd;
  74.                 } else if(*(first2 + i) > *(first2 + j)){
  75.                     ++nc;
  76.                 } else {
  77.                     ++ex;
  78.                 }
  79.             } else {
  80.                 ++ey;
  81.                 if(*(first2 + i) == *(first2 + j)){
  82.                     ++ex;
  83.                 }
  84.             }
  85.         }
  86.     }
  87.     
  88.     return (double)(nc - nd) / sqrt((double)((nc+nd+ey)*(nc+nd+ex)));
  89. };
  90.  
  91. template<class Iter1, class Iter2>
  92. double spearman(Iter1 first1, Iter1 last1, Iter2 first2)
  93. //-------------------------------------------------------------------
  94. // Returns Spearman's correlation coefficient of the corresponding
  95. // values in [first1, last1) and [first2, first2 + (last1 - first1)).
  96. // Note:  Ties in the values are not accounted for.
  97. //-------------------------------------------------------------------
  98. {
  99.     int n = length(first1, last1);
  100.     Vector<double> r1(n);
  101.     Vector<double> r2(n);
  102.     
  103.     rank(first1, last1, r1.begin());
  104.     rank(first2, first2 + n, r2.begin());
  105.     
  106.     return correlation(r1.begin(), r1.end(), r2.begin());
  107. };
  108.  
  109. NUM_END
  110.  
  111. #endif
  112.  
  113. //===================================================================
  114. // Revision History
  115. //
  116. // Version 1.0 - 08/28/1998 - New.
  117. // Version 1.1 - 04/10/1999 - Added Numerics namespace.
  118. //                            Replaced std::vector with num::Vector.
  119. //===================================================================
  120.  
  121.