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