From 075513c093939df211de489167b93b8a1a04b0f7 Mon Sep 17 00:00:00 2001 From: Mostafa Ali Date: Mon, 9 Mar 2015 12:06:59 -0600 Subject: [PATCH 1/3] Adding Minkowski Distance to the histogram class, it can be used to compare two histograms --- src/Numerics/Statistics/Histogram.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Numerics/Statistics/Histogram.cs b/src/Numerics/Statistics/Histogram.cs index 5d9de518..6345ff71 100644 --- a/src/Numerics/Statistics/Histogram.cs +++ b/src/Numerics/Statistics/Histogram.cs @@ -467,5 +467,30 @@ namespace MathNet.Numerics.Statistics return sb.ToString(); } + + /// + /// Calculate the Minkowski Distance between two histograms + /// + /// The histogram that will be compared with this histogram + /// The power used for calculating the distance (e.g. 2 for Ecludian distance) + /// The distance between the two histograms + public double MinkowskiDistance(Histogram h, int power) + { + if (this.BucketCount != h.BucketCount) + { + throw new ArgumentException("The two histograms must have the same number of buckets"); + } + if (power < 1) + { + throw new ArgumentException("The power must be greater than zero"); + } + double dist = 0; + for (int i = 0; i < this.BucketCount; i++) + { + dist += Math.Pow(Math.Abs(this[i].Count - h[i].Count), power); + } + double f = 1 / power; + return Math.Pow(dist, f); + } } } From 98a060ed4ac0b52ee290a26025c3fa677e7dd0c1 Mon Sep 17 00:00:00 2001 From: Mostafa Ali Date: Tue, 30 Jun 2015 16:46:33 -0600 Subject: [PATCH 2/3] Use Delegate for histogram distance --- src/Numerics/Statistics/Histogram.cs | 40 +++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Numerics/Statistics/Histogram.cs b/src/Numerics/Statistics/Histogram.cs index 6345ff71..5ec4c4e1 100644 --- a/src/Numerics/Statistics/Histogram.cs +++ b/src/Numerics/Statistics/Histogram.cs @@ -484,13 +484,45 @@ namespace MathNet.Numerics.Statistics { throw new ArgumentException("The power must be greater than zero"); } - double dist = 0; + double[] a1 = new double[this.BucketCount]; + double[] a2 = new double[this.BucketCount]; for (int i = 0; i < this.BucketCount; i++) { - dist += Math.Pow(Math.Abs(this[i].Count - h[i].Count), power); + a1[i] = this[i].Count; + a2[i] = h[i].Count; } - double f = 1 / power; - return Math.Pow(dist, f); + return Distance.Minkowski(2, a1, a2); + } + + public double HistogramDistance(Histogram h, double power, Func distanceFunction){ + if (this.BucketCount != h.BucketCount) + { + throw new ArgumentException("The two histograms must have the same number of buckets"); + } + double[] a1 = new double[this.BucketCount]; + double[] a2 = new double[this.BucketCount]; + for (int i = 0; i < this.BucketCount; i++) + { + a1[i] = this[i].Count; + a2[i] = h[i].Count; + } + return distanceFunction(power, a1, a2); + } + + public double HistogramDistance(Histogram h, Func distanceFunction) + { + if (this.BucketCount != h.BucketCount) + { + throw new ArgumentException("The two histograms must have the same number of buckets"); + } + double[] a1 = new double[this.BucketCount]; + double[] a2 = new double[this.BucketCount]; + for (int i = 0; i < this.BucketCount; i++) + { + a1[i] = this[i].Count; + a2[i] = h[i].Count; + } + return distanceFunction(a1, a2); } } } From 70b1330253d6545d90cc95369299a19116e9e8da Mon Sep 17 00:00:00 2001 From: Mostafa Ali Date: Tue, 30 Jun 2015 16:51:06 -0600 Subject: [PATCH 3/3] Use Delegate for histogram distance --- src/Numerics/Statistics/Histogram.cs | 29 ++++++++-------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/Numerics/Statistics/Histogram.cs b/src/Numerics/Statistics/Histogram.cs index 5ec4c4e1..3de073c5 100644 --- a/src/Numerics/Statistics/Histogram.cs +++ b/src/Numerics/Statistics/Histogram.cs @@ -469,31 +469,12 @@ namespace MathNet.Numerics.Statistics } /// - /// Calculate the Minkowski Distance between two histograms + /// Calculate the Distance between two histograms /// /// The histogram that will be compared with this histogram /// The power used for calculating the distance (e.g. 2 for Ecludian distance) + /// A delegate for the distance function /// The distance between the two histograms - public double MinkowskiDistance(Histogram h, int power) - { - if (this.BucketCount != h.BucketCount) - { - throw new ArgumentException("The two histograms must have the same number of buckets"); - } - if (power < 1) - { - throw new ArgumentException("The power must be greater than zero"); - } - double[] a1 = new double[this.BucketCount]; - double[] a2 = new double[this.BucketCount]; - for (int i = 0; i < this.BucketCount; i++) - { - a1[i] = this[i].Count; - a2[i] = h[i].Count; - } - return Distance.Minkowski(2, a1, a2); - } - public double HistogramDistance(Histogram h, double power, Func distanceFunction){ if (this.BucketCount != h.BucketCount) { @@ -509,6 +490,12 @@ namespace MathNet.Numerics.Statistics return distanceFunction(power, a1, a2); } + /// + /// Calculate the Distance between two histograms + /// + /// The histogram that will be compared with this histogram + /// A delegate for the distance function + /// The distance between the two histograms public double HistogramDistance(Histogram h, Func distanceFunction) { if (this.BucketCount != h.BucketCount)