예제 #1
0
        public void Test_Poly4()
        {
            double[] param = new double[2];
            param[0] = 1;
            param[1] = 1E10;


            double[] ys   = new double[10];
            int      info = 0;

            do
            {
                NLFit.LevenbergMarquardtFit(new NLFit.LMFunction(this.poly4), param, ys, 1E-10, ref info);
            } while (info == 5);

            Assert.AreEqual(1.08222543840634, param[0], 1E-4, "Fit parameter 0 should be 1.08222543840634 in this model");
            Assert.AreEqual(86441011642265, param[1], 1E4, "Fit parameter 1 should be 86441011642265 in this model");

            double[] covar = new double[2 * 2];
            double   chisqr;

            NLFit.ComputeCovariances(new NLFit.LMFunction(this.poly4), param, 10, 2, covar, out chisqr);
            // Expected error for parameter1 is: 0.115944838291204
            // Expected error for parameter2 is: 14249106469708
            Assert.AreEqual(0.115944838291204, Math.Sqrt(covar[0]), 1E-5, "Square root of covariance of parameter0 should be 0.115944838291204");
            Assert.AreEqual(14249106469708, Math.Sqrt(covar[3]), 142491064, "Square root of covariance of parameter1 should be 14249106469708");
            // Origin calculates a dependence value of 0.92115 out here
            double dependency = (covar[1] * covar[1]) / (covar[0] * covar[3]);

            Assert.AreEqual(0.92115, dependency, 1E-4, "Dependency should be around 0.92115");
        }
예제 #2
0
        public void Test_Poly2()
        {
            double[] param = new double[2];
            param[0] = 0.1;
            param[1] = 1.01;


            double[] ys   = new double[9];
            int      info = 0;

            do
            {
                NLFit.LevenbergMarquardtFit(new NLFit.LMFunction(this.poly2), param, ys, 1E-10, ref info);
            } while (info == 5);

            Assert.AreEqual(0.1111111, param[0], 1E-4, "Fit parameter 0 should be 0.11111 in this model");
            Assert.AreEqual(1, param[1], 1E-4, "Fit parameter 1 should be 1 in this model");

            double[] covar = new double[2 * 2];
            double   chisqr;

            NLFit.ComputeCovariances(new NLFit.LMFunction(this.poly2), param, 9, 2, covar, out chisqr);
            Assert.AreEqual(0.670194, covar[0], 0.001);
            Assert.AreEqual(-0.10582, covar[1], 0.001);
            Assert.AreEqual(-0.10582, covar[2], 0.001);
            Assert.AreEqual(0.021164, covar[3], 0.001);
        }
예제 #3
0
        public void ZeroElements()
        {
            arr[0] = 1;
            double r = NLFit.enorm(0, arr);

            NUnit.Framework.Assert.AreEqual(0.0, r, 0);
        }
예제 #4
0
        public void FitFunction7malCos3xplus1(int numberOfYs, int numberOfParameter, double[] parameter, double[] ys, ref int info)
        {
            Assert.IsTrue(numberOfParameter == 3, "NumberOfParameter must be 3 in this test.");
            Assert.IsTrue(numberOfYs == 3, "NumberOfYs must be 3 in this test.");
            Assert.IsTrue(parameter.Length == 3, "Length of parameter array must be 3 in this test.");
            Assert.IsTrue(ys.Length == 3, "Length of ys array must be 3 in this test.");

            // we use the simplest model y=a+bx in the range -10 ..10 with step size of 1 to
            // calculate the delta
            double a = parameter[0];
            double b = parameter[1];
            double c = parameter[2];

            double sum_se = 0;
            double x;

            for (x = -10; x <= 10; x += 1)
            {
                sum_se += NLFit.sqr((a * Math.Cos(b * x + c)) - (7 * Math.Cos(3 * x + 1)));
            }

            ys[0] = sum_se;
            ys[1] = 0;
            ys[2] = 0;
        }
예제 #5
0
        public void Fit1()
        {
            int info = 0;

            double[] differences = new double[NumberOfData];
            NLFit.LevenbergMarquardtFit(new NLFit.LMFunction(EvaluateFitDifferences), _cachedVaryingParameters, differences, 1E-10, ref info);

            _resultingCovariances = new double[_cachedVaryingParameters.Length * _cachedVaryingParameters.Length];
            NLFit.ComputeCovariances(new NLFit.LMFunction(EvaluateFitDifferences), _cachedVaryingParameters, NumberOfData, _cachedVaryingParameters.Length, _resultingCovariances, out _resultingSumChiSquare, out _resultingSigmaSquare);
        }
