Package org.jquantlib.math.matrixutilities

Examples of org.jquantlib.math.matrixutilities.Matrix$ConstRangeMatrix


        inverse(fFlags);
    }

    private void inverse(final Set<Address.Flags> flags) {

        final Matrix M1 = new Matrix(new double[][] {
              { 1.00.90.7 },
              { 0.91.00.4 },
              { 0.70.41.0 }
        }, flags);

        final Matrix M2 = new Matrix(new double[][] {
              { 1.00.90.7 },
              { 0.91.00.3 },
              { 0.70.31.0 }
        }, flags);

        final Matrix I = new Identity(3, flags);

        // from Higham - nearest correlation matrix
        final Matrix M5 = new Matrix(new double[][] {
              {  2,   -1,     0,    0 },
              { -1,    2,    -1,    0 },
              0,   -1,     2,   -1 },
              0,    0,    -1,    2 },
        }, flags);

        QL.info("Testing LU inverse calculation...");

        final Matrix matrices[] = { M1, M2, I, M5 };

        for (final Matrix m : matrices) {
            inverse(m);
        }
    }
View Full Code Here


    private void inverse(final Matrix m) {
        QL.info("Testing LU inverse calculation...");

        final double tol = 1.0e-12;

        final Matrix A = new Matrix(m);
        // System.out.println("A = "+A.toString());

        final Matrix invA = A.inverse();
        // System.out.println("invA = "+invA.toString());

        final Matrix I1 = invA.mul(A);
        // System.out.println("I1 = "+I1.toString());

        final Matrix I2 = A.mul(invA);
        // System.out.println("I2 = "+I2.toString());

        final Matrix eins = new Identity(A.rows());
        // System.out.println("eins = "+eins.toString());

        final double d = norm(I1.sub(eins));
        // System.out.println("d = "+String.valueOf(d));
View Full Code Here


    @Test
    public void testRangeRow() {

        final Matrix mA = new Matrix(new double[][] {
                { 1.00.90.7 },
                { 0.82.03.2 },
                { 0.63.15.0 }
        });
View Full Code Here


    @Test
    public void testRangeCol() {

        final Matrix mA = new Matrix(new double[][] {
                { 1.00.90.7 },
                { 0.82.03.2 },
                { 0.63.15.0 }
        });
View Full Code Here


    @Test
    public void testRange() {

        final Matrix mA = new Matrix(new double[][] {
                { -1.0,      1.11.11.1,      -9.0 },
                //--------------------------------------
                { -1.0, /**/ 1.00.90.7, /**/ -9.0 },
                { -2.5, /**/ 0.82.03.2, /**/ -6.5 },
                { -4.0, /**/ 0.63.15.0, /**/ -4.0 },
                //--------------------------------------
                { -4.0,      9.99.99.9,      -4.0 },
        });

        final Matrix mB = new Matrix(new double[][] {
                { -1.0,      1.11.11.1,      -9.0 },
                //--------------------------------------
                { -1.0, /**/ 5.03.10.6, /**/ -9.0 },
                { -2.5, /**/ 3.22.00.8, /**/ -6.5 },
                { -4.0, /**/ 0.70.91.0, /**/ -4.0 },
                //--------------------------------------
                { -4.0,      9.99.99.9,      -4.0 },
        });

        Matrix matrix;

        matrix = mA.range(1, 4, 1, 4);
        // System.out.println(matrix.toString());
        testRangeRow(matrix);

View Full Code Here

    }

    private void testEigenvectors(final Set<Address.Flags> flags) {
      QL.info("Testing eigenvalues and eigenvectors calculation...");

        final Matrix M1 = new Matrix(new double[][] {
                { 1.00.90.7 },
                { 0.91.00.4 },
                { 0.70.41.0 }
        }, flags);

        final Matrix M2 = new Matrix(new double[][] {
                { 1.00.90.7 },
                { 0.91.00.3 },
                { 0.70.31.0 }
        }, flags);

        final Matrix testMatrices[] = { M1, M2 };


        for (final Matrix M : testMatrices) {

        final SymmetricSchurDecomposition schur = M.schur();
        final Array eigenValues = schur.eigenvalues();
        final Matrix eigenVectors = schur.eigenvectors();
        final double minHolder = Constants.QL_MAX_REAL;

        final int N = M.cols();
        final int offset = M.offset();

        for (int i=offset; i<N+offset; i++) {
          final Array v = new Array(N, M.flags());
          for (int j=offset; j<N+offset; j++) {
                    v.set( j, eigenVectors.get(j, i) );
                }
          // check definition
          final Array a = M.mul(v);
          final Array b = v.mul(eigenValues.get(i));
          final double tol = norm(a.sub(b));
          if (tol > 1.0e-15) {
                    fail("Eigenvector definition not satisfied");
                }
        }

        // check normalization
        final Matrix m = eigenVectors.mul(eigenVectors.transpose());
        final Identity ID = new Identity(N);
        final double tol = norm(m.sub(ID));
        if (tol > 1.0e-15) {
                fail("Eigenvector not normalized");
            }
      }
    }
View Full Code Here

    }

    private void testQRDecomposition(final Set<Address.Flags> flags) {


        final Matrix M1 = new Matrix(new double[][] {
              { 1.00.90.7 },
              { 0.91.00.4 },
              { 0.70.41.0 }
        }, flags);

        final Matrix M2 = new Matrix(new double[][] {
              { 1.00.90.7 },
              { 0.91.00.3 },
              { 0.70.31.0 }
        }, flags);

        final Matrix I = new Identity(3, flags);

        final Matrix M3 = new Matrix(new double[][] {
              { 1,   2,   3,   4 },
              { 2,   0,   2,   1 },
              { 0,   1,   0,   0 }
        }, flags);

        final Matrix M4 = new Matrix(new double[][] {
              {  1,   2,   400    },
              2,   0,     1    },
              { 30,   2,     0    },
              2,   0,     1.05 }
        }, flags);

        // from Higham - nearest correlation matrix
        final Matrix M5 = new Matrix(new double[][] {
              {  2,   -1,     0,    0 },
              { -1,    2,    -1,    0 },
              0,   -1,     2,   -1 },
              0,    0,    -1,    2 },
        }, flags);

