use of org.apache.commons.math3.stat.correlation.Covariance in project narchy by automenta.
the class MyCMAESOptimizer method updateBD.
/**
* Update B and D from C.
*
* @param negccov Negative covariance factor.
*/
private void updateBD(double negccov) {
if (ccov1 + ccovmu + negccov > 0 && (iterations % 1.0 / (ccov1 + ccovmu + negccov) / dimension / dimensionDivisorWTF) < 1) {
// to achieve O(N^2)
C = triu(C, 0).add(triu(C, 1).transpose());
// enforce symmetry to prevent complex numbers
final EigenDecomposition eig = new EigenDecomposition(C);
// eigen decomposition, B==normalized eigenvectors
B = eig.getV();
D = eig.getD();
diagD = diag(D);
if (min(diagD) <= 0) {
for (int i = 0; i < dimension; i++) {
if (diagD.getEntry(i, 0) < 0) {
diagD.setEntry(i, 0, 0);
}
}
final double tfac = max(diagD) / big_magic_number_WTF;
C = C.add(eye(dimension, dimension).scalarMultiply(tfac));
diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac));
}
if (max(diagD) > big_magic_number_WTF * min(diagD)) {
final double tfac = max(diagD) / big_magic_number_WTF - min(diagD);
C = C.add(eye(dimension, dimension).scalarMultiply(tfac));
diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac));
}
diagC = diag(C);
// D contains standard deviations now
diagD = sqrt(diagD);
// O(n^2)
BD = times(B, repmat(diagD.transpose(), dimension, 1));
}
}
use of org.apache.commons.math3.stat.correlation.Covariance in project MindsEye by SimiaCryptus.
the class PCAUtil method pcaFeatures.
/**
* Pca features inv tensor [ ].
*
* @param covariance the covariance
* @param components the components
* @param featureDimensions the feature dimensions
* @param power the power
* @return the tensor [ ]
*/
public static Tensor[] pcaFeatures(final RealMatrix covariance, final int components, final int[] featureDimensions, final double power) {
@Nonnull final EigenDecomposition decomposition = new EigenDecomposition(covariance);
final int[] orderedVectors = IntStream.range(0, components).mapToObj(x -> x).sorted(Comparator.comparing(x -> -decomposition.getRealEigenvalue(x))).mapToInt(x -> x).toArray();
return IntStream.range(0, orderedVectors.length).mapToObj(i -> {
@Nonnull final Tensor src = new Tensor(decomposition.getEigenvector(orderedVectors[i]).toArray(), featureDimensions).copy();
return src.scale(1.0 / src.rms()).scale((Math.pow(decomposition.getRealEigenvalue(orderedVectors[i]) / decomposition.getRealEigenvalue(orderedVectors[0]), power)));
}).toArray(i -> new Tensor[i]);
}
use of org.apache.commons.math3.stat.correlation.Covariance in project MindsEye by SimiaCryptus.
the class PCAUtil method getCovariance.
/**
* Forked from Apache Commons Math
*
* @param stream the stream
* @return covariance covariance
*/
@Nonnull
public static RealMatrix getCovariance(@Nonnull final Supplier<Stream<double[]>> stream) {
final int dimension = stream.get().findAny().get().length;
final List<DoubleStatistics> statList = IntStream.range(0, dimension * dimension).mapToObj(i -> new DoubleStatistics()).collect(Collectors.toList());
stream.get().forEach(array -> {
for (int i = 0; i < dimension; i++) {
for (int j = 0; j <= i; j++) {
statList.get(i * dimension + j).accept(array[i] * array[j]);
}
}
RecycleBin.DOUBLES.recycle(array, array.length);
});
@Nonnull final RealMatrix covariance = new BlockRealMatrix(dimension, dimension);
for (int i = 0; i < dimension; i++) {
for (int j = 0; j <= i; j++) {
final double v = statList.get(i + dimension * j).getAverage();
covariance.setEntry(i, j, v);
covariance.setEntry(j, i, v);
}
}
return covariance;
}
use of org.apache.commons.math3.stat.correlation.Covariance in project GDSC-SMLM by aherbert.
the class MultivariateGaussianMixtureExpectationMaximizationTest method canComputeCovariance.
@SeededTest
void canComputeCovariance(RandomSeed seed) {
final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
final int rows = 20;
final int cols = 3;
final double[][] data = new double[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = rng.nextDouble();
}
}
// Compute the same mean as the Covariance class
final double[] means = getColumnMeans(data);
final double[][] obs = MultivariateGaussianMixtureExpectationMaximization.covariance(means, data);
final double[][] exp = new Covariance(data).getCovarianceMatrix().getData();
Assertions.assertArrayEquals(exp, obs);
}
Aggregations