public void DefaultConstructor()
        {
            TimeWeightedVariance twv = new TimeWeightedVariance(new Simulation());

            Assert.AreEqual(0, twv.Observations);
            Assert.IsTrue(Double.IsNaN(twv.Value));

            TimeValue lastObservation = twv.LastObservation;

            Assert.IsFalse(lastObservation.IsValid);
        }
        public void VarianceOfFewObservations()
        {
            TimeWeightedVariance twv = new TimeWeightedVariance(new Simulation());

            twv.Observe(3.0, 0L);
            twv.Observe(2.0, 4L);
            twv.Observe(6.0, 7L);
            twv.Observe(4.0, 8L);
            twv.Observe(2.0, 10L);
            Assert.AreEqual(1.36, twv.Value, 0.00001);
        }
        public void VarianceOfZeroWeights()
        {
            double d = 321.0;
            TimeWeightedVariance twv = new TimeWeightedVariance(new Simulation());

            for (int i = 0; i < 1000; i++)
            {
                twv.Observe(d, 0L);
            }

            Assert.AreEqual(Double.NaN, twv.Value);
        }
        public void VarianceOfSameObservations()
        {
            double d = 321.0;
            TimeWeightedVariance twv = new TimeWeightedVariance(new Simulation());

            for (int i = 0; i < 1000; i++)
            {
                twv.Observe(d, i);
            }

            Assert.AreEqual(1000, twv.Observations);
            Assert.AreEqual(0.0, twv.Value);

            TimeValue lastObservation = twv.LastObservation;

            Assert.IsTrue(lastObservation.IsValid);
            Assert.AreEqual(d, lastObservation.Value);
            Assert.AreEqual(999, lastObservation.Time);
        }
        public void VarianceOfManyObservations()
        {
            IUniform  valGen          = UniformStreams.DefaultStreams.GetUniform();
            IUniform  timeGen         = UniformStreams.DefaultStreams.GetUniform();
            TimeValue lastObservation = new TimeValue(0L, valGen.NextDouble() * 100.0);

            double[] weights = new double[1000];
            double[] values  = new double[1000];

            TimeWeightedVariance twv = new TimeWeightedVariance(new Simulation());
            TimeWeightedMean     twm = new TimeWeightedMean(new Simulation());

            twm.Observe(lastObservation.Value, lastObservation.Time);
            twv.Observe(lastObservation.Value, lastObservation.Time);

            for (int i = 0; i < 1000; i++)
            {
                long      newTime        = lastObservation.Time + (long)(timeGen.NextDouble() * 10.0);
                TimeValue newObservation = new TimeValue(newTime, valGen.NextDouble() * 100.0);
                double    weight         = newObservation.Time - lastObservation.Time;

                twm.Observe(newObservation.Value, newObservation.Time);
                twv.Observe(newObservation.Value, newObservation.Time);

                weights[i] = weight;
                values[i]  = lastObservation.Value;

                lastObservation = newObservation;
            }

            double sumW     = 0.0;
            double sumWVals = 0.0;

            for (int i = 0; i < 1000; i++)
            {
                sumW     += weights[i];
                sumWVals += weights[i] * Math.Pow(values[i] - twm.Value, 2.0);
            }

            double variance = sumWVals / sumW;

            Assert.AreEqual(variance, twv.Value, 0.00001);
        }
        public void AttachAndDetach()
        {
            TimeWeightedVariance twv = new TimeWeightedVariance(new Simulation());

            twv.ObserveOnAttach = true;
            twv.ObserveOnDetach = true;

            _valueForTesting = 5.5;
            twv.Attach(this, "ValueForTesting");
            TimeValue lastObservation = twv.LastObservation;

            Assert.AreEqual(1, twv.Observations);
            Assert.AreEqual(5.5, lastObservation.Value);

            _valueForTesting = 10.0;
            twv.Detach(this, "ValueForTesting");
            lastObservation = twv.LastObservation;
            Assert.AreEqual(2, twv.Observations);
            Assert.AreEqual(10.0, lastObservation.Value);
        }
 public void RequiresNonNullSimulation()
 {
     TimeWeightedVariance twv = new TimeWeightedVariance(null);
 }