CovarianceMatricesAggregator implements Serializable { void add(Vector x, double pcxi) { Matrix deltaCol = x.minus(mean).toMatrix(false); Matrix weightedCovComponent = deltaCol.times(deltaCol.transpose()).times(pcxi); if (weightedSum == null) weightedSum = weightedCovComponent; else weightedSum = weightedSum.plus(weightedCovComponent); rowCount += 1; } CovarianceMatricesAggregator(Vector mean);   CovarianceMatricesAggregator(Vector mean, Matrix weightedSum, int rowCount); int rowCount();  }
@Test public void testAdd() { CovarianceMatricesAggregator agg = new CovarianceMatricesAggregator(VectorUtils.of(1., 0.)); assertEquals(0, agg.rowCount()); agg.add(VectorUtils.of(1., 0.), 100.); assertArrayEquals(VectorUtils.of(1., 0.).asArray(), agg.mean().asArray(), 1e-4); assertArrayEquals( agg.weightedSum().getStorage().data(), fromArray(2, 0., 0., 0., 0.).getStorage().data(), 1e-4 ); assertEquals(1, agg.rowCount()); agg.add(VectorUtils.of(0., 1.), 10.); assertArrayEquals(VectorUtils.of(1., 0.).asArray(), agg.mean().asArray(), 1e-4); assertArrayEquals( agg.weightedSum().getStorage().data(), fromArray(2, 10., -10., -10., 10.).getStorage().data(), 1e-4 ); assertEquals(2, agg.rowCount()); }