Esempio n. 1
0
        public void TestCORREL()
        {
            BaseStatisticsBean stat = new BaseStatisticsBean();

            Assert.AreEqual(Double.NaN, stat.Correlation);
            Assert.AreEqual(0, stat.N);

            stat.AddPoint(1, 10);
            Assert.AreEqual(Double.NaN, stat.Correlation);
            Assert.AreEqual(1, stat.N);

            stat.AddPoint(2, 20);
            Assert.AreEqual(1d, stat.Correlation);
            Assert.AreEqual(2, stat.N);

            stat.AddPoint(1.5, 14);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.Correlation, 0.993399268, PRECISION_DIGITS));
            Assert.AreEqual(3, stat.N);

            stat.AddPoint(1.4, 14);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.Correlation, 0.992631989, PRECISION_DIGITS));
            Assert.AreEqual(4, stat.N);

            stat.RemovePoint(1.5, 14);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.Correlation, 1, PRECISION_DIGITS));
            Assert.AreEqual(3, stat.N);

            stat.AddPoint(100, 1);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.Correlation, -0.852632057, PRECISION_DIGITS));
            Assert.AreEqual(4, stat.N);
        }
Esempio n. 2
0
        public void TestClone()
        {
            BaseStatisticsBean stat = new BaseStatisticsBean();

            stat.AddPoint(100, 10);
            stat.AddPoint(200, 20);

            BaseStatisticsBean cloned = (BaseStatisticsBean)stat.Clone();

            Assert.AreEqual(2, cloned.N);
            Assert.AreEqual(300d, cloned.XSum);
            Assert.AreEqual(150d, cloned.XAverage);
            Assert.AreEqual(30d, cloned.YSum);
            Assert.AreEqual(15d, cloned.YAverage);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(cloned.XStandardDeviationPop, 50.0, PRECISION_DIGITS));
        }
Esempio n. 3
0
        public void TestAddRemoveXOnly()
        {
            BaseStatisticsBean stat = new BaseStatisticsBean();

            Assert.AreEqual(Double.NaN, stat.XAverage);
            Assert.AreEqual(0, stat.N);

            stat.AddPoint(10);
            stat.AddPoint(20);
            Assert.AreEqual(15d, stat.XAverage);
            Assert.AreEqual(0d, stat.YAverage);
            Assert.AreEqual(2, stat.N);

            stat.RemovePoint(10);
            Assert.AreEqual(20d, stat.XAverage);
            Assert.AreEqual(0d, stat.YAverage);
            Assert.AreEqual(1, stat.N);
        }
Esempio n. 4
0
        public void TestStddevAndVariance_STDEV()
        {
            BaseStatisticsBean stat = new BaseStatisticsBean();

            Assert.AreEqual(Double.NaN, stat.XStandardDeviationSample);
            Assert.AreEqual(Double.NaN, stat.XVariance);
            Assert.AreEqual(Double.NaN, stat.YStandardDeviationSample);
            Assert.AreEqual(Double.NaN, stat.YVariance);

            stat.AddPoint(100, -1);
            Assert.AreEqual(Double.NaN, stat.XVariance);
            Assert.AreEqual(Double.NaN, stat.XStandardDeviationSample);
            Assert.AreEqual(Double.NaN, stat.YVariance);
            Assert.AreEqual(Double.NaN, stat.YStandardDeviationSample);

            stat.AddPoint(150, -1);
            Assert.AreEqual(1250d, stat.XVariance);
            Assert.AreEqual(0d, stat.YVariance);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XStandardDeviationSample, 35.35533906, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YStandardDeviationSample, 0, PRECISION_DIGITS));

            stat.AddPoint(0, -1.1);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XVariance, 5833.33333333, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YVariance, 0.003333333, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XStandardDeviationSample, 76.37626158, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YStandardDeviationSample, 0.057735027, PRECISION_DIGITS));

            stat.RemovePoint(100, -1);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XVariance, 11250, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YVariance, 0.005, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XStandardDeviationSample, 106.0660172, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YStandardDeviationSample, 0.070710678, PRECISION_DIGITS));

            stat.AddPoint(-149, 0);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XVariance, 22350.333333, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YVariance, 0.37, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XStandardDeviationSample, 149.5002787, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YStandardDeviationSample, 0.608276253, PRECISION_DIGITS));
        }
