Search in sources :

Example 1 with PrimitiveDenseStore

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

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

the class MultidimensionalSimulatorTest method testStepping.

@Test
public void testStepping() {
    final PrimitiveDenseStore tmpCorrelation = PrimitiveDenseStore.FACTORY.makeEye(3, 3);
    final GeometricBrownianMotion tmpOrgProc1 = new SimpleAsset(0.0, 0.01, PrimitiveMath.THIRD).forecast();
    final GeometricBrownianMotion tmpOrgProc2 = new SimpleAsset(0.0, 0.02, PrimitiveMath.THIRD).forecast();
    final GeometricBrownianMotion tmpOrgProc3 = new SimpleAsset(0.0, 0.03, PrimitiveMath.THIRD).forecast();
    TestUtils.assertEquals(0.01, tmpOrgProc1.getStandardDeviation(), 0.005);
    TestUtils.assertEquals(0.02, tmpOrgProc2.getStandardDeviation(), 0.005);
    TestUtils.assertEquals(0.03, tmpOrgProc3.getStandardDeviation(), 0.005);
    final ArrayList<GeometricBrownianMotion> tmpProcs = new ArrayList<>();
    tmpProcs.add(tmpOrgProc1);
    tmpProcs.add(tmpOrgProc2);
    tmpProcs.add(tmpOrgProc3);
    final GeometricBrownian1D tmpGB1D = new GeometricBrownian1D(tmpCorrelation, tmpProcs);
    final List<CalendarDateSeries<Double>> tmpSeries = new ArrayList<>();
    tmpSeries.add(new CalendarDateSeries<Double>(CalendarDateUnit.MONTH));
    tmpSeries.add(new CalendarDateSeries<Double>(CalendarDateUnit.MONTH));
    tmpSeries.add(new CalendarDateSeries<Double>(CalendarDateUnit.MONTH));
    CalendarDate tmpCalendarDateKey = CalendarDate.make(CalendarDateUnit.MONTH);
    tmpSeries.get(0).put(tmpCalendarDateKey, tmpOrgProc1.getValue());
    tmpSeries.get(1).put(tmpCalendarDateKey, tmpOrgProc2.getValue());
    tmpSeries.get(2).put(tmpCalendarDateKey, tmpOrgProc3.getValue());
    for (int t = 0; t < 1000; t++) {
        tmpGB1D.step(1.0 / 12.0);
        tmpCalendarDateKey = tmpCalendarDateKey.step(CalendarDateUnit.MONTH);
        tmpSeries.get(0).put(tmpCalendarDateKey, tmpGB1D.getValue(0));
        tmpSeries.get(1).put(tmpCalendarDateKey, tmpGB1D.getValue(1));
        tmpSeries.get(2).put(tmpCalendarDateKey, tmpGB1D.getValue(2));
    }
    final GeometricBrownianMotion tmpNewProc1 = GeometricBrownianMotion.estimate(tmpSeries.get(0).asPrimitive(), 1.0 / 12.0);
    final GeometricBrownianMotion tmpNewProc2 = GeometricBrownianMotion.estimate(tmpSeries.get(1).asPrimitive(), 1.0 / 12.0);
    final GeometricBrownianMotion tmpNewProc3 = GeometricBrownianMotion.estimate(tmpSeries.get(2).asPrimitive(), 1.0 / 12.0);
    TestUtils.assertEquals(0.01, tmpNewProc1.getStandardDeviation(), 0.005);
    TestUtils.assertEquals(0.02, tmpNewProc2.getStandardDeviation(), 0.005);
    TestUtils.assertEquals(0.03, tmpNewProc3.getStandardDeviation(), 0.005);
}
Also used : GeometricBrownian1D(org.ojalgo.random.process.GeometricBrownian1D) CalendarDate(org.ojalgo.type.CalendarDate) ArrayList(java.util.ArrayList) SimpleAsset(org.ojalgo.finance.portfolio.SimpleAsset) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) GeometricBrownianMotion(org.ojalgo.random.process.GeometricBrownianMotion) CalendarDateSeries(org.ojalgo.series.CalendarDateSeries) Test(org.junit.jupiter.api.Test)

Example 3 with PrimitiveDenseStore

use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.

the class SymmetricEigenvalueProfiling method main.

public static void main(final String[] args) {
    final PrimitiveDenseStore tmpOrg = MatrixUtils.makeSPD(200);
    // final Eigenvalue<Double> tmpEvD = new RawEigenvalue.Symmetric();
    final Eigenvalue<Double> tmpEvD = Eigenvalue.PRIMITIVE.make(tmpOrg, true);
    for (int l = 0; l < 1000000; l++) {
        tmpEvD.decompose(tmpOrg);
    }
}
Also used : PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 4 with PrimitiveDenseStore

use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.

the class ConjugateGradientSolver method resolve.

