Distance.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. namespace VisualMath.Accord.Math
  2. {
  3. /// <summary>
  4. /// Static class Distance. Defines a set of extension methods defining distance measures.
  5. /// </summary>
  6. ///
  7. public static class Distance
  8. {
  9. /// <summary>
  10. /// Gets the Square Mahalanobis distance between two points.
  11. /// </summary>
  12. /// <param name="x">A point in space.</param>
  13. /// <param name="y">A point in space.</param>
  14. /// <param name="precision">
  15. /// The inverse of the covariance matrix of the distribution for the two points x and y.
  16. /// </param>
  17. /// <returns>The Square Mahalanobis distance between x and y.</returns>
  18. public static double SquareMahalanobis(this double[] x, double[] y, double[,] precision)
  19. {
  20. double[] d = new double[x.Length];
  21. for (int i = 0; i < x.Length; i++)
  22. d[i] = x[i] - y[i];
  23. return d.InnerProduct(precision.Multiply(d));
  24. }
  25. /// <summary>
  26. /// Gets the Mahalanobis distance between two points.
  27. /// </summary>
  28. /// <param name="x">A point in space.</param>
  29. /// <param name="y">A point in space.</param>
  30. /// <param name="precision">
  31. /// The inverse of the covariance matrix of the distribution for the two points x and y.
  32. /// </param>
  33. /// <returns>The Mahalanobis distance between x and y.</returns>
  34. public static double Mahalanobis(this double[] x, double[] y, double[,] precision)
  35. {
  36. return System.Math.Sqrt(SquareMahalanobis(x, y, precision));
  37. }
  38. /// <summary>
  39. /// Gets the Manhattan distance between two points.
  40. /// </summary>
  41. /// <param name="x">A point in space.</param>
  42. /// <param name="y">A point in space.</param>
  43. /// <returns>The manhattan distance between x and y.</returns>
  44. public static double Manhattan(this double[] x, double[] y)
  45. {
  46. double sum = 0.0;
  47. for (int i = 0; i < x.Length; i++)
  48. sum += System.Math.Abs(x[i] - y[i]);
  49. return sum;
  50. }
  51. /// <summary>
  52. /// Gets the Square Euclidean distance between two points.
  53. /// </summary>
  54. /// <param name="x">A point in space.</param>
  55. /// <param name="y">A point in space.</param>
  56. /// <returns>The Square Euclidean distance between x and y.</returns>
  57. public static double SquareEuclidean(this double[] x, double[] y)
  58. {
  59. double d = 0.0, u;
  60. for (int i = 0; i < x.Length; i++)
  61. {
  62. u = x[i] - y[i];
  63. d += u * u;
  64. }
  65. return d;
  66. }
  67. /// <summary>
  68. /// Gets the Euclidean distance between two points.
  69. /// </summary>
  70. /// <param name="x">A point in space.</param>
  71. /// <param name="y">A point in space.</param>
  72. /// <returns>The Euclidean distance between x and y.</returns>
  73. public static double Euclidean(this double[] x, double[] y)
  74. {
  75. return System.Math.Sqrt(SquareEuclidean(x, y));
  76. }
  77. /// <summary>
  78. /// Gets the Modulo-m distance between two integers a and b.
  79. /// </summary>
  80. public static int Modular(int a, int b, int modulo)
  81. {
  82. return System.Math.Min(Tools.Mod(a - b, modulo), Tools.Mod(b - a, modulo));
  83. }
  84. /// <summary>
  85. /// Bhattacharyya distance between two normalized histograms.
  86. /// </summary>
  87. /// <param name="histogram1">A normalized histogram.</param>
  88. /// <param name="histogram2">A normalized histogram.</param>
  89. /// <returns>The Bhattacharya distance between the two histograms.</returns>
  90. public static double Bhattacharyya(double[] histogram1, double[] histogram2)
  91. {
  92. int bins = histogram1.Length; // histogram bins
  93. double b = 0; // Bhattacharyya's coefficient
  94. for (int i = 0; i < bins; i++)
  95. b += System.Math.Sqrt(histogram1[i]) * System.Math.Sqrt(histogram2[i]);
  96. // bhattacharyya distance between the two distributions
  97. return System.Math.Sqrt(1.0 - b);
  98. }
  99. /// <summary>
  100. /// Bhattacharyya distance between two gaussian distributions.
  101. /// </summary>
  102. /// <param name="mean1">Mean for the first distribution.</param>
  103. /// <param name="sigma1">Covariance matrix for the first distribution.</param>
  104. /// <param name="mean2">Mean for the second distribution.</param>
  105. /// <param name="sigma2">Covariance matrix for the second distribution.</param>
  106. /// <returns>The Bhattacharia distance between the two distributions.</returns>
  107. public static double Bhattacharyya(double[] mean1, double[,] sigma1, double[] mean2, double[,] sigma2)
  108. {
  109. int n = sigma1.GetLength(0);
  110. // P = (sigma1+sigma2)/2
  111. double[,] P = new double[n, n];
  112. for (int i = 0; i < n; i++)
  113. for (int j = 0; j < n; j++)
  114. P[i, j] = (sigma1[i, j] + sigma2[i, j]) / 2.0;
  115. double detP = P.Determinant();
  116. double detP1 = sigma1.Determinant();
  117. double detP2 = sigma2.Determinant();
  118. return (1.0 / 8.0) * SquareMahalanobis(mean2, mean1, Matrix.Inverse(P))
  119. + (0.5) * System.Math.Log(detP / System.Math.Sqrt(detP1 * detP2));
  120. }
  121. }
  122. }