Ejemplo n.º 1
0
        double[] diffedRange(double z, double[] a)
        {
            Func <double[], double> fbeta3 = x => fD(z, a);
            var fDiffs = new FiniteDifferences(N, fbeta3);

            double[] result = fDiffs.Gradient(a);
            return(result);
        }
        public void LinearTest()
        {
            double[,] quadraticTerms =
            {
                { 0, 0, 0 },
                { 0, 0, 0 },
                { 0, 0, 0 },
            };

            double[] linearTerms = { 1, 2, 3 };

            var target = new QuadraticObjectiveFunction(quadraticTerms, linearTerms);

            var function = target.Function;
            var gradient = target.Gradient;

            FiniteDifferences fd = new FiniteDifferences(3, function);

            double[][] x =
            {
                new double[] {      1,  2,   3 },
                new double[] {      3,  1,   4 },
                new double[] {     -6,  5,   9 },
                new double[] {     31, 25, 246 },
                new double[] { -0.102,  0,  10 },
            };


            { // Function test
                for (int i = 0; i < x.Length; i++)
                {
                    double expected = 0.5 *
                                      (x[i]
                                       .Dot(quadraticTerms))
                                      .Dot(x[i]) + linearTerms
                                      .Dot(x[i]);

                    double actual = function(x[i]);

                    Assert.AreEqual(expected, actual, 1e-8);
                }
            }

            { // Gradient test
                for (int i = 0; i < x.Length; i++)
                {
                    double[] expected = fd.Gradient(x[i]);
                    double[] actual   = gradient(x[i]);

                    for (int j = 0; j < actual.Length; j++)
                    {
                        Assert.AreEqual(expected[j], actual[j], 1e-8);
                    }
                }
            }
        }
        /// <summary>
        ///   Finds the minimum value of a function. The solution vector
        ///   will be made available at the <see cref="IOptimizationMethod{TInput, TOutput}.Solution"/> property.
        /// </summary>
        ///
        /// <returns>Returns <c>true</c> if the method converged to a <see cref="IOptimizationMethod{TInput, TOutput}.Solution"/>.
        ///   In this case, the found value will also be available at the <see cref="IOptimizationMethod{TInput, TOutput}.Value"/>
        ///   property.</returns>
        ///
        public override bool Minimize()
        {
            if (Gradient == null)
            {
                this.Gradient = FiniteDifferences.Gradient(Function, NumberOfVariables);
            }

            NonlinearObjectiveFunction.CheckGradient(Gradient, Solution);

            return(base.Minimize());
        }
        public void ComputeTest()
        {
            int numberOfParameters   = 2;
            FiniteDifferences target = new FiniteDifferences(numberOfParameters);

            double[] inputs = { -1, 0.4 };

            target.Function = BroydenFletcherGoldfarbShannoTest.rosenbrockFunction;

            double[] expected = BroydenFletcherGoldfarbShannoTest.rosenbrockGradient(inputs);
            double[] actual   = target.Gradient(inputs);

            Assert.IsTrue(expected.IsEqual(actual, 0.05));
        }
        /// <summary>
        ///   Finds the maximum value of a function. The solution vector
        ///   will be made available at the <see cref="IOptimizationMethod{TInput, TOutput}.Solution"/> property.
        /// </summary>
        ///
        /// <returns>Returns <c>true</c> if the method converged to a <see cref="IOptimizationMethod{TInput, TOutput}.Solution"/>.
        ///   In this case, the found value will also be available at the <see cref="IOptimizationMethod{TInput, TOutput}.Value"/>
        ///   property.</returns>
        ///
        public override bool Maximize()
        {
            if (Gradient == null)
            {
                this.Gradient = FiniteDifferences.Gradient(Function, NumberOfVariables);
            }

            NonlinearObjectiveFunction.CheckGradient(Gradient, Solution);

            var g = Gradient;

            Gradient = (x) => g(x).Multiply(-1);

            bool success = base.Maximize();

            Gradient = g;

            return(success);
        }
        public void ComputeTest2()
        {
            #region doc_gradient
            // Create a simple function with two parameters: f(x,y) = x² + y
            Func <double[], double> function = x => Math.Pow(x[0], 2) + x[1];

            // The gradient w.r.t to x should be 2x,
            // the gradient w.r.t to y should be  1


            // Create a new finite differences calculator
            var calculator = new FiniteDifferences(2, function);

            // Evaluate the gradient function at the point (2, -1)
            double[] result = calculator.Gradient(new double[] { 2, -1 }); // answer is (4, 1)
            #endregion

            Assert.AreEqual(4, result[0], 1e-10);
            Assert.AreEqual(1, result[1], 1e-10);
        }
        public void test_order()
        {
            // https://www.wolframalpha.com/input/?i=third+derivative+of+(1+-+x)%5E2+%2B+100(y+-+x%5E2)%5E2+at+(-1,0.4)

            int numberOfParameters   = 2;
            FiniteDifferences target = new FiniteDifferences(numberOfParameters)
            {
                NumberOfPoints = 7,
                Order          = 3,
            };

            double[] inputs = { -1, 0.4 };

            target.Function = BroydenFletcherGoldfarbShannoTest.rosenbrockFunction;

            double[] expected = { -2400, 0 };
            double[] actual   = target.Gradient(inputs);

            Assert.IsTrue(expected.IsEqual(actual, 1e-5));
        }
        public void HomogeneousTest()
        {
            double[,] quadraticTerms =
            {
                { 8, 3, 1 },
                { 3, 4, 2 },
                { 1, 2, 6 },
            };

            double[] linearTerms = { 0, 0, 0 };

            var target = new QuadraticObjectiveFunction(quadraticTerms, linearTerms);

            var function = target.Function;
            var gradient = target.Gradient;

            FiniteDifferences fd = new FiniteDifferences(3, function);

            double[][] x =
            {
                new double[] {      1,  2,   3 },
                new double[] {      3,  1,   4 },
                new double[] {     -6,  5,   9 },
                new double[] {     31, 25, 246 },
                new double[] { -0.102,  0,  10 },
            };

            { // Gradient test
                for (int i = 0; i < x.Length; i++)
                {
                    double[] expected = fd.Gradient(x[i]);
                    double[] actual   = gradient(x[i]);

                    for (int j = 0; j < actual.Length; j++)
                    {
                        Assert.AreEqual(expected[j], actual[j], 1e-6);
                    }
                }
            }
        }
        public void gh_853()
        {
            double[] aParam = { 1.790978, 9.408872E-05, 0.9888748, 1E-08 };

            Func <double[], double> funcionObjetivoLBFGS = x =>
            {
                int      iSizeArray            = 10;
                double[] arrayDecimalesTrabajo = { 2.2227, 2.2188, 2.2144, 2.204, 2.2006, 2.2053, 2.2053, 2.2035, 2.1969, 2.2033 };

                double mu    = x[0];
                double omega = x[1];
                double alpha = x[2];
                double beta  = x[3];

                double[] e2      = new double[iSizeArray];
                double   mediae2 = 0.0;
                for (int i = 0; i <= iSizeArray - 1; i++)
                {
                    e2[i]   = Math.Pow((arrayDecimalesTrabajo[i] - mu), 2);
                    mediae2 = mediae2 + e2[i];
                }

                mediae2 = mediae2 / iSizeArray;
                double[] sigmat2 = new double[iSizeArray];
                sigmat2[0] = omega + alpha * mediae2;
                for (int i = 1; i <= iSizeArray - 1; i++)
                {
                    sigmat2[i] = omega + alpha * e2[i - 1];
                }
                sigmat2[0] = sigmat2[0] + beta * mediae2;
                for (int i = 1; i <= iSizeArray - 1; i++)
                {
                    sigmat2[i] = sigmat2[i] + beta * sigmat2[i - 1];
                }

                double sumalikelihood = iSizeArray * Math.Log(2 * Math.PI);
                for (int i = 0; i <= iSizeArray - 1; i++)
                {
                    double dMathlog = Math.Log(sigmat2[i]);
                    if (double.IsInfinity(dMathlog))
                    {
                        break;
                    }
                    if (double.IsNaN(dMathlog))
                    {
                        break;
                    }
                    sumalikelihood = sumalikelihood + dMathlog + e2[i] / sigmat2[i];
                }
                return(0.5 * sumalikelihood);
            };

            var calculator = new FiniteDifferences(variables: 4, function: funcionObjetivoLBFGS)
            {
                NumberOfPoints = 7,
            };

            double[]   result = calculator.Gradient(aParam);
            double[][] actual = calculator.Hessian(aParam);

            Assert.AreEqual(actual[0][0], -57.6160442364672, 1e-2);
            Assert.AreEqual(actual[0][1], -1.27840745168738, 1e-2);
            Assert.AreEqual(actual[0][2], 0.0255249049748045, 1e-2);
            Assert.AreEqual(actual[0][3], 0.0231494572415752, 1e-2);
            Assert.AreEqual(actual[1][0], -1.27840745168738, 1e-2);
            Assert.AreEqual(actual[1][1], 173.045435758468, 1e-1);
            Assert.AreEqual(actual[1][2], 29.9809421733244, 1e-2);
            Assert.AreEqual(actual[1][3], 29.8568458845239, 1e-1);
            Assert.AreEqual(actual[2][0], 0.0255249049748045, 1e-2);
            Assert.AreEqual(actual[2][1], 29.9809421733244, 1e-2);
            Assert.AreEqual(actual[2][2], 5.2012616436059, 1e-2);
            Assert.AreEqual(actual[2][3], 5.17856868498256, 1e-2);
            Assert.AreEqual(actual[3][0], 0.0231494572415752, 1e-2);
            Assert.AreEqual(actual[3][1], 29.8568458845239, 1e-2);
            Assert.AreEqual(actual[3][2], 5.17856868498256, 1e-2);
            Assert.AreEqual(actual[3][3], 5.15878895157584, 1e-2);
        }