System.IComparable.CompareTo(C)

Here are the examples of the csharp api System.IComparable.CompareTo(C) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

19 Source : KendallTauCorrelation.cs
with MIT License
from paulchernoch

public double TauB(IEnumerable<T> data)
		{
			// Compute two Ranks by sorting first by Measure1 and then by Measure2.
			// Group by like values of each in order to handle ties.
			var ranked = data.Select(item => new { M1 = Measure1(item), M2 = Measure2(item) })
				.GroupBy(measures => new { measures.M1 })
				.OrderBy(@group => @group.First().M1)
				.ThenBy(@group => @group.First().M2)
				.AsEnumerable()
				.Select((@group, groupIndex) => new
				{
					Measure1Ranked = @group.Select((measure, index) => new { measure.M1, measure.M2 }),
					Rank = ++groupIndex
				})
				.SelectMany(v => v.Measure1Ranked, (s, i) => new
				{
					i.M1,
					i.M2,
					DenseRank1 = s.Rank
				})
				.GroupBy(measures => new { measures.M2 })
				.OrderBy(@group => @group.First().M2)
				.ThenBy(@group => @group.First().M1)
				.AsEnumerable()
				.Select((@group, groupIndex) => new
				{
					Measure2Ranked = @group.Select((measure, index) => new { measure.M1, measure.M2, measure.DenseRank1 }),
					Rank = ++groupIndex
				})
				.SelectMany(v => v.Measure2Ranked, (s, i) => new { i.M1, i.M2, i.DenseRank1, DenseRank2 = s.Rank })
				.ToArray();
			if (ranked.Length <= 1)
				return 0; // No data or one data point. Impossible to establish correlation.

			// Now that we have ranked the data, compute the correlation.
			var n = ranked.Count();
			var n0 = n * (n - 1) / 2;
			var n1 = 0;
			var n2 = 0;
			var numerator = 0; // Stores nc - nd as a single value, rather than computing them separately.
			for (var i = 1; i < n; i++)
				for (var j = 0; j < i; j++)
				{
					var iRanked = ranked[i];
					var jRanked = ranked[j];
					numerator += Sign(iRanked.DenseRank1 - jRanked.DenseRank1)
							   * Sign(iRanked.DenseRank2 - jRanked.DenseRank2);
					// Keep track of ties. Because we are running the indices in a triangle,
					// we automatically get this for n1 and n2: ties * (ties - 1) / 2
					if (iRanked.M1.CompareTo(jRanked.M1) == 0)
						n1++;
					if (iRanked.M2.CompareTo(jRanked.M2) == 0)
						n2++;
				}
			if (n0 == n1 || n0 == n2)
				return 0; // All ties, so everything as the same rank.
						  // Observe that if n1 = n2 = 0, that this formula is identical to Tau-a.
			return numerator / Sqrt((double)(n0 - n1) * (n0 - n2));
		}