/// <summary>
        /// Gamma is in the  form gamma^i_{j,k} =\partial^2y_j/\partial x_i \partial x_k, where i is the
        /// index of the matrix in the stack (3rd index of the tensor), and j,k are the individual
        /// matrix indices. We would like it in the form H^i_{j,k} =\partial^2y_i/\partial x_j \partial x_k,
        /// so that each matrix is a Hessian (for the dependent variable y_i), hence the reshaping below.
        /// </summary>
        /// <param name="gamma">  the rank 3 tensor </param>
        /// <returns> the reshaped rank 3 tensor </returns>
        private DoubleMatrix[] reshapeTensor(DoubleMatrix[] gamma)
        {
            int m = gamma.Length;
            int n = gamma[0].rowCount();

            ArgChecker.isTrue(gamma[0].columnCount() == m, "tenor wrong size. Seond index is {}, should be {}", gamma[0].columnCount(), m);
            DoubleMatrix[] res = new DoubleMatrix[n];
            for (int i = 0; i < n; i++)
            {
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] temp = new double[m][m];
                double[][] temp = RectangularArrays.ReturnRectangularDoubleArray(m, m);
                for (int j = 0; j < m; j++)
                {
                    DoubleMatrix gammaJ = gamma[j];
                    for (int k = j; k < m; k++)
                    {
                        temp[j][k] = gammaJ.get(i, k);
                    }
                }
                for (int j = 0; j < m; j++)
                {
                    for (int k = 0; k < j; k++)
                    {
                        temp[j][k] = temp[k][j];
                    }
                }
                res[i] = DoubleMatrix.copyOf(temp);
            }
            return(res);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testNotSquare1()
        public virtual void testNotSquare1()
        {
            new LeastSquareResults(1, PARAMS, DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 0.2, 0.3 }
            }));
        }
