Example #1
0
        internal override void Combine(MutableAggregation other, double fraction)
        {
            MutableSum mutable = other as MutableSum;

            if (mutable == null)
            {
                throw new ArgumentException("MutableSum expected.");
            }

            this.Sum += fraction * mutable.Sum;
        }
        private static void Sum(MutableAggregation combined, IAggregationData data)
        {
            data.Match <object>(
                (arg) =>
            {
                MutableSum sum = combined as MutableSum;
                if (sum != null)
                {
                    sum.Add(arg.Sum);
                }
                return(null);
            },
                (arg) =>
            {
                MutableSum sum = combined as MutableSum;
                if (sum != null)
                {
                    sum.Add(arg.Sum);
                }
                return(null);
            },
                (arg) =>
            {
                MutableCount count = combined as MutableCount;
                if (count != null)
                {
                    count.Add(arg.Count);
                }
                return(null);
            },
                (arg) =>
            {
                MutableMean mean = combined as MutableMean;
                if (mean != null)
                {
                    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) =>
            {
                MutableDistribution dist = combined as MutableDistribution;
                if (dist != null)
                {
                    // 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) =>
            {
                MutableLastValue lastValue = combined as MutableLastValue;
                if (lastValue != null)
                {
                    lastValue.initialized = true;
                    if (Double.IsNaN(lastValue.LastValue))
                    {
                        lastValue.LastValue = arg.LastValue;
                    }
                    else
                    {
                        lastValue.LastValue += arg.LastValue;
                    }
                }
                return(null);
            },
                (arg) =>
            {
                MutableLastValue lastValue = combined as MutableLastValue;
                if (lastValue != null)
                {
                    lastValue.initialized = true;
                    if (Double.IsNaN(lastValue.LastValue))
                    {
                        lastValue.LastValue = arg.LastValue;
                    }
                    else
                    {
                        lastValue.LastValue += arg.LastValue;
                    }
                }
                return(null);
            },
                (arg) =>
            {
                throw new ArgumentException();
            });
        }