internal override void Combine(MutableAggregation other, double fraction) { if (!(other is MutableSum mutable)) { throw new ArgumentException("MutableSum expected."); } this.Sum += fraction * mutable.Sum; }
internal override void Combine(MutableAggregation other, double fraction) { if (!(other is MutableCount mutable)) { throw new ArgumentException("MutableCount expected."); } var result = fraction * mutable.Count; long rounded = (long)Math.Round(result); this.Count += rounded; }
// We don't compute fractional MutableDistribution, it's either whole or none. internal override void Combine(MutableAggregation other, double fraction) { MutableDistribution mutableDistribution = other as MutableDistribution; if (mutableDistribution == null) { throw new ArgumentException("MutableDistribution expected."); } if (Math.Abs(1.0 - fraction) > TOLERANCE) { return; } if (!(this.BucketBoundaries.Equals(mutableDistribution.BucketBoundaries))) { throw new ArgumentException("Bucket boundaries should match."); } // Algorithm for calculating the combination of sum of squared deviations: // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm. if (this.Count + mutableDistribution.Count > 0) { double delta = mutableDistribution.Mean - this.Mean; this.SumOfSquaredDeviations = this.SumOfSquaredDeviations + mutableDistribution.SumOfSquaredDeviations + Math.Pow(delta, 2) * this.Count * mutableDistribution.Count / (this.Count + mutableDistribution.Count); } this.Count += mutableDistribution.Count; this.Sum += mutableDistribution.Sum; this.Mean = this.Sum / this.Count; if (mutableDistribution.Min < this.Min) { this.Min = mutableDistribution.Min; } if (mutableDistribution.Max > this.Max) { this.Max = mutableDistribution.Max; } long[] bucketCounts = mutableDistribution.BucketCounts; for (int i = 0; i < bucketCounts.Length; i++) { this.BucketCounts[i] += bucketCounts[i]; } }
internal override void Combine(MutableAggregation other, double fraction) { if (!(other is MutableLastValue mutable)) { throw new ArgumentException("MutableLastValue expected."); } MutableLastValue otherValue = (MutableLastValue)other; // Assume other is always newer than this, because we combined interval buckets in time order. // If there's a newer value, overwrite current value. this.LastValue = otherValue.Initialized ? otherValue.LastValue : this.LastValue; }
internal static IAggregationData CreateAggregationData(MutableAggregation aggregation, IMeasure measure) { return(aggregation.Match <IAggregationData>( (msum) => { return measure.Match <IAggregationData>( (mdouble) => { return SumDataDouble.Create(msum.Sum); }, (mlong) => { return SumDataLong.Create((long)Math.Round(msum.Sum)); }, (invalid) => { throw new ArgumentException(); }); }, CreateCountData, CreateMeanData, CreateDistributionData, (mlval) => { return measure.Match <IAggregationData>( (mdouble) => { return LastValueDataDouble.Create(mlval.LastValue); }, (mlong) => { if (Double.IsNaN(mlval.LastValue)) { return LastValueDataLong.Create(0); } return LastValueDataLong.Create((long)Math.Round(mlval.LastValue)); }, (invalid) => { throw new ArgumentException(); }); } )); }
internal override void Combine(MutableAggregation other, double fraction) { MutableMean mutable = other as MutableMean; if (mutable == null) { throw new ArgumentException("MutableMean expected."); } var result = fraction * mutable.Count; long rounded = (long)Math.Round(result); Count += rounded; this.Sum += mutable.Sum * fraction; if (mutable.Min < this.Min) { this.Min = mutable.Min; } if (mutable.Max > this.Max) { this.Max = mutable.Max; } }
internal abstract void Combine(MutableAggregation other, double fraction);
private static void Sum(MutableAggregation combined, IAggregationData data) { data.Match <object>( (arg) => { if (combined is MutableSum sum) { sum.Add(arg.Sum); } return(null); }, (arg) => { if (combined is MutableSum sum) { sum.Add(arg.Sum); } return(null); }, (arg) => { if (combined is MutableCount count) { count.Add(arg.Count); } return(null); }, (arg) => { if (combined is MutableMean mean) { mean.Count = mean.Count + arg.Count; mean.Sum = mean.Sum + (arg.Count * arg.Mean); if (arg.Min < mean.Min) { mean.Min = arg.Min; } if (arg.Max > mean.Max) { mean.Max = arg.Max; } } return(null); }, (arg) => { if (combined is MutableDistribution dist) { // Algorithm for calculating the combination of sum of squared deviations: // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm. if (dist.Count + arg.Count > 0) { double delta = arg.Mean - dist.Mean; dist.SumOfSquaredDeviations = dist.SumOfSquaredDeviations + arg.SumOfSquaredDeviations + (Math.Pow(delta, 2) * dist.Count * arg.Count / (dist.Count + arg.Count)); } dist.Count += arg.Count; dist.Sum += arg.Mean * arg.Count; dist.Mean = dist.Sum / dist.Count; if (arg.Min < dist.Min) { dist.Min = arg.Min; } if (arg.Max > dist.Max) { dist.Max = arg.Max; } IList <long> bucketCounts = arg.BucketCounts; for (int i = 0; i < bucketCounts.Count; i++) { dist.BucketCounts[i] += bucketCounts[i]; } } return(null); }, (arg) => { if (combined is MutableLastValue lastValue) { lastValue.Initialized = true; if (double.IsNaN(lastValue.LastValue)) { lastValue.LastValue = arg.LastValue; } else { lastValue.LastValue += arg.LastValue; } } return(null); }, (arg) => { if (combined is MutableLastValue lastValue) { lastValue.Initialized = true; if (double.IsNaN(lastValue.LastValue)) { lastValue.LastValue = arg.LastValue; } else { lastValue.LastValue += arg.LastValue; } } return(null); }, (arg) => { throw new ArgumentException(); }); }