Exemple #1
0
        public void MeanOfManyObservations()
        {
            IUniform  valGen          = UniformStreams.DefaultStreams.GetUniform();
            IUniform  timeGen         = UniformStreams.DefaultStreams.GetUniform();
            double    weightedSum     = 0.0;
            double    sumOfWeights    = 0.0;
            TimeValue lastObservation = new TimeValue(0L, valGen.NextDouble() * 100.0);

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

            twm.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;
                weightedSum  += weight * lastObservation.Value;
                sumOfWeights += weight;

                twm.Observe(newObservation.Value, newObservation.Time);
                lastObservation = newObservation;
            }

            double mean = weightedSum / sumOfWeights;

            Assert.AreEqual(mean, twm.Value);
        }
Exemple #2
0
        public void MeanOfFewObservations()
        {
            TimeWeightedMean twm = new TimeWeightedMean(new Simulation());

            twm.Observe(3.0, 0L);
            twm.Observe(2.0, 4L);
            twm.Observe(6.0, 7L);
            twm.Observe(4.0, 8L);
            twm.Observe(2.0, 10L);
            Assert.AreEqual(3.2, twm.Value);
        }
        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);
        }
Exemple #4
0
        public void MeanOfZeroWeights()
        {
            double           d   = 123.0;
            TimeWeightedMean twm = new TimeWeightedMean(new Simulation());

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

            Assert.AreEqual(Double.NaN, twm.Value);
        }
Exemple #5
0
        public void MeanOfSameObservations()
        {
            double           d   = 123.0;
            TimeWeightedMean twm = new TimeWeightedMean(new Simulation());

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

            Assert.AreEqual(1000, twm.Observations);
            Assert.AreEqual(d, twm.Value);

            TimeValue lastObservation = twm.LastObservation;

            Assert.IsTrue(lastObservation.IsValid);
            Assert.AreEqual(d, lastObservation.Value);
            Assert.AreEqual(999, lastObservation.Time);
        }