예제 #6
0
        public void NormalAndSmallElements()
        {
            // we use the law 3^2 + 4^2 = 5^2
            arr[0] = 400E-20;;
            for (int i = 1; i < 90001; i++)
            {
                arr[i] = 1E-20;
            }
            double r = NLFit.enorm(90001, arr);

            NUnit.Framework.Assert.AreEqual(500E-20, r, 0);
        }
예제 #7
0
        public void Test_2plus5xMod()
        {
            double[] param = new double[2];
            param[0] = 1;
            param[1] = 1;
            double[] ys   = new double[1];
            int      info = 0;

            NLFit.LevenbergMarquardtFit(new NLFit.LMFunction(this.FitFunction2plus5xMod), param, ys, 1E-10, ref info);

            Assert.AreEqual(0, info, "Info should be 0 due to inappropriate length of ys in this model");
        }
예제 #8
0
        public void OneElement()
        {
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = (i + 1);
            }

            for (int i = 0; i < arr.Length; i++)
            {
                double r = NLFit.enorm(1, arr, i);
                NUnit.Framework.Assert.AreEqual((double)(i + 1), r, 0);
            }
        }
예제 #9
0
        public void Test_2plus5x()
        {
            double[] param = new double[2];
            param[0] = 1;
            param[1] = 1;
            double[] ys   = new double[2];
            int      info = 0;

            NLFit.LevenbergMarquardtFit(new NLFit.LMFunction(this.FitFunction2plus5x), param, ys, 1E-10, ref info);

            Assert.AreEqual(2, param[0], 1E-5, "Fit parameter 0 should be 2 in this model");
            Assert.AreEqual(5, param[1], 1E-5, "Fit parameter 1 should be 5 in this model");
        }
예제 #10
0
        public void Test_Poly5()
        {
            double[] param = new double[3];
            param[0] = 1;
            param[1] = 1;
            param[2] = 1;

            double[] ys   = new double[10];
            int      info = 0;

            do
            {
                NLFit.LevenbergMarquardtFit(new NLFit.LMFunction(this.poly5), param, ys, 1E-10, ref info);
            } while (info == 5);

            Assert.AreEqual(1.13158682895444, param[0], 1E-4, "Fit parameter 0 should be 1.13158682895444 in this model");
            Assert.AreEqual(0.00823465185064109, param[1], 1E-6, "Fit parameter 1 should be 0.00823465185064109 in this model");
            Assert.AreEqual(-1.14393644797462, param[2], 1E-4, "Fit parameter 1 should be -1.14393644797462 in this model");

            double[] covar = new double[3 * 3];
            double   chisqr;

            NLFit.ComputeCovariances(new NLFit.LMFunction(this.poly5), param, 10, 3, covar, out chisqr);
            // Expected error for parameter1 is: 0.115944838291204
            // Expected error for parameter2 is: 14249106469708
            Assert.AreEqual(0.19511, Math.Sqrt(covar[0]), 1e-4, "Square root of covariance of parameter0 should be 0.19511");
            Assert.AreEqual(0.00197, Math.Sqrt(covar[3 * 1 + 1]), 1e-5, "Square root of covariance of parameter1 should be 0.00197");
            Assert.AreEqual(3.50947, Math.Sqrt(covar[3 * 2 + 2]), 1e-4, "Square root of covariance of parameter1 should be 3.50947");

            double dep;
            double dep01 = (covar[3 * 0 + 1] * covar[3 * 1 + 0]) / (covar[3 * 0 + 0] * covar[3 * 1 + 1]);
            double dep02 = (covar[3 * 0 + 2] * covar[3 * 2 + 0]) / (covar[3 * 0 + 0] * covar[3 * 2 + 2]);

            dep = 1 - (1 - dep01) * (1 - dep02);
            Assert.AreEqual(0.96865, dep, 1E-2, "Dependency should be around 0.97");

            double dep10 = (covar[3 * 1 + 0] * covar[3 * 0 + 1]) / (covar[3 * 1 + 1] * covar[3 * 0 + 0]);
            double dep12 = (covar[3 * 1 + 2] * covar[3 * 2 + 1]) / (covar[3 * 1 + 1] * covar[3 * 2 + 2]);

            dep = 1 - (1 - dep10) * (1 - dep12);
            Assert.AreEqual(0.95335, dep, 1E-2, "Dependency should be around 0.95");

            double dep20 = (covar[3 * 2 + 0] * covar[3 * 0 + 2]) / (covar[3 * 2 + 2] * covar[3 * 0 + 0]);
            double dep21 = (covar[3 * 2 + 1] * covar[3 * 1 + 2]) / (covar[3 * 2 + 2] * covar[3 * 1 + 1]);

            dep = 1 - (1 - dep20) * (1 - dep21);
            Assert.AreEqual(0.755, dep, 1E-2, "Dependency should be around 0.755");
        }
