コード例 #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void penaltyMatrix1DTest()
        public virtual void penaltyMatrix1DTest()
        {
            int          n  = 10;
            DoubleMatrix p0 = PenaltyMatrixGenerator.getPenaltyMatrix(n, 0);     //zeroth order

            AssertMatrix.assertEqualsMatrix(DoubleMatrix.identity(n), p0, 1e-15);

            //constant
            DoubleArray  x = DoubleArray.filled(n, 2.0);
            DoubleMatrix p = PenaltyMatrixGenerator.getPenaltyMatrix(n, 2);
            double       r = MA.getInnerProduct(x, MA.multiply(p, x));

            assertEquals(0.0, r);

            DoubleArray x2 = DoubleArray.of(n, i => i);

            r = MA.getInnerProduct(x2, MA.multiply(p, x2));
            assertEquals(0.0, r);

            DoubleArray x3 = DoubleArray.of(n, i => 0.4 + 0.4 * i + i * i);

            r = MA.getInnerProduct(x3, MA.multiply(p, x3));
            //The second order diff is 2; for 2nd order difference use 8 values (n-2), so expect 8 * 2^2 = 32
            assertEquals(32.0, r, 1e-11);

            p = PenaltyMatrixGenerator.getPenaltyMatrix(n, 3);
            r = MA.getInnerProduct(x3, MA.multiply(p, x3));
            assertEquals(0.0, r, 1e-13);
        }
コード例 #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void zeroOrderSinglePointTest()
        public virtual void zeroOrderSinglePointTest()
        {
            double[]     x  = new double[] { 0.2 };
            DoubleMatrix p1 = PenaltyMatrixGenerator.getPenaltyMatrix(x, 0);

            AssertMatrix.assertEqualsMatrix(DoubleMatrix.identity(1), p1, 1e-15);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void test()
        public virtual void test()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final ParameterizedCurve curve = new ParameterizedCurve()
            ParameterizedCurve curve = new ParameterizedCurveAnonymousInnerClass(this);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final DoublesVectorFunctionProvider pro = new DoublesVectorFunctionProvider()
            DoublesVectorFunctionProvider pro = new DoublesVectorFunctionProviderAnonymousInnerClass(this, curve);

            //a = -2, b = 1, c = 0.5
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray parms = com.opengamma.strata.collect.array.DoubleArray.of(-2.0, 1.0, 0.5);
            DoubleArray parms = DoubleArray.of(-2.0, 1.0, 0.5);

            //sample the curve at x = -1, 0, and 1
            VectorFunction f = pro.from(new double?[] { -1.0, 0.0, 1.0 });
            DoubleArray    y = f.apply(parms);

            AssertMatrix.assertEqualsVectors(DoubleArray.of(-2.5, -2.0, -0.5), y, 1e-15);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.List<double> l = new java.util.ArrayList<>(3);
            IList <double> l = new List <double>(3);

            l.Add(0.0);
            l.Add(2.0);
            l.Add(4.0);
            f = pro.from(l);
            y = f.apply(parms);
            AssertMatrix.assertEqualsVectors(DoubleArray.of(-2.0, 2.0, 10.0), y, 1e-15);
        }
コード例 #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void test()
        public virtual void test()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final ParameterizedCurveVectorFunctionProvider pro = new ParameterizedCurveVectorFunctionProvider(s_PCurve);
            ParameterizedCurveVectorFunctionProvider pro = new ParameterizedCurveVectorFunctionProvider(s_PCurve);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] points = new double[] {-1.0, 0.0, 1.0 };
            double[] points = new double[] { -1.0, 0.0, 1.0 };
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final VectorFunction f = pro.from(points);
            VectorFunction f = pro.from(points);

            assertEquals(2, f.LengthOfDomain);
            assertEquals(3, f.LengthOfRange);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray x = com.opengamma.strata.collect.array.DoubleArray.of(0.5, 2.0);
            DoubleArray x = DoubleArray.of(0.5, 2.0);     //the parameters a & b
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray y = f.apply(x);
            DoubleArray y = f.apply(x);

            assertEquals(0.5 * Math.Sinh(-2.0), y.get(0), 1e-14);
            assertEquals(0.0, y.get(1), 1e-14);
            assertEquals(0.5 * Math.Sinh(2.0), y.get(2), 1e-14);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix jac = f.calculateJacobian(x);
            DoubleMatrix jac = f.calculateJacobian(x);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix fdJac = (new com.opengamma.strata.math.impl.differentiation.VectorFieldFirstOrderDifferentiator().differentiate(f)).apply(x);
            DoubleMatrix fdJac = ((new VectorFieldFirstOrderDifferentiator()).differentiate(f)).apply(x);

            AssertMatrix.assertEqualsMatrix(fdJac, jac, 1e-9);
        }
