Пример #1
0
  /**
   * Get the covariance matrix of unbound estimated parameters.
   *
   * @param problem estimation problem
   * @return covariance matrix
   * @exception EstimationException if the covariance matrix cannot be computed (singular problem)
   */
  public double[][] getCovariances(EstimationProblem problem) throws EstimationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    final int n = problem.getMeasurements().length;
    final int m = problem.getUnboundParameters().length;
    final int max = m * n;
    double[][] jTj = new double[m][m];
    for (int i = 0; i < m; ++i) {
      for (int j = i; j < m; ++j) {
        double sum = 0;
        for (int k = 0; k < max; k += m) {
          sum += jacobian[k + i] * jacobian[k + j];
        }
        jTj[i][j] = sum;
        jTj[j][i] = sum;
      }
    }

    try {
      // compute the covariances matrix
      RealMatrix inverse =
          new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
      return inverse.getData();
    } catch (InvalidMatrixException ime) {
      throw new EstimationException("unable to compute covariances: singular problem");
    }
  }
Пример #2
0
 public static RealMatrix randMatrix(int rows, int cols) {
   RealMatrix res = MathFactory.createRealMatrix(rows, cols);
   for (int i = 0; i < rows; i++) {
     res.setRow(i, randVector(cols).getData());
   }
   return res;
 }
 /**
  * Calculates the variance-covariance matrix of the regression parameters.
  *
  * <p>Var(b) = (X<sup>T</sup>X)<sup>-1</sup>
  *
  * <p>Uses QR decomposition to reduce (X<sup>T</sup>X)<sup>-1</sup> to
  * (R<sup>T</sup>R)<sup>-1</sup>, with only the top p rows of R included, where p = the length of
  * the beta vector.
  *
  * @return The beta variance-covariance matrix
  */
 @Override
 protected RealMatrix calculateBetaVariance() {
   int p = X.getColumnDimension();
   RealMatrix Raug = qr.getR().getSubMatrix(0, p - 1, 0, p - 1);
   RealMatrix Rinv = new LUDecompositionImpl(Raug).getSolver().getInverse();
   return Rinv.multiply(Rinv.transpose());
 }
 /**
  * Throws IllegalArgumentException of the matrix does not have at least two columns and two rows
  *
  * @param matrix matrix to check for sufficiency
  */
 private void checkSufficientData(final RealMatrix matrix) {
   int nRows = matrix.getRowDimension();
   int nCols = matrix.getColumnDimension();
   if (nRows < 2 || nCols < 2) {
     throw MathRuntimeException.createIllegalArgumentException(
         "insufficient data: only {0} rows and {1} columns.", nRows, nCols);
   }
 }
Пример #5
0
  @Override
  public void step() {
    stateHidden.setSubVector(0, weights0.operate(stateIn));

    for (int i : series(h)) stateHidden.setEntry(i, activation.function(stateHidden.getEntry(i)));

    stateOut.setSubVector(0, weights1.operate(stateHidden));
  }
 /**
  * Calculates beta by GLS.
  *
  * <pre>
  *  b=(X' Omega^-1 X)^-1X'Omega^-1 y
  * </pre>
  *
  * @return beta
  */
 @Override
 protected RealVector calculateBeta() {
   RealMatrix OI = getOmegaInverse();
   RealMatrix XT = X.transpose();
   RealMatrix XTOIX = XT.multiply(OI).multiply(X);
   RealMatrix inverse = new LUDecompositionImpl(XTOIX).getSolver().getInverse();
   return inverse.multiply(XT).multiply(OI).operate(Y);
 }
 /**
  * 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);
 }
Пример #8
0
 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();
   }
 }
 /**
  * Build a simple converter for correlated residuals with the specific weights.
  *
  * <p>The scalar objective function value is computed as:
  *
  * <pre>
  * objective = y<sup>T</sup>y with y = scale&times;(observation-objective)
  * </pre>
  *
  * <p>The array computed by the objective function, the observations array and the the scaling
  * matrix must have consistent sizes or a {@link FunctionEvaluationException} will be triggered
  * while computing the scalar objective.
  *
  * @param function vectorial residuals function to wrap
  * @param observations observations to be compared to objective function to compute residuals
  * @param scale scaling matrix
  * @exception IllegalArgumentException if the observations vector and the scale matrix dimensions
  *     don't match (objective function dimension is checked only when the {@link #value(double[])}
  *     method is called)
  */
 public LeastSquaresConverter(
     final MultivariateVectorialFunction function,
     final double[] observations,
     final RealMatrix scale)
     throws IllegalArgumentException {
   if (observations.length != scale.getColumnDimension()) {
     throw MathRuntimeException.createIllegalArgumentException(
         "dimension mismatch {0} != {1}", observations.length, scale.getColumnDimension());
   }
   this.function = function;
   this.observations = observations.clone();
   this.weights = null;
   this.scale = scale.copy();
 }