Esempio n. 5
0
        public void TestLINEST()
        {
            BaseStatisticsBean stat = new BaseStatisticsBean();

            Assert.AreEqual(Double.NaN, stat.Slope);
            Assert.AreEqual(Double.NaN, stat.YIntercept);
            Assert.AreEqual(0, stat.N);

            stat.AddPoint(1, 15);
            Assert.AreEqual(Double.NaN, stat.Slope);
            Assert.AreEqual(Double.NaN, stat.YIntercept);
            Assert.AreEqual(1, stat.N);

            stat.AddPoint(2, 20);
            Assert.AreEqual(5d, stat.Slope);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YIntercept, 10, PRECISION_DIGITS));
            Assert.AreEqual(2, stat.N);

            stat.AddPoint(1, 17);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.Slope, 4, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YIntercept, 12, PRECISION_DIGITS));
            Assert.AreEqual(3, stat.N);

            stat.AddPoint(1.4, 14);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.Slope, 3.731343284, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YIntercept, 11.46268657, PRECISION_DIGITS));
            Assert.AreEqual(4, stat.N);

            stat.RemovePoint(1, 17);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.Slope, 5.394736842, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YIntercept, 8.421052632, PRECISION_DIGITS));
            Assert.AreEqual(3, stat.N);

            stat.AddPoint(0, 0);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.Slope, 9.764150943, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YIntercept, 1.509433962, PRECISION_DIGITS));
            Assert.AreEqual(4, stat.N);
        }
Esempio n. 6
0
        public void TestSum()
        {
            BaseStatisticsBean stat = new BaseStatisticsBean();

            Assert.AreEqual(0d, stat.XSum);
            Assert.AreEqual(0d, stat.YSum);
            Assert.AreEqual(0, stat.N);

            stat.AddPoint(10, -2);
            Assert.AreEqual(10d, stat.XSum);
            Assert.AreEqual(-2d, stat.YSum);
            Assert.AreEqual(1, stat.N);

            stat.AddPoint(3.5, -3);
            Assert.AreEqual(13.5d, stat.XSum);
            Assert.AreEqual(-5d, stat.YSum);
            Assert.AreEqual(2, stat.N);

            stat.AddPoint(1, 5);
            Assert.AreEqual(14.5d, stat.XSum);
            Assert.AreEqual(0d, stat.YSum);
            Assert.AreEqual(3, stat.N);

            stat.RemovePoint(9, 1.5);
            Assert.AreEqual(5.5d, stat.XSum);
            Assert.AreEqual(-1.5d, stat.YSum);
            Assert.AreEqual(2, stat.N);

            stat.RemovePoint(9.5, -1.5);
            Assert.AreEqual(-4d, stat.XSum);
            Assert.AreEqual(0d, stat.YSum);
            Assert.AreEqual(1, stat.N);

            stat.RemovePoint(1, -1);
            Assert.AreEqual(0d, stat.XSum);
            Assert.AreEqual(0d, stat.YSum);
            Assert.AreEqual(0, stat.N);

            stat.RemovePoint(1, 1);
            Assert.AreEqual(0d, stat.XSum);
            Assert.AreEqual(0d, stat.YSum);
            Assert.AreEqual(0, stat.N);

            stat.AddPoint(1.11, -3.333);
            Assert.AreEqual(1.11d, stat.XSum);
            Assert.AreEqual(-3.333d, stat.YSum);
            Assert.AreEqual(1, stat.N);

            stat.AddPoint(2.22, 3.333);
            Assert.AreEqual(1.11d + 2.22d, stat.XSum);
            Assert.AreEqual(0d, stat.YSum);
            Assert.AreEqual(2, stat.N);

            stat.AddPoint(-3.32, 0);
            Assert.AreEqual(1.11d + 2.22d - 3.32d, stat.XSum);
            Assert.AreEqual(0d, stat.YSum);
            Assert.AreEqual(3, stat.N);
        }
Esempio n. 7
0
        public void TestStddev_STDEVPA()
        {
            BaseStatisticsBean stat = new BaseStatisticsBean();

            Assert.AreEqual(Double.NaN, stat.XStandardDeviationPop);
            Assert.AreEqual(Double.NaN, stat.YStandardDeviationPop);
            Assert.AreEqual(0, stat.N);

            stat.AddPoint(1, 10500);
            Assert.AreEqual(0.0d, stat.XStandardDeviationPop);
            Assert.AreEqual(0.0d, stat.YStandardDeviationPop);
            Assert.AreEqual(1, stat.N);

            stat.AddPoint(2, 10200);
            Assert.AreEqual(0.5d, stat.XStandardDeviationPop);
            Assert.AreEqual(150d, stat.YStandardDeviationPop);
            Assert.AreEqual(2, stat.N);

            stat.AddPoint(1.5, 10500);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XStandardDeviationPop, 0.40824829, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YStandardDeviationPop, 141.4213562, PRECISION_DIGITS));
            Assert.AreEqual(3, stat.N);

            stat.AddPoint(-0.1, 10500);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XStandardDeviationPop, 0.777817459, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YStandardDeviationPop, 129.9038106, PRECISION_DIGITS));
            Assert.AreEqual(4, stat.N);

            stat.RemovePoint(2, 10200);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XStandardDeviationPop, 0.668331255, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YStandardDeviationPop, 0.0, PRECISION_DIGITS));
            Assert.AreEqual(3, stat.N);

            stat.AddPoint(0.89, 10499);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XStandardDeviationPop, 0.580102362, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YStandardDeviationPop, 0.433012702, PRECISION_DIGITS));
            Assert.AreEqual(4, stat.N);

            stat.AddPoint(1.23, 10500);
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.XStandardDeviationPop, 0.543860276, PRECISION_DIGITS));
            Assert.IsTrue(DoubleValueAssertionUtil.Equals(stat.YStandardDeviationPop, 0.4, PRECISION_DIGITS));
            Assert.AreEqual(5, stat.N);
        }
