/// <summary> /// Clone this StatCounter /// </summary> /// <returns></returns> internal StatCounter copy() { var other = new StatCounter(); other.n = n; other.mu = mu; other.m2 = m2; other.maxValue = maxValue; other.minValue = minValue; return(other); }
/// <summary> /// Merge another StatCounter into this one, adding up the internal statistics. /// </summary> /// <param name="other"></param> /// <returns></returns> internal StatCounter Merge(StatCounter other) { if (other == this) { return Merge(other.copy()); // Avoid overwriting fields in a weird order } else { if (n == 0) { mu = other.mu; m2 = other.m2; n = other.n; maxValue = other.maxValue; minValue = other.minValue; } else if (other.n != 0) { var delta = other.mu - mu; if (other.n * 10 < n) { mu = mu + (delta * other.n) / (n + other.n); } else if (n * 10 < other.n) { mu = other.mu - (delta * n) / (n + other.n); } else { mu = (mu * n + other.mu * other.n) / (n + other.n); } m2 += other.m2 + (delta * delta * n * other.n) / (n + other.n); n += other.n; maxValue = Math.Max(maxValue, other.maxValue); minValue = Math.Min(minValue, other.minValue); } return this; } }
/// <summary> /// Merge another StatCounter into this one, adding up the internal statistics. /// </summary> /// <param name="other"></param> /// <returns></returns> internal StatCounter Merge(StatCounter other) { if (other == this) { return(Merge(other.copy())); // Avoid overwriting fields in a weird order } else { if (n == 0) { mu = other.mu; m2 = other.m2; n = other.n; maxValue = other.maxValue; minValue = other.minValue; } else if (other.n != 0) { var delta = other.mu - mu; if (other.n * 10 < n) { mu = mu + (delta * other.n) / (n + other.n); } else if (n * 10 < other.n) { mu = other.mu - (delta * n) / (n + other.n); } else { mu = (mu * n + other.mu * other.n) / (n + other.n); } m2 += other.m2 + (delta * delta * n * other.n) / (n + other.n); n += other.n; maxValue = Math.Max(maxValue, other.maxValue); minValue = Math.Min(minValue, other.minValue); } return(this); } }
/// <summary> /// Clone this StatCounter /// </summary> /// <returns></returns> internal StatCounter copy() { var other = new StatCounter(); other.n = n; other.mu = mu; other.m2 = m2; other.maxValue = maxValue; other.minValue = minValue; return other; }