Пример #10
0
 public static String realMatToString(RealMatrix r) {
   String res =
       "RealMatrix("
           + r.getRowDimension()
           + "x"
           + r.getColumnDimension()
           + ") of type '"
           + r.getClass().getName()
           + "'\n";
   for (int i = 0; i < r.getRowDimension(); i++) {
     res += Arrays.toString(r.getRow(i)) + "\n";
   }
   return res;
 }
Пример #11
0
  @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;
  }
Пример #12
0
 public static void main(String[] args) {
   RealMatrix coefficients2 =
       new Array2DRowRealMatrix(
           new double[][] {
             {0.0D, 1.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D},
             {0.0D, 0.0D, 0.857D, 0.0D, 0.054D, 0.018D, 0.0D, 0.071D, 0.0D, 0.0D, 0.0D},
             {0.0D, 0.0D, 0.0D, 1.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D},
             {0.0D, 0.0D, 0.857D, 0.0D, 0.054D, 0.018D, 0.0D, 0.071D, 0.0D, 0.0D, 0.0D},
             {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 1.0D, 0.0D, 0.0D, 0.0D, 0.0D},
             {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 1.0D, 0.0D, 0.0D, 0.0D, 0.0D},
             {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 1.0D, 0.0D, 0.0D},
             {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.6D, 0.4D},
             {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 1.0D},
             {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 1.0D, 0.0D, 0.0D, 1.0D},
             {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D}
           },
           false);
   for (int i = 0; i < 11; i++) {
     coefficients2.setEntry(i, i, -1d);
   }
   coefficients2 = coefficients2.transpose();
   DecompositionSolver solver = new LUDecompositionImpl(coefficients2).getSolver();
   System.out.println("1 method my Value :");
   RealVector constants =
       new ArrayRealVector(new double[] {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false);
   RealVector solution = solver.solve(constants);
   double[] data = solution.getData();
   DecimalFormat df = new DecimalFormat();
   df.setRoundingMode(RoundingMode.DOWN);
   System.out.println("Корни уравнения:");
   for (double dd : data) {
     System.out.print(df.format(dd) + " ");
   }
   System.out.println();
   System.out.println(
       "Среднее число процессорных операций, выполняемых при одном прогоне алгоритма: "
           + operationsByProcess(data, arr));
   System.out.println("Среднее число обращений к файлам:");
   for (int i = 1; i < 4; i++) {
     System.out.println("  Файл " + i + " : " + fileMiddleRequest(data, arr, i));
   }
   System.out.println("Среднее количество информации передаваемой при одном обращении к файлам:");
   for (int i = 1; i < 4; i++) {
     System.out.println("  Файл " + i + " : " + bitsPerFileTransfer(data, arr, i));
   }
   System.out.println(
       "Сумма среднего числа обращений к основным операторам: " + operatorExecute(data, arr));
   System.out.println("Средняя трудоемкость этапа: " + middleWork(data, arr));
 }
  /** {@inheritDoc} */
  public double value(final double[] point) throws FunctionEvaluationException {

    // compute residuals
    final double[] residuals = function.value(point);
    if (residuals.length != observations.length) {
      throw new FunctionEvaluationException(
          point, "dimension mismatch {0} != {1}", residuals.length, observations.length);
    }
    for (int i = 0; i < residuals.length; ++i) {
      residuals[i] -= observations[i];
    }

    // compute sum of squares
    double sumSquares = 0;
    if (weights != null) {
      for (int i = 0; i < residuals.length; ++i) {
        final double ri = residuals[i];
        sumSquares += weights[i] * ri * ri;
      }
    } else if (scale != null) {
      for (final double yi : scale.operate(residuals)) {
        sumSquares += yi * yi;
      }
    } else {
      for (final double ri : residuals) {
        sumSquares += ri * ri;
      }
    }

    return sumSquares;
  }
 /**
  * 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);
 }
  /**
   * Compute the "hat" matrix.
   *
   * <p>The hat matrix is defined in terms of the design matrix X by
   * X(X<sup>T</sup>X)<sup>-1</sup>X<sup>T</sup>
   *
   * <p>The implementation here uses the QR decomposition to compute the hat matrix as Q
   * I<sub>p</sub>Q<sup>T</sup> where I<sub>p</sub> is the p-dimensional identity matrix augmented
   * by 0's. This computational formula is from "The Hat Matrix in Regression and ANOVA", David C.
   * Hoaglin and Roy E. Welsch, <i>The American Statistician</i>, Vol. 32, No. 1 (Feb., 1978), pp.
   * 17-22.
   *
   * @return the hat matrix
   */
  public RealMatrix calculateHat() {
    // Create augmented identity matrix
    RealMatrix Q = qr.getQ();
    final int p = qr.getR().getColumnDimension();
    final int n = Q.getColumnDimension();
    Array2DRowRealMatrix augI = new Array2DRowRealMatrix(n, n);
    double[][] augIData = augI.getDataRef();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j && i < p) {
          augIData[i][j] = 1d;
        } else {
          augIData[i][j] = 0d;
        }
      }
    }

    // Compute and return Hat matrix
    return Q.multiply(augI).multiply(Q.transpose());
  }
