public void MaritzJarrettConfidenceIntervalWeighedTest1() { var random = new Random(42); var distribution = new BetaDistribution(2, 10); double[] data = distribution.Random(random).Next(10); var sample1 = new Sample(data); var sample2 = new Sample(data, new ConstantSequence(1).GenerateArray(data.Length)); var sample3 = new Sample( data.Concat(new[] { 10.0, 10.0, 10.0 }).ToArray(), new ConstantSequence(1).GenerateArray(data.Length).Concat(new[] { 0.0, 0.0, 0.0 }).ToArray() ); var estimator = HarrellDavisQuantileEstimator.Instance; var probability = Probability.Half; var level = ConfidenceLevel.L90; var ci1 = estimator.GetQuantileConfidenceIntervalEstimator(sample1, probability).GetConfidenceInterval(level); var ci2 = estimator.GetQuantileConfidenceIntervalEstimator(sample2, probability).GetConfidenceInterval(level); var ci3 = estimator.GetQuantileConfidenceIntervalEstimator(sample3, probability).GetConfidenceInterval(level); Output.WriteLine("CI1: " + ci1.ToString("N9")); Output.WriteLine("CI2: " + ci2.ToString("N9")); Output.WriteLine("CI3: " + ci3.ToString("N9")); var comparer = new AbsoluteEqualityComparer(1e-9); Assert.True(ci1.Equals(ci2, comparer)); Assert.True(ci1.Equals(ci3, comparer)); }
public void HarrellDavisQuantileEstimatorWeightedTest1() { var random = new Random(42); var distribution = new BetaDistribution(2, 10); double[] data1 = distribution.Random(random).Next(10); double[] data2 = data1.Select(x => x + 100).ToArray(); double[] data = data1.Concat(data2).ToArray(); var samples = new List <Sample> { new Sample(data1) }; for (int i = 1; i <= 40; i++) { samples.Add(new Sample(data, ExponentialDecaySequence.CreateFromHalfLife(i).GenerateArray(data.Length))); } samples.Add(new Sample(data)); var medians = samples.Select(s => HarrellDavisQuantileEstimator.Instance.GetMedian(s)).ToList(); for (int i = 0; i < medians.Count; i++) { Output.WriteLine($"Median[{i}] = {medians[i]:N9}"); } for (int i = 0; i < medians.Count - 1; i++) { Assert.True(medians[i] < medians[i + 1]); } }
public void MaritzJarrettConfidenceIntervalTest(double confidenceLevel, double minSuccessRate, double maxSuccessRate) { var random = new Random(42); var distribution = new BetaDistribution(2, 10); var generator = new LimitedRandomGenerator(distribution.Random(random).Next(1000), random); var estimator = HarrellDavisQuantileEstimator.Instance; double median = distribution.Median; const int iterations = 1000; for (int n = 5; n <= 10; n++) { int successCount = 0; for (int i = 0; i < iterations; i++) { var sample = generator.Next(n).ToSample(); var confidenceInterval = estimator .GetQuantileConfidenceIntervalEstimator(sample, 0.5) .GetConfidenceInterval(confidenceLevel); if (confidenceInterval.Contains(median)) { successCount++; } } double successRate = successCount * 1.0 / iterations; Output.WriteLine($"n = {n}, successRate = {successRate:N4}"); Assert.True(minSuccessRate <= successRate && successRate <= maxSuccessRate); } }
public void BetaGenerateTest() { Accord.Math.Tools.SetupGenerator(0); int n = 100000; double[] samples = BetaDistribution .Random(alpha: 2, beta: 3, samples: n); Assert.AreEqual(n, samples.Length); var actual = BetaDistribution.Estimate(samples); Assert.AreEqual(2, actual.Alpha, 1e-2); Assert.AreEqual(3, actual.Beta, 1e-2); }
public void BetaGenerateTest3() { Accord.Math.Tools.SetupGenerator(0); int n = 1000000; double[] samples = BetaDistribution .Random(alpha: 0.4, beta: 0.2, samples: n); Assert.AreEqual(n, samples.Length); var actual = BetaDistribution.Estimate(samples, new BetaOptions { Method = BetaEstimationMethod.MaximumLikelihood }); Assert.AreEqual(0.4, actual.Alpha, 0.005); Assert.AreEqual(0.2, actual.Beta, 0.003); }