コード例 #5
0
        /// <summary>
        /// /check individual functions first
        /// </summary>
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void functionsTest()
        public virtual void functionsTest()
        {
            for (int i = 0; i < 3; i++)
            {
                DoubleArray  y   = F[i].apply(X[i]);
                DoubleMatrix jac = F[i].calculateJacobian(X[i]);
                AssertMatrix.assertEqualsVectors(Y_EXP[i], y, 1e-15);
                AssertMatrix.assertEqualsMatrix(JAC_EXP[i], jac, 1e-15);
            }
        }
コード例 #6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void matrixTransposeMultipleMatrixTest()
        public virtual void matrixTransposeMultipleMatrixTest()
        {
            DoubleMatrix a = DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 1.0, 2.0, 3.0 },
                new double[] { -3.0, 1.3, 7.0 }
            });
            DoubleMatrix aTa    = ALGEBRA.matrixTransposeMultiplyMatrix(a);
            DoubleMatrix aTaRef = (DoubleMatrix)ALGEBRA.multiply(ALGEBRA.getTranspose(a), a);

            AssertMatrix.assertEqualsMatrix(aTaRef, aTa, 1e-15);
        }
コード例 #7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void conCatTest()
        public virtual void conCatTest()
        {
            DoubleArray cx                = X[0].concat(X[1]).concat(X[2]);
            DoubleArray cyExp             = Y_EXP[0].concat(Y_EXP[1]).concat(Y_EXP[2]);
            ConcatenatedVectorFunction cf = new ConcatenatedVectorFunction(F);
            DoubleArray cy                = cf.apply(cx);

            AssertMatrix.assertEqualsVectors(cyExp, cy, 1e-15);

            DoubleMatrix cJac  = cf.calculateJacobian(cx);
            DoubleMatrix fdJac = DIFF.differentiate(cf).apply(cx);

            AssertMatrix.assertEqualsMatrix(fdJac, cJac, 1e-10);
        }
コード例 #8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(dataProvider = "data") public void derivativeMatrix1DTest(double[] x, com.opengamma.strata.collect.array.DoubleArray y, com.opengamma.strata.collect.array.DoubleArray dydx, com.opengamma.strata.collect.array.DoubleArray d2ydx2)
        public virtual void derivativeMatrix1DTest(double[] x, DoubleArray y, DoubleArray dydx, DoubleArray d2ydx2)
        {
            int          n  = x.Length;
            DoubleMatrix d0 = PenaltyMatrixGenerator.getDerivativeMatrix(x, 0, true);

            AssertMatrix.assertEqualsMatrix(DoubleMatrix.identity(n), d0, 1e-14);

            DoubleMatrix d1  = PenaltyMatrixGenerator.getDerivativeMatrix(x, 1, true);
            DoubleMatrix d2  = PenaltyMatrixGenerator.getDerivativeMatrix(x, 2, true);
            DoubleArray  d1y = (DoubleArray)MA.multiply(d1, y);
            DoubleArray  d2y = (DoubleArray)MA.multiply(d2, y);

            AssertMatrix.assertEqualsVectors(dydx, d1y, 1e-13);
            AssertMatrix.assertEqualsVectors(d2ydx2, d2y, 1e-13);
        }