예제 #3
0
        public override LeastSquaresRegressionResult regress(double[][] x, double[][] weights, double[] y, bool useIntercept)
        {
            if (weights == null)
            {
                throw new System.ArgumentException("Cannot perform GLS regression without an array of weights");
            }
            checkData(x, weights, y);
            double[][] dep   = addInterceptVariable(x, useIntercept);
            double[]   indep = new double[y.Length];
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] wArray = new double[y.Length][y.Length];
            double[][] wArray = RectangularArrays.ReturnRectangularDoubleArray(y.Length, y.Length);
            for (int i = 0; i < y.Length; i++)
            {
                indep[i] = y[i];
                for (int j = 0; j < y.Length; j++)
                {
                    wArray[i][j] = weights[i][j];
                }
            }
            DoubleMatrix matrix      = DoubleMatrix.copyOf(dep);
            DoubleArray  vector      = DoubleArray.copyOf(indep);
            DoubleMatrix w           = DoubleMatrix.copyOf(wArray);
            DoubleMatrix transpose   = ALGEBRA.getTranspose(matrix);
            DoubleMatrix betasVector = (DoubleMatrix)ALGEBRA.multiply(ALGEBRA.multiply(ALGEBRA.multiply(ALGEBRA.getInverse(ALGEBRA.multiply(transpose, ALGEBRA.multiply(w, matrix))), transpose), w), vector);

            double[] yModel = base.writeArrayAsVector(((DoubleMatrix)ALGEBRA.multiply(matrix, betasVector)).toArray());
            double[] betas  = base.writeArrayAsVector(betasVector.toArray());
            return(getResultWithStatistics(x, y, betas, yModel, useIntercept));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testEquals()
        public virtual void testEquals()
        {
            LeastSquareResults ls1 = new LeastSquareResults(1.0, PARAMS, COVAR);
            LeastSquareResults ls2 = new LeastSquareResults(1.0, DoubleArray.of(1.0, 2.0), DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 0.1, 0.2 },
                new double[] { 0.2, 0.3 }
            }));

            assertEquals(ls1, ls2);
            ls2 = new LeastSquareResults(1.0, PARAMS, COVAR, null);
            assertEquals(ls1, ls2);
            ls2 = new LeastSquareResults(1.1, PARAMS, COVAR);
            assertFalse(ls1.Equals(ls2));
            ls2 = new LeastSquareResults(1.0, DoubleArray.of(1.1, 2.0), DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 0.1, 0.2 },
                new double[] { 0.2, 0.3 }
            }));
            assertFalse(ls1.Equals(ls2));
            ls2 = new LeastSquareResults(1.0, DoubleArray.of(1.0, 2.0), DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 0.1, 0.2 },
                new double[] { 0.2, 0.4 }
            }));
            assertFalse(ls1.Equals(ls2));
            ls2 = new LeastSquareResults(1.0, PARAMS, COVAR, INV_JAC);
            assertFalse(ls1.Equals(ls2));
            ls1 = new LeastSquareResults(1, PARAMS, COVAR, INV_JAC);
            ls2 = new LeastSquareResults(1, PARAMS, COVAR, COVAR);
            assertFalse(ls1.Equals(ls2));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testHashCode()
        public virtual void testHashCode()
        {
            LeastSquareResults ls1 = new LeastSquareResults(1.0, PARAMS, COVAR);
            LeastSquareResults ls2 = new LeastSquareResults(1.0, DoubleArray.of(1.0, 2.0), DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 0.1, 0.2 },
                new double[] { 0.2, 0.3 }
            }));

            assertEquals(ls1.GetHashCode(), ls2.GetHashCode(), 0);
            ls2 = new LeastSquareResults(1.0, DoubleArray.of(1.0, 2.0), DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 0.1, 0.2 },
                new double[] { 0.2, 0.3 }
            }), null);
            assertEquals(ls1.GetHashCode(), ls2.GetHashCode(), 0);
            ls1 = new LeastSquareResults(1.0, PARAMS, COVAR, INV_JAC);
            ls2 = new LeastSquareResults(1.0, DoubleArray.of(1.0, 2.0), DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 0.1, 0.2 },
                new double[] { 0.2, 0.3 }
            }), DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 0.5, 0.6 },
                new double[] { 0.7, 0.8 }
            }));
            assertEquals(ls1.GetHashCode(), ls2.GetHashCode(), 0);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testOGTrace2()
        public virtual void testOGTrace2()
        {
            OG.getTrace(DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 1, 2, 3 },
                new double[] { 4, 5, 6 }
            }));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testOGMultiply4()
        public virtual void testOGMultiply4()
        {
            OG.multiply(DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 1, 2, 3 },
                new double[] { 4, 5, 6 }
            }), M3);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testInverse()
        public virtual void testInverse()
        {
            assertMatrixEquals(COMMONS.getInverse(M3), DoubleMatrix.copyOf(new double[][]
            {
                new double[] { -0.3333333333333333, 0.6666666666666666 },
                new double[] { 0.6666666666666666, -0.3333333333333333 }
            }));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testOuterProduct()
        public virtual void testOuterProduct()
        {
            assertMatrixEquals(COMMONS.getOuterProduct(M1, M2), DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 3, 4 },
                new double[] { 6, 8 }
            }));
        }
        /// <summary>
        /// Interpolate.
        /// </summary>
        /// <param name="xValues">  the values </param>
        /// <param name="yValues">  the values </param>
        /// <param name="xMatrix">  the matrix </param>
        /// <returns> Values of the underlying cubic spline function at the values of x </returns>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: public com.opengamma.strata.collect.array.DoubleMatrix interpolate(final double[] xValues, final double[] yValues, final double[][] xMatrix)
        public virtual DoubleMatrix interpolate(double[] xValues, double[] yValues, double[][] xMatrix)
        {
            ArgChecker.notNull(xMatrix, "xMatrix");

            DoubleMatrix matrix = DoubleMatrix.copyOf(xMatrix);

            return(DoubleMatrix.ofArrayObjects(xMatrix.Length, xMatrix[0].Length, i => interpolate(xValues, yValues, matrix.rowArray(i))));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testMultiply()
        public virtual void testMultiply()
        {
            assertMatrixEquals(COMMONS.multiply(DoubleMatrix.identity(2), M3), M3);
            assertMatrixEquals(COMMONS.multiply(M3, M4), DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 19, 22 },
                new double[] { 17, 20 }
            }));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testPower()
        public virtual void testPower()
        {
            assertMatrixEquals(COMMONS.getPower(M3, 3), DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 13, 14 },
                new double[] { 14, 13 }
            }));
            assertMatrixEquals(COMMONS.getPower(M3, 3), COMMONS.multiply(M3, COMMONS.multiply(M3, M3)));
        }
        /// <summary>
        /// Interpolate.
        /// </summary>
        /// <param name="xValues">  the values </param>
        /// <param name="yValuesMatrix">  the matrix </param>
        /// <param name="x">  the s </param>
        /// <returns> Values of the underlying cubic spline functions interpolating {yValuesMatrix.RowVectors} at the values of x </returns>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: public com.opengamma.strata.collect.array.DoubleMatrix interpolate(final double[] xValues, final double[][] yValuesMatrix, final double[] x)
        public virtual DoubleMatrix interpolate(double[] xValues, double[][] yValuesMatrix, double[] x)
        {
            ArgChecker.notNull(x, "x");

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix matrix = com.opengamma.strata.collect.array.DoubleMatrix.copyOf(yValuesMatrix);
            DoubleMatrix matrix = DoubleMatrix.copyOf(yValuesMatrix);

            return(DoubleMatrix.ofArrayObjects(yValuesMatrix.Length, x.Length, i => interpolate(xValues, matrix.rowArray(i), x)));
        }
        public virtual void testSolveForMatrix()
        {
            double[][] expectedRaw = new double[][]
            {
                new double[] { 0.0103938059732010, 0.0181642215147756 },
                new double[] { -0.0147149030138629, -0.0077127926530197 },
                new double[] { -0.0171480759531631, -0.0032615794123952 },
                new double[] { 0.2645342893362958, 0.2856087765235678 }
            };

            assertTrue(FuzzyEquals.ArrayFuzzyEquals(result.solve(DoubleMatrix.copyOf(rawRHSmat)).toArray(), expectedRaw));
        }
