Search in sources :

Example 6 with RationalMatrix

use of org.ojalgo.matrix.RationalMatrix in project ojAlgo by optimatika.

the class ConvexProblems method testP20080118.

/**
 * Ended up with a singular matrix (the equation system body generated by the LagrangeSolver) that
 * resulted in a solution with NaN and Inf elements. This was not recognised and handled.
 */
@Test
public void testP20080118() {
    // create expected returns matrix
    final PrimitiveMatrix expectedReturnsMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 10.003264 }, { 9.989771 }, { 9.987513 }, { 9.988449 }, { 9.996579 }, { 9.990690 }, { 9.994904 }, { 9.994514 }, { 9.984064 }, { 9.987534 } });
    // create covariance matrix
    final PrimitiveMatrix covarianceMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 6.483565230120298E-4, -1.3344603795915894E-4, -4.610345510893708E-4, -7.334405624030001E-4, 1.1551383115707195E-5, -0.00104145662863434, -1.0725896685568462E-4, -1.221384153392056E-4, -4.173413644389791E-4, -2.4861043894946935E-4 }, { -1.3344603795915894E-4, 0.0026045957224784455, 0.0012394355327235707, 9.243919166568456E-4, -8.653805945112411E-5, 8.100239312410631E-4, 4.215960274481846E-4, 5.243272007211247E-4, 0.0013062718630332956, 1.4766450293395405E-4 }, { -4.610345510893708E-4, 0.0012394355327235707, 0.002361436913752224, 0.0020101714731002238, -1.4236763916609785E-5, 0.002120395905829043, 5.399158658928662E-4, 5.048790842067473E-4, 0.0014855261720730444, 4.841458106181396E-4 }, { -7.334405624030001E-4, 9.243919166568456E-4, 0.0020101714731002238, 0.0028542819089926895, -4.311102526746861E-6, 0.0028465650900869476, 6.242643883624462E-4, 4.086484048798765E-4, 0.001647437646316569, 7.58419663970477E-4 }, { 1.1551383115707195E-5, -8.653805945112411E-5, -1.4236763916609785E-5, -4.311102526746861E-6, 1.213366124417227E-4, -9.027529241741836E-5, 7.241389994693716E-6, -3.166855950737129E-5, -1.2445276374560802E-5, -5.3976919759028745E-5 }, { -0.00104145662863434, 8.100239312410631E-4, 0.002120395905829043, 0.0028465650900869476, -9.027529241741836E-5, 0.0064756879298965295, 2.8076277564885113E-4, 3.6082073553997553E-4, 0.001945238279500792, 0.0012421132342988626 }, { -1.0725896685568462E-4, 4.215960274481846E-4, 5.399158658928662E-4, 6.242643883624462E-4, 7.241389994693716E-6, 2.8076277564885113E-4, 0.0010121500024739688, 8.206099676659543E-5, 1.6129237403855146E-4, 7.550465994733837E-4 }, { -1.221384153392056E-4, 5.243272007211247E-4, 5.048790842067473E-4, 4.086484048798765E-4, -3.166855950737129E-5, 3.6082073553997553E-4, 8.206099676659543E-5, 4.504461842318998E-4, 4.7980942831718363E-4, -4.763223568683059E-5 }, { -4.173413644389791E-4, 0.0013062718630332956, 0.0014855261720730444, 0.001647437646316569, -1.2445276374560802E-5, 0.001945238279500792, 1.6129237403855146E-4, 4.7980942831718363E-4, 0.002228245076175045, 3.2083564921169634E-4 }, { -2.4861043894946935E-4, 1.4766450293395405E-4, 4.841458106181396E-4, 7.58419663970477E-4, -5.3976919759028745E-5, 0.0012421132342988626, 7.550465994733837E-4, -4.763223568683059E-5, 3.2083564921169634E-4, 0.0017093327832123186 } });
    // create asset variables - cost and weighting constraints
    final Variable[] tmpVariables = new Variable[(int) expectedReturnsMatrix.countRows()];
    for (int i = 0; i < tmpVariables.length; i++) {
        tmpVariables[i] = new Variable("VAR" + i);
        final int row = i;
        tmpVariables[i].weight(TypeUtils.toBigDecimal(expectedReturnsMatrix.get(row, 0)).negate());
        // set the constraints on the asset weights
        // require at least a 2% allocation to each asset
        tmpVariables[i].lower(new BigDecimal("0.05"));
        // require no more than 80% allocation to each asset
        tmpVariables[i].upper(new BigDecimal("0.35"));
    }
    final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.35 }, { 0.05 }, { 0.05 }, { 0.05 }, { 0.25 }, { 0.05 }, { 0.05 }, { 0.05 }, { 0.05 }, { 0.05 } });
    ConvexProblems.doEarly2008(tmpVariables, covarianceMatrix, tmpExpected);
}
Also used : Variable(org.ojalgo.optimisation.Variable) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) BigDecimal(java.math.BigDecimal) RationalMatrix(org.ojalgo.matrix.RationalMatrix) Test(org.junit.jupiter.api.Test)

Example 7 with RationalMatrix

use of org.ojalgo.matrix.RationalMatrix in project ojAlgo by optimatika.

the class LUTest method testP20061119Case.

