Example usage for org.apache.commons.math3.stat.ranking NaNStrategy REMOVED

List of usage examples for org.apache.commons.math3.stat.ranking NaNStrategy REMOVED

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.ranking NaNStrategy REMOVED.

Prototype

NaNStrategy REMOVED

To view the source code for org.apache.commons.math3.stat.ranking NaNStrategy REMOVED.

Click Source Link

Document

NaNs are removed before computing ranks

Usage

From source file:com.itemanalysis.jmetrik.stats.ranking.RankingAnalysis.java

public String compute() throws SQLException {
    Statement stmt = null;/*ww w .jav a 2  s  .c  o m*/
    ResultSet rs = null;

    try {
        //get data
        ResizableDoubleArray data = getData();

        //create columns - dao uses its own transaction
        int numberOfColumns = dao.getColumnCount(conn, tableName);
        int columnNumber = numberOfColumns + 1;
        String newVariableLabel = "Rank";
        if (blom)
            newVariableLabel = "Blom Normal Score";
        if (tukey)
            newVariableLabel = "Tukey Normal Score";
        if (vdw)
            newVariableLabel = "van der Waerden Normal Score";
        if (ntiles)
            newVariableLabel = "Quantiles: " + numGroups + " groups";
        newVariable = new VariableAttributes(newVariableName, newVariableLabel, ItemType.NOT_ITEM,
                DataType.DOUBLE, columnNumber++, "");

        dao.addColumnToDb(conn, tableName, newVariable);

        //compute ranks
        NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED, tiesStrategy);
        double[] ranks = ranking.rank(data.getElements());

        //begin transaction
        conn.setAutoCommit(false);

        //connect to table and update values
        SelectQuery select = new SelectQuery();
        Table sqlTable = new Table(tableName.getNameForDatabase());
        select.addColumn(sqlTable, newVariable.getName().nameForDatabase());
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        rs = stmt.executeQuery(select.toString());

        int nRanks = ranks.length;
        int rankIndex = 0;//array index for ranks (missing values not included)
        int dbIndex = 0;//db row position for case (missing values included)
        double r = Double.NaN;
        boolean missing = false;
        String tempName = "";
        while (rs.next()) {
            missing = missingIndex.contains(dbIndex);
            if (missing) {
                rs.updateNull(newVariable.getName().nameForDatabase());
            } else {
                r = ranks[rankIndex];
                if (blom) {
                    rs.updateDouble(newVariable.getName().nameForDatabase(),
                            normScore.blom(r, (double) nRanks));
                } else if (tukey) {
                    rs.updateDouble(newVariable.getName().nameForDatabase(),
                            normScore.tukey(r, (double) nRanks));
                } else if (vdw) {
                    rs.updateDouble(newVariable.getName().nameForDatabase(),
                            normScore.vanderWaerden(r, (double) nRanks));
                } else if (ntiles) {
                    rs.updateDouble(newVariable.getName().nameForDatabase(),
                            getGroup(r, (double) nRanks, (double) numGroups));
                } else {
                    rs.updateDouble(newVariable.getName().nameForDatabase(), r);
                }
                rankIndex++;
            }
            rs.updateRow();
            updateProgress();
            dbIndex++;
        }
        conn.commit();
        return "Ranks computed";
    } catch (SQLException ex) {
        conn.rollback();
        throw ex;
    } finally {
        if (rs != null)
            rs.close();
        if (stmt != null)
            stmt.close();
        conn.setAutoCommit(true);
    }

}

From source file:org.caleydo.view.enroute.correlation.wilcoxon.WilcoxonManualResultPage.java

@Override
public void pageChanged(PageChangedEvent event) {
    if (event.getSelectedPage() == this) {
        WilcoxonRankSumTestWizard wizard = (WilcoxonRankSumTestWizard) getWizard();
        DataCellInfo targetInfo = wizard.getTargetInfo();

        // java.util.List<WilcoxonResult> resultList = WilcoxonUtil.applyWilcoxonToAllElements(
        // wizard.getSourceClassifier(), wizard.getSourceInfo(), targetInfo.columnPerspective);
        ///*from w w  w  . j ava 2  s .  c  o  m*/
        // for (WilcoxonResult r : resultList) {
        // System.out.println(r.p);
        // }
        // System.out.println("NumElements: " + resultList.size());

        // WilcoxonUtil.calcWilcoxonRankSumTest(sourceInfo, classifier, targetInfo)

        SimpleIDClassifier derivedClassifier = wizard.getDerivedIDClassifier();
        MannWhitneyUTest test = new MannWhitneyUTest(NaNStrategy.REMOVED, TiesStrategy.AVERAGE);
        double[] values1 = WilcoxonUtil.getSampleValuesArray(targetInfo, derivedClassifier.getClass1IDs());
        double[] values2 = WilcoxonUtil.getSampleValuesArray(targetInfo, derivedClassifier.getClass2IDs());

        double u = test.mannWhitneyU(values1, values2);
        double p = test.mannWhitneyUTest(values1, values2);

        resultsWidget.update(wizard.getSourceInfo(), targetInfo,
                new WilcoxonResult(p, u, wizard.getSourceClassifier(), derivedClassifier));

        // resultsWidget.updateClassSummary(0, values1, derivedClassifier.getDataClasses().get(0));
        // resultsWidget.updateClassSummary(1, values2, derivedClassifier.getDataClasses().get(1));
        //
        // resultsWidget.updateStatistics(u, p);
    }

}

