Search in sources :

Example 1 with Builder

use of org.ojalgo.matrix.BasicMatrix.Builder in project ojAlgo-finance by optimatika.

the class PortfolioProblems method testP20110614.

/**
 * A user claimed he got constraint breaking weights using these figures.
 */
@Test
public void testP20110614() {
    final Builder<PrimitiveMatrix> tmpCovarsBuilder = PrimitiveMatrix.FACTORY.getBuilder(3, 3);
    tmpCovarsBuilder.set(0, 0, 0.04);
    tmpCovarsBuilder.set(0, 1, 0.01);
    tmpCovarsBuilder.set(0, 2, 0.02);
    tmpCovarsBuilder.set(1, 0, 0.01);
    tmpCovarsBuilder.set(1, 1, 0.09);
    tmpCovarsBuilder.set(1, 2, 0.01);
    tmpCovarsBuilder.set(2, 0, 0.02);
    tmpCovarsBuilder.set(2, 1, 0.01);
    tmpCovarsBuilder.set(2, 2, 0.16);
    final BasicMatrix tmpCovars = tmpCovarsBuilder.build();
    final Builder<PrimitiveMatrix> tmpReturnsBuilder = PrimitiveMatrix.FACTORY.getBuilder(3, 1);
    tmpReturnsBuilder.set(0, 0, 0.10);
    tmpReturnsBuilder.set(1, 0, 0.15);
    tmpReturnsBuilder.set(2, 0, 0.18);
    final BasicMatrix tmpReturs = tmpReturnsBuilder.build();
    final MarketEquilibrium tmpME = new MarketEquilibrium(tmpCovars);
    final MarkowitzModel tmpMarkowitz = new MarkowitzModel(tmpME, tmpReturs);
    for (int i = 1; i < 10; i++) {
        tmpMarkowitz.setRiskAversion(new BigDecimal(i));
        final List<BigDecimal> tmpWeights = tmpMarkowitz.getWeights();
        for (final BigDecimal tmpBigDecimal : tmpWeights) {
            if ((tmpBigDecimal.compareTo(BigMath.ZERO) == -1) || (tmpBigDecimal.compareTo(BigMath.ONE) == 1)) {
                TestUtils.fail("!(0.0 <= " + tmpBigDecimal + " <= 1.0)");
            }
        }
    }
    // As the Markowitz model built it the problem
    final MatrixStore<Double> tmpQ = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 4.0, 1.0, 2.0 }, { 1.0, 9.0, 1.0 }, { 2.0, 1.0, 16.0 } });
    final MatrixStore<Double> tmpC = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 10.0 }, { 15.0 }, { 18.0 } });
    final MatrixStore<Double> tmpAE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 1.0, 1.0 } });
    final MatrixStore<Double> tmpBE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0 } });
    MatrixStore<Double> tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -1.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0 }, { 0.0, 0.0, -1.0 } });
    MatrixStore<Double> tmpBI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0 }, { 0.0 }, { 0.0 } });
    final MatrixStore<Double> tmpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0 }, { 0.5217391304347826 }, { 0.4782608695652173 } });
    ConvexSolver.Builder tmpBuilder = new ConvexSolver.Builder(tmpQ, tmpC).equalities(tmpAE, tmpBE).inequalities(tmpAI, tmpBI);
    ConvexSolver tmpSolver = tmpBuilder.build();
    // tmpSolver.options.debug(ConvexSolver.class);
    Optimisation.Result tmpResult = tmpSolver.solve();
    // BasicMatrix tmpSolution = tmpResult.getSolution();
    TestUtils.assertEquals(tmpX, tmpResult, new NumberContext(7, 6));
    // As (I believe) the user built it
    // 
    // The only *problem* I found was that he did not set lower limits
    // on the portfolio weights, which you have to do. No problem with
    // ojAlgo.
    tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } });
    tmpBI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0 }, { 1.0 }, { 1.0 } });
    tmpBuilder = new ConvexSolver.Builder(tmpQ, tmpC).equalities(tmpAE, tmpBE).inequalities(tmpAI, tmpBI);
    tmpSolver = tmpBuilder.build();
    tmpResult = tmpSolver.solve();
    // Should NOT be equal in this case!
    TestUtils.assertFalse(Access1D.equals(tmpX, tmpResult, new NumberContext(7, 6)));
    // No problem with both the lower and upper limits set.
    tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -1.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0 }, { 0.0, 0.0, -1.0 }, { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } });
    tmpBI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0 }, { 0.0 }, { 0.0 }, { 1.0 }, { 1.0 }, { 1.0 } });
    tmpBuilder = new ConvexSolver.Builder(tmpQ, tmpC).equalities(tmpAE, tmpBE).inequalities(tmpAI, tmpBI);
    tmpSolver = tmpBuilder.build();
    tmpResult = tmpSolver.solve();
    TestUtils.assertEquals(tmpX, tmpResult, new NumberContext(7, 6));
}
Also used : Optimisation(org.ojalgo.optimisation.Optimisation) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) Builder(org.ojalgo.matrix.BasicMatrix.Builder) NumberContext(org.ojalgo.type.context.NumberContext) BigDecimal(java.math.BigDecimal) ConvexSolver(org.ojalgo.optimisation.convex.ConvexSolver) BasicMatrix(org.ojalgo.matrix.BasicMatrix) Test(org.junit.jupiter.api.Test)

Aggregations

BigDecimal (java.math.BigDecimal)1 Test (org.junit.jupiter.api.Test)1 BasicMatrix (org.ojalgo.matrix.BasicMatrix)1 Builder (org.ojalgo.matrix.BasicMatrix.Builder)1 PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)1 Optimisation (org.ojalgo.optimisation.Optimisation)1 ConvexSolver (org.ojalgo.optimisation.convex.ConvexSolver)1 NumberContext (org.ojalgo.type.context.NumberContext)1