Java Array Sort calculatePValueForDataPoint(double dataPoint, double[] sortedNullHypothesisSample)

Here you can find the source of calculatePValueForDataPoint(double dataPoint, double[] sortedNullHypothesisSample)

Description

Calculate a p-value for the data point, given the sorted null distribution sample.

License

Open Source License

Parameter

Parameter Description
dataPoint the data point that we're calculating the p-value for
sortedNullHypothesisSample a sample from the null hypothesis

Exception

Parameter Description
IllegalArgumentException if the given sample is empty

Return

the p-value calculated for the given data

Declaration

public static double calculatePValueForDataPoint(double dataPoint, double[] sortedNullHypothesisSample)
        throws IllegalArgumentException 

Method Source Code


//package com.java2s;
/*//from   w  ww  . j  a  v a2  s. c  o  m
 * Copyright (c) 2010 The Jackson Laboratory
 * 
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Arrays;

public class Main {
    /**
     * Calculate a p-value for the data point, given the sorted null
     * distribution sample.
     * @param dataPoint
     *          the data point that we're calculating the p-value for
     * @param sortedNullHypothesisSample
     *          a sample from the null hypothesis
     * @return
     *          the p-value calculated for the given data
     * @throws IllegalArgumentException
     *          if the given sample is empty
     */
    public static double calculatePValueForDataPoint(double dataPoint, double[] sortedNullHypothesisSample)
            throws IllegalArgumentException {
        // TODO validate me
        if (sortedNullHypothesisSample.length == 0) {
            throw new IllegalArgumentException("can't calculate a p-value with zero permutations");
        } else {
            int searchResult = Arrays.binarySearch(sortedNullHypothesisSample, dataPoint);

            int permutationIndexMarker;
            if (searchResult < 1) {
                // the index wasn't found, but we got an insertion point... we just
                // need to massage it a little to turn it into a permutation
                // index
                permutationIndexMarker = (-searchResult) - 1;
            } else {
                // we found the index of a match
                permutationIndexMarker = searchResult;

                // if there are multiple matches we need the index of the
                // 1st one
                while (permutationIndexMarker > 0
                        && sortedNullHypothesisSample[permutationIndexMarker - 1] == dataPoint) {
                    permutationIndexMarker--;
                }
            }

            int numPermutationsGreaterThanOrEqualToSamplePoint = sortedNullHypothesisSample.length
                    - permutationIndexMarker;
            return numPermutationsGreaterThanOrEqualToSamplePoint / (double) sortedNullHypothesisSample.length;
        }
    }
}

Related

  1. addSorted(int[] array, int[] newValues)
  2. addSortedUnique(T[] array, T object, int start)
  3. addToSortedIntArray(int[] a, int value)
  4. bitReversalSort(final short[] real)
  5. collectionToSortedArray(Collection objects)
  6. copyAndSort(String[] input)
  7. copyAndSort(T[] builtinFunctions)
  8. copySortArray(String[] values)