@Override public List<java.lang.Double> parameters() { List<Double> parameters = new ArrayList<Double>((n + 1) * (h) + (h + 1) * n); for (int i : series(h)) // to for (int j : series(n + 1)) // from parameters.add(weights0.getEntry(i, j)); for (int i : series(n)) // to for (int j : series(h + 1)) // from parameters.add(weights1.getEntry(i, j)); return parameters; }
/** * Derives a correlation matrix from a covariance matrix. * * <p>Uses the formula <br> * <code>r(X,Y) = cov(X,Y)/s(X)s(Y)</code> where <code>r(·,·)</code> is the * correlation coefficient and <code>s(·)</code> means standard deviation. * * @param covarianceMatrix the covariance matrix * @return correlation matrix */ public RealMatrix covarianceToCorrelation(RealMatrix covarianceMatrix) { int nVars = covarianceMatrix.getColumnDimension(); RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars); for (int i = 0; i < nVars; i++) { double sigma = Math.sqrt(covarianceMatrix.getEntry(i, i)); outMatrix.setEntry(i, i, 1d); for (int j = 0; j < i; j++) { double entry = covarianceMatrix.getEntry(i, j) / (sigma * Math.sqrt(covarianceMatrix.getEntry(j, j))); outMatrix.setEntry(i, j, entry); outMatrix.setEntry(j, i, entry); } } return outMatrix; }
/** * Returns a matrix of standard errors associated with the estimates in the correlation matrix. * <br> * <code>getCorrelationStandardErrors().getEntry(i,j)</code> is the standard error associated with * <code>getCorrelationMatrix.getEntry(i,j)</code> * * <p>The formula used to compute the standard error is <br> * <code>SE<sub>r</sub> = ((1 - r<sup>2</sup>) / (n - 2))<sup>1/2</sup></code> where <code>r * </code> is the estimated correlation coefficient and <code>n</code> is the number of * observations in the source dataset. * * @return matrix of correlation standard errors */ public RealMatrix getCorrelationStandardErrors() { int nVars = correlationMatrix.getColumnDimension(); double[][] out = new double[nVars][nVars]; for (int i = 0; i < nVars; i++) { for (int j = 0; j < nVars; j++) { double r = correlationMatrix.getEntry(i, j); out[i][j] = Math.sqrt((1 - r * r) / (nObs - 2)); } } return new BlockRealMatrix(out); }
public void print(RealMatrix matrix, PrintWriter output, NumberFormat format, int width) { for (int row = 0; row < matrix.getRowDimension(); row++) { for (int column = 0; column < matrix.getColumnDimension(); column++) { String number = format.format(matrix.getEntry(row, column)); int padding = Math.max(1, width - number.length()); for (int p = 0; p < padding; p++) output.print(' '); output.print(number); } output.println(); } }
/** * Returns a matrix of p-values associated with the (two-sided) null hypothesis that the * corresponding correlation coefficient is zero. * * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability that a random variable * distributed as <code>t<sub>n-2</sub></code> takes a value with absolute value greater than or * equal to <br> * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code> * * <p>The values in the matrix are sometimes referred to as the <i>significance</i> of the * corresponding correlation coefficients. * * @return matrix of p-values * @throws MathException if an error occurs estimating probabilities */ public RealMatrix getCorrelationPValues() throws MathException { TDistribution tDistribution = new TDistributionImpl(nObs - 2); int nVars = correlationMatrix.getColumnDimension(); double[][] out = new double[nVars][nVars]; for (int i = 0; i < nVars; i++) { for (int j = 0; j < nVars; j++) { if (i == j) { out[i][j] = 0d; } else { double r = correlationMatrix.getEntry(i, j); double t = Math.abs(r * Math.sqrt((nObs - 2) / (1 - r * r))); out[i][j] = 2 * (1 - tDistribution.cumulativeProbability(t)); } } } return new BlockRealMatrix(out); }