edu.neu.ccs.util
Class ProbStatTools

java.lang.Object
  extended byedu.neu.ccs.util.ProbStatTools

public class ProbStatTools
extends Object

Class ProbStatTools collects static methods that are useful for randomization, probability, and statistics.

In 2.6.0b, added tools to compute the number of reversals in a data list and the sign of a data list viewed as a permutation. Also, added methods to perform random even permutations that are similar to the earlier methods for general random permutations. Finally, added methods to compute the next permutation of a list in lexicographic order or the previous permuation. These methods allow one to find all permutations of a list by proceeding in both directions from the given list.

Since:
2.3.3
Version:
2.6.0b

Constructor Summary
private ProbStatTools()
          Prevent instantiation.
 
Method Summary
static int[] integerSequence(int n)
          Returns an array with n integers in sequence: 0, 1, etc, up to (n-1).
static int[] integerSequence(int n, int start)
          Returns an array with n integers in sequence: start, (start+1), etc, up to (start+n-1).
static int[] nextList(int[] data)
          Returns the next list in lexicographic order that follows this list and has the same data elements.
static int[] previousList(int[] data)
          Returns the previous list in lexicographic order that precedes this list and has the same data elements.
static int[] randomEvenPermutation(int n)
          Returns an array whose contents is a random even permutation of the integers from 0 to (n-1).
static int[] randomEvenPermutation(int[] data)
          Returns an array whose contents is a random even permutation of the contents in the data array.
static int[] randomEvenPermutation(int n, int start)
          Returns an array whose contents is a random even permutation of the integers from start to (start+n-1).
static void randomEvenPermutationInSitu(int[] data)
          Rearranges the contents of the given array using a random even permutation.
static int[] randomPermutation(int n)
          Returns an array whose contents is a random permutation of the integers from 0 to (n-1).
static int[] randomPermutation(int[] data)
          Returns an array whose contents is a random permutation of the contents in the data array.
static int[] randomPermutation(int n, int start)
          Returns an array whose contents is a random permutation of the integers from start to (start+n-1).
static void randomPermutationInSitu(int[] data)
          Rearranges the contents of the data array using a random permutation.
static int[] repeatData(int[] data, int k)
          Returns an int array that repeats the data in the given array sequentially k times.
static int reversals(int[] data)
          Returns the number of reversals of the array viewed as a permutation of its data.
static int[] selectWithNoRepetition(int[] data, int k)
          Returns an array whose contents is a random selection of k elements from the contents of the given data with no repetition of a selected index.
static int[] selectWithNoRepetition(int n, int k)
          Returns an array of length k consisting of integers chosen from 0 to (n-1) with no repetition permitted.
static int[] selectWithRepetition(int[] data, int k)
          Returns an array whose contents is a random selection of k elements from the contents of the given data with repetition of a selected index permitted.
static int[] selectWithRepetition(int n, int k)
          Returns an array of length k consisting of integers chosen from 0 to (n-1) with repetition permitted.
static int sign(int[] data)
          Returns the sign of the array viewed as a permutation of its data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProbStatTools

private ProbStatTools()
Prevent instantiation.

Method Detail

integerSequence

public static int[] integerSequence(int n)

Returns an array with n integers in sequence: 0, 1, etc, up to (n-1).

If n is less than or equal to zero, then returns an empty array of integers.


integerSequence

public static int[] integerSequence(int n,
                                    int start)

Returns an array with n integers in sequence: start, (start+1), etc, up to (start+n-1).

If n is less than or equal to zero, then returns an empty array of integers.


randomPermutation

public static int[] randomPermutation(int n)

Returns an array whose contents is a random permutation of the integers from 0 to (n-1).

If n is less than or equal to zero, then returns an empty array of integers.


randomPermutation

public static int[] randomPermutation(int n,
                                      int start)

Returns an array whose contents is a random permutation of the integers from start to (start+n-1).

If n is less than or equal to zero, then returns an empty array of integers.


randomPermutation

public static int[] randomPermutation(int[] data)

Returns an array whose contents is a random permutation of the contents in the data array.

The data array is unchanged.

If the data array is null, then returns an empty array of integers.


randomPermutationInSitu

public static void randomPermutationInSitu(int[] data)

Rearranges the contents of the data array using a random permutation.

If the data array is null or has length less than 2, then does nothing.


randomEvenPermutation

public static int[] randomEvenPermutation(int n)

Returns an array whose contents is a random even permutation of the integers from 0 to (n-1).

If n is less than or equal to zero, then returns an empty array of integers.


randomEvenPermutation

public static int[] randomEvenPermutation(int n,
                                          int start)

Returns an array whose contents is a random even permutation of the integers from start to (start+n-1).

If n is less than or equal to zero, then returns an empty array of integers.


randomEvenPermutation

public static int[] randomEvenPermutation(int[] data)

Returns an array whose contents is a random even permutation of the contents in the data array.

The data array is unchanged.

If the data array is null, then returns an empty array of integers.


randomEvenPermutationInSitu

public static void randomEvenPermutationInSitu(int[] data)

Rearranges the contents of the given array using a random even permutation.

If the data array is null or has length less than 3, then does nothing.


reversals

public static int reversals(int[] data)

Returns the number of reversals of the array viewed as a permutation of its data.

If the array is null or contains repeated elements, then returns -1 to signal an error.

If all elements in the array are distinct, then counts the number k of element pairs (i,j) where:

    i < j    but    data[i] > data[j]

Then returns k.


sign

public static int sign(int[] data)

Returns the sign of the array viewed as a permutation of its data.

Computes k = reversals(data).

If k is -1, returns 0 to signal an error. Otherwise returns 1 if k is even and -1 if k is odd.


nextList

public static int[] nextList(int[] data)

Returns the next list in lexicographic order that follows this list and has the same data elements.

Returns null if this list is the last list in lexicographic order.

This method works correctly for lists that have duplicate elements.

This method may be used as a tool when enumerating the permutations of a list.


previousList

public static int[] previousList(int[] data)

Returns the previous list in lexicographic order that precedes this list and has the same data elements.

Returns null if this list is the first list in lexicographic order.

This method works correctly for lists that have duplicate elements.

This method may be used as a tool when enumerating the permutations of a list.


selectWithRepetition

public static int[] selectWithRepetition(int n,
                                         int k)

Returns an array of length k consisting of integers chosen from 0 to (n-1) with repetition permitted.

If n or k is less than or equal to zero, then returns an empty array of integers.


selectWithNoRepetition

public static int[] selectWithNoRepetition(int n,
                                           int k)

Returns an array of length k consisting of integers chosen from 0 to (n-1) with no repetition permitted.

If k is less than or equal to zero or greater than n, then returns an empty array of integers.


selectWithRepetition

public static int[] selectWithRepetition(int[] data,
                                         int k)

Returns an array whose contents is a random selection of k elements from the contents of the given data with repetition of a selected index permitted.

The data array is unchanged.

If the data array is null or if k is less than or equal to zero, then returns an empty array of integers.


selectWithNoRepetition

public static int[] selectWithNoRepetition(int[] data,
                                           int k)

Returns an array whose contents is a random selection of k elements from the contents of the given data with no repetition of a selected index.

The data array is unchanged.

If the data array is null or if k is less than or equal to zero or if k is greater than the data array size, then returns an empty array of integers.


repeatData

public static int[] repeatData(int[] data,
                               int k)

Returns an int array that repeats the data in the given array sequentially k times.

If the data array is null or if k is less than or equal to zero, then returns an empty array of integers.

If k equals one, then copies the given data.