예제 #1
0
        internal override void Combine(MutableAggregation other, double fraction)
        {
            if (!(other is MutableSum mutable))
            {
                throw new ArgumentException("MutableSum expected.");
            }

            this.Sum += fraction * mutable.Sum;
        }
예제 #2
0
        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;
        }
예제 #3
0
        // We don't compute fractional MutableDistribution, it's either whole or none.
        internal override void Combine(MutableAggregation other, double fraction)
        {
            if (!(other is MutableDistribution mutableDistribution))
            {
                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)
            {
                var 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;
            }

            var bucketCounts = mutableDistribution.BucketCounts;

            for (var i = 0; i < bucketCounts.Length; i++)
            {
                this.BucketCounts[i] += bucketCounts[i];
            }
        }
예제 #4
0
        internal override void Combine(MutableAggregation other, double fraction)
        {
            if (!(other is MutableLastValue mutable))
            {
                throw new ArgumentException("MutableLastValue expected.");
            }

            var 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();
                });
            }));
        }
예제 #6
0
        internal override void Combine(MutableAggregation other, double fraction)
        {
            if (!(other is MutableMean mutable))
            {
                throw new ArgumentException("MutableMean expected.");
            }

            var result  = fraction * mutable.Count;
            var rounded = (long)Math.Round(result);

            this.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;
            }
        }
예제 #7
0
        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)
                    {
                        var 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;
                    }

                    var bucketCounts = arg.BucketCounts;
                    for (var 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();
            });
        }
 internal abstract void Combine(MutableAggregation other, double fraction);