public double resolve(final List<Equation> equations, final PhysicalStore<Double> solution) {
    final int tmpCountRows = equations.size();
    double tmpNormErr = POSITIVE_INFINITY;
    double tmpNormRHS = ONE;
    final PrimitiveDenseStore tmpResidual = this.residual(solution);
    final PrimitiveDenseStore tmpDirection = this.direction(solution);
    final PrimitiveDenseStore tmpPreconditioned = this.preconditioned(solution);
    final PrimitiveDenseStore tmpVector = this.vector(solution);
    double tmpStepLength;
    double tmpGradientCorrectionFactor;
    double zr0 = 1;
    double zr1 = 1;
    double pAp0 = 0;
    for (int r = 0; r < tmpCountRows; r++) {
        final Equation tmpRow = equations.get(r);
        double tmpVal = tmpRow.getRHS();
        tmpNormRHS = PrimitiveFunction.HYPOT.invoke(tmpNormRHS, tmpVal);
        tmpVal -= tmpRow.dot(solution);
        tmpResidual.set(tmpRow.index, tmpVal);
        // precondition
        tmpPreconditioned.set(tmpRow.index, tmpVal / tmpRow.getPivot());
    }
    // tmpPreconditioned.supplyNonZerosTo(tmpDirection);
    tmpDirection.fillMatching(tmpPreconditioned);
    int tmpIterations = 0;
    final int tmpLimit = this.getIterationsLimit();
    final NumberContext tmpCntxt = this.getAccuracyContext();
    // zr1 = tmpPreconditioned.transpose().multiply(tmpResidual).doubleValue(0L);
    zr1 = tmpPreconditioned.dot(tmpResidual);
    do {
        zr0 = zr1;
        for (int i = 0; i < tmpCountRows; i++) {
            final Equation tmpRow = equations.get(i);
            final double tmpVal = tmpRow.dot(tmpDirection);
            tmpVector.set(tmpRow.index, tmpVal);
        }
        // pAp0 = tmpVector.multiplyLeft(tmpDirection.transpose()).get().doubleValue(0L);
        pAp0 = tmpDirection.dot(tmpVector);
        tmpStepLength = zr0 / pAp0;
        if (!Double.isNaN(tmpStepLength)) {
            // solution.maxpy(tmpStepLength, tmpDirection);
            tmpDirection.axpy(tmpStepLength, solution);
            // tmpResidual.maxpy(-tmpStepLength, tmpVector);
            tmpVector.axpy(-tmpStepLength, tmpResidual);
        }
        tmpNormErr = ZERO;
        for (int r = 0; r < tmpCountRows; r++) {
            final Equation tmpRow = equations.get(r);
            final double tmpValue = tmpResidual.doubleValue(tmpRow.index);
            tmpNormErr = PrimitiveFunction.HYPOT.invoke(tmpNormErr, tmpValue);
            tmpPreconditioned.set(tmpRow.index, tmpValue / tmpRow.getPivot());
        }
        zr1 = tmpPreconditioned.dot(tmpResidual);
        tmpGradientCorrectionFactor = zr1 / zr0;
        tmpDirection.modifyAll(PrimitiveFunction.MULTIPLY.second(tmpGradientCorrectionFactor));
        tmpDirection.modifyMatching(PrimitiveFunction.ADD, tmpPreconditioned);
        tmpIterations++;
        if (this.isDebugPrinterSet()) {
            this.debug(tmpIterations, solution);
        }
    } while ((tmpIterations < tmpLimit) && !Double.isNaN(tmpNormErr) && !tmpCntxt.isSmall(tmpNormRHS, tmpNormErr));
    return tmpNormErr / tmpNormRHS;
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 5 with PrimitiveDenseStore

use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.

the class MatrixUtils method getComplexModulus.

/**
 * Extracts the modulus of the ComplexNumber elements to a new primitive double valued matrix.
 */
public static PrimitiveDenseStore getComplexModulus(final Access2D<ComplexNumber> arg) {
    final long tmpRows = arg.countRows();
    final long tmpColumns = arg.countColumns();
    final PrimitiveDenseStore retVal = PrimitiveDenseStore.FACTORY.makeZero(tmpRows, tmpColumns);
    MatrixUtils.copyComplexModulus(arg, retVal);
    return retVal;
}
Also used : PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Aggregations

PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)72 Test (org.junit.jupiter.api.Test)37 Optimisation (org.ojalgo.optimisation.Optimisation)16 NumberContext (org.ojalgo.type.context.NumberContext)15 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)12 ComparableToDouble (org.ojalgo.type.keyvalue.ComparableToDouble)7 Result (org.ojalgo.optimisation.Optimisation.Result)6 ComplexNumber (org.ojalgo.scalar.ComplexNumber)4 SimultaneousPrimitive (org.ojalgo.matrix.decomposition.HermitianEvD.SimultaneousPrimitive)3 PhysicalStore (org.ojalgo.matrix.store.PhysicalStore)3 Builder (org.ojalgo.optimisation.convex.ConvexSolver.Builder)3 IntRowColumn (org.ojalgo.access.Structure2D.IntRowColumn)2 ComplexDenseStore (org.ojalgo.matrix.store.ComplexDenseStore)2 LinearSolver (org.ojalgo.optimisation.linear.LinearSolver)2 Normal (org.ojalgo.random.Normal)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Disabled (org.junit.jupiter.api.Disabled)1 Tag (org.junit.jupiter.api.Tag)1 ProgrammingError (org.ojalgo.ProgrammingError)1