Esempio n. 8
0
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            using (Instrument.With(
                       i => i.QViewProcessIRStream(this, NAME, newData, oldData),
                       i => i.AViewProcessIRStream()))
            {
                // If we have child views, keep a reference to the old values, so we can fireStatementStopped them as old data event.
                EventBean oldValues = null;
                if (_lastNewEvent == null)
                {
                    if (HasViews)
                    {
                        oldValues = PopulateMap(_statisticsBean, AgentInstanceContext.StatementContext.EventAdapterService, _eventType, _additionalProps, _lastValuesEventNew);
                    }
                }

                var evaluateParams = new EvaluateParams(_eventsPerStream, true, AgentInstanceContext);

                // add data points to the bean
                if (newData != null)
                {
                    for (var i = 0; i < newData.Length; i++)
                    {
                        _eventsPerStream[0] = newData[i];
                        var xnum = _expressionXEval.Evaluate(evaluateParams);
                        var ynum = _expressionYEval.Evaluate(evaluateParams);
                        if (xnum != null && ynum != null)
                        {
                            var x = xnum.AsDouble();
                            var y = ynum.AsDouble();
                            _statisticsBean.AddPoint(x, y);
                        }
                    }

                    if ((_additionalProps != null) && (newData.Length != 0))
                    {
                        if (_lastValuesEventNew == null)
                        {
                            _lastValuesEventNew = new Object[_additionalProps.AdditionalExpr.Length];
                        }
                        for (var val = 0; val < _additionalProps.AdditionalExpr.Length; val++)
                        {
                            _lastValuesEventNew[val] = _additionalProps.AdditionalExpr[val].Evaluate(evaluateParams);
                        }
                    }
                }

                // remove data points from the bean
                if (oldData != null)
                {
                    for (var i = 0; i < oldData.Length; i++)
                    {
                        _eventsPerStream[0] = oldData[i];
                        var xnum = _expressionXEval.Evaluate(evaluateParams);
                        var ynum = _expressionYEval.Evaluate(evaluateParams);
                        if (xnum != null && ynum != null)
                        {
                            var x = xnum.AsDouble();
                            var y = ynum.AsDouble();
                            _statisticsBean.RemovePoint(x, y);
                        }
                    }
                }

                // If there are child view, fireStatementStopped Update method
                if (HasViews)
                {
                    var         newDataMap = PopulateMap(_statisticsBean, AgentInstanceContext.StatementContext.EventAdapterService, _eventType, _additionalProps, _lastValuesEventNew);
                    var         newEvents  = new EventBean[] { newDataMap };
                    EventBean[] oldEvents;
                    if (_lastNewEvent == null)
                    {
                        oldEvents = new EventBean[] { oldValues };
                    }
                    else
                    {
                        oldEvents = new EventBean[] { _lastNewEvent };
                    }

                    Instrument.With(
                        i => i.QViewIndicate(this, NAME, newEvents, oldEvents),
                        i => i.AViewIndicate(),
                        () => UpdateChildren(newEvents, oldEvents));

                    _lastNewEvent = newDataMap;
                }
            }
        }