//        // from Higham - nearest correlation matrix to M5
//        Matrix M6 = new Matrix(new double[][] {
//              {  1,              -0.8084124981,    0.1915875019,    0.106775049  },
//              { -0.8084124981,    1,              -0.6562326948,    0.1915875019 },
//              {  0.1915875019,   -0.6562326948,    1,              -0.8084124981 },
//              {  0.106775049,     0.1915875019,   -0.8084124981,    1            }
//        });
//
//        Matrix M7 = M1.clone();
//        M7.set(0, 1, 0.3); M7.set(0, 2, 0.2); M7.set(2, 1, 1.2);


        QL.info("Testing QR decomposition...");

        final double tolerance = 1.0e-12;

        final Matrix testMatrices[] = { M1, M2, I, M3, M3.transpose(), M4, M4.transpose(), M5 };

        for (final Matrix A : testMatrices) {

            QRDecomposition qr;
            Matrix Q;
            Matrix R;
            final Matrix P;
            final Matrix mT;
            Matrix mul1;
            final Matrix mul2;
            double norm;

            // System.out.println("///////////////////////////////////////////////");

            // System.out.println("Matrix A = "+A.toString());

            // System.out.println("// QR decomposition with column pivoting");
            qr = new Matrix(A).qr(true);
            R = qr.R();
            Q = qr.Q();
            P = qr.P();

            // norm(Q*R - A*P)
            mul1 = Q.mul(R);
            mul2 = A.mul(P);
            norm = norm( mul1.sub(mul2) );
            if (norm > tolerance) {
                fail("Q*R (pivot=true) does not match matrix A*P :: norm = "+String.valueOf(norm));
            }



            // System.out.println("// QR decomposition without column pivoting");
            qr = new Matrix(A).qr();
            // norm(Q*R - A)
            R = qr.R();
            Q = qr.Q();

            mul1 = Q.mul(R);
View Full Code Here

        return sqrtCorrelation_;
    }

    @Override
    public Matrix covariance(final /*@Time*/ double t0, final Array x0, final /*@Time*/ double dt)  {
        final Matrix tmp = stdDeviation(t0, x0, dt);
        return tmp.mul(tmp.transpose());
    }
View Full Code Here

  public TreeLattice2D(final T tree1, final T tree2, final double correlation) {

    super(tree1.timeGrid(), T.branches.getValue() * T.branches.getValue());
    this.tree1 = tree1;
    this.tree2 = tree2;
    this.m = new Matrix(T.branches.getValue(), T.branches.getValue());
    rho = Math.abs(correlation);
    // what happens here?
    if (correlation < 0.0 && T.branches.getValue() == 3) {
      m.set(0, 0, -1.0);
      m.set(0, 1, -4.0);
View Full Code Here

TOP

Related Classes of org.jquantlib.math.matrixutilities.Matrix$ConstRangeMatrix

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.