예제 #15
0
        /// <summary>
        /// Evaluates the function.
        /// </summary>
        /// <param name="pp">  the PiecewisePolynomialResult </param>
        /// <param name="xKeys">  the key </param>
        /// <returns> the values of piecewise polynomial functions at xKeys
        ///   When _dim in PiecewisePolynomialResult is greater than 1, i.e., the struct contains
        ///   multiple piecewise polynomials, a row vector of return value corresponds to each piecewise polynomial </returns>
        public virtual DoubleMatrix evaluate(PiecewisePolynomialResult pp, double[] xKeys)
        {
            ArgChecker.notNull(pp, "pp");
            ArgChecker.notNull(xKeys, "xKeys");

            int keyLength = xKeys.Length;

            for (int i = 0; i < keyLength; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(xKeys[i]), "xKeys containing NaN");
                ArgChecker.isFalse(double.IsInfinity(xKeys[i]), "xKeys containing Infinity");
            }

            DoubleArray  knots      = pp.Knots;
            int          nKnots     = knots.size();
            DoubleMatrix coefMatrix = pp.CoefMatrix;
            int          dim        = pp.Dimensions;

//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] res = new double[dim][keyLength];
            double[][] res = RectangularArrays.ReturnRectangularDoubleArray(dim, keyLength);

            for (int k = 0; k < dim; ++k)
            {
                for (int j = 0; j < keyLength; ++j)
                {
                    int indicator = 0;
                    if (xKeys[j] < knots.get(1))
                    {
                        indicator = 0;
                    }
                    else
                    {
                        for (int i = 1; i < nKnots - 1; ++i)
                        {
                            if (knots.get(i) <= xKeys[j])
                            {
                                indicator = i;
                            }
                        }
                    }
                    DoubleArray coefs = coefMatrix.row(dim * indicator + k);
                    res[k][j] = getValue(coefs, xKeys[j], knots.get(indicator));
                    ArgChecker.isFalse(double.IsInfinity(res[k][j]), "Too large input");
                    ArgChecker.isFalse(double.IsNaN(res[k][j]), "Too large input");
                }
            }

            return(DoubleMatrix.copyOf(res));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("synthetic-access") @Override public com.opengamma.strata.collect.array.DoubleMatrix[] apply(com.opengamma.strata.collect.array.DoubleArray x)
            public override DoubleMatrix[] apply(DoubleArray x)
            {
                ArgChecker.notNull(x, "x");
                DoubleArray y = function(x);
                int         n = x.size();
                int         m = y.size();

//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][][] res = new double[m][n][n];
                double[][][] res = RectangularArrays.ReturnRectangularDoubleArray(m, n, n);

                for (int j = 0; j < n; j++)
                {
                    double      xj           = x.get(j);
                    DoubleArray xPlusOneEps  = x.with(j, xj + outerInstance.eps);
                    DoubleArray xMinusOneEps = x.with(j, xj - outerInstance.eps);
                    DoubleArray up           = function(x.with(j, xj + outerInstance.eps));
                    DoubleArray down         = function(xMinusOneEps);
                    for (int i = 0; i < m; i++)
                    {
                        res[i][j][j] = (up.get(i) + down.get(i) - 2 * y.get(i)) / outerInstance.epsSqr;
                    }
                    for (int k = j + 1; k < n; k++)
                    {
                        double      xk       = x.get(k);
                        DoubleArray downup   = function(xMinusOneEps.with(k, xk + outerInstance.eps));
                        DoubleArray downdown = function(xMinusOneEps.with(k, xk - outerInstance.eps));
                        DoubleArray updown   = function(xPlusOneEps.with(k, xk - outerInstance.eps));
                        DoubleArray upup     = function(xPlusOneEps.with(k, xk + outerInstance.eps));
                        for (int i = 0; i < m; i++)
                        {
                            res[i][j][k] = (upup.get(i) + downdown.get(i) - updown.get(i) - downup.get(i)) / 4 / outerInstance.epsSqr;
                        }
                    }
                }
                DoubleMatrix[] mres = new DoubleMatrix[m];
                for (int i = 0; i < m; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        for (int k = 0; k < j; k++)
                        {
                            res[i][j][k] = res[i][k][j];
                        }
                    }
                    mres[i] = DoubleMatrix.copyOf(res[i]);
                }
                return(mres);
            }
        public virtual void test_sensitivity_LegalEntityDiscountingProvider()
        {
            CurrencyParameterSensitivities computed = CALC.sensitivity(PARAMETER_SENSITIVITIES, PROVIDER);

            assertEquals(computed.Sensitivities.size(), 4);
            DoubleArray expected11 = (DoubleArray)MATRIX_ALGEBRA.multiply(SENSI_1, DoubleMatrix.copyOf(MATRIX_11));
            DoubleArray expected12 = (DoubleArray)MATRIX_ALGEBRA.multiply(SENSI_1, DoubleMatrix.copyOf(MATRIX_12));
            DoubleArray expected21 = (DoubleArray)MATRIX_ALGEBRA.multiply(SENSI_2, DoubleMatrix.copyOf(MATRIX_21));
            DoubleArray expected22 = (DoubleArray)MATRIX_ALGEBRA.multiply(SENSI_2, DoubleMatrix.copyOf(MATRIX_22));

            assertTrue(computed.getSensitivity(CURVE_NAME_1, USD).Sensitivity.equalWithTolerance(expected11, TOL));
            assertTrue(computed.getSensitivity(CURVE_NAME_1, GBP).Sensitivity.equalWithTolerance(expected21, TOL));
            assertTrue(computed.getSensitivity(CURVE_NAME_2, USD).Sensitivity.equalWithTolerance(expected12, TOL));
            assertTrue(computed.getSensitivity(CURVE_NAME_2, GBP).Sensitivity.equalWithTolerance(expected22, TOL));
        }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: @Override protected com.opengamma.strata.collect.array.DoubleArray[] combinedMatrixEqnSolver(final double[][] doubMat1, final double[] doubVec, final double[][] doubMat2)
        protected internal override DoubleArray[] combinedMatrixEqnSolver(double[][] doubMat1, double[] doubVec, double[][] doubMat2)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int size = doubVec.length;
            int size = doubVec.Length;

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray[] res = new com.opengamma.strata.collect.array.DoubleArray[size + 1];
            DoubleArray[] res = new DoubleArray[size + 1];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix doubMat2Matrix = com.opengamma.strata.collect.array.DoubleMatrix.copyOf(doubMat2);
            DoubleMatrix doubMat2Matrix = DoubleMatrix.copyOf(doubMat2);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] u = new double[size - 1];
            double[] u = new double[size - 1];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] d = new double[size];
            double[] d = new double[size];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] l = new double[size - 1];
            double[] l = new double[size - 1];

            for (int i = 0; i < size - 1; ++i)
            {
                u[i] = doubMat1[i][i + 1];
                d[i] = doubMat1[i][i];
                l[i] = doubMat1[i + 1][i];
            }
            d[size - 1] = doubMat1[size - 1][size - 1];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.math.impl.linearalgebra.TridiagonalMatrix m = new com.opengamma.strata.math.impl.linearalgebra.TridiagonalMatrix(d, u, l);
            TridiagonalMatrix m = new TridiagonalMatrix(d, u, l);

            res[0] = DoubleArray.copyOf(TridiagonalSolver.solvTriDag(m, doubVec));
            for (int i = 0; i < size; ++i)
            {
                DoubleArray doubMat2Colum = doubMat2Matrix.column(i);
                res[i + 1] = TridiagonalSolver.solvTriDag(m, doubMat2Colum);
            }
            return(res);
        }
        /// <summary>
        /// Tests solve AX = B from A and B.
        /// </summary>
        public virtual void solveMatrix()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final CholeskyDecompositionResult result = CDOG.apply(A5);
            CholeskyDecompositionResult result = CDOG.apply(A5);

            double[][] b = new double[][]
            {
                new double[] { 1.0, 2.0 },
                new double[] { 2.0, 3.0 },
                new double[] { 3.0, 4.0 },
                new double[] { 4.0, -2.0 },
                new double[] { -1.0, -1.0 }
            };
            DoubleMatrix x  = result.solve(DoubleMatrix.copyOf(b));
            DoubleMatrix ax = (DoubleMatrix)ALGEBRA.multiply(A5, x);

            ArrayAsserts.assertArrayEquals("Cholesky decomposition OpenGamma - solve", b[0], ax.rowArray(0), 1.0E-10);
            ArrayAsserts.assertArrayEquals("Cholesky decomposition OpenGamma - solve", b[1], ax.rowArray(1), 1.0E-10);
        }
        /// <summary>
        /// Interpolate.
        /// </summary>
        /// <param name="xValues">  the values </param>
        /// <param name="yValuesMatrix">  the matrix </param>
        /// <param name="xMatrix">  the matrix </param>
        /// <returns> Values of the underlying cubic spline functions interpolating {yValuesMatrix.RowVectors} at the values of xMatrix </returns>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: public com.opengamma.strata.collect.array.DoubleMatrix[] interpolate(final double[] xValues, final double[][] yValuesMatrix, final double[][] xMatrix)
        public virtual DoubleMatrix[] interpolate(double[] xValues, double[][] yValuesMatrix, double[][] xMatrix)
        {
            ArgChecker.notNull(xMatrix, "xMatrix");

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int keyColumn = xMatrix[0].length;
            int keyColumn = xMatrix[0].Length;

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix matrix = com.opengamma.strata.collect.array.DoubleMatrix.copyOf(xMatrix);
            DoubleMatrix matrix = DoubleMatrix.copyOf(xMatrix);

            DoubleMatrix[] resMatrix2D = new DoubleMatrix[keyColumn];

            for (int i = 0; i < keyColumn; ++i)
            {
                resMatrix2D[i] = interpolate(xValues, yValuesMatrix, matrix.columnArray(i));
            }

            return(resMatrix2D);
        }
