public void InnerQuantileTest() { double[] quantiles = { -2.3396737042130806, -2.1060851851919309, -1.8587796492436919, -1.7515040214502977, -1.6631549706936311, -1.5649421094540212, -1.4760970897199182, -1.4120516891795316, -1.3472276831887715, -1.2800915764085863, -1.2315546431485036, -1.1733035015194753, -1.1275506999997809, -1.0868191452824896, -1.0423720676050061, -1.0030087867587449, -0.96427545374863111, -0.917480799606264, -0.88868683894166878, -0.85040868414900372, -0.80942702953353063, -0.78299794937710787, -0.74791530550923879, -0.71057667829968463, -0.6764786230399974, -0.64937712088706545, -0.61647747819758114, -0.585418062478127, -0.55212155586237877, -0.52794712262708809, -0.49602391921870309, -0.4699661621821, -0.44707572988386468, -0.41779003649017038, -0.38751278424822111, -0.3659754249474671, -0.33671101603741, -0.30844051169056652, -0.28736460398884939, -0.26394181175383763, -0.2339421108026867, -0.20421395179821347, -0.17975005820876525, -0.15495505128166037, -0.12881080807789203, -0.10882854018038969, -0.080502768973386082, -0.054592779524389491, -0.030746556623353873, 0.0010699779508669754, 0.018476164506076323, 0.042997842018717161, 0.068170326454891988, 0.098939711480485845, 0.12364243085219064, 0.14897752107634207, 0.17232065117344095, 0.19510514320430472, 0.21967681963331126, 0.25144866739098226, 0.26627058021030359, 0.28976112810281413, 0.325183138022793, 0.34611510490686043, 0.37135045464414679, 0.40484250840269187, 0.423660564514518, 0.45260008550109493, 0.47897070643517381, 0.513466904702678, 0.54074552445523427, 0.56782579073247685, 0.59191546380311844, 0.630594130276651, 0.66170186000470765, 0.69059427870805967, 0.72267836185626344, 0.75388989983592025, 0.78095231060517345, 0.81945443104186122, 0.85806474163877222, 0.88543000730858912, 0.9254742516670329, 0.96663287584250224, 1.0081099518226813, 1.0414716524617549, 1.0873521052324735, 1.138068925150572, 1.1769604530537776, 1.2209510765755074, 1.2805602443304192, 1.3529085306332467, 1.4111760504339896, 1.4822842454846386, 1.5518312997748602, 1.6439254270476189, 1.7357210363862619, 1.9281504259252962, 2.064331420559117, 2.3554568165928291, }; InnerQuantiles inner = new InnerQuantiles(quantiles); inner.GetQuantile(0.49471653842100138); inner.GetQuantile((double)quantiles.Length / (quantiles.Length + 1)); }
public void QuantileEstimator_SinglePointIsMedian() { QuantileEstimator est = new QuantileEstimator(0.1); double point = 2; est.Add(point); Assert.Equal(point, est.GetQuantile(0.5)); OuterQuantiles outer = new OuterQuantiles(new[] { point }); Assert.Equal(point, outer.GetQuantile(0.5)); InnerQuantiles inner = new InnerQuantiles(new[] { point }); Assert.Equal(point, inner.GetQuantile(0.5)); }
public void InnerQuantiles_GetQuantile_IsIncreasing() { var quantiles = new double[] { 0, 4.94065645841247E-324, 4.94065645841247E-324, 4.0000000000000009 }; var innerQuantiles = new InnerQuantiles(quantiles); int n = 100; double previousQuantile = double.MinValue; for (int i = 0; i < n; i++) { double probability = (i + 1.0) / (n + 1); double quantile = innerQuantiles.GetQuantile(probability); Assert.True(quantile >= previousQuantile); previousQuantile = quantile; } }
public void QuantileEstimator_MedianTest() { double middle = 3.4; double[] x = { 1.2, middle, 5.6 }; var outer = new OuterQuantiles(x); Assert.Equal(outer.GetQuantile(0.5), middle); var inner = new InnerQuantiles(3, outer); Assert.Equal(inner.GetQuantile(0.5), middle); var est = new QuantileEstimator(0.01); est.AddRange(x); Assert.Equal(est.GetQuantile(0.5), middle); }
public void QuantileTest() { // draw many samples from N(m,v) Rand.Restart(0); int n = 10000; double m = 2; double stddev = 3; Gaussian prior = new Gaussian(m, stddev * stddev); List <double> x = new List <double>(); for (int i = 0; i < n; i++) { x.Add(prior.Sample()); } x.Sort(); var sortedData = new OuterQuantiles(x.ToArray()); // compute quantiles var quantiles = new InnerQuantiles(100, sortedData); // loop over x's and compare true quantile rank var testPoints = EpTests.linspace(MMath.Min(x) - stddev, MMath.Max(x) + stddev, 100); double maxError = 0; foreach (var testPoint in testPoints) { var trueRank = MMath.NormalCdf((testPoint - m) / stddev); var estRank = quantiles.GetProbLessThan(testPoint); var error = System.Math.Abs(trueRank - estRank); //Trace.WriteLine($"{testPoint} trueRank={trueRank} estRank={estRank} error={error}"); Assert.True(error < 0.02); maxError = System.Math.Max(maxError, error); double estQuantile = quantiles.GetQuantile(estRank); error = MMath.AbsDiff(estQuantile, testPoint, 1e-8); //Trace.WriteLine($"{testPoint} estRank={estRank} estQuantile={estQuantile} error={error}"); Assert.True(error < 1e-8); estRank = sortedData.GetProbLessThan(testPoint); error = System.Math.Abs(trueRank - estRank); //Trace.WriteLine($"{testPoint} trueRank={trueRank} estRank={estRank} error={error}"); Assert.True(error < 0.02); } //Trace.WriteLine($"max rank error = {maxError}"); }
public void QuantileEstimator_MedianTest() { double left = 1.2; double middle = 3.4; double right = 5.6; double[] x = { left, middle, right }; var outer = new OuterQuantiles(x); Assert.Equal(middle, outer.GetQuantile(0.5)); var inner = new InnerQuantiles(3, outer); Assert.Equal(middle, inner.GetQuantile(0.5)); inner = new InnerQuantiles(x); CheckGetQuantile(inner, inner, 25, 75); var est = new QuantileEstimator(0.01); est.AddRange(x); Assert.Equal(est.GetQuantile(0.5), middle); }