use of org.ojalgo.type.context.NumberContext 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.type.context.NumberContext in project ojAlgo by optimatika.
the class GaussianProcessTest method testTutorial.
@Test
public void testTutorial() {
final GaussianField.Covariance<Double> tmpCovar = new GaussianField.Covariance<Double>() {
public void calibrate(final Collection<ComparableToDouble<Double>> observations, final Mean<Double> mean) {
}
public double invoke(final Double anArg1, final Double anArg2) {
return this.invoke(anArg1.doubleValue(), anArg2.doubleValue());
}
double invoke(final double anArg1, final double anArg2) {
final double tmpSF = 1.27;
final double tmpSN = 0.3;
final double tmpL = 1.0;
double retVal = tmpSF * tmpSF * PrimitiveFunction.EXP.invoke(-PrimitiveFunction.POW.invoke(anArg1 - anArg2, TWO) / (TWO * tmpL * tmpL));
if (anArg1 == anArg2) {
retVal += tmpSN * tmpSN;
}
return retVal;
}
};
final GaussianProcess tmpProc = new GaussianProcess(tmpCovar);
tmpProc.addObservation(-1.5, -1.6);
tmpProc.addObservation(-1.0, -1.1);
tmpProc.addObservation(-0.75, -0.4);
tmpProc.addObservation(-0.4, 0.1);
tmpProc.addObservation(-0.25, 0.5);
tmpProc.addObservation(0.0, 0.8);
final PrimitiveDenseStore tmpExpected = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.7029, 1.423379254178694, 1.2174807940480699, 0.8807634427271873, 0.7384394292014367, 0.5236319646022823 }, { 1.423379254178694, 1.7029, 1.5632762838868954, 1.3472073239852407, 1.2174807940480699, 0.9782733010505065 }, { 1.2174807940480699, 1.5632762838868954, 1.7029, 1.5170744874003474, 1.423379254178694, 1.2174807940480699 }, { 0.8807634427271873, 1.3472073239852407, 1.5170744874003474, 1.7029, 1.5948565596534579, 1.4888943550870049 }, { 0.7384394292014367, 1.2174807940480699, 1.423379254178694, 1.5948565596534579, 1.7029, 1.5632762838868954 }, { 0.5236319646022823, 0.9782733010505065, 1.2174807940480699, 1.4888943550870049, 1.5632762838868954, 1.7029 } });
TestUtils.assertEquals(tmpExpected, tmpProc.getCovariances(), new NumberContext(8, 2));
final Normal tmpDistr = tmpProc.getDistribution(0.2);
TestUtils.assertEquals("Mean", 0.911277527445648, tmpDistr.getExpected(), 0.005);
TestUtils.assertEquals("Variance", 0.20604504349662636, tmpDistr.getVariance(), 0.005);
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class GeometricBrownianMotionTest method testDistributionConsistency.
@Test
public void testDistributionConsistency() {
final double tmpError = new NumberContext(7, 9).epsilon();
GeometricBrownianMotion tmpProcess;
LogNormal tmpDistribution;
for (int tmpCreateHorizon = 1; tmpCreateHorizon < 10; tmpCreateHorizon++) {
for (double tmpExpected = 1.0; tmpExpected <= 2.0; tmpExpected += 0.1) {
for (double tmpVariance = 0.0; tmpVariance <= 1.0; tmpVariance += 0.1) {
tmpProcess = GeometricBrownianMotion.make(tmpExpected, tmpVariance, tmpCreateHorizon);
TestUtils.assertEquals(tmpExpected, tmpProcess.getExpected(tmpCreateHorizon), tmpError);
TestUtils.assertEquals(tmpVariance, tmpProcess.getVariance(tmpCreateHorizon), tmpError);
tmpDistribution = tmpProcess.getDistribution(tmpCreateHorizon);
TestUtils.assertEquals(tmpExpected, tmpDistribution.getExpected(), tmpError);
TestUtils.assertEquals(tmpVariance, tmpDistribution.getVariance(), tmpError);
for (int tmpTestHorison = 0; tmpTestHorison < 10; tmpTestHorison++) {
tmpDistribution = tmpProcess.getDistribution(tmpTestHorison);
TestUtils.assertEquals(tmpDistribution.getExpected(), tmpProcess.getExpected(tmpTestHorison), tmpError);
TestUtils.assertEquals(tmpDistribution.getVariance(), tmpProcess.getVariance(tmpTestHorison), tmpError);
TestUtils.assertEquals(tmpDistribution.getStandardDeviation(), tmpProcess.getStandardDeviation(tmpTestHorison), tmpError);
TestUtils.assertEquals(tmpDistribution.getUpperConfidenceQuantile(0.95), tmpProcess.getUpperConfidenceQuantile(tmpTestHorison, 0.95), tmpError);
TestUtils.assertEquals(tmpDistribution.getLowerConfidenceQuantile(0.95), tmpProcess.getLowerConfidenceQuantile(tmpTestHorison, 0.95), tmpError);
TestUtils.assertEquals(tmpDistribution.getUpperConfidenceQuantile(0.05), tmpProcess.getUpperConfidenceQuantile(tmpTestHorison, 0.05), tmpError);
TestUtils.assertEquals(tmpDistribution.getLowerConfidenceQuantile(0.05), tmpProcess.getLowerConfidenceQuantile(tmpTestHorison, 0.05), tmpError);
}
}
}
}
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo-finance by optimatika.
the class PortfolioProblems method testP20110614.
/**
* A user claimed he got constraint breaking weights using these figures.
*/
@Test
public void testP20110614() {
final Builder<PrimitiveMatrix> tmpCovarsBuilder = PrimitiveMatrix.FACTORY.getBuilder(3, 3);
tmpCovarsBuilder.set(0, 0, 0.04);
tmpCovarsBuilder.set(0, 1, 0.01);
tmpCovarsBuilder.set(0, 2, 0.02);
tmpCovarsBuilder.set(1, 0, 0.01);
tmpCovarsBuilder.set(1, 1, 0.09);
tmpCovarsBuilder.set(1, 2, 0.01);
tmpCovarsBuilder.set(2, 0, 0.02);
tmpCovarsBuilder.set(2, 1, 0.01);
tmpCovarsBuilder.set(2, 2, 0.16);
final BasicMatrix tmpCovars = tmpCovarsBuilder.build();
final Builder<PrimitiveMatrix> tmpReturnsBuilder = PrimitiveMatrix.FACTORY.getBuilder(3, 1);
tmpReturnsBuilder.set(0, 0, 0.10);
tmpReturnsBuilder.set(1, 0, 0.15);
tmpReturnsBuilder.set(2, 0, 0.18);
final BasicMatrix tmpReturs = tmpReturnsBuilder.build();
final MarketEquilibrium tmpME = new MarketEquilibrium(tmpCovars);
final MarkowitzModel tmpMarkowitz = new MarkowitzModel(tmpME, tmpReturs);
for (int i = 1; i < 10; i++) {
tmpMarkowitz.setRiskAversion(new BigDecimal(i));
final List<BigDecimal> tmpWeights = tmpMarkowitz.getWeights();
for (final BigDecimal tmpBigDecimal : tmpWeights) {
if ((tmpBigDecimal.compareTo(BigMath.ZERO) == -1) || (tmpBigDecimal.compareTo(BigMath.ONE) == 1)) {
TestUtils.fail("!(0.0 <= " + tmpBigDecimal + " <= 1.0)");
}
}
}
// As the Markowitz model built it the problem
final MatrixStore<Double> tmpQ = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 4.0, 1.0, 2.0 }, { 1.0, 9.0, 1.0 }, { 2.0, 1.0, 16.0 } });
final MatrixStore<Double> tmpC = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 10.0 }, { 15.0 }, { 18.0 } });
final MatrixStore<Double> tmpAE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 1.0, 1.0 } });
final MatrixStore<Double> tmpBE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0 } });
MatrixStore<Double> tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -1.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0 }, { 0.0, 0.0, -1.0 } });
MatrixStore<Double> tmpBI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0 }, { 0.0 }, { 0.0 } });
final MatrixStore<Double> tmpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0 }, { 0.5217391304347826 }, { 0.4782608695652173 } });
ConvexSolver.Builder tmpBuilder = new ConvexSolver.Builder(tmpQ, tmpC).equalities(tmpAE, tmpBE).inequalities(tmpAI, tmpBI);
ConvexSolver tmpSolver = tmpBuilder.build();
// tmpSolver.options.debug(ConvexSolver.class);
Optimisation.Result tmpResult = tmpSolver.solve();
// BasicMatrix tmpSolution = tmpResult.getSolution();
TestUtils.assertEquals(tmpX, tmpResult, new NumberContext(7, 6));
// As (I believe) the user built it
//
// The only *problem* I found was that he did not set lower limits
// on the portfolio weights, which you have to do. No problem with
// ojAlgo.
tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } });
tmpBI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0 }, { 1.0 }, { 1.0 } });
tmpBuilder = new ConvexSolver.Builder(tmpQ, tmpC).equalities(tmpAE, tmpBE).inequalities(tmpAI, tmpBI);
tmpSolver = tmpBuilder.build();
tmpResult = tmpSolver.solve();
// Should NOT be equal in this case!
TestUtils.assertFalse(Access1D.equals(tmpX, tmpResult, new NumberContext(7, 6)));
// No problem with both the lower and upper limits set.
tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -1.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0 }, { 0.0, 0.0, -1.0 }, { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } });
tmpBI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0 }, { 0.0 }, { 0.0 }, { 1.0 }, { 1.0 }, { 1.0 } });
tmpBuilder = new ConvexSolver.Builder(tmpQ, tmpC).equalities(tmpAE, tmpBE).inequalities(tmpAI, tmpBI);
tmpSolver = tmpBuilder.build();
tmpResult = tmpSolver.solve();
TestUtils.assertEquals(tmpX, tmpResult, new NumberContext(7, 6));
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo-finance by optimatika.
the class PortfolioProblems method testP20141202.
/**
* <p>
* First of all, let me say that I really like ojAlgo so thank you for making it! I do, however, think
* that you should make tmpIterCount and _0_000005 variables fields (with getters and setters) in the
* MarkowitzModel.java class. We are finding that we get suboptimal solutions with the hard-coded limit of
* 20 iterations in a mean variance optimisation (solving for the highest return given a target variance).
* We are now testing it (against our own Python model) with a limit of 100. Please let me know what you
* think when you get a chance.
* </p>
* <p>
* Borrowed test data from {@link #testP20090115()}.
* </p>
* <p>
* 2015-04-14: Changed test evaluation context from <7,14> to <4,4>.
* </p>
*/
@Test
public void testP20141202() {
final double[][] assets_return = { { -1.5905837442343828E-4, -0.03062360801781757, -0.029857534032853142, -0.011811692726036832, -0.017972310602803136, 0.017338003502626997, 0.0 }, { -0.02757158006362653, -0.02562704471101405, -0.011751538891997735, -0.024915062287655786, -0.01684088269454123, 0.013585351447135364, 0.0 }, { -0.00699300699300693, -0.033802816901408676, -0.04675196850393671, -0.021166752710376546, -0.007911392405063583, 0.03827751196172254, 0.0 }, { -0.007626310772164015, 0.0038424591738713027, 0.02488038277511978, 0.025210084033613675, -0.02003642987249557, -0.09758364312267642, 0.0 }, { -0.03965053763440893, 0.021693491952414375, 0.01643835616438392, -0.007412398921833087, 0.01765105227427014, -0.010006671114076025, 0.0 }, { -0.017821782178217872, 0.005040322580645311, 0.006018054162487363, 9.008107296569024E-4, 0.002999999999999824, -0.01196410767696908, 0.0 }, { 2.630552127527583E-4, 2.5867028174649627E-4, 2.3866431891514327E-4, 1.9564035993080523E-4, 2.351016690966669E-4, 1.9070675120065465E-4, 0.0 } };
final P20090115 tm = new P20090115();
final BasicMatrix tmpCovariances = tm.getCovariances(assets_return);
final BasicMatrix tmpExpectedExcessReturns = tm.getExpectedExcessReturns(assets_return).negate();
final MarketEquilibrium tmpME = new MarketEquilibrium(tmpCovariances).clean();
final MarkowitzModel tmpMarkowitz = new MarkowitzModel(tmpME, tmpExpectedExcessReturns);
final BigDecimal[] tmpRiskAversions = new BigDecimal[] { BigMath.HUNDREDTH, BigMath.TWELFTH, BigMath.EIGHTH, BigMath.HALF, BigMath.ONE, BigMath.TWO, BigMath.EIGHT, BigMath.TWELVE, BigMath.HUNDRED, BigMath.THOUSAND };
final double[] tmpPortfolioReturn = new double[tmpRiskAversions.length];
final double[] tmpPortfolioVariance = new double[tmpRiskAversions.length];
final BigDecimal tmpInitialRiskAversion = tmpMarkowitz.getRiskAversion().toBigDecimal();
for (int ra = 0; ra < tmpRiskAversions.length; ra++) {
tmpMarkowitz.setRiskAversion(tmpRiskAversions[ra]);
tmpMarkowitz.getWeights();
tmpPortfolioReturn[ra] = tmpMarkowitz.getMeanReturn();
tmpPortfolioVariance[ra] = tmpMarkowitz.getReturnVariance();
if (DEBUG) {
BasicLogger.debug("RA: {}\tret: {}\tvar: {}\tweights: {}", tmpRiskAversions[ra], tmpMarkowitz.getMeanReturn(), tmpMarkowitz.getReturnVariance(), tmpMarkowitz.getWeights());
}
}
tmpMarkowitz.setRiskAversion(tmpInitialRiskAversion);
// test evaluation context
final NumberContext tmpPrecision = StandardType.PERCENT.newPrecision(4);
for (int r = 0; r < tmpPortfolioReturn.length; r++) {
tmpMarkowitz.setRiskAversion(tmpInitialRiskAversion);
tmpMarkowitz.setTargetReturn(BigDecimal.valueOf(tmpPortfolioReturn[r]));
tmpMarkowitz.getWeights();
// BasicLogger.debug("Exp={}, Act={}, Quoat={}", tmpPortfolioReturn[r], tmpMarkowitz.getMeanReturn(), tmpMarkowitz.getMeanReturn() / tmpPortfolioReturn[r]);
TestUtils.assertEquals("Return: " + tmpRiskAversions[r], tmpPortfolioReturn[r], tmpMarkowitz.getMeanReturn(), tmpPrecision);
}
for (int v = 0; v < tmpPortfolioVariance.length; v++) {
tmpMarkowitz.setRiskAversion(tmpInitialRiskAversion);
tmpMarkowitz.setTargetVariance(BigDecimal.valueOf(tmpPortfolioVariance[v]));
tmpMarkowitz.getWeights();
TestUtils.assertEquals("Variance: " + tmpRiskAversions[v], tmpPortfolioVariance[v], tmpMarkowitz.getReturnVariance(), tmpPrecision);
}
}
Aggregations