Search in sources :

Example 1 with RegressionTrainingRow

use of org.knime.base.node.mine.regression.RegressionTrainingRow in project knime-core by knime.

the class Learner method likelihood.

/**
 * Compute the likelihood at given beta.
 *
 * @param iter iterator over trainings data.
 * @param beta parameter vector
 * @param rC regressors count
 * @param tcC target category count
 * @throws CanceledExecutionException when method is cancelled
 */
private double likelihood(final Iterator<RegressionTrainingRow> iter, final RealMatrix beta, final int rC, final int tcC, final ExecutionContext exec) throws CanceledExecutionException {
    double loglike = 0;
    RealMatrix x = new Array2DRowRealMatrix(1, rC + 1);
    while (iter.hasNext()) {
        exec.checkCanceled();
        RegressionTrainingRow row = iter.next();
        x.setEntry(0, 0, 1);
        x.setSubMatrix(row.getParameter().getData(), 0, 1);
        double sumEBetaTx = 0;
        for (int i = 0; i < tcC - 1; i++) {
            RealMatrix betaITx = x.multiply(beta.getSubMatrix(0, 0, i * (rC + 1), (i + 1) * (rC + 1) - 1).transpose());
            sumEBetaTx += Math.exp(betaITx.getEntry(0, 0));
        }
        int y = (int) row.getTarget();
        double yBetaTx = 0;
        if (y < tcC - 1) {
            yBetaTx = x.multiply(beta.getSubMatrix(0, 0, y * (rC + 1), (y + 1) * (rC + 1) - 1).transpose()).getEntry(0, 0);
        }
        loglike += yBetaTx - Math.log(1 + sumEBetaTx);
    }
    return loglike;
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RegressionTrainingRow(org.knime.base.node.mine.regression.RegressionTrainingRow)

Example 2 with RegressionTrainingRow

use of org.knime.base.node.mine.regression.RegressionTrainingRow in project knime-core by knime.

the class Learner method processTable.

/**
 * @param exec
 * @param trainingData
 * @param stats
 * @param regr
 * @throws CanceledExecutionException
 */
@Override
protected void processTable(final ExecutionMonitor exec, final RegressionTrainingData trainingData, final SummaryStatistics[] stats, final UpdatingMultipleLinearRegression regr) throws CanceledExecutionException {
    long r = 1;
    double progress = 0;
    final long rowCount = trainingData.getRowCount();
    for (RegressionTrainingRow row : trainingData) {
        exec.checkCanceled();
        if (!row.hasMissingCells()) {
            double[] parameter = row.getParameter().getRow(0);
            double[] params = new double[trainingData.getRegressorCount() * m_maxExponent];
            for (int i = 0; i < trainingData.getRegressorCount(); i++) {
                final double v = parameter[i];
                double value = v;
                for (int n = 0; n < m_maxExponent; ++n) {
                    int index = i + n * trainingData.getRegressorCount();
                    stats[index].addValue(value);
                    params[index] = value;
                    value *= v;
                }
            }
            regr.addObservation(params, row.getTarget() - m_offsetValue);
        }
        double progressUpdate = r / (double) rowCount;
        // report progress if update above 1%
        if (progressUpdate - progress > 0.01) {
            progress = progressUpdate;
            exec.setProgress(progress);
        }
        r++;
    }
}
Also used : RegressionTrainingRow(org.knime.base.node.mine.regression.RegressionTrainingRow)

Example 3 with RegressionTrainingRow

use of org.knime.base.node.mine.regression.RegressionTrainingRow in project knime-core by knime.

the class Learner method processTable.

/**
 * {@inheritDoc}
 */
@Override
protected void processTable(final ExecutionMonitor exec, final RegressionTrainingData trainingData, final SummaryStatistics[] stats, final UpdatingMultipleLinearRegression regr) throws CanceledExecutionException {
    long r = 1;
    final long rowCount = trainingData.getRowCount();
    for (RegressionTrainingRow row : trainingData) {
        exec.checkCanceled();
        if (!row.hasMissingCells()) {
            double[] parameter = row.getParameter().getRow(0);
            for (int i = 0; i < trainingData.getRegressorCount(); i++) {
                stats[i].addValue(parameter[i]);
            }
            regr.addObservation(parameter, row.getTarget() + (m_includeConstant ? 0 : -m_offsetValue));
        }
        double progressUpdate = r / (double) rowCount;
        exec.setProgress(progressUpdate, String.format("Row %d/%d", r, rowCount));
        r++;
    }
}
Also used : RegressionTrainingRow(org.knime.base.node.mine.regression.RegressionTrainingRow)

Example 4 with RegressionTrainingRow

use of org.knime.base.node.mine.regression.RegressionTrainingRow in project knime-core by knime.

the class Learner method irlsRls.

/**
 * Do a irls step. The result is stored in beta.
 *
 * @param data over trainings data.
 * @param beta parameter vector
 * @param rC regressors count
 * @param tcC target category count
 * @throws CanceledExecutionException when method is cancelled
 */
private void irlsRls(final RegressionTrainingData data, final RealMatrix beta, final int rC, final int tcC, final ExecutionMonitor exec) throws CanceledExecutionException {
    Iterator<RegressionTrainingRow> iter = data.iterator();
    long rowCount = 0;
    int dim = (rC + 1) * (tcC - 1);
    RealMatrix xTwx = new Array2DRowRealMatrix(dim, dim);
    RealMatrix xTyu = new Array2DRowRealMatrix(dim, 1);
    RealMatrix x = new Array2DRowRealMatrix(1, rC + 1);
    RealMatrix eBetaTx = new Array2DRowRealMatrix(1, tcC - 1);
    RealMatrix pi = new Array2DRowRealMatrix(1, tcC - 1);
    final long totalRowCount = data.getRowCount();
    while (iter.hasNext()) {
        rowCount++;
        RegressionTrainingRow row = iter.next();
        exec.checkCanceled();
        exec.setProgress(rowCount / (double) totalRowCount, "Row " + rowCount + "/" + totalRowCount);
        x.setEntry(0, 0, 1);
        x.setSubMatrix(row.getParameter().getData(), 0, 1);
        for (int k = 0; k < tcC - 1; k++) {
            RealMatrix betaITx = x.multiply(beta.getSubMatrix(0, 0, k * (rC + 1), (k + 1) * (rC + 1) - 1).transpose());
            eBetaTx.setEntry(0, k, Math.exp(betaITx.getEntry(0, 0)));
        }
        double sumEBetaTx = 0;
        for (int k = 0; k < tcC - 1; k++) {
            sumEBetaTx += eBetaTx.getEntry(0, k);
        }
        for (int k = 0; k < tcC - 1; k++) {
            double pik = eBetaTx.getEntry(0, k) / (1 + sumEBetaTx);
            pi.setEntry(0, k, pik);
        }
        // fill the diagonal blocks of matrix xTwx (k = k')
        for (int k = 0; k < tcC - 1; k++) {
            for (int i = 0; i < rC + 1; i++) {
                for (int ii = i; ii < rC + 1; ii++) {
                    int o = k * (rC + 1);
                    double v = xTwx.getEntry(o + i, o + ii);
                    double w = pi.getEntry(0, k) * (1 - pi.getEntry(0, k));
                    v += x.getEntry(0, i) * w * x.getEntry(0, ii);
                    xTwx.setEntry(o + i, o + ii, v);
                    xTwx.setEntry(o + ii, o + i, v);
                }
            }
        }
        // fill the rest of xTwx (k != k')
        for (int k = 0; k < tcC - 1; k++) {
            for (int kk = k + 1; kk < tcC - 1; kk++) {
                for (int i = 0; i < rC + 1; i++) {
                    for (int ii = i; ii < rC + 1; ii++) {
                        int o1 = k * (rC + 1);
                        int o2 = kk * (rC + 1);
                        double v = xTwx.getEntry(o1 + i, o2 + ii);
                        double w = -pi.getEntry(0, k) * pi.getEntry(0, kk);
                        v += x.getEntry(0, i) * w * x.getEntry(0, ii);
                        xTwx.setEntry(o1 + i, o2 + ii, v);
                        xTwx.setEntry(o1 + ii, o2 + i, v);
                        xTwx.setEntry(o2 + ii, o1 + i, v);
                        xTwx.setEntry(o2 + i, o1 + ii, v);
                    }
                }
            }
        }
        int g = (int) row.getTarget();
        // fill matrix xTyu
        for (int k = 0; k < tcC - 1; k++) {
            for (int i = 0; i < rC + 1; i++) {
                int o = k * (rC + 1);
                double v = xTyu.getEntry(o + i, 0);
                double y = k == g ? 1 : 0;
                v += (y - pi.getEntry(0, k)) * x.getEntry(0, i);
                xTyu.setEntry(o + i, 0, v);
            }
        }
    }
    if (m_penaltyTerm > 0.0) {
        RealMatrix stdError = getStdErrorMatrix(xTwx);
        // do not penalize the constant terms
        for (int i = 0; i < tcC - 1; i++) {
            stdError.setEntry(i * (rC + 1), i * (rC + 1), 0);
        }
        xTwx = xTwx.add(stdError.scalarMultiply(-0.00001));
    }
    exec.checkCanceled();
    b = xTwx.multiply(beta.transpose()).add(xTyu);
    A = xTwx;
    if (rowCount < A.getColumnDimension()) {
        throw new IllegalStateException("The dataset must have at least " + A.getColumnDimension() + " rows, but it has only " + rowCount + " rows. It is recommended to use a " + "larger dataset in order to increase accuracy.");
    }
    DecompositionSolver solver = new QRDecomposition(A).getSolver();
    boolean isNonSingular = solver.isNonSingular();
    if (isNonSingular) {
        RealMatrix betaNew = solver.solve(b);
        beta.setSubMatrix(betaNew.transpose().getData(), 0, 0);
    } else {
        throw new RuntimeException(FAILING_MSG);
    }
}
Also used : QRDecomposition(org.apache.commons.math3.linear.QRDecomposition) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) DecompositionSolver(org.apache.commons.math3.linear.DecompositionSolver) RegressionTrainingRow(org.knime.base.node.mine.regression.RegressionTrainingRow)