예제 #21
0
        private void calMatrix()
        {
            int n = _a.Length;

//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] data = new double[n][n];
            double[][] data = RectangularArrays.ReturnRectangularDoubleArray(n, n);
            for (int i = 0; i < n; i++)
            {
                data[i][i] = _a[i];
            }
            for (int i = 1; i < n; i++)
            {
                data[i - 1][i] = _b[i - 1];
            }
            for (int i = 1; i < n; i++)
            {
                data[i][i - 1] = _c[i - 1];
            }
            _matrix = DoubleMatrix.copyOf(data);
        }
예제 #22
0
        /// <summary>
        /// Differentiates the node sensitivity.
        /// </summary>
        /// <param name="pp">  the <seealso cref="PiecewisePolynomialResultsWithSensitivity"/> </param>
        /// <param name="xKeys">  the keys </param>
        /// <returns> the node sensitivity of second derivative value at x=xKeys </returns>
        public virtual DoubleArray[] differentiateTwiceNodeSensitivity(PiecewisePolynomialResultsWithSensitivity pp, double[] xKeys)
        {
            ArgChecker.notNull(pp, "null pp");

            if (pp.Dimensions > 1)
            {
                throw new System.NotSupportedException();
            }
            int nCoefs = pp.Order;

            ArgChecker.isFalse(nCoefs < 3, "Polynomial degree is too low");
            int nIntervals = pp.NumberOfIntervals;

            DoubleMatrix[] diffSense = new DoubleMatrix[nIntervals];
            DoubleMatrix[] senseMat  = pp.CoefficientSensitivityAll;
            int            nData     = senseMat[0].columnCount();

            for (int i = 0; i < nIntervals; ++i)
            {
                double[][] senseMatArray = senseMat[i].toArray();
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] tmp = new double[nCoefs - 2][nData];
                double[][] tmp = RectangularArrays.ReturnRectangularDoubleArray(nCoefs - 2, nData);
                for (int j = 0; j < nCoefs - 2; ++j)
                {
                    for (int k = 0; k < nData; ++k)
                    {
                        tmp[j][k] = (nCoefs - 1 - j) * (nCoefs - 2 - j) * senseMatArray[j][k];
                    }
                }
                diffSense[i] = DoubleMatrix.copyOf(tmp);
            }

            PiecewisePolynomialResultsWithSensitivity ppDiff = new PiecewisePolynomialResultsWithSensitivity(pp.Knots, pp.CoefMatrix, nCoefs - 2, pp.Dimensions, diffSense);

            return(nodeSensitivity(ppDiff, xKeys));
        }
        /// <summary>
        /// Interpolate.
        /// </summary>
        /// <param name="xValues">  the values </param>
        /// <param name="yValuesMatrix">  the matrix </param>
        /// <param name="x">  the x </param>
        /// <returns> Values of the underlying cubic spline functions interpolating {yValuesMatrix.RowVectors} at the value of x </returns>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: public com.opengamma.strata.collect.array.DoubleArray interpolate(final double[] xValues, final double[][] yValuesMatrix, final double x)
        public virtual DoubleArray interpolate(double[] xValues, double[][] yValuesMatrix, double x)
        {
            DoubleMatrix matrix = DoubleMatrix.copyOf(yValuesMatrix);

            return(DoubleArray.of(matrix.rowCount(), i => interpolate(xValues, matrix.rowArray(i), x)));
        }
