Search in sources :

Example 11 with PrimitiveMatrix

use of org.ojalgo.matrix.PrimitiveMatrix in project pyramid by cheng-li.

the class CBMInspector method covariance.

public static void covariance(CBM CBM, Vector vector, LabelTranslator labelTranslator) {
    int numClusters = CBM.getNumComponents();
    int numClasses = CBM.getNumClasses();
    double[] proportions = CBM.getMultiClassClassifier().predictClassProbs(vector);
    double[][] probabilities = new double[numClusters][numClasses];
    for (int k = 0; k < numClusters; k++) {
        for (int l = 0; l < numClasses; l++) {
            probabilities[k][l] = CBM.getBinaryClassifiers()[k][l].predictClassProb(vector, 1);
        }
    }
    // column vector
    DenseReceiver meanBuilder = factory.makeDense(numClasses, 1);
    for (int l = 0; l < numClasses; l++) {
        double sum = 0;
        for (int k = 0; k < numClusters; k++) {
            sum += proportions[k] * probabilities[k][l];
        }
        meanBuilder.set(l, 0, sum);
    }
    PrimitiveMatrix mean = meanBuilder.build();
    // System.out.println(mean);
    List<PrimitiveMatrix> mus = new ArrayList<>();
    for (int k = 0; k < numClusters; k++) {
        DenseReceiver muBuilder = factory.makeDense(numClasses, 1);
        for (int l = 0; l < numClasses; l++) {
            muBuilder.set(l, 0, probabilities[k][l]);
        }
        PrimitiveMatrix muK = muBuilder.build();
        mus.add(muK);
    }
    List<PrimitiveMatrix> sigmas = new ArrayList<>();
    for (int k = 0; k < numClusters; k++) {
        DenseReceiver sigmaBuilder = factory.makeDense(numClasses, numClasses);
        for (int l = 0; l < numClasses; l++) {
            double v = probabilities[k][l] * (1 - probabilities[k][l]);
            sigmaBuilder.set(l, l, v);
        }
        PrimitiveMatrix sigmaK = sigmaBuilder.build();
        sigmas.add(sigmaK);
    }
    PrimitiveMatrix covariance = factory.makeZero(numClasses, numClasses);
    for (int k = 0; k < numClusters; k++) {
        PrimitiveMatrix muk = mus.get(k);
        PrimitiveMatrix toadd = (sigmas.get(k).add(muk.multiply(muk.transpose()))).multiply(proportions[k]);
        covariance = covariance.add(toadd);
    }
    covariance = covariance.subtract(mean.multiply(mean.transpose()));
    // System.out.println("covariance = "+ Matrices.display(covariance));
    DenseReceiver correlationBuilder = factory.makeDense(numClasses, numClasses);
    for (int l = 0; l < numClasses; l++) {
        for (int j = 0; j < numClasses; j++) {
            double v = covariance.get(l, j).doubleValue() / (Math.sqrt(covariance.get(l, l).doubleValue()) * Math.sqrt(covariance.get(j, j).doubleValue()));
            correlationBuilder.set(l, j, v);
        }
    }
    PrimitiveMatrix correlation = correlationBuilder.build();
    // System.out.println("correlation = "+ Matrices.display(correlation));
    List<Pair<String, Double>> list = new ArrayList<>();
    for (int l = 0; l < numClasses; l++) {
        for (int j = 0; j < l; j++) {
            String s = "" + labelTranslator.toExtLabel(l) + ", " + labelTranslator.toExtLabel(j);
            double v = correlation.get(l, j).doubleValue();
            Pair<String, Double> pair = new Pair<>(s, v);
            list.add(pair);
        }
    }
    Comparator<Pair<String, Double>> comparator = Comparator.comparing(pair -> Math.abs(pair.getSecond()));
    List<Pair<String, Double>> top = list.stream().sorted(comparator.reversed()).limit(20).collect(Collectors.toList());
    System.out.println(top);
}
Also used : DenseReceiver(org.ojalgo.matrix.PrimitiveMatrix.DenseReceiver) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) Pair(edu.neu.ccs.pyramid.util.Pair)

Example 12 with PrimitiveMatrix

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

the class ConvexProblems method testP20080819.

/**
 * Another case of looping in the ActiveSetSolver's constraint (de)activation. Slightly different case (I
 * believe). The main reason/difficulty seemed to be that the algorithm would both add and remove
 * constraints in the iteration. Modified the algorithm to only do one thing with each iteration - either
 * add or remove.
 */