From source file:stats.SpearmansCorrelation.java

/**
 * Computes the Spearman's rank correlation coefficient between the two
 * arrays./*from  www. j  a v a  2 s  .co  m*/
 *
 * @param xArray
 *          first data array
 * @param yArray
 *          second data array
 * @return Returns Spearman's rank correlation coefficient for the two arrays
 * @throws DimensionMismatchException
 *           if the arrays lengths do not match
 * @throws MathIllegalArgumentException
 *           if the array length is less than 2
 */
public Pair<Double, Double> correlation(final double[] xArray, final double[] yArray) {
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2);
    } else {
        double[] x = xArray;
        double[] y = yArray;
        if (rankingAlgorithm instanceof NaturalRanking
                && NaNStrategy.REMOVED == ((NaturalRanking) rankingAlgorithm).getNanStrategy()) {
            final Set<Integer> nanPositions = new HashSet<Integer>();

            nanPositions.addAll(getNaNPositions(xArray));
            nanPositions.addAll(getNaNPositions(yArray));

            x = removeValues(xArray, nanPositions);
            y = removeValues(yArray, nanPositions);
        }
        double $ = new PearsonsCorrelation().correlation(rankingAlgorithm.rank(x), rankingAlgorithm.rank(y));

        int degreesOfFreedom = x.length - 2;
        double t = $ * Math.sqrt(degreesOfFreedom / (1 - ($ * $)));
        double pValue = 2 * new TDistribution(degreesOfFreedom).cumulativeProbability(-1 * Math.abs(t));
        return new Pair<>($, pValue);
    }
}

From source file:stats.SpearmansCorrelation.java

/**
 * Applies rank transform to each of the columns of <code>matrix</code> using
 * the current <code>rankingAlgorithm</code>.
 *
 * @param matrix/*ww  w. j  a  v a 2  s .c  om*/
 *          matrix to transform
 * @return a rank-transformed matrix
 */
private RealMatrix rankTransform(final RealMatrix matrix) {
    RealMatrix transformed = null;

    if (rankingAlgorithm instanceof NaturalRanking
            && ((NaturalRanking) rankingAlgorithm).getNanStrategy() == NaNStrategy.REMOVED) {
        final Set<Integer> nanPositions = new HashSet<Integer>();
        for (int i = 0; i < matrix.getColumnDimension(); i++) {
            nanPositions.addAll(getNaNPositions(matrix.getColumn(i)));
        }

        // if we have found NaN values, we have to update the matrix size
        if (!nanPositions.isEmpty()) {
            transformed = new BlockRealMatrix(matrix.getRowDimension() - nanPositions.size(),
                    matrix.getColumnDimension());
            for (int i = 0; i < transformed.getColumnDimension(); i++) {
                transformed.setColumn(i, removeValues(matrix.getColumn(i), nanPositions));
            }
        }
    }

    if (transformed == null) {
        transformed = matrix.copy();
    }

    for (int i = 0; i < transformed.getColumnDimension(); i++) {
        transformed.setColumn(i, rankingAlgorithm.rank(transformed.getColumn(i)));
    }

    return transformed;
}

From source file:weka.filters.supervised.attribute.ifcfilter.numericalgorithme.Ntil.java

/**
 * Computes the index of the n-tile./*from www  . j a  v  a2  s  .co  m*/
 *
 * @param table double array containing the target values and sorted numeric
 * values.
 * @param ntil int sets number of n-tile.
 */
public void set(double[][] table, int ntil) {
    numData = new double[3][table[1].length];
    numData[0] = table[0];
    numData[1] = table[1];
    PercentileRank ranking = new PercentileRank(NaNStrategy.REMOVED, TiesStrategy.MINIMUM);
    double[] ranks = ranking.rank(table[1]);

    int numq = 0;
    double qold = 0;
    double qnew = 0;
    int n11 = 0;
    for (int i = 0; i < table[1].length; i++) {
        qnew = java.lang.Math.floor(ranks[i] * ntil) + 1;
        if (qold != qnew) {
            numq++;
        }
        qold = qnew;
        numData[2][i] = qnew; //
        //numData[2][i] = java.lang.Math.floor(i * ntil / table[1].length) + 1;
        if (table[0][i] == 1 && numData[2][i] == 1) {
            n11++;
        }
    }
    String[] labels = { "Y", "X", "N" };
    //System.out.print(contingencyCrosstable.ar2s(numData, labels));
    new Date();
}