4

Use case: Streaming large amounts of event source data that may have inserts, updates, and deletes and has guaranteed order.

Assuming Welford's Algorithm in this form in an event stream for insert:

private double _count = 0;
private double _mean = 0;
private double _s = 0;

public void Insert(double value)
{
    var prev_mean = _mean;
    _count = _count + 1;
    if (_count == 1)
    {
        _mean = value;
        _s = 0;
    }
    else
    {
        _mean = _mean + (value - _mean) / _count;
        _s = _s + (value - _mean) * (value - prev_mean);
    }
}

public double Var  => ((_count > 1) ? _s / (_count - 1) : 0.0);

public double StDev => Math.Sqrt(Var);

Would it be possible to change the online statistics given a known pre-existing value. Or would there be a more appropriate approach than Welford's Algorithm to accommodate the need?

public void Update(double previousValue, double value)
{
   //I got this value correct
   var prev_mean = (_count * _mean - value) / (_count - 1);

   //I did the inversion, but this doesn't give the right values
   var prev_s = -previousValue^2 + previousValue* prev_mean + _mean * previousValue - _mean * prev_mean + _s
}
public void Delete(double previousValue)
{
    _count = _count - 1;
}

Edit

The specific questions are:

How can I calculate a correct value for _mean and _s in the case of an Update?

How can I calculate a correct value for _mean and _s in the case of an Delete?

ChaseAucoin
  • 725
  • 1
  • 8
  • 16
  • Hmm, it's not clear what you mean by "change the online statistics given a known pre-existing value". I think maybe you need to say more about what you are trying to do. Also, it looks like this is more of a discussion question than a specific programming question, so stats.stackexchange.com is a more appropriate forum. – Robert Dodier Oct 21 '20 at 17:45
  • @RobertDodier I have updated the question for clarity. – ChaseAucoin Oct 21 '20 at 18:02
  • I see what you mean now. This is actually a really interesting question, and it's off topic for SO. Stats.stackexchange.com will have much more useful input on it. Good luck and have fun with it. – Robert Dodier Oct 21 '20 at 18:38

1 Answers1

2

Partial answer (will update if I finish the solve):

I got the inversion wrong on the Update originally.

Edit

Delete was trivial once I solved update

private void Update(double previousValue, double value)
{
    if (_count == 1)
    {
        _mean = value;
        _s = 0;
    }
    else
    {
        var prev_mean = (_count * _mean - previousValue) / (_count - 1);
        var prev_s = -(_mean * prev_mean) + (_mean * previousValue) + (prev_mean * previousValue) - Math.Pow(previousValue, 2) + _s;

        //Revert Mean and S
        _mean = prev_mean;
        _s = prev_s;

        //Do same operation as Insert
        _mean = _mean + (value - _mean) / _count;
        _s = prev_s + (value - _mean) * (value - prev_mean);
    }
}
public void Delete(double previousValue)
{
    _count = _count - 1;

    if (_count == 0)
    {
        _mean = 0;
        _s = 0;
        return;
    }
    if (_count == 1)
    {
        _mean = (_count * _mean - previousValue) / (_count - 1);
        _s = 0;
        return;
    }
    else
    {
        var prev_mean = (_count * _mean - previousValue) / (_count - 1);
        var prev_s = -(_mean * prev_mean) + (_mean * previousValue) + (prev_mean * previousValue) - Math.Pow(previousValue, 2) + _s;

        //Revert Mean and S
        _mean = prev_mean;
        _s = prev_s;
    }
}

Edit Okay found a better implementation of the original algorithm to base this off of, then with the help of Wolfram Mathematica I was able to solve for the inversions I needed. I did a test run locally with a million random activities (insert, update, delete in random order)

I used this as the logic Assert.IsTrue(Math.Abs(x - y) < .0000001);

Where x is a native 2 pass algo in c# and y is the value from this implementation. It looks like the native implementation rounds a few things that this does not.

Insert method based on work here https://www.johndcook.com/blog/skewness_kurtosis/

Remove method is my own work.

public class StatisticsTracker
    {
        private long n = 0;
        private double _sum, _s, M1, M2, M3, M4 = 0.0;

        public long Count => n;

        public double Avg => (n > 2) ? M1 : 0.0;

        public double Sum => _sum;

        public double Var => M2 / (n - 1.0);

        public double StDev => Math.Sqrt(Var);

        //public double Skewness => Math.Sqrt(n) * M3 / Math.Pow(M2, 1.5);

        //public double Kurtosis => (double)n * M4 / (M2 * M2) - 3.0;



        public void Insert(double x)
        {
            double delta, delta_n, delta_n2, term1;
            _sum += x;

            long n1 = n;
            n++;
            delta = x - M1;
            delta_n = delta / n;            
            term1 = delta * delta_n * n1;
            M1 = M1 + delta_n;
            M2 += term1;

            //Required for skewness and Kurtosis
            //Will solve later
            //delta_n2 = delta_n * delta_n;
            //M3 += term1 * delta_n * (n - 2) - 3 * delta_n * M2;
            //M4 += term1 * delta_n2 * (n * n - 3 * n + 3) + 6 * delta_n2 * M2 - 4 * delta_n * M3;            
        }

        public void Update(double previousvalue, double value)
        {
            Delete(previousvalue);
            Insert(value);
        }

        public void Delete(double x)
        {
            var o = ((M1 * n) - x) / (n - 1.0);
            var v = M2;
            var y2 = (-(n - 1.0) * Math.Pow(o, 2.0) + (2.0 * (n - 1) * o * x) + (n * (v - Math.Pow(x, 2.0))) + Math.Pow(x, 2.0)) / n;

            M1 = o;
            M2 = y2;

            n = n - 1;
            _sum -= x;
        }
    }
ChaseAucoin
  • 725
  • 1
  • 8
  • 16