use of org.ojalgo.array.Primitive64Array in project ojAlgo-finance by optimatika.
the class PortfolioSimulator method simulate.
RandomProcess.SimulationResults simulate(final int aNumberOfRealisations, final int aNumberOfSteps, final double aStepSize, final Integer rebalancingInterval) {
final int tmpProcDim = myProcess.size();
final Primitive64Array tmpInitialValues = myProcess.getValues();
final Number[] tmpValues = new Number[tmpProcDim];
for (int p = 0; p < tmpProcDim; p++) {
tmpValues[p] = tmpInitialValues.get(p);
}
final List<BigDecimal> tmpWeights = new SimplePortfolio(tmpValues).normalise().getWeights();
final Array2D<Double> tmpRealisationValues = Array2D.PRIMITIVE64.makeZero(aNumberOfRealisations, aNumberOfSteps);
for (int r = 0; r < aNumberOfRealisations; r++) {
for (int s = 0; s < aNumberOfSteps; s++) {
if ((rebalancingInterval != null) && (s != 0) && ((s % rebalancingInterval) == 0)) {
final double tmpPortfolioValue = tmpRealisationValues.doubleValue(r, s - 1);
for (int p = 0; p < tmpProcDim; p++) {
myProcess.setValue(p, tmpPortfolioValue * tmpWeights.get(p).doubleValue());
}
}
final Array1D<Double> tmpRealisation = myProcess.step(aStepSize);
final AggregatorFunction<Double> tmpAggregator = Aggregator.SUM.getFunction(PrimitiveAggregator.getSet());
tmpRealisation.visitAll(tmpAggregator);
tmpRealisationValues.set(r, s, tmpAggregator.doubleValue());
}
myProcess.setValues(tmpInitialValues);
}
final AggregatorFunction<Double> tmpAggregator = Aggregator.SUM.getFunction(PrimitiveAggregator.getSet());
for (int i = 0; i < tmpInitialValues.count(); i++) {
tmpAggregator.invoke(tmpInitialValues.doubleValue(i));
}
return new RandomProcess.SimulationResults(tmpAggregator.doubleValue(), tmpRealisationValues);
}
use of org.ojalgo.array.Primitive64Array in project ojAlgo by optimatika.
the class PrimitiveDenseStore method applyLU.
public void applyLU(final int iterationPoint, final BasicArray<Double> multipliers) {
final double[] tmpData = data;
final double[] tmpColumn = ((Primitive64Array) multipliers).data;
if ((myColDim - iterationPoint - 1) > ApplyLU.THRESHOLD) {
final DivideAndConquer tmpConquerer = new DivideAndConquer() {
@Override
protected void conquer(final int first, final int limit) {
ApplyLU.invoke(tmpData, myRowDim, first, limit, tmpColumn, iterationPoint);
}
};
tmpConquerer.invoke(iterationPoint + 1, myColDim, ApplyLU.THRESHOLD);
} else {
ApplyLU.invoke(tmpData, myRowDim, iterationPoint + 1, myColDim, tmpColumn, iterationPoint);
}
}
use of org.ojalgo.array.Primitive64Array in project ojAlgo by optimatika.
the class PrimitiveDenseStore method applyLDL.
public void applyLDL(final int iterationPoint, final BasicArray<Double> multipliers) {
final double[] tmpData = data;
final double[] tmpColumn = ((Primitive64Array) multipliers).data;
if ((myColDim - iterationPoint - 1) > ApplyLDL.THRESHOLD) {
final DivideAndConquer tmpConquerer = new DivideAndConquer() {
@Override
protected void conquer(final int first, final int limit) {
ApplyLDL.invoke(tmpData, myRowDim, first, limit, tmpColumn, iterationPoint);
}
};
tmpConquerer.invoke(iterationPoint + 1, myColDim, ApplyLDL.THRESHOLD);
} else {
ApplyLDL.invoke(tmpData, myRowDim, iterationPoint + 1, myColDim, tmpColumn, iterationPoint);
}
}
use of org.ojalgo.array.Primitive64Array in project ojAlgo by optimatika.
the class SampleSet method make.
public static SampleSet make(final RandomNumber randomNumber, final int size) {
final Primitive64Array retVal = Primitive64Array.make(size);
final double[] tmpData = retVal.data;
for (int i = 0; i < size; i++) {
tmpData[i] = randomNumber.doubleValue();
}
return new SampleSet(retVal);
}
use of org.ojalgo.array.Primitive64Array in project ojAlgo by optimatika.
the class ConvexProblems method testP20150809.
/**
* Issue reported at GitHub. A set of problems related to when Q is zero - a linear problem. Generally the
* ConvexSolver is not the right option to handle linear problems, but there is some desireable behaviour.
*/
@Test
public void testP20150809() {
final NumberContext precision = new NumberContext(11, 14, RoundingMode.HALF_EVEN);
final Primitive64Array tmpExpectedSolution = Primitive64Array.wrap(new double[] { 0.12, -0.05, 0.08, 0.07 });
final Primitive64Array tmpBoundedSolution = Primitive64Array.wrap(new double[] { 99999, -99999, 99999, 99999 });
ConvexSolver tmpSolver = P20150809.buildModel(true, false);
Result tmpResult = tmpSolver.solve();
TestUtils.assertStateNotLessThanOptimal(tmpResult);
TestUtils.assertEquals(tmpExpectedSolution, tmpResult, precision);
tmpSolver = P20150809.buildModel(true, true);
tmpResult = tmpSolver.solve();
TestUtils.assertStateNotLessThanOptimal(tmpResult);
TestUtils.assertEquals(tmpExpectedSolution, tmpResult, precision);
tmpSolver = P20150809.buildModel(false, false);
tmpResult = tmpSolver.solve();
TestUtils.assertEquals(Optimisation.State.UNBOUNDED, tmpResult.getState());
tmpSolver = P20150809.buildModel(false, true);
tmpResult = tmpSolver.solve();
// Since it is now constrained, the solver should be able find the optimal solution.
TestUtils.assertStateNotLessThanOptimal(tmpResult);
TestUtils.assertEquals(tmpBoundedSolution, tmpResult, precision);
}
Aggregations