@Test
public void testP20061119Case() {
    final RationalMatrix tmpProblematic = P20061119Case.getProblematic();
    final LU<BigDecimal> tmpBig = LU.BIG.make();
    tmpBig.decompose(BigDenseStore.FACTORY.copy(tmpProblematic));
    final LU<ComplexNumber> tmpComplex = LU.COMPLEX.make();
    tmpComplex.decompose(ComplexDenseStore.FACTORY.copy(tmpProblematic));
    final LU<Double> tmpPrimitive = LU.PRIMITIVE.make();
    tmpPrimitive.decompose(PrimitiveDenseStore.FACTORY.copy(tmpProblematic));
    final LU<Double> tmpJama = new RawLU();
    tmpJama.decompose(PrimitiveDenseStore.FACTORY.copy(tmpProblematic));
    final NumberContext tmpPrintContext = NumberContext.getGeneral(20);
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Big L", tmpBig.getL(), tmpPrintContext);
        BasicLogger.debug("Complex L", tmpComplex.getL(), tmpPrintContext);
        BasicLogger.debug("Primitive L", tmpPrimitive.getL(), tmpPrintContext);
        BasicLogger.debug("Jama L", tmpJama.getL(), tmpPrintContext);
    }
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Big U", tmpBig.getU(), tmpPrintContext);
        BasicLogger.debug("Complex U", tmpComplex.getU(), tmpPrintContext);
        BasicLogger.debug("Primitive U", tmpPrimitive.getU(), tmpPrintContext);
        BasicLogger.debug("Jama U", tmpJama.getU(), tmpPrintContext);
    }
    final SingularValue<Double> tmpSVD = new RawSingularValue();
    tmpSVD.decompose(PrimitiveDenseStore.FACTORY.copy(tmpProblematic));
    TestUtils.assertEquals("LU.rank SVD vs Big", tmpSVD.getRank(), tmpBig.getRank());
    TestUtils.assertEquals("LU.rank SVD vs Complex", tmpSVD.getRank(), tmpComplex.getRank());
    TestUtils.assertEquals("LU.rank SVD vs Primitive", tmpSVD.getRank(), tmpPrimitive.getRank());
    TestUtils.assertEquals("LU.rank SVD vs Jama", tmpSVD.getRank(), tmpJama.getRank());
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) RationalMatrix(org.ojalgo.matrix.RationalMatrix) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 8 with RationalMatrix

use of org.ojalgo.matrix.RationalMatrix in project ojAlgo by optimatika.

the class QRTest method testP20030422Case.

@Test
public void testP20030422Case() {
    final RationalMatrix tmpOriginal = P20030422Case.getProblematic();
    final QR<BigDecimal> tmpBigDecomp = QR.BIG.make();
    final QR<ComplexNumber> tmpComplexDecomp = QR.COMPLEX.make();
    final QR<Double> tmpPrimitiveDecomp = QR.PRIMITIVE.make();
    tmpBigDecomp.decompose(BigDenseStore.FACTORY.copy(tmpOriginal));
    tmpComplexDecomp.decompose(ComplexDenseStore.FACTORY.copy(tmpOriginal));
    tmpPrimitiveDecomp.decompose(PrimitiveDenseStore.FACTORY.copy(tmpOriginal));
    final MatrixStore<BigDecimal> tmpBigQ = tmpBigDecomp.getQ();
    final MatrixStore<ComplexNumber> tmpComplexQ = tmpComplexDecomp.getQ();
    final MatrixStore<Double> tmpPrimitiveQ = tmpPrimitiveDecomp.getQ();
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Big Q", tmpBigQ);
        BasicLogger.debug("Complex Q", tmpComplexQ);
        BasicLogger.debug("Primitive Q", tmpPrimitiveQ);
    }
    final MatrixStore<BigDecimal> tmpBigR = tmpBigDecomp.getR();
    final MatrixStore<ComplexNumber> tmpComplexR = tmpComplexDecomp.getR();
    final MatrixStore<Double> tmpPrimitiveR = tmpPrimitiveDecomp.getR();
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Big R", tmpBigR);
        BasicLogger.debug("Complex R", tmpComplexR);
        BasicLogger.debug("Primitive R", tmpPrimitiveR);
    }
    TestUtils.assertEquals(BigDenseStore.FACTORY.copy(tmpOriginal), tmpBigDecomp, new NumberContext(7, 14));
    TestUtils.assertEquals(ComplexDenseStore.FACTORY.copy(tmpOriginal), tmpComplexDecomp, new NumberContext(7, 14));
    TestUtils.assertEquals(PrimitiveDenseStore.FACTORY.copy(tmpOriginal), tmpPrimitiveDecomp, new NumberContext(7, 14));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) RationalMatrix(org.ojalgo.matrix.RationalMatrix) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Aggregations

BigDecimal (java.math.BigDecimal)8 Test (org.junit.jupiter.api.Test)8 RationalMatrix (org.ojalgo.matrix.RationalMatrix)8 PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)5 Variable (org.ojalgo.optimisation.Variable)5 ComplexNumber (org.ojalgo.scalar.ComplexNumber)2 NumberContext (org.ojalgo.type.context.NumberContext)2 BasicMatrix (org.ojalgo.matrix.BasicMatrix)1 State (org.ojalgo.optimisation.Optimisation.State)1