public Dictionary<int, double> CrossCorrelation(Statistics s, bool normalize) { if (Length != s.Length) return null; double mean1 = Mean(); double mean2 = s.Mean(); double invdenominator = 1; // 1/denominator for normalization int length = Length; // cache this - profiling shows a surprising cost for repeated access var result = new Dictionary<int, double>(1 + (2 * length)); // Normalized cross-correlation = subtract the mean and divide by the standard deviation if (normalize) { double sqsum1 = 0; double sqsum2 = 0; foreach (double v in _list) sqsum1 += (v - mean1)*(v - mean1); foreach (double v in s._list) sqsum2 += (v - mean2)*(v - mean2); // sigma_1 * sigma_2 * n double denominator = Math.Sqrt(sqsum1*sqsum2); // find the demominator if (denominator > 0) { invdenominator = 1.0/denominator; // for speed, we'll multiply by invdenominator rather than divide by denominator } else { // all datapoints are zero for (int delay = -length; delay <= length; delay++) { result.Add(delay,0); } return result; } } for (int delay = -length; delay <= length; delay++) { double sxy = 0; int upper = Math.Min(length, length - delay); // i and i+delay must both be in range(0,length) for (int i = Math.Max(0, -delay); i < upper; i++) // i and i+delay must both be in range(0,length) { if (normalize) sxy += (_list[i] - mean1) * (s._list[i + delay] - mean2); else sxy += (_list[i]) * (s._list[i + delay]); } result.Add(delay, sxy * invdenominator); } return result; }
/// <summary> /// Calculates the variance for a set of numbers from a weighted mean. /// See: /// http://en.wikipedia.org/wiki/Weighted_mean /// </summary> /// <param name="weights">The weights</param> /// <returns>Variance from weighted mean</returns> public double Variance(Statistics weights) { if (_list.Length < 2) return 0; try { double s = 0; for (int i = 0; i < _list.Length; i++) s += weights._list[i] * Math.Pow(_list[i], 2); return (s/weights.Mean() - _list.Length*Math.Pow(Mean(weights), 2)) / (_list.Length - 1); } catch (Exception) { return double.NaN; } }
/// <summary> /// Calculates the covariance between two sets of numbers. /// </summary> /// <param name="s1">First set of numbers</param> /// <param name="s2">Second set of numbers</param> /// <returns></returns> public static double Covariance(Statistics s1, Statistics s2) { try { if (s1.Length != s2.Length) return double.NaN; int len = s1.Length; double sumMul = 0; for (int i = 0; i < len; i++) sumMul += (s1._list[i]*s2._list[i]); return (sumMul - len*s1.Mean()*s2.Mean())/(len - 1); } catch (Exception) { return double.NaN; } }
public void QValueTest() { var statPvalues = new Statistics(STOREY_P_VALUES); Assert.AreEqual(0.676341, statPvalues.PiZero(0.5), 1e-6); Assert.AreEqual(0.671857, statPvalues.PiZero(), 1e-6); // First try q values with 0.5 lambda from the paper var qvalues = statPvalues.Qvalues(0.5); Assert.AreEqual(STOREY_P_VALUES.Length, qvalues.Length); Assert.AreEqual(STOREY_Q_VALUES.Length, qvalues.Length); var deltas = new Statistics(qvalues.Select((v, i) => Math.Abs(v - STOREY_Q_VALUES[i]))); Assert.IsTrue(deltas.Mean() < 1.1e-5); Assert.IsTrue(deltas.Max() < 4e-4); // Then try it with automated lambda calculation qvalues = statPvalues.Qvalues(); deltas = new Statistics(qvalues.Select((v, i) => Math.Abs(v - STOREY_Q_VALUES[i]))); Assert.IsTrue(deltas.Mean() < 2.6e-3); Assert.IsTrue(deltas.Max() < 4.5e-3); }
/// <summary> /// Calculates the b term (y-intercept) of the linear /// regression function (y = a*x + b) given the Y and X values. /// </summary> /// <param name="y">Y values</param> /// <param name="x">X values</param> /// <returns>The b coefficient of y = a*x + b</returns> public static double BTerm2(Statistics y, Statistics x) { return y.Mean() - ATerm2(y, x)*x.Mean(); }
public SummaryValue(Statistics statistics) { Mean = statistics.Mean(); if (statistics.Length > 1) { Stdev = statistics.StdDev(); Cv = Stdev/Mean; } }
public void MeanWeightedTest() { Statistics s = new Statistics(80, 90); Statistics weights = new Statistics(20, 30); Assert.AreEqual(86, s.Mean(weights)); Assert.AreEqual(79.50, _interventionPercents.Mean(_interventionRequests), 0.01); }
private string GetDotProductResultsText(TransitionGroupDocNode nodeGroup, int indexResult) { var replicateIndices = IndexOfReplicate(indexResult); IEnumerable<float?> values; switch (ExpectedVisible) { case AreaExpectedValue.library: if (replicateIndices.IsEmpty) { values = new[] {nodeGroup.GetLibraryDotProduct(-1)}; } else { values = replicateIndices.Select(nodeGroup.GetLibraryDotProduct); } break; case AreaExpectedValue.isotope_dist: if (replicateIndices.IsEmpty) { values = new[] {nodeGroup.GetIsotopeDotProduct(-1)}; } else { values = replicateIndices.Select(nodeGroup.GetIsotopeDotProduct); } break; default: return null; } var statistics = new Statistics(values .Select(value => value.HasValue ? (double?) value : null) .Where(value=>value.HasValue) .Cast<double>()); if (statistics.Length == 0) { return null; } return GetDotProductText((float) statistics.Mean()); }
private static PointPair CreatePointPair(int iGroup, ICollection<double> listTimes, IEnumerable<double> listStarts, IEnumerable<double> listEnds, IEnumerable<double> listFwhms, ref double maxY, ref double minY) { if (listTimes.Count == 0) return RTPointPairMissing(iGroup); var statTimes = new Statistics(listTimes); var statStarts = new Statistics(listStarts); var statEnds = new Statistics(listEnds); var statFwhms = new Statistics(listFwhms); double endY = statEnds.Mean(); double startY = statStarts.Mean(); var pointPair = HiLowMiddleErrorBarItem.MakePointPair(iGroup, endY, startY, statTimes.Mean(), statFwhms.Mean()); maxY = Math.Max(maxY, endY); minY = Math.Min(minY, startY); return pointPair; }
private static PointPair CreatePointPair(int iGroup, ICollection<double> listValues, ref double maxY) { if (listValues.Count == 0) return PointPairMissing(iGroup); var statValues = new Statistics(listValues); PointPair pointPair; if (Settings.Default.ShowPeptideCV) { double cvRatio = statValues.StdDev() / statValues.Mean(); if (!Settings.Default.PeakDecimalCv) cvRatio *= 100; pointPair = MeanErrorBarItem.MakePointPair(iGroup, cvRatio, 0); } else pointPair = MeanErrorBarItem.MakePointPair(iGroup, statValues.Mean(), statValues.StdDev()); maxY = Math.Max(maxY, MeanErrorBarItem.GetYTotal(pointPair)); return pointPair; }