Search in sources :

Example 91 with NumberContext

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

the class TestEquilibrium method testRandomProblemsComparedToEquilibrium.

@Test
public void testRandomProblemsComparedToEquilibrium() {
    final NumberContext tmpWeightsContext = StandardType.PERCENT.newPrecision(5);
    final int tmpDim = 9;
    final Uniform tmpRndmCorrelation = new Uniform(-0.5, 1.0);
    final Uniform tmpRndmVolatility = new Uniform(0.01, 0.10);
    final Uniform tmpRndmRiskAversionExponent = new Uniform(-1.0, 3.0);
    final Uniform tmpRndmWeight = new Uniform(0.0, 1.0);
    final PhysicalStore<Double> tmpCovariances = PrimitiveDenseStore.FACTORY.makeFilled(tmpDim, tmpDim, tmpRndmCorrelation);
    tmpCovariances.fillDiagonal(0, 0, 0.5);
    tmpCovariances.modifyMatching(PrimitiveFunction.ADD, tmpCovariances.transpose());
    for (int ij = 0; ij < tmpDim; ij++) {
        final UnaryFunction<Double> tmpFunc = PrimitiveFunction.MULTIPLY.first(tmpRndmVolatility.doubleValue());
        tmpCovariances.modifyRow(ij, 0, tmpFunc);
        tmpCovariances.modifyColumn(0, ij, tmpFunc);
    }
    final BigDecimal tmpRAF = new BigDecimal(PrimitiveFunction.POW.invoke(10.0, tmpRndmRiskAversionExponent.doubleValue()));
    final MarketEquilibrium tmpEquilibrium = new MarketEquilibrium(PrimitiveMatrix.FACTORY.copy(tmpCovariances), tmpRAF).clean();
    final double[] tmpRawWeights = PrimitiveMatrix.FACTORY.makeFilled(tmpDim, 1, tmpRndmWeight).toRawCopy1D();
    final List<BigDecimal> tmpNormalisedWeights = new SimplePortfolio(tmpRawWeights).normalise().getWeights();
    @SuppressWarnings("unchecked") final PrimitiveMatrix tmpGeneratedWeights = PrimitiveMatrix.FACTORY.columns(tmpNormalisedWeights);
    final BasicMatrix tmpMatchingReturns = tmpEquilibrium.calculateAssetReturns(tmpGeneratedWeights);
    TestUtils.assertEquals(tmpGeneratedWeights, tmpEquilibrium.calculateAssetWeights(tmpMatchingReturns), tmpWeightsContext);
    final FixedWeightsPortfolio tmpFW = new FixedWeightsPortfolio(tmpEquilibrium, tmpGeneratedWeights);
    TestUtils.assertEquals(tmpMatchingReturns, tmpFW.getAssetReturns(), tmpWeightsContext);
    final FixedReturnsPortfolio tmpFR = new FixedReturnsPortfolio(tmpEquilibrium, tmpMatchingReturns);
    TestUtils.assertEquals(tmpGeneratedWeights, tmpFR.getAssetWeights(), tmpWeightsContext);
    final BlackLittermanModel tmpBLM = new BlackLittermanModel(tmpEquilibrium, tmpGeneratedWeights);
    for (int i = 0; i < tmpDim; i++) {
        final List<BigDecimal> tmpViewAssetWeights = new ArrayList<>();
        for (int j = 0; j < tmpDim; j++) {
            if (i == j) {
                tmpViewAssetWeights.add(BigMath.ONE);
            } else {
                tmpViewAssetWeights.add(BigMath.ZERO);
            }
        }
        final int row = i;
        final BigDecimal tmpViewReturn = TypeUtils.toBigDecimal(tmpMatchingReturns.get(row, 0));
        tmpBLM.addViewWithScaledConfidence(tmpViewAssetWeights, tmpViewReturn, BigMath.ONE);
    }
    TestUtils.assertEquals(tmpGeneratedWeights, tmpBLM.getAssetWeights(), tmpWeightsContext);
    final MarkowitzModel tmpMM = new MarkowitzModel(tmpEquilibrium, tmpMatchingReturns);
    final BasicMatrix tmpActual = tmpMM.getAssetWeights();
    TestUtils.assertEquals(tmpGeneratedWeights, tmpActual, tmpWeightsContext);
}
Also used : PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) ArrayList(java.util.ArrayList) NumberContext(org.ojalgo.type.context.NumberContext) Uniform(org.ojalgo.random.Uniform) BigDecimal(java.math.BigDecimal) BasicMatrix(org.ojalgo.matrix.BasicMatrix) Test(org.junit.jupiter.api.Test)

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