@SuppressWarnings("unchecked")
@Test
public void testP20080819() {
    final Factory<PrimitiveMatrix> tmpMtrxFact = PrimitiveMatrix.FACTORY;
    final NumberContext tmpEvalCntxt = StandardType.DECIMAL_032;
    final BasicMatrix[] tmpMatrices = new PrimitiveMatrix[8];
    tmpMatrices[0] = tmpMtrxFact.rows(new double[][] { { 1.0, 1.0, 1.0, 1.0 } });
    tmpMatrices[1] = tmpMtrxFact.rows(new double[][] { { 1.0 } });
    tmpMatrices[2] = tmpMtrxFact.rows(new double[][] { { 15.889978159746546, 7.506345724913546, 0.8416674706550127, 0.435643236753381 }, { 7.506345724913546, 8.325860065234632, 0.4230651628792374, 0.1670802923999648 }, { 0.8416674706550127, 0.4230651628792374, 1.00134099479915, 0.6558469727234849 }, { 0.435643236753381, 0.1670802923999648, 0.6558469727234849, 0.6420451103682865 } });
    tmpMatrices[3] = tmpMtrxFact.rows(new double[][] { { -0.15804736429388952 }, { -0.11226063792731895 }, { -0.10509261785657838 }, { -0.0848686735786316 } });
    tmpMatrices[4] = tmpMtrxFact.rows(new double[][] { { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 1.0 }, { -0.15804736429388952, -0.11226063792731895, -0.10509261785657838, -0.0848686735786316 }, { -1.0, 0.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0, 0.0 }, { 0.0, 0.0, -1.0, 0.0 }, { 0.0, 0.0, 0.0, -1.0 } });
    tmpMatrices[5] = tmpMtrxFact.rows(new double[][] { { 0.9 }, { 0.8 }, { 0.7 }, { 0.6 }, { 0.0 }, { -0.1 }, { -0.2 }, { -0.3 }, { -0.4 } });
    tmpMatrices[6] = tmpMtrxFact.rows(new double[][] { { 0.1 }, { 0.2 }, { 0.3 }, { 0.4 } });
    tmpMatrices[7] = null;
    final MatrixStore<Double>[] retVal = new MatrixStore[tmpMatrices.length];
    for (int i = 0; i < retVal.length; i++) {
        if (tmpMatrices[i] != null) {
            if (i == 3) {
                retVal[i] = PrimitiveDenseStore.FACTORY.copy(tmpMatrices[i].negate());
            } else {
                retVal[i] = PrimitiveDenseStore.FACTORY.copy(tmpMatrices[i]);
            }
        }
    }
    final ConvexSolver.Builder tmpBuilder = new ConvexSolver.Builder(retVal);
    // final ActiveSetSolver tmpSolver = new ActiveSetSolver(tmpMatrices);
    final ConvexSolver tmpSolver = tmpBuilder.build();
    // Test that the matrices were input in the right order
    // JUnitUtils.assertEquals(tmpSolver.getAE(), tmpMatrices[0].toPrimitiveStore(),
    // tmpEvalCntxt);
    // JUnitUtils.assertEquals(tmpSolver.getBE(), tmpMatrices[1].toPrimitiveStore(),
    // tmpEvalCntxt);
    // JUnitUtils.assertEquals(tmpSolver.getQ(), tmpMatrices[2].toPrimitiveStore(),
    // tmpEvalCntxt);
    // JUnitUtils.assertEquals(tmpSolver.getC(), tmpMatrices[3].negate().toPrimitiveStore(),
    // tmpEvalCntxt);
    // JUnitUtils.assertEquals(tmpSolver.getAI(), tmpMatrices[4].toPrimitiveStore(),
    // tmpEvalCntxt);
    // JUnitUtils.assertEquals(tmpSolver.getBI(), tmpMatrices[5].toPrimitiveStore(),
    // tmpEvalCntxt);
    final Optimisation.Result tmpResult = tmpSolver.solve();
    TestUtils.assertEquals(tmpMatrices[6], RationalMatrix.FACTORY.columns(tmpResult), tmpEvalCntxt);
    OptimisationConvexTests.assertDirectAndIterativeEquals(tmpBuilder, null);
}
Also used : MatrixStore(org.ojalgo.matrix.store.MatrixStore) Optimisation(org.ojalgo.optimisation.Optimisation) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) NumberContext(org.ojalgo.type.context.NumberContext) Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) BasicMatrix(org.ojalgo.matrix.BasicMatrix) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 13 with PrimitiveMatrix

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

