use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo-finance by optimatika.
the class FinanceUtilsTest method doTestCleaning.
private static void doTestCleaning(final double[][] original) {
final NumberContext tmpEvalCntx = NumberContext.getGeneral(6, 12);
final PrimitiveDenseStore tmpOriginal = PrimitiveDenseStore.FACTORY.rows(original);
final SingularValue<Double> tmpSVD = SingularValue.make(tmpOriginal);
tmpSVD.decompose(tmpOriginal);
final double tmpRefCond = tmpSVD.getCondition();
final int tmpRefRank = tmpSVD.getRank();
final double tmpRefNorm = tmpSVD.getFrobeniusNorm();
final PrimitiveMatrix tmpCorrelations = FinanceUtils.toCorrelations(tmpOriginal, true);
final PrimitiveMatrix tmpVolatilities = FinanceUtils.toVolatilities(tmpOriginal, true);
final PrimitiveMatrix tmpCovariances = FinanceUtils.toCovariances(tmpVolatilities, tmpCorrelations);
tmpSVD.decompose(PrimitiveDenseStore.FACTORY.copy(tmpCovariances));
final double tmpNewCond = tmpSVD.getCondition();
final int tmpNewRank = tmpSVD.getRank();
final double tmpNewNorm = tmpSVD.getFrobeniusNorm();
TestUtils.assertTrue("Improved the condition", tmpNewCond <= tmpRefCond);
TestUtils.assertTrue("Improved the rank", tmpNewRank >= tmpRefRank);
TestUtils.assertEquals("Full rank", original.length, tmpNewRank);
TestUtils.assertEquals("Roughly the same frob norm", tmpRefNorm, tmpNewNorm, tmpEvalCntx);
if (DEBUG) {
BasicLogger.debug("Original", tmpOriginal);
BasicLogger.debug("Cleaned", tmpCovariances);
BasicLogger.debug("Difference", tmpOriginal.subtract(PrimitiveDenseStore.FACTORY.copy(tmpCovariances)), tmpEvalCntx);
}
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo-finance by optimatika.
the class MultidimensionalSimulatorTest method testStepping.
@Test
public void testStepping() {
final PrimitiveDenseStore tmpCorrelation = PrimitiveDenseStore.FACTORY.makeEye(3, 3);
final GeometricBrownianMotion tmpOrgProc1 = new SimpleAsset(0.0, 0.01, PrimitiveMath.THIRD).forecast();
final GeometricBrownianMotion tmpOrgProc2 = new SimpleAsset(0.0, 0.02, PrimitiveMath.THIRD).forecast();
final GeometricBrownianMotion tmpOrgProc3 = new SimpleAsset(0.0, 0.03, PrimitiveMath.THIRD).forecast();
TestUtils.assertEquals(0.01, tmpOrgProc1.getStandardDeviation(), 0.005);
TestUtils.assertEquals(0.02, tmpOrgProc2.getStandardDeviation(), 0.005);
TestUtils.assertEquals(0.03, tmpOrgProc3.getStandardDeviation(), 0.005);
final ArrayList<GeometricBrownianMotion> tmpProcs = new ArrayList<>();
tmpProcs.add(tmpOrgProc1);
tmpProcs.add(tmpOrgProc2);
tmpProcs.add(tmpOrgProc3);
final GeometricBrownian1D tmpGB1D = new GeometricBrownian1D(tmpCorrelation, tmpProcs);
final List<CalendarDateSeries<Double>> tmpSeries = new ArrayList<>();
tmpSeries.add(new CalendarDateSeries<Double>(CalendarDateUnit.MONTH));
tmpSeries.add(new CalendarDateSeries<Double>(CalendarDateUnit.MONTH));
tmpSeries.add(new CalendarDateSeries<Double>(CalendarDateUnit.MONTH));
CalendarDate tmpCalendarDateKey = CalendarDate.make(CalendarDateUnit.MONTH);
tmpSeries.get(0).put(tmpCalendarDateKey, tmpOrgProc1.getValue());
tmpSeries.get(1).put(tmpCalendarDateKey, tmpOrgProc2.getValue());
tmpSeries.get(2).put(tmpCalendarDateKey, tmpOrgProc3.getValue());
for (int t = 0; t < 1000; t++) {
tmpGB1D.step(1.0 / 12.0);
tmpCalendarDateKey = tmpCalendarDateKey.step(CalendarDateUnit.MONTH);
tmpSeries.get(0).put(tmpCalendarDateKey, tmpGB1D.getValue(0));
tmpSeries.get(1).put(tmpCalendarDateKey, tmpGB1D.getValue(1));
tmpSeries.get(2).put(tmpCalendarDateKey, tmpGB1D.getValue(2));
}
final GeometricBrownianMotion tmpNewProc1 = GeometricBrownianMotion.estimate(tmpSeries.get(0).asPrimitive(), 1.0 / 12.0);
final GeometricBrownianMotion tmpNewProc2 = GeometricBrownianMotion.estimate(tmpSeries.get(1).asPrimitive(), 1.0 / 12.0);
final GeometricBrownianMotion tmpNewProc3 = GeometricBrownianMotion.estimate(tmpSeries.get(2).asPrimitive(), 1.0 / 12.0);
TestUtils.assertEquals(0.01, tmpNewProc1.getStandardDeviation(), 0.005);
TestUtils.assertEquals(0.02, tmpNewProc2.getStandardDeviation(), 0.005);
TestUtils.assertEquals(0.03, tmpNewProc3.getStandardDeviation(), 0.005);
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class SymmetricEigenvalueProfiling method main.
public static void main(final String[] args) {
final PrimitiveDenseStore tmpOrg = MatrixUtils.makeSPD(200);
// final Eigenvalue<Double> tmpEvD = new RawEigenvalue.Symmetric();
final Eigenvalue<Double> tmpEvD = Eigenvalue.PRIMITIVE.make(tmpOrg, true);
for (int l = 0; l < 1000000; l++) {
tmpEvD.decompose(tmpOrg);
}
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class ConjugateGradientSolver method resolve.
public double resolve(final List<Equation> equations, final PhysicalStore<Double> solution) {
final int tmpCountRows = equations.size();
double tmpNormErr = POSITIVE_INFINITY;
double tmpNormRHS = ONE;
final PrimitiveDenseStore tmpResidual = this.residual(solution);
final PrimitiveDenseStore tmpDirection = this.direction(solution);
final PrimitiveDenseStore tmpPreconditioned = this.preconditioned(solution);
final PrimitiveDenseStore tmpVector = this.vector(solution);
double tmpStepLength;
double tmpGradientCorrectionFactor;
double zr0 = 1;
double zr1 = 1;
double pAp0 = 0;
for (int r = 0; r < tmpCountRows; r++) {
final Equation tmpRow = equations.get(r);
double tmpVal = tmpRow.getRHS();
tmpNormRHS = PrimitiveFunction.HYPOT.invoke(tmpNormRHS, tmpVal);
tmpVal -= tmpRow.dot(solution);
tmpResidual.set(tmpRow.index, tmpVal);
// precondition
tmpPreconditioned.set(tmpRow.index, tmpVal / tmpRow.getPivot());
}
// tmpPreconditioned.supplyNonZerosTo(tmpDirection);
tmpDirection.fillMatching(tmpPreconditioned);
int tmpIterations = 0;
final int tmpLimit = this.getIterationsLimit();
final NumberContext tmpCntxt = this.getAccuracyContext();
// zr1 = tmpPreconditioned.transpose().multiply(tmpResidual).doubleValue(0L);
zr1 = tmpPreconditioned.dot(tmpResidual);
do {
zr0 = zr1;
for (int i = 0; i < tmpCountRows; i++) {
final Equation tmpRow = equations.get(i);
final double tmpVal = tmpRow.dot(tmpDirection);
tmpVector.set(tmpRow.index, tmpVal);
}
// pAp0 = tmpVector.multiplyLeft(tmpDirection.transpose()).get().doubleValue(0L);
pAp0 = tmpDirection.dot(tmpVector);
tmpStepLength = zr0 / pAp0;
if (!Double.isNaN(tmpStepLength)) {
// solution.maxpy(tmpStepLength, tmpDirection);
tmpDirection.axpy(tmpStepLength, solution);
// tmpResidual.maxpy(-tmpStepLength, tmpVector);
tmpVector.axpy(-tmpStepLength, tmpResidual);
}
tmpNormErr = ZERO;
for (int r = 0; r < tmpCountRows; r++) {
final Equation tmpRow = equations.get(r);
final double tmpValue = tmpResidual.doubleValue(tmpRow.index);
tmpNormErr = PrimitiveFunction.HYPOT.invoke(tmpNormErr, tmpValue);
tmpPreconditioned.set(tmpRow.index, tmpValue / tmpRow.getPivot());
}
zr1 = tmpPreconditioned.dot(tmpResidual);
tmpGradientCorrectionFactor = zr1 / zr0;
tmpDirection.modifyAll(PrimitiveFunction.MULTIPLY.second(tmpGradientCorrectionFactor));
tmpDirection.modifyMatching(PrimitiveFunction.ADD, tmpPreconditioned);
tmpIterations++;
if (this.isDebugPrinterSet()) {
this.debug(tmpIterations, solution);
}
} while ((tmpIterations < tmpLimit) && !Double.isNaN(tmpNormErr) && !tmpCntxt.isSmall(tmpNormRHS, tmpNormErr));
return tmpNormErr / tmpNormRHS;
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class MatrixUtils method getComplexModulus.
/**
* Extracts the modulus of the ComplexNumber elements to a new primitive double valued matrix.
*/
public static PrimitiveDenseStore getComplexModulus(final Access2D<ComplexNumber> arg) {
final long tmpRows = arg.countRows();
final long tmpColumns = arg.countColumns();
final PrimitiveDenseStore retVal = PrimitiveDenseStore.FACTORY.makeZero(tmpRows, tmpColumns);
MatrixUtils.copyComplexModulus(arg, retVal);
return retVal;
}
Aggregations