use of org.ojalgo.matrix.PrimitiveMatrix in project ojAlgo by optimatika.
the class ConvexProblems method testP20080117.
/**
* The ActiveSetSolver ended up in a loop activating/deactivating constraints. Eventually it returned
* null, and that eventually resulted in a NullPointerException. Since Q is not positive semidefinite
* validation has to be turned off
*/
@Test
public void testP20080117() {
// create expected returns matrix
final PrimitiveMatrix tmpReturns = PrimitiveMatrix.FACTORY.rows(new double[][] { { -0.007155942261937039 }, { -0.003665887902733331 }, { -0.004130184341000032 }, { -0.005639860515211043 }, { 0.0007211966666666817 }, { 0.0003258225000000077 }, { -0.005754291666666666 }, { -0.004264291666666667 }, { -0.0017500000000000003 } });
// create covariance matrix
final PrimitiveMatrix tmpCovariances = PrimitiveMatrix.FACTORY.rows(new double[][] { { 0.001561410465201063, 0.00006366128201274021, -0.0001323096896759724, 0.0000909074052724909, 0.00003172000033558704, 0.00001955483223848944, -0.00013771504482647386, -0.00004858457275314645, -0.000012954723060403266 }, { 0.00006366128201274021, 0.00016419786524761803, -0.00001566288911558343, -0.00008688646089751923, 0.0000027349925543017186, 0.0000012356159598500247, -0.000024367796639005863, -0.000017576048221096555, -0.0000070052245518771815 }, { -0.0001323096896759724, -0.00001566288911558343, 0.0001430155985985913, 0.00007675339168559199, -0.00007600590426518823, 0.000032976538909267937, 0.00009520305608240259, 0.00007373075639042642, -0.000007477057858706954 }, { 0.0000909074052724909, -0.00008688646089751923, 0.00007675339168559199, 0.000967519991100896, -0.0000533460293834595, 0.00008665760416026126, 0.00014591175388747613, 0.0001232364989586903, 0.00011097998789484925 }, { 0.00003172000033558704, 0.0000027349925543017186, -0.00007600590426518823, -0.0000533460293834595, 0.000025267064307337795, -0.00003089584520279407, -0.00005593123237578969, -0.000017013960349712132, 0.0000013056146551724419 }, { 0.00001955483223848944, 0.0000012356159598500247, 0.000032976538909267937, 0.00008665760416026126, -0.00003089584520279407, 0.0001625499447274783, 0.00008242949058588471, 0.00010276895784859992, 0.0000005898510775862205 }, { -0.00013771504482647386, -0.000024367796639005863, 0.00009520305608240259, 0.00014591175388747613, -0.00005593123237578969, 0.00008242949058588471, 0.000560956958802083, 0.0002838794236862429, 0.00009143821659482758 }, { -0.00004858457275314645, -0.000017576048221096555, 0.00007373075639042642, 0.0001232364989586903, -0.000017013960349712132, 0.00010276895784859992, 0.0002838794236862429, 0.00021068964250359204, 0.00004461044181034483 }, { -0.000012954723060403266, -0.0000070052245518771815, -0.000007477057858706954, 0.00011097998789484925, 0.0000013056146551724419, 0.0000005898510775862205, 0.00009143821659482758, 0.00004461044181034483, 0.00006761920797413792 } });
// final MarketEquilibrium tmpME = new MarketEquilibrium(tmpCovariances, BigMath.PI.multiply(BigMath.E));
// create asset variables - cost and weighting constraints
final Variable[] tmpVariables = new Variable[(int) tmpReturns.countRows()];
for (int i = 0; i < tmpVariables.length; i++) {
tmpVariables[i] = new Variable("VAR" + i);
final int row = i;
tmpVariables[i].weight(TypeUtils.toBigDecimal(tmpReturns.get(row, 0)).negate());
// set the constraints on the asset weights
// require at least a 2% allocation to each asset
tmpVariables[i].lower(new BigDecimal("0.02"));
// require no more than 80% allocation to each asset
tmpVariables[i].upper(new BigDecimal("0.80"));
}
final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.02 }, { 0.02 }, { 0.02 }, { 0.02 }, { 0.80 }, { 0.06 }, { 0.02 }, { 0.02 }, { 0.02 } });
ConvexProblems.doEarly2008(tmpVariables, tmpCovariances, tmpExpected);
}
use of org.ojalgo.matrix.PrimitiveMatrix in project ojAlgo by optimatika.
the class ConvexProblems method testP20080204.
/**
* Another case of looping in the ActiveSetSolver's constraint (de)activation.
*/
@Test
public void testP20080204() {
// create expected returns matrix
final PrimitiveMatrix tmpExpectedReturns = PrimitiveMatrix.FACTORY.rows(new double[][] { { 9.994620 }, { 10.011389 }, { 10.004353 }, { 9.998293 }, { 10.056851 }, { 9.997920 }, { 9.999011 }, { 10.050971 }, { 9.989124 }, { 9.989912 } });
// create covariance matrix
final PrimitiveMatrix tmpCovariances = PrimitiveMatrix.FACTORY.rows(new double[][] { { 0.014531344652473037, 4.444675045533674E-4, 0.007234717654072837, -9.455312097865225E-4, 0.0016345464996349748, 1.5256808879495097E-4, 0.00226325818749439, 0.003534367267672946, -4.2669306842991344E-5, 6.902267133060073E-5 }, { 4.444675045533674E-4, 0.008511422662647488, 0.0039821105759899845, 5.543408872612397E-4, -0.0015797828516888929, 1.3505400134130176E-4, -1.5215492836142527E-4, 9.381119889780555E-4, -4.5861204247023084E-4, -2.4226694503921645E-5 }, { 0.007234717654072837, 0.0039821105759899845, 0.031037646466036784, -0.0022701157440735394, -3.187028053841407E-4, 5.182461519304137E-4, -3.681340242039795E-4, 0.001526984686166616, 1.603885118040309E-4, -1.359858314115312E-4 }, { -9.455312097865225E-4, 5.543408872612397E-4, -0.0022701157440735394, 0.005637141895898889, 7.89377521930992E-4, 5.004781934410127E-4, -9.79221967172284E-4, -2.912861228906251E-4, 7.842012412867984E-4, 0.0010866808807429532 }, { 0.0016345464996349748, -0.0015797828516888929, -3.187028053841407E-4, 7.89377521930992E-4, 0.03263062480163135, 6.041130577612135E-5, 6.883489096710362E-4, 0.010830183513887228, 0.0016425608963272292, 0.002481787652249504 }, { 1.5256808879495097E-4, 1.3505400134130176E-4, 5.182461519304137E-4, 5.004781934410127E-4, 6.041130577612135E-5, 0.001733612375709255, 2.8742157640452992E-5, -3.654534740999083E-4, 9.896178753749563E-5, -1.703972415991329E-5 }, { 0.00226325818749439, -1.5215492836142527E-4, -3.681340242039795E-4, -9.79221967172284E-4, 6.883489096710362E-4, 2.8742157640452992E-5, 0.008167191690212253, -0.0010075092076978207, -4.293010139199468E-4, -6.615640978331292E-4 }, { 0.003534367267672946, 9.381119889780555E-4, 0.001526984686166616, -2.912861228906251E-4, 0.010830183513887228, -3.654534740999083E-4, -0.0010075092076978207, 0.013796198054188104, 0.0013541164478127973, -2.2401086720669167E-5 }, { -4.2669306842991344E-5, -4.5861204247023084E-4, 1.603885118040309E-4, 7.842012412867984E-4, 0.0016425608963272292, 9.896178753749563E-5, -4.293010139199468E-4, 0.0013541164478127973, 0.004743485149287524, 0.0011464293217708277 }, { 6.902267133060073E-5, -2.4226694503921645E-5, -1.359858314115312E-4, 0.0010866808807429532, 0.002481787652249504, -1.703972415991329E-5, -6.615640978331292E-4, -2.2401086720669167E-5, 0.0011464293217708277, 0.007398229661528494 } });
// create asset variables - cost and weighting constraints
final Variable[] tmpVariables = new Variable[(int) tmpExpectedReturns.countRows()];
for (int i = 0; i < tmpVariables.length; i++) {
tmpVariables[i] = new Variable("VAR" + i);
final int row = i;
tmpVariables[i].weight(TypeUtils.toBigDecimal(tmpExpectedReturns.get(row, 0)).negate());
// set the constraints on the asset weights
// require at least a 8% allocation to each asset
tmpVariables[i].lower(new BigDecimal("0.08"));
// require no more than 12% allocation to each asset
tmpVariables[i].upper(new BigDecimal("0.12"));
}
// exception here...
final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.08000000000000602 }, { 0.12000000000002384 }, { 0.08000000000000054 }, { 0.10643232489190736 }, { 0.12000000000002252 }, { 0.11999999999979595 }, { 0.09356767510776097 }, { 0.11999999999998154 }, { 0.07999999999999653 }, { 0.08000000000000498 } });
ConvexProblems.doEarly2008(tmpVariables, tmpCovariances, tmpExpected);
}
use of org.ojalgo.matrix.PrimitiveMatrix in project ojAlgo by optimatika.
the class ConvexProblems method testP20080118.
/**
* Ended up with a singular matrix (the equation system body generated by the LagrangeSolver) that
* resulted in a solution with NaN and Inf elements. This was not recognised and handled.
*/
@Test
public void testP20080118() {
// create expected returns matrix
final PrimitiveMatrix expectedReturnsMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 10.003264 }, { 9.989771 }, { 9.987513 }, { 9.988449 }, { 9.996579 }, { 9.990690 }, { 9.994904 }, { 9.994514 }, { 9.984064 }, { 9.987534 } });
// create covariance matrix
final PrimitiveMatrix covarianceMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 6.483565230120298E-4, -1.3344603795915894E-4, -4.610345510893708E-4, -7.334405624030001E-4, 1.1551383115707195E-5, -0.00104145662863434, -1.0725896685568462E-4, -1.221384153392056E-4, -4.173413644389791E-4, -2.4861043894946935E-4 }, { -1.3344603795915894E-4, 0.0026045957224784455, 0.0012394355327235707, 9.243919166568456E-4, -8.653805945112411E-5, 8.100239312410631E-4, 4.215960274481846E-4, 5.243272007211247E-4, 0.0013062718630332956, 1.4766450293395405E-4 }, { -4.610345510893708E-4, 0.0012394355327235707, 0.002361436913752224, 0.0020101714731002238, -1.4236763916609785E-5, 0.002120395905829043, 5.399158658928662E-4, 5.048790842067473E-4, 0.0014855261720730444, 4.841458106181396E-4 }, { -7.334405624030001E-4, 9.243919166568456E-4, 0.0020101714731002238, 0.0028542819089926895, -4.311102526746861E-6, 0.0028465650900869476, 6.242643883624462E-4, 4.086484048798765E-4, 0.001647437646316569, 7.58419663970477E-4 }, { 1.1551383115707195E-5, -8.653805945112411E-5, -1.4236763916609785E-5, -4.311102526746861E-6, 1.213366124417227E-4, -9.027529241741836E-5, 7.241389994693716E-6, -3.166855950737129E-5, -1.2445276374560802E-5, -5.3976919759028745E-5 }, { -0.00104145662863434, 8.100239312410631E-4, 0.002120395905829043, 0.0028465650900869476, -9.027529241741836E-5, 0.0064756879298965295, 2.8076277564885113E-4, 3.6082073553997553E-4, 0.001945238279500792, 0.0012421132342988626 }, { -1.0725896685568462E-4, 4.215960274481846E-4, 5.399158658928662E-4, 6.242643883624462E-4, 7.241389994693716E-6, 2.8076277564885113E-4, 0.0010121500024739688, 8.206099676659543E-5, 1.6129237403855146E-4, 7.550465994733837E-4 }, { -1.221384153392056E-4, 5.243272007211247E-4, 5.048790842067473E-4, 4.086484048798765E-4, -3.166855950737129E-5, 3.6082073553997553E-4, 8.206099676659543E-5, 4.504461842318998E-4, 4.7980942831718363E-4, -4.763223568683059E-5 }, { -4.173413644389791E-4, 0.0013062718630332956, 0.0014855261720730444, 0.001647437646316569, -1.2445276374560802E-5, 0.001945238279500792, 1.6129237403855146E-4, 4.7980942831718363E-4, 0.002228245076175045, 3.2083564921169634E-4 }, { -2.4861043894946935E-4, 1.4766450293395405E-4, 4.841458106181396E-4, 7.58419663970477E-4, -5.3976919759028745E-5, 0.0012421132342988626, 7.550465994733837E-4, -4.763223568683059E-5, 3.2083564921169634E-4, 0.0017093327832123186 } });
// create asset variables - cost and weighting constraints
final Variable[] tmpVariables = new Variable[(int) expectedReturnsMatrix.countRows()];
for (int i = 0; i < tmpVariables.length; i++) {
tmpVariables[i] = new Variable("VAR" + i);
final int row = i;
tmpVariables[i].weight(TypeUtils.toBigDecimal(expectedReturnsMatrix.get(row, 0)).negate());
// set the constraints on the asset weights
// require at least a 2% allocation to each asset
tmpVariables[i].lower(new BigDecimal("0.05"));
// require no more than 80% allocation to each asset
tmpVariables[i].upper(new BigDecimal("0.35"));
}
final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.35 }, { 0.05 }, { 0.05 }, { 0.05 }, { 0.25 }, { 0.05 }, { 0.05 }, { 0.05 }, { 0.05 }, { 0.05 } });
ConvexProblems.doEarly2008(tmpVariables, covarianceMatrix, tmpExpected);
}
use of org.ojalgo.matrix.PrimitiveMatrix in project ojAlgo by optimatika.
the class LinearProblems method testMath286.
@Test
public void testMath286() {
final Variable tmpX1 = new Variable("X1").weight(TENTH.multiply(EIGHT)).lower(TEN);
final Variable tmpX2 = new Variable("X2").weight(TENTH.multiply(TWO)).lower(ZERO);
final Variable tmpX3 = new Variable("X3").weight(TENTH.multiply(SEVEN)).lower(EIGHT);
final Variable tmpX4 = new Variable("X4").weight(TENTH.multiply(THREE)).lower(ZERO);
final Variable tmpX5 = new Variable("X5").weight(TENTH.multiply(SIX)).lower(FIVE);
final Variable tmpX6 = new Variable("X6").weight(TENTH.multiply(FOUR)).lower(ZERO);
final Variable[] tmpFullVars = new Variable[] { tmpX1.copy(), tmpX2.copy(), tmpX3.copy(), tmpX4.copy(), tmpX5.copy(), tmpX6.copy() };
final Variable[] tmpOddVars = new Variable[] { tmpX1.copy(), tmpX3.copy(), tmpX5.copy() };
final Variable[] tmpEvenVars = new Variable[] { tmpX2.copy(), tmpX4.copy(), tmpX6.copy() };
final ExpressionsBasedModel tmpFullModel = new ExpressionsBasedModel(tmpFullVars);
// tmpFullModel.setMaximisation();
final ExpressionsBasedModel tmpOddModel = new ExpressionsBasedModel(tmpOddVars);
// tmpOddModel.setMaximisation();
final ExpressionsBasedModel tmpEvenModel = new ExpressionsBasedModel(tmpEvenVars);
// tmpEvenModel.setMaximisation();
// tmpFullModel.options.debug(LinearSolver.class);
// tmpOddModel.options.debug(LinearSolver.class);
// tmpEvenModel.options.debug(LinearSolver.class);
final BigDecimal tmpRHS = new BigDecimal("23.0");
final int tmpLength = tmpFullModel.countVariables();
final Expression retVal = tmpFullModel.addExpression("C1");
for (int i = 0; i < tmpLength; i++) {
retVal.set(i, new BigDecimal[] { ONE, ZERO, ONE, ZERO, ONE, ZERO }[i]);
}
final Expression tmpAddWeightExpression = retVal;
tmpAddWeightExpression.level(tmpRHS);
final int tmpLength1 = tmpOddModel.countVariables();
final Expression retVal1 = tmpOddModel.addExpression("C1");
for (int i = 0; i < tmpLength1; i++) {
retVal1.set(i, new BigDecimal[] { ONE, ONE, ONE }[i]);
}
final Expression tmpAddWeightExpression2 = retVal1;
tmpAddWeightExpression2.level(tmpRHS);
final int tmpLength2 = tmpFullModel.countVariables();
final Expression retVal2 = tmpFullModel.addExpression("C2");
for (int i = 0; i < tmpLength2; i++) {
retVal2.set(i, new BigDecimal[] { ZERO, ONE, ZERO, ONE, ZERO, ONE }[i]);
}
final Expression tmpAddWeightExpression3 = retVal2;
tmpAddWeightExpression3.level(tmpRHS);
final int tmpLength3 = tmpEvenModel.countVariables();
final Expression retVal3 = tmpEvenModel.addExpression("C2");
for (int i = 0; i < tmpLength3; i++) {
retVal3.set(i, new BigDecimal[] { ONE, ONE, ONE }[i]);
}
final Expression tmpAddWeightExpression4 = retVal3;
tmpAddWeightExpression4.level(tmpRHS);
final Expression tmpFullObjective = tmpFullModel.objective();
final Expression tmpOddObjective = tmpOddModel.objective();
final Expression tmpEvenObjective = tmpEvenModel.objective();
// A valid solution of 25.8 can be produced with:
// X1=10, X2=0, X3=8, X4=0, X5=5, X6=23
final BigDecimal tmpClaimedValue = new BigDecimal("25.8");
final Builder<PrimitiveMatrix> tmpBuilder = PrimitiveMatrix.FACTORY.getBuilder(6, 1);
tmpBuilder.set(0, 0, 10);
tmpBuilder.set(2, 0, 8);
tmpBuilder.set(4, 0, 5);
tmpBuilder.set(5, 0, 23);
final BasicMatrix tmpFullSolution = tmpBuilder.build();
final BasicMatrix tmpOddSolution = tmpFullSolution.selectRows(0, 2, 4);
final BasicMatrix tmpEvenSolution = tmpFullSolution.selectRows(1, 3, 5);
TestUtils.assertEquals("Claimed solution not valid!", true, tmpFullModel.validate(BigDenseStore.FACTORY.copy(tmpFullSolution), new NumberContext(7, 6)));
final Double tmpActualValue = tmpFullObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(tmpFullSolution));
// final BigDecimal tmpActualValue = TypeUtils.toBigDecimal(tmpObjectiveValue);
// JUnitUtils.assertEquals("Claimed objective value wrong!", 0, tmpClaimedValue.compareTo(tmpActualValue));
TestUtils.assertEquals(tmpClaimedValue, tmpActualValue, new NumberContext(7, 6));
// Start validating ojAlgo results
final Optimisation.Result tmpEvenResult = tmpEvenModel.maximise();
final Optimisation.Result tmpOddResult = tmpOddModel.maximise();
final Optimisation.Result tmpFullResult = tmpFullModel.maximise();
TestUtils.assertEquals(true, tmpEvenModel.validate(tmpEvenResult, new NumberContext(7, 6)));
TestUtils.assertEquals(true, tmpOddModel.validate(tmpOddResult, new NumberContext(7, 6)));
TestUtils.assertEquals(true, tmpFullModel.validate(tmpFullResult, new NumberContext(7, 6)));
TestUtils.assertEquals(tmpEvenSolution, RationalMatrix.FACTORY.columns(tmpEvenResult).selectRows(0, 1, 2), new NumberContext(7, 6));
TestUtils.assertEquals(tmpOddSolution, RationalMatrix.FACTORY.columns(tmpOddResult).selectRows(0, 1, 2), new NumberContext(7, 6));
TestUtils.assertEquals(tmpFullSolution, RationalMatrix.FACTORY.columns(tmpFullResult).selectRows(0, 1, 2, 3, 4, 5), new NumberContext(7, 6));
final BigDecimal tmpEvenValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpEvenObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpEvenResult).selectRows(0, 1, 2)))));
final BigDecimal tmpOddValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpOddObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpOddResult).selectRows(0, 1, 2)))));
final BigDecimal tmpFullValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpFullObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpFullResult).selectRows(0, 1, 2, 3, 4, 5)))));
TestUtils.assertEquals(0, tmpFullValue.compareTo(tmpEvenValue.add(tmpOddValue)));
TestUtils.assertEquals(0, tmpClaimedValue.compareTo(tmpFullValue));
}
use of org.ojalgo.matrix.PrimitiveMatrix in project ojAlgo by optimatika.
the class DecompositionProblems method testP20160510InvertLargeMatrix.
/**
* A user discovered that some large (relatively uniform) matrices causes the algorithm to never finsh
* https://github.com/optimatika/ojAlgo/issues/22
*/
@Test
@Tag("slow")
public void testP20160510InvertLargeMatrix() {
final double[][] data = new double[3000][3000];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data.length; j++) {
data[i][j] = 0.9;
}
}
data[0][1] = 1.01;
final PrimitiveMatrix input = PrimitiveMatrix.FACTORY.rows(data);
try {
// final SingularValue<Double> svd = SingularValue.make(input);
final SingularValue<Double> svd = new SingularValueDecomposition.Primitive();
svd.invert(input);
} catch (final RecoverableCondition exception) {
// TODO Auto-generated catch block
exception.printStackTrace();
}
// The issue:can't be reached here!!!
}
Aggregations