Search in sources :

Example 1 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo-finance by optimatika.

the class FinanceUtilsTest method doTestCleaning.

private static void doTestCleaning(final double[][] original) {
    final NumberContext tmpEvalCntx = NumberContext.getGeneral(6, 12);
    final PrimitiveDenseStore tmpOriginal = PrimitiveDenseStore.FACTORY.rows(original);
    final SingularValue<Double> tmpSVD = SingularValue.make(tmpOriginal);
    tmpSVD.decompose(tmpOriginal);
    final double tmpRefCond = tmpSVD.getCondition();
    final int tmpRefRank = tmpSVD.getRank();
    final double tmpRefNorm = tmpSVD.getFrobeniusNorm();
    final PrimitiveMatrix tmpCorrelations = FinanceUtils.toCorrelations(tmpOriginal, true);
    final PrimitiveMatrix tmpVolatilities = FinanceUtils.toVolatilities(tmpOriginal, true);
    final PrimitiveMatrix tmpCovariances = FinanceUtils.toCovariances(tmpVolatilities, tmpCorrelations);
    tmpSVD.decompose(PrimitiveDenseStore.FACTORY.copy(tmpCovariances));
    final double tmpNewCond = tmpSVD.getCondition();
    final int tmpNewRank = tmpSVD.getRank();
    final double tmpNewNorm = tmpSVD.getFrobeniusNorm();
    TestUtils.assertTrue("Improved the condition", tmpNewCond <= tmpRefCond);
    TestUtils.assertTrue("Improved the rank", tmpNewRank >= tmpRefRank);
    TestUtils.assertEquals("Full rank", original.length, tmpNewRank);
    TestUtils.assertEquals("Roughly the same frob norm", tmpRefNorm, tmpNewNorm, tmpEvalCntx);
    if (DEBUG) {
        BasicLogger.debug("Original", tmpOriginal);
        BasicLogger.debug("Cleaned", tmpCovariances);
        BasicLogger.debug("Difference", tmpOriginal.subtract(PrimitiveDenseStore.FACTORY.copy(tmpCovariances)), tmpEvalCntx);
    }
}
Also used : PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) NumberContext(org.ojalgo.type.context.NumberContext) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 2 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo-finance by optimatika.

the class PortfolioProblems method testP20160608.

/**
 * <a href="https://github.com/optimatika/ojAlgo/issues/23">GitHub Issue 23</a> The problem was that since
 * the model allows shorting the pure profit maximisation is unbounded (initial LP). The algorithm did not
 * handle the case where "target" could be >= the max possible when shorting not allowed (bounded LP).
 */
@Test
public void testP20160608() {
    final BasicMatrix.Factory<PrimitiveMatrix> matrixFactory = PrimitiveMatrix.FACTORY;
    final PrimitiveMatrix cov = matrixFactory.rows(new double[][] { { 0.01, 0.0018, 0.0011 }, { 0.0018, 0.0109, 0.0026 }, { 0.0011, 0.0026, 0.0199 } });
    final PrimitiveMatrix ret = matrixFactory.columns(new double[] { 0.0427, 0.0015, 0.0285 });
    final MarketEquilibrium marketEquilibrium = new MarketEquilibrium(cov);
    final MarkowitzModel markowitz = new MarkowitzModel(marketEquilibrium, ret);
    markowitz.setShortingAllowed(true);
    markowitz.setTargetReturn(BigDecimal.valueOf(0.0427));
    List<BigDecimal> tmpWeights = markowitz.getWeights();
    TestUtils.assertTrue(markowitz.optimiser().getState().isFeasible());
    final NumberContext tmpTestPrecision = StandardType.PERCENT.newPrecision(4);
    // Solution reachable without shorting, but since it is allowed the optimal solution is different
    // 0.82745
    TestUtils.assertEquals(0.82745, tmpWeights.get(0).doubleValue(), tmpTestPrecision);
    // -0.09075
    TestUtils.assertEquals(-0.09075, tmpWeights.get(1).doubleValue(), tmpTestPrecision);
    // 0.26329
    TestUtils.assertEquals(0.26329, tmpWeights.get(2).doubleValue(), tmpTestPrecision);
    TestUtils.assertEquals(0.0427, markowitz.getMeanReturn(), tmpTestPrecision);
    TestUtils.assertEquals(0.0084, markowitz.getReturnVariance(), tmpTestPrecision);
    // Also verify that it's posible to reach 10% return by shorting
    markowitz.setTargetReturn(BigDecimal.valueOf(0.1));
    TestUtils.assertEquals(0.1, markowitz.getMeanReturn(), tmpTestPrecision);
    TestUtils.assertTrue(markowitz.optimiser().getState().isFeasible());
    // Min risk portfolio, very high risk aversion means minimum risk.
    markowitz.setTargetReturn(null);
    markowitz.setRiskAversion(new BigDecimal(1000000));
    tmpWeights = markowitz.getWeights();
    TestUtils.assertTrue(markowitz.optimiser().getState().isFeasible());
    // 0.4411
    TestUtils.assertEquals(0.4411, tmpWeights.get(0).doubleValue(), tmpTestPrecision);
    // 0.3656
    TestUtils.assertEquals(0.3656, tmpWeights.get(1).doubleValue(), tmpTestPrecision);
    // 0.1933
    TestUtils.assertEquals(0.1933, tmpWeights.get(2).doubleValue(), tmpTestPrecision);
}
Also used : BasicMatrix(org.ojalgo.matrix.BasicMatrix) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) NumberContext(org.ojalgo.type.context.NumberContext) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 3 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.

