// Helper functions private static double ComputeDensityEstimation(double pickupDelay, double duration, double interval, IEnumerable <Leg> legs) { MultivariateEmpiricalDistribution dist = new MultivariateEmpiricalDistribution(legs .Where(l => l.NumOfPassengersPickedUp > 0) .Select(l => (new double[] { GetPickupDelay(l), l.ArrivalTime.Subtract(l.StartTime).TotalMinutes })).ToArray()); double probDist = 1 - dist.DistributionFunction(new double[] { pickupDelay, duration }); // compute appropriate univariate distribution if (Math.Abs(dist.Variance[0]) < Double.Epsilon) { dist = new MultivariateEmpiricalDistribution(legs .Where(l => l.NumOfPassengersPickedUp > 0) .Select(l => (new double[] { l.ArrivalTime.Subtract(l.StartTime).TotalMinutes })).ToArray()); probDist = 1 - dist.DistributionFunction(new double[] { duration }); } double frequency = legs.Count(l => Math.Abs(l.StartTime.Subtract(DateTime.Now).TotalDays) < 2 && l.NumOfPassengersPickedUp > 0) / 5760.0; return(probDist * frequency * interval); }
public void WeightedEmpiricalDistributionConstructorTest2() { double[] original = { 5, 5, 1, 4, 1, 2, 2, 3, 3, 3, 4, 3, 3, 3, 4, 3, 2, 3 }; var distribution = new MultivariateEmpiricalDistribution(original.ToArray()); double[] weights = { 2, 1, 1, 1, 2, 3, 1, 3, 1, 1, 1, 1 }; double[] source = { 5, 1, 4, 1, 2, 3, 4, 3, 4, 3, 2, 3 }; double[][] samples = source.ToArray(); weights = weights.Divide(weights.Sum()); var target = new MultivariateEmpiricalDistribution(samples, weights, distribution.Smoothing); Assert.AreEqual(distribution.Mean[0], target.Mean[0]); Assert.AreEqual(distribution.Median[0], target.Median[0]); Assert.AreEqual(distribution.Mode[0], target.Mode[0]); Assert.AreEqual(distribution.Smoothing[0, 0], target.Smoothing[0, 0]); Assert.AreEqual(1.3655172413793104, target.Variance[0]); Assert.AreEqual(target.Weights, weights); Assert.AreEqual(target.Samples, samples); for (double x = 0; x < 6; x += 0.1) { double actual, expected; expected = distribution.ComplementaryDistributionFunction(x); actual = target.ComplementaryDistributionFunction(x); Assert.AreEqual(expected, actual, 1e-15); expected = distribution.DistributionFunction(x); actual = target.DistributionFunction(x); Assert.AreEqual(expected, actual, 1e-15); expected = distribution.LogProbabilityDensityFunction(x); actual = target.LogProbabilityDensityFunction(x); Assert.AreEqual(expected, actual, 1e-15); expected = distribution.ProbabilityDensityFunction(x); actual = target.ProbabilityDensityFunction(x); Assert.AreEqual(expected, actual, 1e-15); } }
public void FitTest() { double[] original = { 5, 5, 1, 4, 1, 2, 2, 3, 3, 3, 4, 3, 3, 3, 4, 3, 2, 3 }; var distribution = new MultivariateEmpiricalDistribution(original.ToJagged()); int[] weights = { 2, 1, 1, 1, 2, 3, 1, 3, 1, 1, 1, 1 }; double[] sources = { 5, 1, 4, 1, 2, 3, 4, 3, 4, 3, 2, 3 }; double[][] samples = sources.ToJagged(); var target = new MultivariateEmpiricalDistribution(Jagged.Zeros(1, 1)); target.Fit(samples, weights); Assert.AreEqual(distribution.Mean[0], target.Mean[0]); Assert.AreEqual(distribution.Median[0], target.Median[0]); Assert.AreEqual(distribution.Mode[0], target.Mode[0]); Assert.AreEqual(distribution.Smoothing[0, 0], target.Smoothing[0, 0]); Assert.AreEqual(distribution.Variance[0], target.Variance[0]); Assert.IsTrue(target.Weights.IsEqual(weights.Divide(weights.Sum()))); Assert.AreEqual(target.Samples, samples); for (double x = 0; x < 6; x += 0.1) { double actual, expected; expected = distribution.ComplementaryDistributionFunction(x); actual = target.ComplementaryDistributionFunction(x); Assert.AreEqual(expected, actual); expected = distribution.DistributionFunction(x); actual = target.DistributionFunction(x); Assert.AreEqual(expected, actual); expected = distribution.LogProbabilityDensityFunction(x); actual = target.LogProbabilityDensityFunction(x); Assert.AreEqual(expected, actual, 1e-15); expected = distribution.ProbabilityDensityFunction(x); actual = target.ProbabilityDensityFunction(x); Assert.AreEqual(expected, actual, 1e-15); } }
public void WeightedEmpiricalDistribution_DistributionFunction() { double[][] samples = { new double[] { 5, 2 }, new double[] { 1, 5 }, new double[] { 4, 7 }, new double[] { 1, 6 }, new double[] { 2, 2 }, new double[] { 3, 4 }, new double[] { 4, 8 }, new double[] { 3, 2 }, new double[] { 4, 4 }, new double[] { 3, 7 }, new double[] { 2, 4 }, new double[] { 3, 1 }, }; var target = new MultivariateEmpiricalDistribution(samples); double[] expected = { 0.33333333333333331, 0.083333333333333329, 0.83333333333333337, 0.16666666666666666, 0.083333333333333329, 0.41666666666666669, 0.91666666666666663, 0.25, 0.5, 0.66666666666666663, 0.16666666666666666, 0.083333333333333329 }; for (int i = 0; i < samples.Length; i++) { double e = expected[i]; double a = target.DistributionFunction(samples[i]); Assert.AreEqual(e, a); } }