public static double cov(Univariate x, Univariate y) { double sumxy = 0; int i, n = (x.size() >= y.size() ? x.size() : y.size()); try { for (i = 0; i < x.size(); i++) sumxy += (x.elementAt(i) - x.mean()) * (y.elementAt(i) - y.mean()); } catch (ArrayIndexOutOfBoundsException e) { logger.info("size of x != size of y"); e.printStackTrace(); } return (sumxy / (n - 1)); }
public static double[] ols(Univariate x, Univariate y) { double[] coef = new double[2]; int i, n = (x.size() <= y.size() ? x.size() : y.size()); double sxy = 0.0, sxx = 0.0; double xbar = x.mean(), ybar = y.mean(), xi, yi; for (i = 0; i < n; i++) { xi = x.elementAt(i); yi = y.elementAt(i); sxy += (xi - xbar) * (yi - ybar); sxx += (xi - xbar) * (xi - xbar); } coef[0] = sxy / sxx; coef[1] = ybar - coef[0] * xbar; return (coef); }
public static double corr(Univariate x, Univariate y) { double cov = cov(x, y); return (cov / (x.stdev() * y.stdev())); }