Search in sources :

Example 11 with Uniform

use of org.ojalgo.random.Uniform in project ojAlgo by optimatika.

the class PolynomialImplTest method testEstimation.

@Test
public void testEstimation() {
    final int tmpMaxSamples = 9;
    for (int tmpSamples = 1; tmpSamples <= tmpMaxSamples; tmpSamples++) {
        final Uniform tmpRndm = new Uniform(-100, 200);
        final double[] x = new double[tmpSamples];
        final double[] y = new double[tmpSamples];
        for (int i = 0; i < tmpSamples; i++) {
            x[i] = tmpRndm.doubleValue();
            y[i] = tmpRndm.doubleValue();
        }
        final int tmpDegree = tmpSamples - 1;
        final PrimitivePolynomial tmpPoly = new PrimitivePolynomial(tmpDegree);
        tmpPoly.estimate(Access1D.wrap(x), Access1D.wrap(y));
        final NumberContext tmpEquals = new NumberContext(7, 14);
        for (int i = 0; i < tmpSamples; i++) {
            TestUtils.assertEquals(y[i], tmpPoly.invoke(x[i]), tmpEquals);
        }
    }
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Uniform(org.ojalgo.random.Uniform) Test(org.junit.jupiter.api.Test)

Example 12 with Uniform

use of org.ojalgo.random.Uniform in project ojAlgo by optimatika.

the class MatrixUtils method makeRandomComplexStore.

public static PhysicalStore<ComplexNumber> makeRandomComplexStore(final int aRowDim, final int aColDim) {
    final PhysicalStore<ComplexNumber> retVal = GenericDenseStore.COMPLEX.makeZero(aRowDim, aColDim);
    final Uniform tmpArgGen = new Uniform(PrimitiveMath.ZERO, PrimitiveMath.TWO_PI);
    for (int j = 0; j < aColDim; j++) {
        for (int i = 0; i < aRowDim; i++) {
            retVal.set(i, j, ComplexNumber.makePolar(PrimitiveMath.E, tmpArgGen.doubleValue()).add(PrimitiveMath.PI));
        }
    }
    return retVal;
}
Also used : Uniform(org.ojalgo.random.Uniform) ComplexNumber(org.ojalgo.scalar.ComplexNumber)

Example 13 with Uniform

use of org.ojalgo.random.Uniform in project ojAlgo-finance by optimatika.

the class CalendarDateSeriesTest method testDoubleKeys.

@Test
public void testDoubleKeys() {
    final int dim = 1000;
    final Uniform tmpUniform = new Uniform(0, Double.MAX_VALUE);
    final double[] keys = new double[dim];
    final long[] indices = new long[dim];
    for (int i = 0; i < dim; i++) {
        keys[i] = tmpUniform.doubleValue();
        indices[i] = Double.doubleToLongBits(keys[i]);
    }
    Arrays.sort(keys);
    Arrays.sort(indices);
    for (int i = 0; i < dim; i++) {
        TestUtils.assertEquals(keys[i], Double.longBitsToDouble(indices[i]));
    }
}
Also used : Uniform(org.ojalgo.random.Uniform) Test(org.junit.jupiter.api.Test)

Example 14 with Uniform

use of org.ojalgo.random.Uniform 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

Uniform (org.ojalgo.random.Uniform)14 Test (org.junit.jupiter.api.Test)8 NumberContext (org.ojalgo.type.context.NumberContext)4 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Tag (org.junit.jupiter.api.Tag)1 RecoverableCondition (org.ojalgo.RecoverableCondition)1 BasicMatrix (org.ojalgo.matrix.BasicMatrix)1 PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)1 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)1 SolverTask (org.ojalgo.matrix.task.SolverTask)1 ComplexNumber (org.ojalgo.scalar.ComplexNumber)1