the class P20030512Case method setUp.

@BeforeEach
@Override
public void setUp() {
    DEFINITION = new NumberContext(7, 12);
    EVALUATION = new NumberContext(5, 6);
    myBigAA = P20030512Case.getProblematic();
    myBigAX = BasicMatrixTest.getIdentity(myBigAA.countColumns(), myBigAA.countColumns(), DEFINITION);
    myBigAB = myBigAA;
    myBigI = BasicMatrixTest.getIdentity(myBigAA.countRows(), myBigAA.countColumns(), DEFINITION);
    myBigSafe = BasicMatrixTest.getSafe(myBigAA.countRows(), myBigAA.countColumns(), DEFINITION);
    super.setUp();
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.

the class P20030528Case method setUp.

@BeforeEach
@Override
public void setUp() {
    DEFINITION = new NumberContext(7, 1);
    EVALUATION = new NumberContext(7, 9);
    myBigAA = P20030528Case.getProblematic();
    myBigAX = BasicMatrixTest.getIdentity(myBigAA.countColumns(), myBigAA.countColumns(), DEFINITION);
    myBigAB = myBigAA;
    myBigI = BasicMatrixTest.getIdentity(myBigAA.countRows(), myBigAA.countColumns(), DEFINITION);
    myBigSafe = BasicMatrixTest.getSafe(myBigAA.countRows(), myBigAA.countColumns(), DEFINITION);
    super.setUp();
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.

the class P20050125Case method setUp.

@BeforeEach
@Override
public void setUp() {
    DEFINITION = new NumberContext(7, 9);
    EVALUATION = new NumberContext(7, 6);
    myBigAA = P20050125Case.getProblematic();
    myBigAX = BasicMatrixTest.getIdentity(myBigAA.countColumns(), myBigAA.countColumns(), DEFINITION);
    myBigAB = myBigAA;
    myBigI = BasicMatrixTest.getIdentity(myBigAA.countRows(), myBigAA.countColumns(), DEFINITION);
    myBigSafe = BasicMatrixTest.getSafe(myBigAA.countRows(), myBigAA.countColumns(), DEFINITION);
    super.setUp();
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

NumberContext (org.ojalgo.type.context.NumberContext)91 Test (org.junit.jupiter.api.Test)63 ComplexNumber (org.ojalgo.scalar.ComplexNumber)16 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)15 BigDecimal (java.math.BigDecimal)14 BasicMatrix (org.ojalgo.matrix.BasicMatrix)13 BeforeEach (org.junit.jupiter.api.BeforeEach)12 Result (org.ojalgo.optimisation.Optimisation.Result)12 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)9 Expression (org.ojalgo.optimisation.Expression)8 Variable (org.ojalgo.optimisation.Variable)8 PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)6 Optimisation (org.ojalgo.optimisation.Optimisation)6 Uniform (org.ojalgo.random.Uniform)4 BigArray (org.ojalgo.array.BigArray)3 SimultaneousPrimitive (org.ojalgo.matrix.decomposition.HermitianEvD.SimultaneousPrimitive)3 MatrixStore (org.ojalgo.matrix.store.MatrixStore)3 Tag (org.junit.jupiter.api.Tag)2 RationalMatrix (org.ojalgo.matrix.RationalMatrix)2 Solver (org.ojalgo.matrix.decomposition.MatrixDecomposition.Solver)2