Пример #16
0
  /**
   * Modifies this map through a single backpropagation iteration using the given error values on
   * the output nodes.
   *
   * @param error
   */
  public void train(List<Double> error, double learningRate) {
    RealVector eOut = new ArrayRealVector(error.size());
    for (int i : series(error.size())) eOut.setEntry(i, error.get(i));

    // * gHidden: delta for the non-bias nodes of the hidden layer
    gHidden.setSubVector(0, stateHidden.getSubVector(0, n)); // optimize

    for (int i : Series.series(gHidden.getDimension()))
      gHidden.setEntry(i, activation.derivative(gHidden.getEntry(i)));

    eHiddenL = weights1.transpose().operate(eOut);
    eHidden.setSubVector(0, eHiddenL.getSubVector(0, h));
    for (int i : series(h)) eHidden.setEntry(i, eHidden.getEntry(i) * gHidden.getEntry(i));

    weights1Delta = MatrixTools.outer(eOut, stateHidden);
    weights1Delta = weights1Delta.scalarMultiply(-1.0 * learningRate); // optimize

    weights0Delta = MatrixTools.outer(eHidden, stateIn);
    weights0Delta = weights0Delta.scalarMultiply(-1.0 * learningRate);

    weights0 = weights0.add(weights0Delta);
    weights1 = weights1.add(weights1Delta);
  }
 /**
  * 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(&middot,&middot;)</code> is the
  * correlation coefficient and <code>s(&middot;)</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;
 }
 /**
  * Computes the correlation matrix for the columns of the input matrix.
  *
  * @param matrix matrix with columns representing variables to correlate
  * @return correlation matrix
  */
 public RealMatrix computeCorrelationMatrix(RealMatrix matrix) {
   int nVars = matrix.getColumnDimension();
   RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);
   for (int i = 0; i < nVars; i++) {
     for (int j = 0; j < i; j++) {
       double corr = correlation(matrix.getColumn(i), matrix.getColumn(j));
       outMatrix.setEntry(i, j, corr);
       outMatrix.setEntry(j, i, corr);
     }
     outMatrix.setEntry(i, i, 1d);
   }
   return outMatrix;
 }
 /** {@inheritDoc} */
 public double[] estimateRegressionParameters() {
   RealMatrix b = calculateBeta();
   return b.getColumn(0);
 }
 /**
  * Create a PearsonsCorrelation from a RealMatrix whose columns represent variables to be
  * correlated.
  *
  * @param matrix matrix with columns representing variables to correlate
  */
 public PearsonsCorrelation(RealMatrix matrix) {
   checkSufficientData(matrix);
   nObs = matrix.getRowDimension();
   correlationMatrix = computeCorrelationMatrix(matrix);
 }
 /** {@inheritDoc} */
 public double[] estimateResiduals() {
   RealMatrix b = calculateBeta();
   RealMatrix e = Y.subtract(X.multiply(b));
   return e.getColumn(0);
 }
 /**
  * Calculates the residuals of multiple linear regression in matrix notation.
  *
  * <pre>
  * u = y - X * b
  * </pre>
  *
  * @return The residuals [n,1] matrix
  */
 protected RealMatrix calculateResiduals() {
   RealMatrix b = calculateBeta();
   return Y.subtract(X.multiply(b));
 }