Example 5 with RegressionTrainingRow

use of org.knime.base.node.mine.regression.RegressionTrainingRow in project knime-core by knime.

the class Learner method likelihood.

/**
 * Compute the likelihood at given beta.
 *
 * @param iter iterator over trainings data.
 * @param beta parameter vector
 * @param rC regressors count
 * @param tcC target category count
 * @throws CanceledExecutionException when method is cancelled
 */
private double likelihood(final Iterator<RegressionTrainingRow> iter, final RealMatrix beta, final int rC, final int tcC, final ExecutionContext exec) throws CanceledExecutionException {
    double loglike = 0;
    RealMatrix x = new Array2DRowRealMatrix(1, rC + 1);
    while (iter.hasNext()) {
        exec.checkCanceled();
        RegressionTrainingRow row = iter.next();
        x.setEntry(0, 0, 1);
        x.setSubMatrix(row.getParameter().getData(), 0, 1);
        double sumEBetaTx = 0;
        for (int i = 0; i < tcC - 1; i++) {
            RealMatrix betaITx = x.multiply(beta.getSubMatrix(0, 0, i * (rC + 1), (i + 1) * (rC + 1) - 1).transpose());
            sumEBetaTx += Math.exp(betaITx.getEntry(0, 0));
        }
        int y = (int) row.getTarget();
        double yBetaTx = 0;
        if (y < tcC - 1) {
            yBetaTx = x.multiply(beta.getSubMatrix(0, 0, y * (rC + 1), (y + 1) * (rC + 1) - 1).transpose()).getEntry(0, 0);
        }
        loglike += yBetaTx - Math.log(1 + sumEBetaTx);
    }
    return loglike;
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RegressionTrainingRow(org.knime.base.node.mine.regression.RegressionTrainingRow)

Aggregations

RegressionTrainingRow (org.knime.base.node.mine.regression.RegressionTrainingRow)6 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)4 RealMatrix (org.apache.commons.math3.linear.RealMatrix)4 DecompositionSolver (org.apache.commons.math3.linear.DecompositionSolver)2 QRDecomposition (org.apache.commons.math3.linear.QRDecomposition)1 SingularValueDecomposition (org.apache.commons.math3.linear.SingularValueDecomposition)1