Example #1
0
        public void ParametricHessianTest()
        {
            // function is null
            {
                string parameterName = "function";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Hessian(
                        function: null,
                        argument: EngineFailureModel.EstimatedParameters,
                        parameter: EngineFailureModel.Data);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // argument is null
            {
                string parameterName = "argument";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Hessian(
                        function: EngineFailureModel.LogLikelihood,
                        argument: null,
                        parameter: EngineFailureModel.Data);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // valid input
            {
                var expected =
                    EngineFailureModel.LogLikelihoodHessian(
                        argument: EngineFailureModel.EstimatedParameters,
                        parameter: EngineFailureModel.Data);

                var actual =
                    NumericalDifferentiation.Hessian(
                        function: EngineFailureModel.LogLikelihood,
                        argument: EngineFailureModel.EstimatedParameters,
                        parameter: EngineFailureModel.Data);

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }
Example #2
0
        public void testDerivativeWeightsOnNonUniformGrids()
        {
            Fdm1dMesher mesherX =
                new Concentrating1dMesher(-2.0, 3.0, 50, new Pair <double?, double?>(0.5, 0.01));
            Fdm1dMesher mesherY =
                new Concentrating1dMesher(0.5, 5.0, 25, new Pair <double?, double?>(0.5, 0.1));
            Fdm1dMesher mesherZ =
                new Concentrating1dMesher(-1.0, 2.0, 31, new Pair <double?, double?>(1.5, 0.01));

            FdmMesher meshers =
                new FdmMesherComposite(mesherX, mesherY, mesherZ);

            FdmLinearOpLayout   layout  = meshers.layout();
            FdmLinearOpIterator endIter = layout.end();

            double tol = 1e-13;

            for (int direction = 0; direction < 3; ++direction)
            {
                SparseMatrix dfdx
                    = new FirstDerivativeOp(direction, meshers).toMatrix();
                SparseMatrix d2fdx2
                    = new SecondDerivativeOp(direction, meshers).toMatrix();

                Vector gridPoints = meshers.locations(direction);

                for (FdmLinearOpIterator iter = layout.begin();
                     iter != endIter; ++iter)
                {
                    int c       = iter.coordinates()[direction];
                    int index   = iter.index();
                    int indexM1 = layout.neighbourhood(iter, direction, -1);
                    int indexP1 = layout.neighbourhood(iter, direction, +1);

                    // test only if not on the boundary
                    if (c == 0)
                    {
                        Vector twoPoints = new Vector(2);
                        twoPoints[0] = 0.0;
                        twoPoints[1] = gridPoints[indexP1] - gridPoints[index];

                        Vector ndWeights1st = new NumericalDifferentiation(x => x, 1, twoPoints).weights();

                        double beta1  = dfdx[index, index];
                        double gamma1 = dfdx[index, indexP1];
                        if (Math.Abs((beta1 - ndWeights1st[0]) / beta1) > tol ||
                            Math.Abs((gamma1 - ndWeights1st[1]) / gamma1) > tol)
                        {
                            QAssert.Fail("can not reproduce the weights of the "
                                         + "first order derivative operator "
                                         + "on the lower boundary"
                                         + "\n expected beta:    " + ndWeights1st[0]
                                         + "\n calculated beta:  " + beta1
                                         + "\n difference beta:  "
                                         + (beta1 - ndWeights1st[0])
                                         + "\n expected gamma:   " + ndWeights1st[1]
                                         + "\n calculated gamma: " + gamma1
                                         + "\n difference gamma: "
                                         + (gamma1 - ndWeights1st[1]));
                        }

                        // free boundary condition by default
                        double beta2  = d2fdx2[index, index];
                        double gamma2 = d2fdx2[index, indexP1];

                        if (Math.Abs(beta2) > Const.QL_EPSILON ||
                            Math.Abs(gamma2) > Const.QL_EPSILON)
                        {
                            QAssert.Fail("can not reproduce the weights of the "
                                         + "second order derivative operator "
                                         + "on the lower boundary"
                                         + "\n expected beta:    " + 0.0
                                         + "\n calculated beta:  " + beta2
                                         + "\n expected gamma:   " + 0.0
                                         + "\n calculated gamma: " + gamma2);
                        }
                    }
                    else if (c == layout.dim()[direction] - 1)
                    {
                        Vector twoPoints = new Vector(2);
                        twoPoints[0] = gridPoints[indexM1] - gridPoints[index];
                        twoPoints[1] = 0.0;

                        Vector ndWeights1st = new NumericalDifferentiation(x => x, 1, twoPoints).weights();

                        double alpha1 = dfdx[index, indexM1];
                        double beta1  = dfdx[index, index];
                        if (Math.Abs((alpha1 - ndWeights1st[0]) / alpha1) > tol ||
                            Math.Abs((beta1 - ndWeights1st[1]) / beta1) > tol)
                        {
                            QAssert.Fail("can not reproduce the weights of the "
                                         + "first order derivative operator "
                                         + "on the upper boundary"
                                         + "\n expected alpha:   " + ndWeights1st[0]
                                         + "\n calculated alpha: " + alpha1
                                         + "\n difference alpha: "
                                         + (alpha1 - ndWeights1st[0])
                                         + "\n expected beta:    " + ndWeights1st[1]
                                         + "\n calculated beta:  " + beta1
                                         + "\n difference beta:  "
                                         + (beta1 - ndWeights1st[1]));
                        }

                        // free boundary condition by default
                        double alpha2 = d2fdx2[index, indexM1];
                        double beta2  = d2fdx2[index, index];

                        if (Math.Abs(alpha2) > Const.QL_EPSILON ||
                            Math.Abs(beta2) > Const.QL_EPSILON)
                        {
                            QAssert.Fail("can not reproduce the weights of the "
                                         + "second order derivative operator "
                                         + "on the upper boundary"
                                         + "\n expected alpha:   " + 0.0
                                         + "\n calculated alpha: " + alpha2
                                         + "\n expected beta:    " + 0.0
                                         + "\n calculated beta:  " + beta2);
                        }
                    }
                    else
                    {
                        Vector threePoints = new Vector(3);
                        threePoints[0] = gridPoints[indexM1] - gridPoints[index];
                        threePoints[1] = 0.0;
                        threePoints[2] = gridPoints[indexP1] - gridPoints[index];

                        Vector ndWeights1st = new NumericalDifferentiation(x => x, 1, threePoints).weights();

                        double alpha1 = dfdx[index, indexM1];
                        double beta1  = dfdx[index, index];
                        double gamma1 = dfdx[index, indexP1];

                        if (Math.Abs((alpha1 - ndWeights1st[0]) / alpha1) > tol ||
                            Math.Abs((beta1 - ndWeights1st[1]) / beta1) > tol ||
                            Math.Abs((gamma1 - ndWeights1st[2]) / gamma1) > tol)
                        {
                            QAssert.Fail("can not reproduce the weights of the "
                                         + "first order derivative operator"
                                         + "\n expected alpha:   " + ndWeights1st[0]
                                         + "\n calculated alpha: " + alpha1
                                         + "\n difference alpha: "
                                         + (alpha1 - ndWeights1st[0])
                                         + "\n expected beta:    " + ndWeights1st[1]
                                         + "\n calculated beta:  " + beta1
                                         + "\n difference beta:  "
                                         + (beta1 - ndWeights1st[1])
                                         + "\n expected gamma:   " + ndWeights1st[2]
                                         + "\n calculated gamma: " + gamma1
                                         + "\n difference gamma: "
                                         + (gamma1 - ndWeights1st[2]));
                        }

                        Vector ndWeights2nd = new NumericalDifferentiation(x => x, 2, threePoints).weights();

                        double alpha2 = d2fdx2[index, indexM1];
                        double beta2  = d2fdx2[index, index];
                        double gamma2 = d2fdx2[index, indexP1];
                        if (Math.Abs((alpha2 - ndWeights2nd[0]) / alpha2) > tol ||
                            Math.Abs((beta2 - ndWeights2nd[1]) / beta2) > tol ||
                            Math.Abs((gamma2 - ndWeights2nd[2]) / gamma2) > tol)
                        {
                            QAssert.Fail("can not reproduce the weights of the "
                                         + "second order derivative operator"
                                         + "\n expected alpha:   " + ndWeights2nd[0]
                                         + "\n calculated alpha: " + alpha2
                                         + "\n difference alpha: "
                                         + (alpha2 - ndWeights2nd[0])
                                         + "\n expected beta:    " + ndWeights2nd[1]
                                         + "\n calculated beta:  " + beta2
                                         + "\n difference beta:  "
                                         + (beta2 - ndWeights2nd[1])
                                         + "\n expected gamma:   " + ndWeights2nd[2]
                                         + "\n calculated gamma: " + gamma2
                                         + "\n difference gamma: "
                                         + (gamma2 - ndWeights2nd[2]));
                        }
                    }
                }
            }
        }
 public void TestInitialize()
 {
     library = new NumericalDifferentiation();
 }
Example #4
0
        public void NonparametricHessianTest()
        {
            // function is null
            {
                string parameterName = "function";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Hessian(
                        function: null,
                        argument: DoubleMatrix.Dense(2, 1));
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // argument is null
            {
                string parameterName = "argument";

                double function(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    double f = x * x * y + Math.Exp(y);

                    return(f);
                };

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Hessian(
                        function: function,
                        argument: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // valid input
            {
                double function(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    double f = x * x * y + Math.Exp(y);

                    return(f);
                };

                DoubleMatrix hessian(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    DoubleMatrix h = DoubleMatrix.Dense(2, 2);

                    h[0, 0] = 2.0 * y;
                    h[0, 1] = 2.0 * x;
                    h[1, 0] = h[0, 1];
                    h[1, 1] = Math.Exp(y);

                    return(h);
                }

                var arg = DoubleMatrix.Dense(2, 1, new double[2]
                {
                    9.0, -2.1
                });

                var expected =
                    hessian(
                        argument: arg);

                var actual =
                    NumericalDifferentiation.Hessian(
                        function: function,
                        argument: arg);

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }
Example #5
0
        public void NonparametricGradientTest()
        {
            // function is null
            {
                string parameterName = "function";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Gradient(
                        function: null,
                        argument: DoubleMatrix.Dense(2, 1));
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // argument is null
            {
                string parameterName = "argument";

                double function(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    double f = x * x * y + Math.Exp(y);

                    return(f);
                };

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Gradient(
                        function: function,
                        argument: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // valid input - Non zero arguments
            {
                double function(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    double f = x * x * y + Math.Exp(y);

                    return(f);
                };

                DoubleMatrix gradient(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    DoubleMatrix g = DoubleMatrix.Dense(2, 1);

                    g[0] = 2.0 * x * y;
                    g[1] = x * x + Math.Exp(y);

                    return(g);
                };

                var arg = DoubleMatrix.Dense(2, 1, new double[2]
                {
                    9.0, -2.1
                });

                var expected =
                    gradient(
                        argument: arg);

                var actual =
                    NumericalDifferentiation.Gradient(
                        function: function,
                        argument: arg);

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);
            }

            // valid input - Some zero arguments
            {
                double function(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    double f = x * x * y + Math.Exp(y);

                    return(f);
                };

                DoubleMatrix gradient(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    DoubleMatrix g = DoubleMatrix.Dense(2, 1);

                    g[0] = 2.0 * x * y;
                    g[1] = x * x + Math.Exp(y);

                    return(g);
                };

                var arg = DoubleMatrix.Dense(2, 1, new double[2]
                {
                    0.0, -2.1
                });

                var expected =
                    gradient(
                        argument: arg);

                var actual =
                    NumericalDifferentiation.Gradient(
                        function: function,
                        argument: arg);

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);

                arg = DoubleMatrix.Dense(2, 1, new double[2]
                {
                    9.0, 0.0
                });

                expected =
                    gradient(
                        argument: arg);

                actual =
                    NumericalDifferentiation.Gradient(
                        function: function,
                        argument: arg);

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }