### Question:
ColumnVector { public double correlation(final ColumnVector vector) { if (this.size != vector.size) { throw new IllegalArgumentException( "cannot determine the correlation between vectors with different sizes"); } final ColumnVector meanAdjustedX = this.meanAdjust(); final ColumnVector meanAdjustedY = vector.meanAdjust(); final double squaredDeviationX = meanAdjustedX.multiplyElementWise(meanAdjustedX).sum(); final double squaredDeviationY = meanAdjustedY.multiplyElementWise(meanAdjustedY).sum(); final double deviationProduct = meanAdjustedX.multiplyElementWise(meanAdjustedY).sum(); return deviationProduct / Math.sqrt(squaredDeviationX * squaredDeviationY); } ColumnVector(final int size);  ColumnVector(final double... vector); private  ColumnVector(final Matrix matrix); int size(); double getAt(final int index); void setAt(final int index, final double val); void incrementAt(final int index, final double val); void normalize(); void scale(final double scale); ColumnVector multiplyElementWise(final ColumnVector vector); ColumnVector addElementWise(final ColumnVector vector); double absSum(); double sum(); ColumnVector roundTo(final int numPlaces); ColumnVector add(final double scalar); ColumnVector multiply(final double scalar); ColumnVector sqrt(); ColumnVector abs(); final boolean isZeroVector(); double[] getRaw(); void setAll(final double val); boolean align(); double max(); double median(); double getMagnitude(); double l1Distance(final ColumnVector vector); double l2Distance(final ColumnVector vector); double correlation(final ColumnVector vector); @Override String toString(); void removeNegatives(); @Override int hashCode(); @Override boolean equals(final Object obj);  }### Answer:
@Test public void correlationCannotBeCalculatedForDifferentSizedVectors() { final ColumnVector largerVector = new ColumnVector(8); final ColumnVector smallerVector = new ColumnVector(7); ExceptionAssert.assertThrows( v -> largerVector.correlation(smallerVector), IllegalArgumentException.class); ExceptionAssert.assertThrows( v -> smallerVector.correlation(largerVector), IllegalArgumentException.class); }
@Test public void correlationCanBeCalculatedForPerfectlyCorrelatedVectors() { final ColumnVector lhs = new ColumnVector(1, 2, 3, 4, 5); final ColumnVector rhs = new ColumnVector(5, 10, 15, 20, 25); MatcherAssert.assertThat(lhs.correlation(lhs), IsEqual.equalTo(1.0)); MatcherAssert.assertThat(lhs.correlation(rhs), IsEqual.equalTo(1.0)); MatcherAssert.assertThat(rhs.correlation(lhs), IsEqual.equalTo(1.0)); MatcherAssert.assertThat(rhs.correlation(rhs), IsEqual.equalTo(1.0)); }
@Test public void correlationCanBeCalculatedForPerfectlyAntiCorrelatedVectors() { final ColumnVector lhs = new ColumnVector(5, 4, 3, 2, 1); final ColumnVector rhs = new ColumnVector(5, 10, 15, 20, 25); MatcherAssert.assertThat(lhs.correlation(rhs), IsEqual.equalTo(-1.0)); MatcherAssert.assertThat(rhs.correlation(lhs), IsEqual.equalTo(-1.0)); }
@Test public void correlationCanBeCalculatedForPartiallyCorrelatedVectors() { final ColumnVector lhs = new ColumnVector(10.00, 200.0, 7.000, 150.0, 2.000); final ColumnVector rhs = new ColumnVector(0.001, 0.450, 0.007, 0.200, 0.300); MatcherAssert.assertThat(lhs.correlation(rhs), IsRoundedEqual.equalTo(0.6877, 4)); MatcherAssert.assertThat(rhs.correlation(lhs), IsRoundedEqual.equalTo(0.6877, 4)); }