Example usage for org.apache.commons.math3.util BigReal doubleValue

List of usage examples for org.apache.commons.math3.util BigReal doubleValue

Introduction

In this page you can find the example usage for org.apache.commons.math3.util BigReal doubleValue.

Prototype

public double doubleValue() 

Source Link

Document

Get the double value corresponding to the instance.

Usage

From source file:edu.macalester.tagrelatedness.KendallsCorrelation.java

/**
 * Computes the Kendall's Tau rank correlation coefficient between the two
 * lists./*  w w  w.j ava2s  .  c  o m*/
 *
 * @param xs first data list
 * @param ys second data list
 * @return Returns Kendall's Tau rank correlation coefficient for the two
 * lists
 * @throws DimensionMismatchException if the list lengths do not match
 */
static public <E extends Comparable<E>, F extends Comparable<F>> double correlation(final List<E> xs,
        final List<F> ys) {

    final int n = xs.size();
    final long numPairs = (long) n * (n - 1) / 2;

    ComparablePair[] pairs = new ComparablePair[n];
    for (int i = 0; i < n; i++) {
        pairs[i] = new ComparablePair(xs.get(i), ys.get(i));
    }

    Arrays.sort(pairs);

    long tiedXPairs = 0;
    long tiedXYPairs = 0;
    long consecutiveXTies = 1;
    long consecutiveXYTies = 1;
    ComparablePair prev = pairs[0];
    for (int i = 1; i < n; i++) {
        final ComparablePair curr = pairs[i];
        if (curr.x.equals(prev.x)) {
            consecutiveXTies++;
            if (curr.y.equals(prev.y)) {
                consecutiveXYTies++;
            } else {
                tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2;
                consecutiveXYTies = 1;
            }
        } else {
            tiedXPairs += consecutiveXTies * (consecutiveXTies - 1) / 2;
            consecutiveXTies = 1;
            tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2;
            consecutiveXYTies = 1;
        }
        prev = curr;
    }
    tiedXPairs += consecutiveXTies * (consecutiveXTies - 1) / 2;
    tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2;

    long swaps = 0;
    ComparablePair[] pairsDestination = new ComparablePair[n];
    for (int segmentSize = 1; segmentSize < n; segmentSize <<= 1) {
        for (int offset = 0; offset < n; offset += 2 * segmentSize) {
            int i = offset;
            final int iEnd = Math.min(i + segmentSize, n);
            int j = iEnd;
            final int jEnd = Math.min(j + segmentSize, n);

            int copyLocation = offset;
            while (i < iEnd || j < jEnd) {
                if (i < iEnd) {
                    if (j < jEnd) {
                        if (pairs[i].y.compareTo(pairs[j].y) <= 0) {
                            pairsDestination[copyLocation] = pairs[i];
                            i++;
                        } else {
                            pairsDestination[copyLocation] = pairs[j];
                            j++;
                            swaps += iEnd - i;
                        }
                    } else {
                        pairsDestination[copyLocation] = pairs[i];
                        i++;
                    }
                } else {
                    pairsDestination[copyLocation] = pairs[j];
                    j++;
                }
                copyLocation++;
            }
        }
        final ComparablePair[] pairsTemp = pairs;
        pairs = pairsDestination;
        pairsDestination = pairsTemp;

    }

    long tiedYPairs = 0;
    long consecutiveYTies = 1;
    prev = pairs[0];
    for (int i = 1; i < n; i++) {
        final ComparablePair curr = pairs[i];
        if (curr.y.equals(prev.y)) {
            consecutiveYTies++;
        } else {
            tiedYPairs += consecutiveYTies * (consecutiveYTies - 1) / 2;
            consecutiveYTies = 1;
        }
        prev = curr;
    }
    tiedYPairs += consecutiveYTies * (consecutiveYTies - 1) / 2;

    long concordantMinusDiscordant = (long) numPairs - tiedXPairs - tiedYPairs + tiedXYPairs - 2 * swaps;

    //      The formula for tau is annotated below. Here, it is done with several objects to handle big numbers
    //      concordantMinusDiscordant / Math.sqrt((numPairs - tiedXPairs) * (numPairs - tiedYPairs));

    BigSquareRoot bigSqrt = new BigSquareRoot();

    BigInteger numPairsMinusTiedX = new BigInteger("" + (numPairs - tiedXPairs));
    BigInteger numPairsMinusTiedY = new BigInteger("" + (numPairs - tiedYPairs));

    BigReal numerator = new BigReal(concordantMinusDiscordant);
    BigDecimal denominator = bigSqrt.get(numPairsMinusTiedX.multiply(numPairsMinusTiedY));

    BigReal result = numerator.divide(new BigReal(denominator));

    return result.doubleValue();
}