コード例 #9
0
        /// <summary>
        /// create a quadratic function on a non-uniform 2D grid, then flatten this to a vector and check the first and
        /// second order differentiation matrices and penalty matrices work in both dimensions
        /// </summary>
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void penalty2DTest()
        public virtual void penalty2DTest()
        {
            double[] x  = new double[] { 0.0, 0.3, 0.7, 0.8, 1.2, 2.0 };
            double[] y  = new double[] { -20.0, -10.0, 0.0, 5.0, 15.0, 19.0, 20.0 };
            int      nx = x.Length;
            int      ny = y.Length;

            DoubleMatrix p0 = PenaltyMatrixGenerator.getPenaltyMatrix(new double[][] { x, y }, 0, 0);

            AssertMatrix.assertEqualsMatrix(DoubleMatrix.identity(nx * ny), p0, 1e-14);
            p0 = PenaltyMatrixGenerator.getPenaltyMatrix(new double[][] { x, y }, 0, 1);
            AssertMatrix.assertEqualsMatrix(DoubleMatrix.identity(nx * ny), p0, 1e-14);

            DoubleMatrix diffX1DFirstOrder = PenaltyMatrixGenerator.getDerivativeMatrix(x, 1, true);
            DoubleMatrix diffY1DFirstOrder = PenaltyMatrixGenerator.getDerivativeMatrix(y, 1, true);
            DoubleMatrix diffX1DSecOrder   = PenaltyMatrixGenerator.getDerivativeMatrix(x, 2, true);
            DoubleMatrix diffY1DSecOrder   = PenaltyMatrixGenerator.getDerivativeMatrix(y, 2, true);
            DoubleMatrix diffX2DFirstOrder = PenaltyMatrixGenerator.getMatrixForFlattened(new int[] { nx, ny }, diffX1DFirstOrder, 0);
            DoubleMatrix diffY2DFirstOrder = PenaltyMatrixGenerator.getMatrixForFlattened(new int[] { nx, ny }, diffY1DFirstOrder, 1);
            DoubleMatrix diffX2DSecOrder   = PenaltyMatrixGenerator.getMatrixForFlattened(new int[] { nx, ny }, diffX1DSecOrder, 0);
            DoubleMatrix diffY2DSecOrder   = PenaltyMatrixGenerator.getMatrixForFlattened(new int[] { nx, ny }, diffY1DSecOrder, 1);

            DoubleArray z         = DoubleArray.filled(nx * ny);
            DoubleArray dzdx      = DoubleArray.filled(nx * ny);
            DoubleArray d2zdx2    = DoubleArray.filled(nx * ny);
            DoubleArray dzdy      = DoubleArray.filled(nx * ny);
            DoubleArray d2zdy2    = DoubleArray.filled(nx * ny);
            double      dzdxSum   = 0;
            double      d2zdx2Sum = 0;
            double      dzdySum   = 0;
            double      d2zdy2Sum = 0;

            for (int i = 0; i < nx; i++)
            {
                double xi = x[i];
                for (int j = 0; j < ny; j++)
                {
                    double yj    = y[j];
                    int    index = i * ny + j;
                    z      = z.with(index, 0.3 + xi + 0.4 * xi * xi + 0.01 * yj - 1e-4 * yj * yj + 0.1 * xi * yj);
                    dzdx   = dzdx.with(index, 1.0 + 0.8 * xi + 0.1 * yj);
                    d2zdx2 = d2zdx2.with(index, 0.8);
                    dzdy   = dzdy.with(index, 0.01 - 2e-4 * yj + 0.1 * xi);
                    d2zdy2 = d2zdy2.with(index, -2e-4);

                    //The penalty matrix does not use end points, so don't include them here
                    if (i != 0 & i != (nx - 1))
                    {
                        dzdxSum   += FunctionUtils.square(dzdx.get(index));
                        d2zdx2Sum += FunctionUtils.square(d2zdx2.get(index));
                    }
                    if (j != 0 & j != (ny - 1))
                    {
                        dzdySum   += FunctionUtils.square(dzdy.get(index));
                        d2zdy2Sum += FunctionUtils.square(d2zdy2.get(index));
                    }
                }
            }

            AssertMatrix.assertEqualsVectors(dzdx, (DoubleArray)MA.multiply(diffX2DFirstOrder, z), 1e-12);
            AssertMatrix.assertEqualsVectors(dzdy, (DoubleArray)MA.multiply(diffY2DFirstOrder, z), 1e-12);
            AssertMatrix.assertEqualsVectors(d2zdx2, (DoubleArray)MA.multiply(diffX2DSecOrder, z), 1e-12);
            AssertMatrix.assertEqualsVectors(d2zdy2, (DoubleArray)MA.multiply(diffY2DSecOrder, z), 1e-12);

            DoubleMatrix p1x = PenaltyMatrixGenerator.getPenaltyMatrix(new double[][] { x, y }, 1, 0);
            DoubleMatrix p2x = PenaltyMatrixGenerator.getPenaltyMatrix(new double[][] { x, y }, 2, 0);
            DoubleMatrix p1y = PenaltyMatrixGenerator.getPenaltyMatrix(new double[][] { x, y }, 1, 1);
            DoubleMatrix p2y = PenaltyMatrixGenerator.getPenaltyMatrix(new double[][] { x, y }, 2, 1);
            double       r1x = MA.getInnerProduct(z, MA.multiply(p1x, z));
            double       r2x = MA.getInnerProduct(z, MA.multiply(p2x, z));
            double       r1y = MA.getInnerProduct(z, MA.multiply(p1y, z));
            double       r2y = MA.getInnerProduct(z, MA.multiply(p2y, z));

            double xRange = x[nx - 1] - x[0];
            double yRange = y[ny - 1] - y[0];

            assertEquals("first order x", Math.Pow(xRange, 2) * dzdxSum, r1x, 1e-10);
            assertEquals("second order x", Math.Pow(xRange, 4) * d2zdx2Sum, r2x, 1e-9);
            assertEquals("first order y", Math.Pow(yRange, 2) * dzdySum, r1y, 1e-10);
            assertEquals("second order y", Math.Pow(yRange, 4) * d2zdy2Sum, r2y, 1e-8);

            double lambdaX = 0.7;
            double lambdaY = Math.PI;
            //second order in x and first order in y
            DoubleMatrix p    = PenaltyMatrixGenerator.getPenaltyMatrix(new double[][] { x, y }, new int[] { 2, 1 }, new double[] { lambdaX, lambdaY });
            double       r    = MA.getInnerProduct(z, MA.multiply(p, z));
            double       expR = Math.Pow(xRange, 4) * d2zdx2Sum * lambdaX + Math.Pow(yRange, 2) * dzdySum * lambdaY;

            assertEquals(expR, r, 1e-9);
        }