Пример #23
0
  /**
   * Solve an estimation problem using a least squares criterion.
   *
   * <p>This method set the unbound parameters of the given problem starting from their current
   * values through several iterations. At each step, the unbound parameters are changed in order to
   * minimize a weighted least square criterion based on the measurements of the problem.
   *
   * <p>The iterations are stopped either when the criterion goes below a physical threshold under
   * which improvement are considered useless or when the algorithm is unable to improve it (even if
   * it is still high). The first condition that is met stops the iterations. If the convergence it
   * not reached before the maximum number of iterations, an {@link EstimationException} is thrown.
   *
   * @param problem estimation problem to solve
   * @exception EstimationException if the problem cannot be solved
   * @see EstimationProblem
   */
  @Override
  public void estimate(EstimationProblem problem) throws EstimationException {

    initializeEstimate(problem);

    // work matrices
    double[] grad = new double[parameters.length];
    ArrayRealVector bDecrement = new ArrayRealVector(parameters.length);
    double[] bDecrementData = bDecrement.getDataRef();
    RealMatrix wGradGradT = MatrixUtils.createRealMatrix(parameters.length, parameters.length);

    // iterate until convergence is reached
    double previous = Double.POSITIVE_INFINITY;
    do {

      // build the linear problem
      incrementJacobianEvaluationsCounter();
      RealVector b = new ArrayRealVector(parameters.length);
      RealMatrix a = MatrixUtils.createRealMatrix(parameters.length, parameters.length);
      for (int i = 0; i < measurements.length; ++i) {
        if (!measurements[i].isIgnored()) {

          double weight = measurements[i].getWeight();
          double residual = measurements[i].getResidual();

          // compute the normal equation
          for (int j = 0; j < parameters.length; ++j) {
            grad[j] = measurements[i].getPartial(parameters[j]);
            bDecrementData[j] = weight * residual * grad[j];
          }

          // build the contribution matrix for measurement i
          for (int k = 0; k < parameters.length; ++k) {
            double gk = grad[k];
            for (int l = 0; l < parameters.length; ++l) {
              wGradGradT.setEntry(k, l, weight * gk * grad[l]);
            }
          }

          // update the matrices
          a = a.add(wGradGradT);
          b = b.add(bDecrement);
        }
      }

      try {

        // solve the linearized least squares problem
        RealVector dX = new LUDecompositionImpl(a).getSolver().solve(b);

        // update the estimated parameters
        for (int i = 0; i < parameters.length; ++i) {
          parameters[i].setEstimate(parameters[i].getEstimate() + dX.getEntry(i));
        }

      } catch (InvalidMatrixException e) {
        throw new EstimationException("unable to solve: singular problem");
      }

      previous = cost;
      updateResidualsAndCost();

    } while ((getCostEvaluations() < 2)
        || (Math.abs(previous - cost) > (cost * steadyStateThreshold)
            && (Math.abs(cost) > convergence)));
  }