예제 #24
0
        /// <summary>
        /// Integration.
        /// </summary>
        /// <param name="pp"> the PiecewisePolynomialResult </param>
        /// <param name="initialKey">  the initial key </param>
        /// <param name="xKeys">  the keys </param>
        /// <returns> the integral of piecewise polynomial between initialKey and xKeys  </returns>
        public virtual DoubleArray integrate(PiecewisePolynomialResult pp, double initialKey, double[] xKeys)
        {
            ArgChecker.notNull(pp, "pp");
            ArgChecker.notNull(xKeys, "xKeys");

            ArgChecker.isFalse(double.IsNaN(initialKey), "initialKey containing NaN");
            ArgChecker.isFalse(double.IsInfinity(initialKey), "initialKey containing Infinity");
            ArgChecker.isTrue(pp.Dimensions == 1, "Dimension should be 1");

            DoubleArray knots  = pp.Knots;
            int         nCoefs = pp.Order;
            int         nKnots = pp.NumberOfIntervals + 1;

            int rowCount = nKnots - 1;
            int colCount = nCoefs + 1;

//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] res = new double[rowCount][colCount];
            double[][] res = RectangularArrays.ReturnRectangularDoubleArray(rowCount, colCount);
            for (int i = 0; i < rowCount; ++i)
            {
                for (int j = 0; j < nCoefs; ++j)
                {
                    res[i][j] = pp.CoefMatrix.get(i, j) / (nCoefs - j);
                }
            }

            double[] constTerms = new double[rowCount];
            int      indicator  = 0;

            if (initialKey <= knots.get(1))
            {
                indicator = 0;
            }
            else
            {
                for (int i = 1; i < rowCount; ++i)
                {
                    if (knots.get(i) < initialKey)
                    {
                        indicator = i;
                    }
                }
            }

            double sum = getValue(res[indicator], initialKey, knots.get(indicator));

            for (int i = indicator; i < nKnots - 2; ++i)
            {
                constTerms[i + 1] = constTerms[i] + getValue(res[i], knots.get(i + 1), knots.get(i)) - sum;
                sum = 0.0;
            }

            constTerms[indicator] = -getValue(res[indicator], initialKey, knots.get(indicator));
            for (int i = indicator - 1; i > -1; --i)
            {
                constTerms[i] = constTerms[i + 1] - getValue(res[i], knots.get(i + 1), knots.get(i));
            }
            for (int i = 0; i < rowCount; ++i)
            {
                res[i][nCoefs] = constTerms[i];
            }

            PiecewisePolynomialResult ppInt = new PiecewisePolynomialResult(pp.Knots, DoubleMatrix.copyOf(res), colCount, 1);

            return(evaluate(ppInt, xKeys).row(0));
        }
        public virtual void test_createMetadata_black()
        {
            SabrIborCapletFloorletVolatilityBootstrapDefinition @base = SabrIborCapletFloorletVolatilityBootstrapDefinition.ofFixedBeta(NAME, USD_LIBOR_3M, ACT_ACT_ISDA, 0.5, LINEAR, FLAT, FLAT, SabrVolatilityFormula.hagan());
            RawOptionData capData = RawOptionData.of(ImmutableList.of(Period.ofYears(1), Period.ofYears(5)), DoubleArray.of(0.005, 0.01, 0.015), ValueType.STRIKE, DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 0.15, 0.12, 0.13 },
                new double[] { 0.1, 0.08, 0.09 }
            }), ValueType.BLACK_VOLATILITY);
            SurfaceMetadata expected = Surfaces.blackVolatilityByExpiryStrike(NAME.Name, ACT_ACT_ISDA);
            SurfaceMetadata computed = @base.createMetadata(capData);

            assertEquals(computed, expected);
        }
        public virtual void test_createMetadata_wrongValueType()
        {
            SabrIborCapletFloorletVolatilityBootstrapDefinition @base = SabrIborCapletFloorletVolatilityBootstrapDefinition.ofFixedBeta(NAME, USD_LIBOR_3M, ACT_ACT_ISDA, 0.5, LINEAR, FLAT, FLAT, SabrVolatilityFormula.hagan());
            RawOptionData capData = RawOptionData.of(ImmutableList.of(Period.ofYears(1), Period.ofYears(5)), DoubleArray.of(0.005, 0.01, 0.015), ValueType.STRIKE, DoubleMatrix.copyOf(new double[][]
            {
                new double[] { 0.15, 0.12, 0.13 },
                new double[] { 0.1, 0.08, 0.09 }
            }), ValueType.PRICE);

            assertThrowsIllegalArg(() => @base.createMetadata(capData));
        }