private Classification <UnsignedPoint, string> TestData(int[] clusterSizes, int dimensions, int maxCoordinate) { var clusterCount = clusterSizes.Length; var minClusterSize = clusterSizes.Min(); var maxClusterSize = clusterSizes.Max(); var data = new GaussianClustering { ClusterCount = clusterCount, Dimensions = dimensions, MaxCoordinate = maxCoordinate, MinClusterSize = minClusterSize, MaxClusterSize = maxClusterSize, ClusterSizes = clusterSizes }; return(data.MakeClusters()); }
UnsignedPoint[] TestData(int numPoints, int dimensions, int clusterCount, int maxCoordinate, int minStdDeviation, int maxStdDeviation, out int bitsPerDimension) { var avgClusterSize = numPoints / clusterCount; var data = new GaussianClustering { ClusterCount = clusterCount, Dimensions = dimensions, MaxCoordinate = maxCoordinate, MinClusterSize = avgClusterSize - 100, MaxClusterSize = avgClusterSize + 100, MaxDistanceStdDev = maxStdDeviation, MinDistanceStdDev = minStdDeviation }; var clusters = data.MakeClusters(); var points = clusters.Points().ToArray(); bitsPerDimension = (maxCoordinate + 1).SmallestPowerOfTwo(); return(points); }
public void DensityCorrelation() { var bitsPerDimension = 10; var data = new GaussianClustering { ClusterCount = 50, Dimensions = 100, MaxCoordinate = (1 << bitsPerDimension) - 1, MinClusterSize = 100, MaxClusterSize = 500 }; var expectedClusters = data.MakeClusters(); var hIndex = new HilbertIndex(expectedClusters, bitsPerDimension); var cc = new ClusterCounter { NoiseSkipBy = 10, OutlierSize = 5, ReducedNoiseSkipBy = 1 }; var count = cc.Count(hIndex.SortedPoints); // Choice of neighborhoodDistance is crucial. // - If it is too large, then a huge number of neighbors will be caught up in the dragnet, and estimating // that value with a window into the Hilbert curve will yield poor results. Why? If there are 200 neighbors // and your window size is 100 then many points will have their neighbor count saturate near 100 and // no meaningful variation in density will be found. // - If it is too small, then too few neighbors (or none!) will be found, and we get no meaningful density. // - We know that almost every point has two neighbors within MaximumSquareDistance, so we should // make it smaller than MaximumSquareDistance. var neighborhoodDistance = count.MaximumSquareDistance * 2 / 5; var numPoints = hIndex.SortedPoints.Count; var windowRadius = (int)Math.Sqrt(numPoints / 2); var dMeter = new DensityMeter(hIndex, neighborhoodDistance, windowRadius); Func <HilbertPoint, long> exactMetric = p => (long)dMeter.ExactNeighbors(p); Func <HilbertPoint, long> estimatedMetric = p => (long)dMeter.EstimatedDensity(p, windowRadius); var correlator = new KendallTauCorrelation <HilbertPoint, long>(exactMetric, estimatedMetric); var correlation = correlator.TauB(hIndex.SortedPoints.Take(1000)); Console.WriteLine($"Correlation between exact and estimated density is: {correlation}"); Assert.GreaterOrEqual(correlation, 0.90, $"Correlation {correlation} is not high enough"); }
public void ClosestOfFiftyClusters() { int hilbertTries = 1000; var correctColorCount = 0; var correctCrosscheckCount = 0; var correctDistanceCount = 0; var nPoints = 100; var dimensions = 100; var clusterCount = 50; var data = new GaussianClustering { ClusterCount = clusterCount, Dimensions = dimensions, MaxCoordinate = 1000, MinClusterSize = nPoints, MaxClusterSize = nPoints }; var closestExact = new PolyChromaticClosestPoint <string> .ClosestPair(); var closestApproximate = new PolyChromaticClosestPoint <string> .ClosestPair(); var bitsPerDimension = (1 + data.MaxCoordinate).SmallestPowerOfTwo(); var clusters = data.MakeClusters(); Assert.AreEqual(clusterCount, clusters.NumPartitions, "Test data are grouped into fewer clusters than requested."); PolyChromaticClosestPoint <string> pccp; if (hilbertTries <= 1) { pccp = new PolyChromaticClosestPoint <string>(clusters); } else { var reducedNoiseSkipBy = 1; var results = OptimalIndex.Search( clusters.Points().Select(up => HilbertPoint.CastOrConvert(up, bitsPerDimension, true)).ToList(), 5 /*outlier size */, 10 /* NoiseSkipBy */, reducedNoiseSkipBy, hilbertTries ); pccp = new PolyChromaticClosestPoint <string>(clusters, results.Index); } foreach (var color in pccp.Clusters.ClassLabels()) { var exact = pccp.FindClusterExhaustively(color); var approximate = pccp.FindClusterApproximately(color); var crosscheck = pccp.FindClusterIteratively(color); if (exact.SquareDistance >= approximate.SquareDistance) { correctDistanceCount++; } if (exact.Color2.Equals(approximate.Color2)) { correctColorCount++; } if (exact.Color2.Equals(crosscheck.Color2)) { correctCrosscheckCount++; } if (exact.SquareDistance < closestExact.SquareDistance) { closestExact = exact; } if (approximate.SquareDistance < closestApproximate.SquareDistance) { closestApproximate = approximate; } var ratio = approximate.SquareDistance / (double)exact.SquareDistance; Console.WriteLine(string.Format("Exact {0} vs Approx. {1} vs Cross {2}. Over by {3:N3}%", exact, approximate, crosscheck, (ratio - 1.0) * 100.0)); } if (closestExact.SquareDistance >= closestApproximate.SquareDistance) { Console.WriteLine("DID FIND the closest pair of points overall. Exact {0}. Approx {1}", closestExact, closestApproximate); } else { Console.WriteLine("DID NOT FIND the closest pair of points overall. Exact {0}. Approx {1}", closestExact, closestApproximate); } Assert.IsTrue(correctColorCount == clusterCount && correctDistanceCount == clusterCount, string.Format("Of {0} clusters, only {1} searches found the closest cluster and {2} found the shortest distance. Crosscheck = {3}", clusterCount, correctColorCount, correctDistanceCount, correctCrosscheckCount ) ); }
public void ClosestClusterTest(int nPoints, int dimensions, int numClusters, int numCurvesToTry, int numCurvesToKeep) { var correctColorCount = 0; var correctDistanceCount = 0; var data = new GaussianClustering { ClusterCount = numClusters, Dimensions = dimensions, MaxCoordinate = 1000, MinClusterSize = nPoints, MaxClusterSize = nPoints }; var closestExact = new PolyChromaticClosestPoint <string> .ClosestPair(); var closestApproximate = new PolyChromaticClosestPoint <string> .ClosestPair(); var clusters = data.MakeClusters(); var pccps = new List <PolyChromaticClosestPoint <string> >(); var bitsPerDimension = (1 + data.MaxCoordinate).SmallestPowerOfTwo(); var bestIndices = OptimalIndex.SearchMany( clusters.Points().Select(up => HilbertPoint.CastOrConvert(up, bitsPerDimension, true)).ToList(), numCurvesToKeep, 5 /*outlier size */, 10 /* NoiseSkipBy */, 1 /* ReducedNoiseSkipBy */, numCurvesToTry ); //var pointLists = bestIndices.Select(result => result.Index.SortedPoints).ToList(); //foreach (var pList in pointLists) // pccps.Add(new PolyChromaticClosestPoint<string>(clusters, pList)); var indices = bestIndices.Select(result => result.Index).ToList(); foreach (var index in indices) { pccps.Add(new PolyChromaticClosestPoint <string>(clusters, index)); } var pccp1 = pccps[0]; foreach (var color in pccp1.Clusters.ClassLabels()) { var exact = pccp1.FindClusterExhaustively(color); var approximate = pccps.Select(pccp => pccp.FindClusterApproximately(color)).OrderBy(cp => cp).First(); if (exact.SquareDistance >= approximate.SquareDistance) { correctDistanceCount++; } if (exact.Color2.Equals(approximate.Color2)) { correctColorCount++; } if (exact.SquareDistance < closestExact.SquareDistance) { closestExact = exact; } if (approximate.SquareDistance < closestApproximate.SquareDistance) { closestApproximate = approximate; } var ratio = approximate.SquareDistance / (double)exact.SquareDistance; Console.WriteLine(string.Format("Exact {0} vs Approx. {1}. Over by {2:N3}%", exact, approximate, (ratio - 1.0) * 100.0)); } if (closestExact.SquareDistance >= closestApproximate.SquareDistance) { Console.WriteLine("DID FIND the closest pair of points overall. Exact {0}. Approx {1}", closestExact, closestApproximate); } else { Console.WriteLine("DID NOT FIND the closest pair of points overall. Exact {0}. Approx {1}", closestExact, closestApproximate); } Assert.IsTrue(correctColorCount == numClusters && correctDistanceCount == numClusters, string.Format("Of {0} clusters, only {1} searches found the closest cluster and {2} found the shortest distance.", numClusters, correctColorCount, correctDistanceCount ) ); }
/// <summary> /// A test case for PolyChromaticClosestPoint.FindPairApproximately where clusters conform to a Gaussian distribution. /// </summary> /// <param name="nPoints">Number of points in each cluster.</param> /// <param name="dimensions">Number of Dimensions in each point.</param> /// <param name="numClusters">Number of clusters to create.</param> /// <param name="hilbertsToTry">Number of randomly generated Hilbert curves to try.</param> public void GaussianPolyChromaticPairTestCase(int nPoints, int dimensions, int numClusters, int hilbertsToTry = 1) { var successes = 0; var worstRatio = 1.0; var color1 = "0"; var data = new GaussianClustering { ClusterCount = numClusters, Dimensions = dimensions, MaxCoordinate = 1000, MinClusterSize = nPoints, MaxClusterSize = nPoints }; var clusters = data.MakeClusters(); PolyChromaticClosestPoint <string> pccp; if (hilbertsToTry <= 1) { pccp = new PolyChromaticClosestPoint <string>(clusters); } else { var bitsPerDimension = (1 + data.MaxCoordinate).SmallestPowerOfTwo(); var results = OptimalIndex.Search( clusters.Points().Select(up => HilbertPoint.CastOrConvert(up, bitsPerDimension, true)).ToList(), 5 /*outlier size */, 10 /* NoiseSkipBy */, 1 /* ReducedNoiseSkipBy */, hilbertsToTry ); pccp = new PolyChromaticClosestPoint <string>(clusters, results.Index); } for (var iColor2 = 1; iColor2 < numClusters; iColor2++) { var color2 = iColor2.ToString(); var exact = pccp.FindPairExhaustively(color1, color2); var approximate = pccp.FindPairApproximately(color1, color2); var expectedDistance = exact.SquareDistance; var actualDistance = approximate.SquareDistance; if (actualDistance <= expectedDistance) { successes++; } else { worstRatio = Math.Max(worstRatio, actualDistance / (double)expectedDistance); } if (exact.SquareDistance >= approximate.SquareDistance) { Console.WriteLine("FindPairApproximately CORRECT. Exact {0}. Approx {1}", exact, approximate); } else { Console.WriteLine("FindPairApproximately INCORRECT. Exact {0}. Approx {1}. Too high by {2:N3}%", exact, approximate, 100.0 * (approximate.SquareDistance / (double)exact.SquareDistance - 1.0)); } } Assert.AreEqual(numClusters - 1, successes, string.Format("Did not succeed every time. Failed {0} of {1} times. Worst distance ratio is {2:N4}. {3} points of {4} dimensions.", numClusters - successes - 1, numClusters - 1, worstRatio, nPoints, dimensions ) ); }
public void DistanceDistribution() { /* * Percentile,By Index,By Random * ----------------------------- * 0%,111.35,146.55 * 1%,142.06,255.96 * 2%,147.21,2163.43 * 3%,151.2,2214.15 * 4%,154.06,2245.2 * 5%,156.24,2271.37 * 6%,158.38,2292.29 * 7%,160.42,2313.55 * 8%,162.29,2327.14 * 9%,164.07,2345.25 * 10%,165.41,2359.95 * 11%,166.72,2372.83 * 12%,167.99,2386.15 * 13%,169.29,2398.47 * 14%,170.43,2410.01 * 15%,171.53,2422.34 * 16%,172.48,2432.43 * 17%,173.58,2443.08 * 18%,174.73,2454.27 * 19%,175.56,2463.71 * 20%,176.35,2472.97 * 21%,177.35,2483.24 * 22%,178.3,2491.9 * 23%,179.1,2501.44 * 24%,179.82,2510.26 * 25%,180.64,2517.73 * 26%,181.55,2524.97 * 27%,182.33,2531.58 * 28%,182.98,2538.08 * 29%,183.67,2543.83 * 30%,184.33,2550.93 * 31%,185.09,2556.59 * 32%,185.7,2563.37 * 33%,186.41,2570.29 * 34%,187.09,2577.29 * 35%,187.7,2583.56 * 36%,188.43,2589.95 * 37%,189.07,2596.13 * 38%,189.71,2602.24 * 39%,190.46,2608.28 * 40%,191.08,2615.25 * 41%,191.79,2620.81 * 42%,192.46,2626.02 * 43%,193.09,2632.7 * 44%,193.71,2638.18 * 45%,194.31,2643.35 * 46%,194.98,2648.69 * 47%,195.65,2655.47 * 48%,196.3,2660.26 * 49%,196.96,2666.37 * 50%,197.66,2670.94 * 51%,198.34,2677.09 * 52%,199.07,2681.9 * 53%,199.72,2687.11 * 54%,200.3,2692.42 * 55%,201.06,2697.92 * 56%,201.71,2703.76 * 57%,202.4,2710.17 * 58%,203.16,2715.06 * 59%,203.82,2720.25 * 60%,204.51,2725.99 * 61%,205.32,2731.6 * 62%,206.08,2736.59 * 63%,206.79,2741.72 * 64%,207.58,2746.59 * 65%,208.29,2754.03 * 66%,209.07,2760.81 * 67%,209.8,2766.65 * 68%,210.68,2771.98 * 69%,211.71,2778.27 * 70%,212.38,2784.23 * 71%,213.19,2790.71 * 72%,213.92,2796.42 * 73%,214.82,2802.84 * 74%,215.68,2809.36 * 75%,216.54,2814.55 * 76%,217.48,2821.32 * 77%,218.43,2827.56 * 78%,219.35,2833.35 * 79%,220.28,2840.72 * 80%,221.33,2848.87 * 81%,222.31,2856.89 * 82%,223.42,2864 * 83%,224.46,2872.51 * 84%,225.83,2881.09 * 85%,227.06,2891.57 * 86%,228.27,2900.46 * 87%,229.63,2910.46 * 88%,231.55,2919.5 * 89%,233.59,2933.76 * 90%,235.6,2944.88 * 91%,237.25,2959.45 * 92%,239.83,2976.08 * 93%,241.88,2990.4 * 94%,244.97,3010.08 * 95%,248.23,3029.15 * 96%,252.34,3052.37 * 97%,260.68,3074.84 * 98%,282.76,3112.43 *** Note the jump from 282 to 2550, which shows that the characteristic distance is about 282. * 99%,2550.87,3170.93 * 100%,3114.89,3412.57 */ var data = new GaussianClustering { ClusterCount = 100, Dimensions = 50, MaxCoordinate = 1000, MinClusterSize = 50, MaxClusterSize = 150 }; var clusters = data.MakeClusters(); var bitsPerDimension = 10; var points = clusters.Points().Select(p => HilbertPoint.CastOrConvert(p, bitsPerDimension, true)).ToList(); var results = OptimalIndex.Search( points, 5, // outlierSize 10, // noiseSkipBy 1000, // maxTrials 4 // maxIterationsWithoutImprovement ); var pointsFromIndex = results.Index.SortedPoints; var distancesRandom = new List <long>(); var distancesHilbert = new List <long>(); var n = pointsFromIndex.Count; var rng = new FastRandom(); for (var i = 0; i < n - 1; i++) { var p1 = pointsFromIndex[i]; var p2 = pointsFromIndex[i + 1]; distancesHilbert.Add(p1.Measure(p2)); var p3 = pointsFromIndex[rng.Next(n)]; var p4 = pointsFromIndex[rng.Next(n)]; distancesRandom.Add(p3.Measure(p4)); } distancesHilbert.Sort(); distancesRandom.Sort(); Console.WriteLine("Percentile,By Index,By Random"); for (var percentile = 0; percentile <= 100; percentile++) { var i = Math.Min(n - 2, (n - 1) * percentile / 100); var distHilbert = Math.Round(Math.Sqrt(distancesHilbert[i]), 2); var distRandom = Math.Round(Math.Sqrt(distancesRandom[i]), 2); Console.Write($"{percentile}%,{distHilbert},{distRandom}"); } }
public void LowresVersusHiresCase(int numPoints, int dimensions, int clusterCount, int lowresBits) { var maxCoordinate = 1000; var clusterSizeVariation = 100; var minClusterSize = (numPoints / clusterCount) - clusterSizeVariation; var maxClusterSize = (numPoints / clusterCount) + clusterSizeVariation; var data = new GaussianClustering { ClusterCount = clusterCount, Dimensions = dimensions, MaxCoordinate = maxCoordinate, MinClusterSize = minClusterSize, MaxClusterSize = maxClusterSize }; var clusters = data.MakeClusters(); var points = clusters.Points().ToList(); PointBalancer balancer = null; var hiresSort = HilbertSort.BalancedSort(points, ref balancer); var lowresSort = HilbertSort.SortWithTies(points, lowresBits, ref balancer); var lowresPositions = new Dictionary <UnsignedPoint, int>(); var hiresPosition = new Dictionary <UnsignedPoint, int>(); foreach (var p in hiresSort.Select((p, i) => { hiresPosition[p] = i; return(p); })) { ; } foreach (var ties in lowresSort.Select((p, i) => new { Points = p, Position = i })) { foreach (var point in ties.Points) { lowresPositions[point] = ties.Position; } } // Compare the positions of many pairs of points in the two orderings to see that // they are either in the same relative order // or tied for position in the lowres ordering. var actualNumPoints = points.Count; var largestBucket = lowresSort.Select(bucket => bucket.Length).Max(); var caseDescription = $"N = {actualNumPoints} D = {dimensions} K = {clusterCount} B = {lowresBits}"; Console.WriteLine(caseDescription); Console.WriteLine($"Lowres buckets = {lowresSort.Count} Largest bucket = {largestBucket}"); int outOfPlaceCount = 0; for (var i = 0; i < actualNumPoints - 1; i++) { var p1 = points[i]; for (var j = i + 1; j < actualNumPoints; j++) { var p2 = points[j]; var lowresPosition1 = lowresPositions[p1]; var lowresPosition2 = lowresPositions[p2]; var hiresPosition1 = hiresPosition[p1]; var hiresPosition2 = hiresPosition[p2]; if (lowresPosition1 != lowresPosition2) { if (lowresPosition1 < lowresPosition2 != hiresPosition1 < hiresPosition2) { outOfPlaceCount++; } } } } var msg = $"Out of place count = {outOfPlaceCount}"; Console.WriteLine(msg); Assert.AreEqual(0, outOfPlaceCount, msg); }
/// <summary> /// UnsignedPoint.SquareDistanceCompare has an optimization. This tests how often this optimization /// can be exploited in a realistic test. The comparison will be against an estimated characteristic distance /// between points. This distance is assumed to be close enough to trigger two points to be merged into a single cluster. /// </summary> private double SquareDistanceCompareOptimizableCase(int totalComparisons, bool useExtendedOptimization = false) { // 1. Make test data. var bitsPerDimension = 10; var data = new GaussianClustering { ClusterCount = 100, Dimensions = 100, MaxCoordinate = (1 << bitsPerDimension) - 1, MinClusterSize = 50, MaxClusterSize = 150 }; var clusters = data.MakeClusters(); // 2. Create HilbertIndex for points. var hIndex = new HilbertIndex(clusters, bitsPerDimension); // 3. Deduce the characteristic distance. var counter = new ClusterCounter { OutlierSize = 5, NoiseSkipBy = 10 }; var count = counter.Count(hIndex.SortedPoints); var mergeDistance = count.MaximumSquareDistance; var longDistance = 5 * mergeDistance; // 4. Select random pairs of points and see how many distance comparisons can exploit the optimization. var rng = new FastRandom(); var points = clusters.Points().ToList(); var ableToUseOptimizationsAtShortDistance = 0; var ableToUseOptimizationsAtLongDistance = 0; for (var i = 0; i < totalComparisons; i++) { var p1 = points[rng.Next(points.Count)]; var p2 = points[rng.Next(points.Count)]; if (useExtendedOptimization) { if (IsExtendedDistanceOptimizationUsable(p1, p2, mergeDistance, bitsPerDimension)) { ableToUseOptimizationsAtShortDistance++; } if (IsExtendedDistanceOptimizationUsable(p1, p2, longDistance, bitsPerDimension)) { ableToUseOptimizationsAtLongDistance++; } } else { if (IsDistanceOptimizationUsable(p1, p2, mergeDistance)) { ableToUseOptimizationsAtShortDistance++; } if (IsDistanceOptimizationUsable(p1, p2, longDistance)) { ableToUseOptimizationsAtLongDistance++; } } } var percentOptimizable = 100.0 * ableToUseOptimizationsAtShortDistance / totalComparisons; var percentOptimizableLongDistance = 100.0 * ableToUseOptimizationsAtLongDistance / totalComparisons; var message = $"Comparisons were {percentOptimizable} % Optimizable at short distance, {percentOptimizableLongDistance} % at long distance"; Console.WriteLine(message); return(percentOptimizable); }
public double SquareDistanceCompareValidationCase(int numTriangulationPoints) { var correctResult = 0; var wrongResult = 0; var totalComparisons = 10000; var extraShortTrianagulatable = 0; var extraShortNotTrianagulatable = 0; var shortTrianagulatable = 0; var shortNotTrianagulatable = 0; var longTrianagulatable = 0; var longNotTrianagulatable = 0; // 1. Make test data. var bitsPerDimension = 10; var data = new GaussianClustering { ClusterCount = 100, Dimensions = 100, MaxCoordinate = (1 << bitsPerDimension) - 1, MinClusterSize = 50, MaxClusterSize = 150 }; var clusters = data.MakeClusters(); // 2. Create HilbertIndex for points. var hIndex = new HilbertIndex(clusters, bitsPerDimension); hIndex.SetTriangulation(numTriangulationPoints); // 3. Deduce the characteristic distance. var counter = new ClusterCounter { OutlierSize = 5, NoiseSkipBy = 10 }; var count = counter.Count(hIndex.SortedPoints); var mergeDistance = count.MaximumSquareDistance; var longDistance = 5 * mergeDistance; // 4. Select random pairs of the HilbertPoints points and see how many distance comparisons yield the correct result. var rng = new FastRandom(); var points = hIndex.SortedPoints.ToList(); for (var i = 0; i < totalComparisons; i++) { var p1 = points[rng.Next(points.Count)]; var p2 = points[rng.Next(points.Count)]; var d = p1.Measure(p2); if (d.CompareTo(mergeDistance) == p1.SquareDistanceCompare(p2, mergeDistance)) { correctResult++; } else { wrongResult++; } if (d.CompareTo(longDistance) == p1.SquareDistanceCompare(p2, longDistance)) { correctResult++; } else { wrongResult++; } if (p1.Triangulatable(p2, mergeDistance / 2)) { extraShortTrianagulatable++; } else { extraShortNotTrianagulatable++; } if (p1.Triangulatable(p2, mergeDistance)) { shortTrianagulatable++; } else { shortNotTrianagulatable++; } if (p1.Triangulatable(p2, longDistance)) { longTrianagulatable++; } else { longNotTrianagulatable++; } } var extraShortPct = 100.0 * extraShortTrianagulatable / (extraShortTrianagulatable + extraShortNotTrianagulatable); var shortPct = 100.0 * shortTrianagulatable / (shortTrianagulatable + shortNotTrianagulatable); var longPct = 100.0 * longTrianagulatable / (longTrianagulatable + longNotTrianagulatable); Console.WriteLine($"Triangulatable? \n XS: {extraShortPct} % \n Short: {shortPct} % Yes {shortTrianagulatable}, No {shortNotTrianagulatable}\n Long: {longPct} % Yes {longTrianagulatable}, No {longNotTrianagulatable}"); Assert.AreEqual(wrongResult, 0, $"{correctResult} correct, {wrongResult} wrong"); return(shortPct); }