コード例 #10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void differenceMatrix1DTest()
        public virtual void differenceMatrix1DTest()
        {
            int n = 7;

            DoubleMatrix d0 = PenaltyMatrixGenerator.getDifferenceMatrix(n, 0);     //zeroth order

            AssertMatrix.assertEqualsMatrix(DoubleMatrix.identity(n), d0, 1e-15);

            DoubleArray  zeroVector = DoubleArray.filled(n);
            DoubleMatrix d1         = PenaltyMatrixGenerator.getDifferenceMatrix(n, 1); //first order difference matrix

            assertEquals(n, d1.rowCount());
            assertEquals(n, d1.columnCount());
            AssertMatrix.assertEqualsVectors(zeroVector, d1.row(0), 1e-15);     //first row should be zero

            DoubleArray x   = DoubleArray.filled(n, 1.0);
            DoubleArray d1x = (DoubleArray)MA.multiply(d1, x);

            //a constant vector should have zero first order differences
            AssertMatrix.assertEqualsVectors(zeroVector, d1x, 1e-14);

            DoubleMatrix d2 = PenaltyMatrixGenerator.getDifferenceMatrix(n, 2);     //second order difference matrix

            assertEquals(n, d2.rowCount());
            assertEquals(n, d2.columnCount());
            AssertMatrix.assertEqualsVectors(zeroVector, d2.row(0), 1e-15);     //first two rows should be zero
            AssertMatrix.assertEqualsVectors(zeroVector, d2.row(1), 1e-15);

            DoubleArray x2 = DoubleArray.of(n, i => i);

            d1x = (DoubleArray)MA.multiply(d1, x2);
            //first element of the diff vector is set to zero
            DoubleArray ones = DoubleArray.filled(n, 1.0).with(0, 0);

            //vector with differences of one
            AssertMatrix.assertEqualsVectors(ones, d1x, 1e-14);

            DoubleArray d2x = (DoubleArray)MA.multiply(d2, x2);

            //a linear vector should have zero second order differences
            AssertMatrix.assertEqualsVectors(zeroVector, d2x, 1e-14);

            DoubleMatrix d3 = PenaltyMatrixGenerator.getDifferenceMatrix(n, 3);     //third order difference matrix

            assertEquals(n, d3.rowCount());
            assertEquals(n, d3.columnCount());
            AssertMatrix.assertEqualsVectors(zeroVector, d3.row(0), 1e-15);     //first three rows should be zero
            AssertMatrix.assertEqualsVectors(zeroVector, d3.row(1), 1e-15);
            AssertMatrix.assertEqualsVectors(zeroVector, d3.row(2), 1e-15);

            DoubleArray x3 = DoubleArray.of(n, i => 0.5 + i + 0.1 * i * i);

            d1x = (DoubleArray)MA.multiply(d1, x3);
            // expected first order diff, first element is zero
            DoubleArray exp = DoubleArray.of(n, i => 0.9 + 0.2 * i).with(0, 0);

            AssertMatrix.assertEqualsVectors(exp, d1x, 1e-14);

            // expected second order diff, first two elements are zero
            exp = DoubleArray.filled(n, 0.2).with(0, 0).with(1, 0);
            d2x = (DoubleArray)MA.multiply(d2, x3);
            AssertMatrix.assertEqualsVectors(exp, d2x, 1e-14);

            DoubleArray d3x = (DoubleArray)MA.multiply(d3, x3);

            //a quadratic vector should have zero third order differences
            AssertMatrix.assertEqualsVectors(zeroVector, d3x, 1e-14);
        }