예제 #11
0
        public void Test_7malCos3xplus1()
        {
            double[] param = new double[3];
            param[0] = 1;
            param[1] = 3.1;
            param[2] = 1.1;

            double[] ys   = new double[3];
            int      info = 0;

            NLFit.LevenbergMarquardtFit(new NLFit.LMFunction(this.FitFunction7malCos3xplus1), param, ys, 1E-10, ref info);

            Assert.AreEqual(7, param[0], 1E-4, "Fit parameter 0 should be 7 in this model");
            Assert.AreEqual(3, param[1], 1E-4, "Fit parameter 1 should be 3 in this model");
            Assert.AreEqual(1, param[2], 1E-4, "Fit parameter 2 should be 1 in this model");
        }
예제 #12
0
        public void FitFunction7malCos3xplus1Mod(int numberOfYs, int numberOfParameter, double[] parameter, double[] ys, ref int info)
        {
            Assert.IsTrue(numberOfParameter == 3, "NumberOfParameter must be 3 in this test.");
            Assert.IsTrue(numberOfYs == 21, "NumberOfYs must be 21 in this test.");
            Assert.IsTrue(parameter.Length == 3, "Length of parameter array must be 3 in this test.");
            Assert.IsTrue(ys.Length == 21, "Length of ys array must be 21 in this test.");

            // we use the simplest model y=a+bx in the range -10 ..10 with step size of 1 to
            // calculate the delta
            double a = parameter[0];
            double b = parameter[1];
            double c = parameter[2];

            for (int i = -10; i <= 10; i++)
            {
                ys[i + 10] = NLFit.sqr((a * Math.Cos(b * i + c)) - (7 * Math.Cos(3 * i + 1)));
            }
        }
예제 #13
0
        public void Test_Poly3()
        {
            double[] param = new double[2];
            param[0] = 1;
            param[1] = 1;

            double[] ys   = new double[2];
            int      info = 0;

            do
            {
                NLFit.LevenbergMarquardtFit(new NLFit.LMFunction(poly3), param, ys, 1E-10, ref info);
            } while (info == 5);

            Assert.AreEqual(0.1111111, param[0], 1E-4, "Fit parameter 0 should be 0.11111 in this model");
            Assert.AreEqual(1, param[1], 1E-4, "Fit parameter 1 should be 1 in this model");

            double[] covar = new double[2 * 2];
            NLFit.ComputeCovariances(new NLFit.LMFunction(poly3), param, 2, 2, covar, out var chisqr, out var sigmaSquare);
        }
예제 #14
0
        public void FitFunction2plus5xMod(int numberOfYs, int numberOfParameter, double[] parameter, double[] ys, ref int info)
        {
            Assert.IsTrue(numberOfParameter == 2, "NumberOfParameter must be 2 in this test.");
            Assert.IsTrue(numberOfYs == 1, "NumberOfYs must be 1 in this test.");
            Assert.IsTrue(parameter.Length == 2, "Length of parameter array must be 2 in this test.");
            Assert.IsTrue(ys.Length == 1, "Length of ys array must be 1 in this test.");

            // we use the simplest model y=a+bx in the range -10 ..10 with step size of 1 to
            // calculate the delta
            double a = parameter[0];
            double b = parameter[1];

            double sum_se = 0;
            double x;

            for (x = -10; x <= 10; x += 1)
            {
                sum_se += NLFit.sqr((a + b * x) - (2 + 5 * x));
            }

            ys[0] = sum_se;
        }
예제 #15
0
        /// <summary>
        /// Can only be used, if all fit functions provide the jacobian.
        /// </summary>
        public void Fit2Jac()
        {
            //int info = 0;
            object workingmemory = null;

            NonLinearFit2.LEVMAR_DER(
                new NonLinearFit2.FitFunction(EvalulateFitValues),
                new NonLinearFit2.JacobianFunction(EvaluateFitJacobian),
                _cachedVaryingParameters,
                _cachedDependentValues,
                _cachedWeights,
                100,  // itmax
                null, // opts,
                null, // info,
                ref workingmemory,
                null, // covar,
                null  // arbitrary data
                );

            _resultingCovariances = new double[_cachedVaryingParameters.Length * _cachedVaryingParameters.Length];
            NLFit.ComputeCovariances(new NLFit.LMFunction(EvaluateFitDifferences), _cachedVaryingParameters, NumberOfData, _cachedVaryingParameters.Length, _resultingCovariances, out _resultingSumChiSquare, out _resultingSigmaSquare);
        }
예제 #16
0
 static void Main(string[] args)
 {
     double[] arr    = { 1E20, 1, 1E-20 };
     double   result = NLFit.enorm(3, arr);
 }