Search in sources :

Example 1 with PrimitiveMatrix

use of org.ojalgo.matrix.PrimitiveMatrix 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 PrimitiveMatrix

use of org.ojalgo.matrix.PrimitiveMatrix 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 PrimitiveMatrix

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

the class FinanceUtils method makeCovarianceMatrix.

/**
 * @param listOfTimeSeries An ordered collection of time series
 * @param mayBeMissingValues Individual series may be missing some values - try to fix this or not
 * @return Annualised covariances
 */
public static <N extends Number> PrimitiveMatrix makeCovarianceMatrix(final List<CalendarDateSeries<N>> listOfTimeSeries, final boolean mayBeMissingValues) {
    final int tmpSize = listOfTimeSeries.size();
    final CoordinationSet<N> tmpUncoordinated = new CoordinationSet<>(listOfTimeSeries);
    final CalendarDateUnit tmpDataResolution = tmpUncoordinated.getResolution();
    if (mayBeMissingValues) {
        tmpUncoordinated.complete();
    }
    final CoordinationSet<N> tmpCoordinated = tmpUncoordinated.prune(tmpDataResolution);
    final Builder<PrimitiveMatrix> tmpMatrixBuilder = PrimitiveMatrix.FACTORY.getBuilder(tmpSize, tmpSize);
    final double tmpToYearFactor = (double) CalendarDateUnit.YEAR.size() / (double) tmpDataResolution.size();
    SampleSet tmpSampleSet;
    final SampleSet[] tmpSampleSets = new SampleSet[tmpSize];
    for (int j = 0; j < tmpSize; j++) {
        final PrimitiveSeries tmpPrimitiveSeries = tmpCoordinated.get(listOfTimeSeries.get(j).getName()).asPrimitive();
        tmpSampleSet = SampleSet.wrap(tmpPrimitiveSeries.quotients().log().toDataSeries());
        tmpMatrixBuilder.set(j, j, tmpToYearFactor * tmpSampleSet.getVariance());
        for (int i = 0; i < j; i++) {
            final double tmpCovariance = tmpToYearFactor * tmpSampleSets[i].getCovariance(tmpSampleSet);
            tmpMatrixBuilder.set(i, j, tmpCovariance);
            tmpMatrixBuilder.set(j, i, tmpCovariance);
        }
        tmpSampleSets[j] = tmpSampleSet;
    }
    return tmpMatrixBuilder.get();
}
Also used : CalendarDateUnit(org.ojalgo.type.CalendarDateUnit) SampleSet(org.ojalgo.random.SampleSet) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) CoordinationSet(org.ojalgo.series.CoordinationSet) PrimitiveSeries(org.ojalgo.series.primitive.PrimitiveSeries)

Example 4 with PrimitiveMatrix

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

the class ConvexProblems method testP20080208.

/**
 * Another case of looping in the ActiveSetSolver's constraint (de)activation.
 */
