public static void mergeCorrelationState(CorrelationState state, CorrelationState otherState) { if (otherState.getCount() == 0) { return; } long na = state.getCount(); long nb = otherState.getCount(); state.setM2X(state.getM2X() + otherState.getM2X() + na * nb * Math.pow(state.getMeanX() - otherState.getMeanX(), 2) / (double) (na + nb)); state.setM2Y(state.getM2Y() + otherState.getM2Y() + na * nb * Math.pow(state.getMeanY() - otherState.getMeanY(), 2) / (double) (na + nb)); updateCovarianceState(state, otherState); }
public static double getCorrelation(CorrelationState state) { // This is defined as covariance(x, y) / (stdev(x) * stdev(y)) double covariance = state.getC2(); double stdevX = Math.sqrt(state.getM2X()); double stdevY = Math.sqrt(state.getM2Y()); // stdevX and stdevY deliberately not checked for zero because the result can be Infinity or NaN even // if they are both not zero return covariance / stdevX / stdevY; }
public static void updateCorrelationState(CorrelationState state, double x, double y) { double oldMeanX = state.getMeanX(); double oldMeanY = state.getMeanY(); updateCovarianceState(state, x, y); state.setM2X(state.getM2X() + (x - oldMeanX) * (x - state.getMeanX())); state.setM2Y(state.getM2Y() + (y - oldMeanY) * (y - state.getMeanY())); }
public static void mergeCorrelationState(CorrelationState state, CorrelationState otherState) { if (otherState.getCount() == 0) { return; } updateCovarianceState(state, otherState); state.setSumXSquare(state.getSumXSquare() + otherState.getSumXSquare()); state.setSumYSquare(state.getSumYSquare() + otherState.getSumYSquare()); }
@OutputFunction(StandardTypes.DOUBLE) public static void corr(CorrelationState state, BlockBuilder out) { // Math comes from ISO9075-2:2011(E) 10.9 General Rules 7 c x double dividend = state.getCount() * state.getSumXY() - state.getSumX() * state.getSumY(); dividend = dividend * dividend; double divisor1 = state.getCount() * state.getSumXSquare() - state.getSumX() * state.getSumX(); double divisor2 = state.getCount() * state.getSumYSquare() - state.getSumY() * state.getSumY(); // divisor1 and divisor2 deliberately not checked for zero because the result can be Infty or NaN even if they are both not zero double result = dividend / divisor1 / divisor2; // When the left expression yields a finite value, dividend / (divisor1 * divisor2) can yield Infty or NaN. if (Double.isFinite(result)) { DOUBLE.writeDouble(out, Math.sqrt(result)); // sqrt cannot turn finite value to non-finite value } else { out.appendNull(); } } }
public static void updateCorrelationState(CorrelationState state, double x, double y) { updateCovarianceState(state, x, y); state.setSumXSquare(state.getSumXSquare() + x * x); state.setSumYSquare(state.getSumYSquare() + y * y); }