public static MetaNum Min(this IList <MetaNum> self) { if (self.Count == 0) { return(TypeTrait <MetaNum> .GetNaNOrRaiseException("No elements")); } var i = 0; while (TypeTrait <MetaNum> .IsNaN(self[0]) && i < self.Count - 1) { ++i; } MetaNum min = self[i]; for (++i; i < self.Count; ++i) { if (self[i] < min) { min = self[i]; } } return(min); }
public static MetaFloat Correlation(this IList <MetaNum> self, IList <MetaNum> other, bool skipNaN = true) { if (self.Count != other.Count) { return(MetaFloat.NaN); } if (skipNaN) { var s1 = new List <MetaNum>(self.Count); var s2 = new List <MetaNum>(self.Count); for (var i = 0; i < self.Count; ++i) { if (TypeTrait <MetaNum> .IsNaN(self[i]) || TypeTrait <MetaNum> .IsNaN(other[i])) { continue; } s1.Add(self[i]); s2.Add(other[i]); } return(Covariance(s1, s2) / s1.StandardDeviation() / s2.StandardDeviation()); } return(Covariance(self, other) / self.StandardDeviation() / other.StandardDeviation()); }
public static int ArgMin(this IList <MetaNum> self) { if (self.Count == 0) { throw new InvalidCastException("No elements"); } var i = 0; while (TypeTrait <MetaNum> .IsNaN(self[0]) && i < self.Count) { ++i; } if (i == self.Count) { throw new InvalidCastException("No elements"); } MetaNum min = self[i]; int argmin = i; for (++i; i < self.Count; ++i) { if (self[i] < min) { min = self[i]; argmin = i; } } return(argmin); }
public void TestStringIsNaN() { Assert.False(TypeTrait <string> .IsNaN(" xxx ")); Assert.True(TypeTrait <string> .IsNaN(null)); Assert.True(TypeTrait <string> .IsNaN(string.Empty)); Assert.True(TypeTrait <string> .IsNaN("")); Assert.True(TypeTrait <string> .IsNaN("\t ")); }
public static void FillNaNFill <T>(this IList <T> self, T fillValue) { for (var i = 0; i < self.Count; ++i) { if (TypeTrait <T> .IsNaN(self[i])) { self[i] = fillValue; } } }
public static void FillNaNFill(this IList <MetaNum> self, MetaNum fillValue) { for (var i = 0; i < self.Count; ++i) { if (TypeTrait <MetaNum> .IsNaN(self[i])) { self[i] = fillValue; } } }
public static IList <bool> IsNaN <T>(this IList <T> self) { var result = new List <bool>(self.Count); foreach (var item in self) { result.Add(TypeTrait <T> .IsNaN(item)); } return(result); }
public static List <MetaNum> RemoveNaN(this IList <MetaNum> self) { var result = new List <MetaNum>(self.Count); foreach (var value in self) { if (!TypeTrait <MetaNum> .IsNaN(value)) { result.Add(value); } } return(result); }
public static int CountNaN(this IList <MetaNum> self) { int count = 0; foreach (var value in self) { if (TypeTrait <MetaNum> .IsNaN(value)) { ++count; } } return(count); }
public static MetaNum Mode(this IList <MetaNum> self, bool skipNaN = true) { if (self.Count == 0) { return(TypeTrait <MetaNum> .GetNaNOrRaiseException("No elements")); } var values = self.ToArray(); Array.Sort(values); MetaNum currentValue = values[0]; MetaNum bestValue = currentValue; var currentCount = 1; var bestCount = 1; int i = 1; if (skipNaN) { // After sort, NaNs should be collected to the first location of the sequence. while (TypeTrait <MetaNum> .IsNaN(values[i])) { ++i; } } for (; i < values.Length; ++i) { if (currentValue == values[i]) { currentCount += 1; } else { currentValue = values[i]; currentCount = 1; } if (currentCount > bestCount) { bestCount = currentCount; bestValue = currentValue; } } return(bestValue); }
public static MetaFloat Covariance(this IList <MetaNum> self, IList <MetaNum> other, bool unbiased = true, bool skipNaN = true) { if (self.Count != other.Count) { return(MetaFloat.NaN); } var mean0 = self.Mean(skipNaN); var mean1 = other.Mean(skipNaN); if (MetaFloat.IsNaN(mean0) || MetaFloat.IsNaN(mean1)) { return(MetaFloat.NaN); } int actualCount = self.Count; MetaFloat c = (MetaFloat)0.0; for (int i = 0; i < self.Count; ++i) { if (TypeTrait <MetaNum> .IsNaN(self[i]) || TypeTrait <MetaNum> .IsNaN(other[i])) { if (skipNaN) { --actualCount; continue; } else { return(MetaFloat.NaN); } } var a = (MetaFloat)self[i] - mean0; var b = (MetaFloat)other[i] - mean1; c += a * b; } if (unbiased) { return(c / (actualCount - 1)); } else { return(c / actualCount); } }
public static MetaFloat Variance(this IList <MetaNum> self, bool unbiased = true, bool skipNaN = true) { if (self.Count == 0) { return(MetaFloat.NaN); } MetaFloat mean = Mean(self, skipNaN); if (MetaFloat.IsNaN(mean)) { return(MetaFloat.NaN); } MetaFloat variance = (MetaFloat)0.0; int actualCount = self.Count; foreach (var value in self) { MetaFloat v = (MetaFloat)value; if (TypeTrait <MetaFloat> .IsNaN(v)) { if (skipNaN) { --actualCount; continue; } else { return(MetaFloat.NaN); } } MetaFloat x = v - mean; variance += x * x; } if (unbiased) { return(variance / (actualCount - 1)); } else { return(variance / actualCount); } }
public static List <MetaNum> FillNaN(this IList <MetaNum> self, MetaNum fillValue) { var result = new List <MetaNum>(self.Count); foreach (var value in self) { if (TypeTrait <MetaNum> .IsNaN(value)) { result.Add(fillValue); } else { result.Add(value); } } return(result); }
// Histogram private static Tuple <int[], int> CollectHistogram(IList <MetaNum> data, HistogramInterval intervals) { var result = new int[intervals.BinCount]; var total = 0; foreach (var value in data) { if (TypeTrait <MetaNum> .IsNaN(value)) { continue; } var bin = (int)Math.Floor(((double)value - intervals.AdjustedLower) / intervals.BinWidth); ++result[bin]; ++total; } return(Tuple.Create(result, total)); }
public static MetaNum Product(this IList <MetaNum> self, bool skipNaN = true, int minCount = 0) { MetaNum product = (MetaNum)1; int count = 0; foreach (var value in self) { if (skipNaN && TypeTrait <MetaNum> .IsNaN(value)) { continue; } product *= value; ++count; } if (count < minCount) { return(TypeTrait <MetaNum> .GetNaN()); } return(product); }
public static MetaNum Sum(this IList <MetaNum> self, bool skipNaN = true, int minCount = 0) { MetaNum sum = (MetaNum)0.0; int count = 0; foreach (var value in self) { if (skipNaN && TypeTrait <MetaNum> .IsNaN(value)) { continue; } sum += value; ++count; } if (count < minCount) { return(TypeTrait <MetaNum> .GetNaN()); } return(sum); }
public void TestDoubleIsNaN() { Assert.True(TypeTrait <double> .IsNaN(double.NaN)); Assert.False(TypeTrait <double> .IsNaN(10.2)); }
public void TestDateTimeIsNaN() { Assert.False(TypeTrait <DateTime> .IsNaN(DateTime.Now)); }