public void TestInterpolationPointReplacement() { IWriteableInterpolable interp = new SmallDoubleInterpolable(2); IInterpolable interp2 = (IInterpolable)interp; interp.SetYValue(3.3, 6.6); interp.SetYValue(4.4, 8.8); interp.SetYValue(7.7, 12.8); interp.SetYValue(13.2, 22.8); interp.SetYValue(7.7, 17.8); Verify(interp2, 8.8, 18.8); }
public void TestInterpolationFrom2Points() { IWriteableInterpolable interp = new SmallDoubleInterpolable(2); IInterpolable interp2 = (IInterpolable)interp; interp.SetYValue(3.3, 6.6); interp.SetYValue(4.4, 8.8); Verify(interp2, 5.5, 11); Verify(interp2, 4.4, 8.8); Verify(interp2, 3.85, 7.7); Verify(interp2, 3.3, 6.6); Verify(interp2, 2.2, 4.4); Verify(interp2, 0.0, 0.0); Verify(interp2, -2.2, -4.4); }
public void TestInterpolationFrom4Points() { IWriteableInterpolable interp = new SmallDoubleInterpolable(4); IInterpolable interp2 = (IInterpolable)interp; interp.SetYValue(4.8, 2.7); interp.SetYValue(3.2, 1.5); interp.SetYValue(6.0, 4.5); interp.SetYValue(0.8, 0.6); Verify(interp2, 7.2, 6.3); Verify(interp2, 6.0, 4.5); Verify(interp2, 5.2, 3.3); Verify(interp2, 4.8, 2.7); Verify(interp2, 4.0, 2.1); Verify(interp2, 3.2, 1.5); Verify(interp2, 1.6, 0.9); Verify(interp2, 0.8, 0.6); Verify(interp2, 0.0, 0.3); Verify(interp2, -0.8, 0.0); }
/// <summary> /// Given a list of source items, and a function that returns a double value for each, this /// method ascertains the double value that represents the given percentile in that population. /// For example, given a list of students, and a function that returns their grades, this method /// will return the grade that represents the 50th percentile across that population of students. /// </summary> /// <typeparam name="T">The type of items in the list of source items.</typeparam> /// <param name="srcItems">The source items for which we want to know the percentile.</param> /// <param name="percentile">The percentile on the interval [0.0, 100.0] at which we seek the value from the source items.</param> /// <param name="valueGetter">The function that ascertains the value of each source item.</param> /// <param name="interpolate">if set to <c>true</c>, we interpolate between located items' values.</param> /// <returns></returns> public static double GetValueAtPercentile <T>(this List <T> srcItems, double percentile, Func <T, double> valueGetter, bool interpolate) { _Debug.Assert(srcItems.Count > 0, "Percentile was requested from a population of zero items. Percentile source populations must have at least one member."); _Debug.Assert(percentile >= 0.0 && percentile <= 100.0, string.Format("Percentile was requested as {0} - it must be a double on the interval [0.0 ... 100.0]", percentile)); percentile = percentile / 100.0; if (srcItems.Count == 1) { return(valueGetter(srcItems[0])); } else { srcItems.Sort((t1, t2) => Comparer <double> .Default.Compare(valueGetter(t1), valueGetter(t2))); double index = (srcItems.Count - 1) * percentile; double lowNdx = Math.Floor(index); double hiNdx = Math.Ceiling(index); if (lowNdx == hiNdx) { if (lowNdx == 0) { hiNdx++; } else { lowNdx--; } } double lowVal = valueGetter(srcItems[(int)lowNdx]); double hiVal = valueGetter(srcItems[(int)hiNdx]); if (interpolate) { SmallDoubleInterpolable sdi = new SmallDoubleInterpolable(new[] { lowNdx, hiNdx }, new[] { lowVal, hiVal }); return(sdi.GetYValue(index)); } else { return((index - lowNdx) < 0.5 ? lowVal : hiVal); } } }