Example 14 with PrimitiveMatrix

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

the class PortfolioProblems method testP20170508.

@Test
public void testP20170508() {
    Builder<PrimitiveMatrix> tmpBuilder = PrimitiveMatrix.FACTORY.getBuilder(2, 2);
    tmpBuilder.add(0, 0, 0.040000);
    tmpBuilder.add(0, 1, 0.1000);
    tmpBuilder.add(1, 0, 0.1000);
    tmpBuilder.add(1, 1, 0.250000);
    final BasicMatrix covariances = tmpBuilder.build();
    tmpBuilder = PrimitiveMatrix.FACTORY.getBuilder(2);
    tmpBuilder.add(0, 0.20000);
    tmpBuilder.add(1, 0.40000);
    final BasicMatrix returns = tmpBuilder.build();
    final MarketEquilibrium marketEq = new MarketEquilibrium(covariances);
    final MarkowitzModel markowitzModel = new MarkowitzModel(marketEq, returns);
    for (int r = 0; r <= 10; r++) {
        final BigDecimal targetReturn = StandardType.PERCENT.enforce(new BigDecimal(0.2 + (0.02 * r)));
        markowitzModel.setTargetReturn(targetReturn);
        markowitzModel.optimiser().validate(false);
        markowitzModel.optimiser().debug(false);
        final List<BigDecimal> tmpWeights = markowitzModel.getWeights();
        if (DEBUG) {
            BasicLogger.debug("{} => {} {}", targetReturn, markowitzModel.optimiser().getState(), markowitzModel.toSimplePortfolio());
        }
        TestUtils.assertTrue("Optimiser completed normally", markowitzModel.optimiser().getState().isOptimal());
        TestUtils.assertTrue("Weights sum to 100%", tmpWeights.get(0).add(tmpWeights.get(1)).setScale(2, RoundingMode.HALF_EVEN).compareTo(BigMath.ONE) == 0);
        TestUtils.assertEquals("Return is close to target", targetReturn, markowitzModel.getMeanReturn(), StandardType.PERCENT.newPrecision(2).newScale(2));
    }
}
Also used : BasicMatrix(org.ojalgo.matrix.BasicMatrix) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 15 with PrimitiveMatrix

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

the class FinanceUtils method makeCovarianceMatrix.

/**
 * @param timeSeriesCollection
 * @return Annualised covariances
 */
public static <V extends Number> BasicMatrix makeCovarianceMatrix(final Collection<CalendarDateSeries<V>> timeSeriesCollection) {
    final CoordinationSet<V> tmpCoordinator = new CoordinationSet<>(timeSeriesCollection).prune();
    final ArrayList<SampleSet> tmpSampleSets = new ArrayList<>();
    for (final CalendarDateSeries<V> tmpTimeSeries : timeSeriesCollection) {
        final double[] values = tmpCoordinator.get(tmpTimeSeries.getName()).asPrimitive().toRawCopy1D();
        final int tmpSize1 = values.length - 1;
        final double[] retVal = new double[tmpSize1];
        for (int i = 0; i < tmpSize1; i++) {
            retVal[i] = PrimitiveFunction.LOG.invoke(values[i + 1] / values[i]);
        }
        final SampleSet tmpMakeUsingLogarithmicChanges = SampleSet.wrap(Access1D.wrap(retVal));
        tmpSampleSets.add(tmpMakeUsingLogarithmicChanges);
    }
    final int tmpSize = timeSeriesCollection.size();
    final Builder<PrimitiveMatrix> retValStore = PrimitiveMatrix.FACTORY.getBuilder(tmpSize, tmpSize);
    final double tmpToYearFactor = (double) CalendarDateUnit.YEAR.size() / (double) tmpCoordinator.getResolution().size();
    SampleSet tmpRowSet;
    SampleSet tmpColSet;
    for (int j = 0; j < tmpSize; j++) {
        tmpColSet = tmpSampleSets.get(j);
        for (int i = 0; i < tmpSize; i++) {
            tmpRowSet = tmpSampleSets.get(i);
            retValStore.set(i, j, tmpToYearFactor * tmpRowSet.getCovariance(tmpColSet));
        }
    }
    return retValStore.build();
}
Also used : SampleSet(org.ojalgo.random.SampleSet) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) ArrayList(java.util.ArrayList)

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