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);
}
Aggregations