@Test
public void testP20080208() {
    // create expected returns matrix
    final PrimitiveMatrix tmpExpectedReturns = PrimitiveMatrix.FACTORY.rows(new double[][] { { 9.997829 }, { 10.008909 }, { 10.010849 }, { 9.998919 }, { 10.055549 }, { 9.999127 }, { 9.999720 }, { 10.049002 }, { 9.988769 }, { 9.990095 } });
    // create covariance matrix
    final PrimitiveMatrix tmpCovariances = PrimitiveMatrix.FACTORY.rows(new double[][] { { 0.014661954677318977, 3.459112088561122E-4, 0.007798752920910871, 0.0020921425081866503, 0.001846944297640248, 1.0531906931335766E-4, -2.7515614291198E-4, 0.0034083900074454894, 1.1859491261103433E-4, -0.0027421673864628264 }, { 3.459112088561122E-4, 0.008695862475003915, 0.004154360841751649, -2.661685231819661E-4, -0.0015999007544258263, 3.590680217774603E-4, -0.00186976624370318, 0.0010975416828213752, -5.512038393911129E-4, -0.0010605923775744853 }, { 0.007798752920910871, 0.004154360841751649, 0.032945930970836965, 0.0037716078815399324, -2.2919474365382624E-4, 3.3938035033219876E-4, -0.0015613122026082874, 0.0010975697179894332, 2.296422665244149E-4, -0.001709517941787044 }, { 0.0020921425081866503, -2.661685231819661E-4, 0.0037716078815399324, 0.0057162979859706736, 5.573137056500744E-4, 4.91132887765294E-4, -9.94830474250937E-4, 8.331708084069932E-4, -6.595917138470072E-4, -0.0018693519327569541 }, { 0.001846944297640248, -0.0015999007544258263, -2.2919474365382624E-4, 5.573137056500744E-4, 0.03230071314144326, -2.2320789666419312E-4, -2.2639506820057415E-4, 0.010695663287043154, 0.0014569847730040847, 0.002160537177809949 }, { 1.0531906931335766E-4, 3.590680217774603E-4, 3.3938035033219876E-4, 4.91132887765294E-4, -2.2320789666419312E-4, 0.0017540170708301957, 5.153195618913916E-5, 7.339825618468765E-4, -9.309096233432093E-6, -1.814362059740286E-4 }, { -2.7515614291198E-4, -0.00186976624370318, -0.0015613122026082874, -9.94830474250937E-4, -2.2639506820057415E-4, 5.153195618913916E-5, 0.00809348822665732, -0.0017672866424053742, 3.058672988166145E-4, 0.001201578905822851 }, { 0.0034083900074454894, 0.0010975416828213752, 0.0010975697179894332, 8.331708084069932E-4, 0.010695663287043154, 7.339825618468765E-4, -0.0017672866424053742, 0.013913761913235494, 0.0012785124957521252, 5.298368056593439E-4 }, { 1.1859491261103433E-4, -5.512038393911129E-4, 2.296422665244149E-4, -6.595917138470072E-4, 0.0014569847730040847, -9.309096233432093E-6, 3.058672988166145E-4, 0.0012785124957521252, 0.004650801896027841, 5.437156659657787E-4 }, { -0.0027421673864628264, -0.0010605923775744853, -0.001709517941787044, -0.0018693519327569541, 0.002160537177809949, -1.814362059740286E-4, 0.001201578905822851, 5.298368056593439E-4, 5.437156659657787E-4, 0.007359495478781133 } });
    // create asset variables - cost and weighting constraints
    final Variable[] tmpVariables = new Variable[(int) tmpExpectedReturns.countRows()];
    for (int i = 0; i < tmpVariables.length; i++) {
        tmpVariables[i] = new Variable("VAR" + i);
        final int row = i;
        tmpVariables[i].weight(TypeUtils.toBigDecimal(tmpExpectedReturns.get(row, 0)).negate());
        // set the constraints on the asset weights
        // require at least a 8% allocation to each asset
        tmpVariables[i].lower(new BigDecimal("0.08"));
        // require no more than 12% allocation to each asset
        tmpVariables[i].upper(new BigDecimal("0.12"));
    }
    // exception here...
    final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.07999999999998897 }, { 0.1199999999999636 }, { 0.07999999999999526 }, { 0.08000000000004488 }, { 0.11999999999999084 }, { 0.12000000000018606 }, { 0.11999999999996151 }, { 0.12000000000000167 }, { 0.08000000000001738 }, { 0.08000000000005617 } });
    ConvexProblems.doEarly2008(tmpVariables, tmpCovariances, 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 5 with PrimitiveMatrix

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

the class ConvexProblems method testP20080124.

/**
 * Another case of looping in the ActiveSetSolver's constraint (de)activation.
 */
@Test
public void testP20080124() {
    // create expected returns matrix
    final PrimitiveMatrix expectedReturnsMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 10.012158 }, { 9.996046 }, { 10.000744 }, { 9.990585 }, { 9.998392 }, { 9.996614 }, { 10.010531 }, { 10.001401 }, { 9.997447 }, { 9.993817 }, { 9.998537 }, { 9.995741 }, { 9.987224 }, { 9.992392 } });
    // create covariance matrix
    final PrimitiveMatrix covarianceMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 0.0013191354374342357, 7.786471466322114E-5, -3.810886655309235E-5, -2.28102405899103E-4, -1.2589115740653127E-4, -1.3247692268411991E-5, 1.422624656557158E-4, -2.7176361887359125E-5, 8.675127894495302E-5, -8.116577287090551E-5, -8.468380774247271E-6, 4.930080166695193E-5, -2.774138231533918E-4, -3.148322898570031E-5 }, { 7.786471466322114E-5, 0.001028250547816086, 8.986425197170406E-4, -1.0341435238579975E-5, 6.472902968147139E-4, 2.9014435841747375E-4, 1.0640414444602855E-4, 5.638694128451113E-4, 6.024515366195699E-4, -1.094867665517237E-4, 6.177221606260711E-6, -5.682215091954099E-5, 2.7178074500896235E-4, 0.0010146062950574643 }, { -3.810886655309235E-5, 8.986425197170406E-4, 0.0012477403456464075, -1.8104847201530489E-4, 9.299199981666304E-4, 3.486383951982303E-4, 1.0246402606579107E-4, 7.009722990366382E-4, 6.545695073447614E-4, -1.1680969171500155E-4, 7.123493385355658E-5, 1.559414390174896E-5, 1.972605480880284E-4, 9.368808845809186E-4 }, { -2.28102405899103E-4, -1.0341435238579975E-5, -1.8104847201530489E-4, 6.250793590180099E-4, -5.4721911720097E-6, 1.3081826023829458E-4, -5.644046856412501E-5, -1.1282043806099452E-5, -6.729835202722053E-5, 1.3929681542737307E-4, 3.698155248637573E-6, 5.0269944317023966E-5, 5.344931460074395E-4, -1.1654882792112444E-4 }, { -1.2589115740653127E-4, 6.472902968147139E-4, 9.299199981666304E-4, -5.4721911720097E-6, 0.001181357476541527, 3.0334522038028824E-4, 2.6983840497611894E-4, 6.983493701701867E-4, 5.68816790613126E-4, -7.899505299987754E-5, 1.05074262063586E-5, 1.137295188785598E-4, 1.9732025136606058E-4, 6.631330613471645E-4 }, { -1.3247692268411991E-5, 2.9014435841747375E-4, 3.486383951982303E-4, 1.3081826023829458E-4, 3.0334522038028824E-4, 3.372068413122505E-4, 1.1067468759384309E-4, 2.6589126866881173E-4, 2.1364931019670806E-4, -4.201239472520589E-5, 2.32769639721745E-5, 5.847559594073046E-6, 1.9925897592339058E-4, 1.9671375386540353E-4 }, { 1.422624656557158E-4, 1.0640414444602855E-4, 1.0246402606579107E-4, -5.644046856412501E-5, 2.6983840497611894E-4, 1.1067468759384309E-4, 0.001484755064835215, 1.2295961703024863E-4, 1.0843198781689372E-4, -2.1292328294313923E-5, -4.152686600769749E-6, 1.163599038579726E-4, -3.14739599261259E-4, 2.4519847977412686E-4 }, { -2.7176361887359125E-5, 5.638694128451113E-4, 7.009722990366382E-4, -1.1282043806099452E-5, 6.983493701701867E-4, 2.6589126866881173E-4, 1.2295961703024863E-4, 5.563328439145604E-4, 4.4816730200338125E-4, -3.4729832814007256E-5, -6.028818604193519E-7, 3.192976987126335E-5, 1.7402262469809026E-4, 5.182632389125651E-4 }, { 8.675127894495302E-5, 6.024515366195699E-4, 6.545695073447614E-4, -6.729835202722053E-5, 5.68816790613126E-4, 2.1364931019670806E-4, 1.0843198781689372E-4, 4.4816730200338125E-4, 6.277134808325468E-4, -4.988229718603287E-5, -5.5018781802344255E-6, -1.3231260300518203E-5, 8.214207901880769E-5, 5.841470978796527E-4 }, { -8.116577287090551E-5, -1.094867665517237E-4, -1.1680969171500155E-4, 1.3929681542737307E-4, -7.899505299987754E-5, -4.201239472520589E-5, -2.1292328294313923E-5, -3.4729832814007256E-5, -4.988229718603287E-5, 3.5152692612068785E-4, -9.358092257358399E-6, 4.962216896551324E-6, 1.291957229930161E-4, -1.5046975508620905E-4 }, { -8.468380774247271E-6, 6.177221606260711E-6, 7.123493385355658E-5, 3.698155248637573E-6, 1.05074262063586E-5, 2.32769639721745E-5, -4.152686600769749E-6, -6.028818604193519E-7, -5.5018781802344255E-6, -9.358092257358399E-6, 4.8495980378967104E-5, 1.1704645004909169E-5, 1.814918597253607E-5, 1.2448218299234062E-5 }, { 4.930080166695193E-5, -5.682215091954099E-5, 1.559414390174896E-5, 5.0269944317023966E-5, 1.137295188785598E-4, 5.847559594073046E-6, 1.163599038579726E-4, 3.192976987126335E-5, -1.3231260300518203E-5, 4.962216896551324E-6, 1.1704645004909169E-5, 1.802684481609152E-4, 1.0475986793792914E-5, -4.113641419540392E-5 }, { -2.774138231533918E-4, 2.7178074500896235E-4, 1.972605480880284E-4, 5.344931460074395E-4, 1.9732025136606058E-4, 1.9925897592339058E-4, -3.14739599261259E-4, 1.7402262469809026E-4, 8.214207901880769E-5, 1.291957229930161E-4, 1.814918597253607E-5, 1.0475986793792914E-5, 7.843917688960864E-4, 1.231995848356005E-4 }, { -3.148322898570031E-5, 0.0010146062950574643, 9.368808845809186E-4, -1.1654882792112444E-4, 6.631330613471645E-4, 1.9671375386540353E-4, 2.4519847977412686E-4, 5.182632389125651E-4, 5.841470978796527E-4, -1.5046975508620905E-4, 1.2448218299234062E-5, -4.113641419540392E-5, 1.231995848356005E-4, 0.0011885193322126312 } });
    // 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"));
    // tmpVariables[i].setUpperLimit(new BigDecimal("1.00"));
    }
    final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.3166116715239731 }, { 0.050000000001624065 }, { 0.04999999999827016 }, { 0.05000000000034928 }, { 0.049999999999891145 }, { 0.049999999997416125 }, { 0.08338832846287945 }, { 0.05000000000178943 }, { 0.05000000000085164 }, { 0.04999999999937388 }, { 0.050000000012470555 }, { 0.04999999999966884 }, { 0.050000000000484546 }, { 0.049999999995857476 } });
    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)

Aggregations

PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)19 BigDecimal (java.math.BigDecimal)12 Test (org.junit.jupiter.api.Test)12 BasicMatrix (org.ojalgo.matrix.BasicMatrix)7 Variable (org.ojalgo.optimisation.Variable)6 NumberContext (org.ojalgo.type.context.NumberContext)6 RationalMatrix (org.ojalgo.matrix.RationalMatrix)5 Optimisation (org.ojalgo.optimisation.Optimisation)3 ArrayList (java.util.ArrayList)2 Result (org.ojalgo.optimisation.Optimisation.Result)2 SampleSet (org.ojalgo.random.SampleSet)2 Pair (edu.neu.ccs.pyramid.util.Pair)1 Tag (org.junit.jupiter.api.Tag)1 RecoverableCondition (org.ojalgo.RecoverableCondition)1 Builder (org.ojalgo.matrix.BasicMatrix.Builder)1 DenseReceiver (org.ojalgo.matrix.PrimitiveMatrix.DenseReceiver)1 MatrixStore (org.ojalgo.matrix.store.MatrixStore)1 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)1 Expression (org.ojalgo.optimisation.Expression)1 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)1