protected CalibrationCurve AddRSquared(CalibrationCurve curve, IList <WeightedPoint> points) { List <double> yValues = new List <double>(); List <double> residuals = new List <double>(); foreach (var point in points) { double?yFitted = curve.GetY(point.X); if (!yFitted.HasValue) { continue; } yValues.Add(point.Y); residuals.Add(point.Y - yFitted.Value); } if (!residuals.Any()) { return(curve); } double yMean = yValues.Average(); double totalSumOfSquares = yValues.Sum(y => (y - yMean) * (y - yMean)); double sumOfSquaresOfResiduals = residuals.Sum(r => r * r); double rSquared = 1 - sumOfSquaresOfResiduals / totalSumOfSquares; return(curve.ChangeRSquared(rSquared)); }
/// <summary> /// Optimization function used when doing NelderMeadSimplex to find the best Limit of Detection. /// </summary> private static double LodObjectiveFunction(double lod, IList <WeightedPoint> weightedPoints) { CalibrationCurve calibrationCurve = GetCalibrationCurveWithLod(lod, weightedPoints); if (calibrationCurve == null || !calibrationCurve.TurningPoint.HasValue) { return(double.MaxValue); } double totalDelta = 0; double totalWeight = 0; foreach (var pt in weightedPoints) { double delta = pt.Y - calibrationCurve.GetY(pt.X).Value; totalWeight += pt.Weight; totalDelta += pt.Weight * delta * delta; } return(totalDelta / totalWeight); }
public CalibrationCurve AddRSquared(CalibrationCurve curve, IList<WeightedPoint> points) { List<double> yValues = new List<double>(); List<double> residuals = new List<double>(); foreach (var point in points) { double? yFitted = curve.GetY(point.X); if (!yFitted.HasValue) { continue; } yValues.Add(point.Y); residuals.Add(point.Y - yFitted.Value); } if (!residuals.Any()) { return curve; } double yMean = yValues.Average(); double totalSumOfSquares = yValues.Sum(y => (y - yMean)*(y - yMean)); double sumOfSquaresOfResiduals = residuals.Sum(r => r*r); double rSquared = 1 - sumOfSquaresOfResiduals/totalSumOfSquares; return curve.ChangeRSquared(rSquared); }
private LineItem CreateInterpolatedLine(CalibrationCurve calibrationCurve, double minX, double maxX, int pointCount, bool logPlot) { PointPairList pointPairList = new PointPairList(); for (int i = 0; i < pointCount; i++) { double x; if (logPlot) { x = Math.Exp((Math.Log(minX)*(pointCount - 1 - i) + Math.Log(maxX)*i)/(pointCount - 1)); } else { x = (minX*(pointCount - 1 - i) + maxX*i)/(pointCount - 1); } double? y = calibrationCurve.GetY(x); if (y.HasValue) { pointPairList.Add(x, y.Value); } } if (!pointPairList.Any()) { return null; } return new LineItem(QuantificationStrings.Calibration_Curve, pointPairList, Color.Gray, SymbolType.None); }