Esempio n. 9
0
        public void TestAverage()
        {
            BaseStatisticsBean stat = new BaseStatisticsBean();

            Assert.AreEqual(Double.NaN, stat.XAverage);
            Assert.AreEqual(Double.NaN, stat.YAverage);
            Assert.AreEqual(0, stat.N);

            stat.RemovePoint(2, 363636);
            Assert.AreEqual(Double.NaN, stat.XAverage);
            Assert.AreEqual(Double.NaN, stat.YAverage);
            Assert.AreEqual(0, stat.N);

            stat.AddPoint(10, -2);
            Assert.AreEqual(10d, stat.XAverage);
            Assert.AreEqual(-2d, stat.YAverage);
            Assert.AreEqual(1, stat.N);

            stat.AddPoint(20, 4);
            Assert.AreEqual(15d, stat.XAverage);
            Assert.AreEqual(1d, stat.YAverage);
            Assert.AreEqual(2, stat.N);

            stat.AddPoint(1, 4);
            Assert.AreEqual(31d / 3d, stat.XAverage);
            Assert.AreEqual(6d / 3d, stat.YAverage);
            Assert.AreEqual(3, stat.N);

            stat.AddPoint(1, -10);
            Assert.AreEqual(8d, stat.XAverage);
            Assert.AreEqual(-4d / 4d, stat.YAverage);
            Assert.AreEqual(4, stat.N);

            stat.AddPoint(-32, -11);
            Assert.AreEqual(0d, stat.XAverage);
            Assert.AreEqual(-15d / 5d, stat.YAverage);
            Assert.AreEqual(5, stat.N);

            stat.RemovePoint(-32, -10);
            Assert.AreEqual(32d / 4d, stat.XAverage);
            Assert.AreEqual(-5d / 4d, stat.YAverage);
            Assert.AreEqual(4, stat.N);

            stat.RemovePoint(8, -5);
            Assert.AreEqual(24d / 3d, stat.XAverage);
            Assert.AreEqual(0d, stat.YAverage);
            Assert.AreEqual(3, stat.N);

            stat.RemovePoint(2, 50);
            Assert.AreEqual(22d / 2d, stat.XAverage);
            Assert.AreEqual(-50d / 2d, stat.YAverage);
            Assert.AreEqual(2, stat.N);

            stat.RemovePoint(1, 1);
            Assert.AreEqual(21d / 1d, stat.XAverage);
            Assert.AreEqual(-51d, stat.YAverage);
            Assert.AreEqual(1, stat.N);

            stat.RemovePoint(3, 3);
            Assert.AreEqual(Double.NaN, stat.XAverage);
            Assert.AreEqual(Double.NaN, stat.YAverage);
            Assert.AreEqual(0, stat.N);
        }
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.Get().QViewProcessIRStream(this, UnivariateStatisticsViewFactory.NAME, newData, oldData);
            }

            // If we have child views, keep a reference to the old values, so we can Update them as old data event.
            EventBean oldDataMap = null;

            if (_lastNewEvent == null)
            {
                if (HasViews)
                {
                    oldDataMap = PopulateMap(_baseStatisticsBean, _agentInstanceContext.StatementContext.EventAdapterService, _viewFactory.EventType, _viewFactory.AdditionalProps, _lastValuesEventNew);
                }
            }

            var evaluateParams = new EvaluateParams(_eventsPerStream, true, _agentInstanceContext);

            // add data points to the bean
            if (newData != null)
            {
                for (int i = 0; i < newData.Length; i++)
                {
                    _eventsPerStream[0] = newData[i];
                    var pointnum = _fieldExpressionEvaluator.Evaluate(evaluateParams);
                    if (pointnum != null)
                    {
                        double point = pointnum.AsDouble();
                        _baseStatisticsBean.AddPoint(point, 0);
                    }
                }

                if ((_viewFactory.AdditionalProps != null) && (newData.Length != 0))
                {
                    if (_lastValuesEventNew == null)
                    {
                        _lastValuesEventNew = new Object[_viewFactory.AdditionalProps.AdditionalExpr.Length];
                    }
                    for (int val = 0; val < _viewFactory.AdditionalProps.AdditionalExpr.Length; val++)
                    {
                        _lastValuesEventNew[val] = _viewFactory.AdditionalProps.AdditionalExpr[val].Evaluate(evaluateParams);
                    }
                }
            }

            // remove data points from the bean
            if (oldData != null)
            {
                for (int i = 0; i < oldData.Length; i++)
                {
                    _eventsPerStream[0] = oldData[i];
                    var pointnum = _fieldExpressionEvaluator.Evaluate(evaluateParams);
                    if (pointnum != null)
                    {
                        double point = pointnum.AsDouble();
                        _baseStatisticsBean.RemovePoint(point, 0);
                    }
                }
            }

            // If there are child view, call Update method
            if (HasViews)
            {
                EventBean newDataMap = PopulateMap(_baseStatisticsBean, _agentInstanceContext.StatementContext.EventAdapterService, _viewFactory.EventType, _viewFactory.AdditionalProps, _lastValuesEventNew);

                EventBean[] oldEvents;
                EventBean[] newEvents = new EventBean[] { newDataMap };
                if (_lastNewEvent == null)
                {
                    oldEvents = new EventBean[] { oldDataMap };
                }
                else
                {
                    oldEvents = new EventBean[] { _lastNewEvent };
                }

                Instrument.With(
                    i => i.QViewIndicate(this, UnivariateStatisticsViewFactory.NAME, newEvents, oldEvents),
                    i => i.AViewIndicate(),
                    () => UpdateChildren(newEvents, oldEvents));

                _lastNewEvent = newDataMap;
            }

            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.Get().AViewProcessIRStream();
            }
        }