Beispiel #1
0
        public void CompareIntegerWeights()
        {
            Random        random          = new Random((int)DateTime.Now.Ticks);
            List <double> xRepeatedValues = new List <double>();
            List <double> yRepeatedValues = new List <double>();
            List <int>    weights         = new List <int>();
            var           sampleTuples    = new List <Tuple <double[], double> >();

            for (int i = 0; i < 10; i++)
            {
                int weight = random.Next(1, 10);
                weights.Add(weight);
                double x = random.NextDouble();
                xRepeatedValues.AddRange(Enumerable.Repeat(x, weight));
                double y = random.NextDouble();
                yRepeatedValues.AddRange(Enumerable.Repeat(y, weight));
                sampleTuples.Add(new Tuple <double[], double>(new[] { x }, y));
            }
            const double epsilon           = 1E-12;
            var          repeatedIntercept = Statistics.Intercept(new Statistics(yRepeatedValues),
                                                                  new Statistics(xRepeatedValues));
            var repeatedSlope = Statistics.Slope(new Statistics(yRepeatedValues), new Statistics(xRepeatedValues));
            var repeatedSlopeWithoutIntercept = SlopeWithoutIntercept(yRepeatedValues, xRepeatedValues);

            var weightedRegression = WeightedRegression.Weighted(sampleTuples, weights.Select(w => (double)w).ToArray(), true);

            Assert.AreEqual(repeatedIntercept, weightedRegression[0], epsilon);
            Assert.AreEqual(repeatedSlope, weightedRegression[1], epsilon);
            var weightedRegressionWithoutIntercept = WeightedRegression.Weighted(sampleTuples,
                                                                                 weights.Select(w => (double)w).ToArray());

            Assert.AreEqual(repeatedSlopeWithoutIntercept, weightedRegressionWithoutIntercept[0], epsilon);
        }
Beispiel #2
0
        public static CalibrationCurve LinearFitThroughZero(IList <WeightedPoint> points)
        {
            // ReSharper disable RedundantArgumentDefaultValue
            var values = WeightedRegression.Weighted(points.Select(p => new Tuple <double[], double>(new[] { p.X }, p.Y)),
                                                     points.Select(p => p.Weight).ToArray(), false);

            // ReSharper restore RedundantArgumentDefaultValue
            return(new CalibrationCurve().ChangePointCount(points.Count).ChangeSlope(values[0]));
        }
Beispiel #3
0
        protected static CalibrationCurve LinearFit(IList <WeightedPoint> points)
        {
            CalibrationCurve calibrationCurve = new CalibrationCurve().ChangePointCount(points.Count);

            try
            {
                double[] values = WeightedRegression.Weighted(points.Select(p => new Tuple <double[], double>(new[] { p.X }, p.Y)),
                                                              points.Select(p => p.Weight).ToArray(), true);
                calibrationCurve = calibrationCurve.ChangeSlope(values[1]).ChangeIntercept(values[0]);
            }
            catch (Exception ex)
            {
                calibrationCurve = calibrationCurve.ChangeErrorMessage(ex.Message);
            }
            return(calibrationCurve);
        }
Beispiel #4
0
 /// <summary>
 /// Weighted Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) and weights w to a linear surface y : X -> p0*x0 + p1*x1 + ... + pk*xk,
 /// returning its best fitting parameters as [p0, p1, p2, ..., pk] array.
 /// </summary>
 public static double[] MultiDimWeighted(double[][] x, double[] y, double[] w)
 {
     return(WeightedRegression.Weighted(x, y, w));
 }
Beispiel #5
0
        /// <summary>
        /// Weighted Least-Squares fitting the points (x,y) and weights w to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k,
        /// returning its best fitting parameters as [p0, p1, p2, ..., pk] array, compatible with Evaluate.Polynomial.
        /// A polynomial with order/degree k has (k+1) coefficients and thus requires at least (k+1) samples.
        /// </summary>
        public static double[] PolynomialWeighted(double[] x, double[] y, double[] w, int order)
        {
            var design = Matrix <double> .Build.Dense(x.Length, order + 1, (i, j) => Math.Pow(x[i], j));

            return(WeightedRegression.Weighted(design, Vector <double> .Build.Dense(y), Matrix <double> .Build.Diagonal(w)).ToArray());
        }