Class resample.ResampleVector
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class resample.ResampleVector

java.lang.Object
   |
   +----resample.ResampleVector

public class ResampleVector
extends Object
implements Cloneable
This class is a modification of the original java.util.vector class. The interface is a superset of the modified class. The only limitation to the original class is that only vector elements of type 'double' are accepted because for resampling statistics it's not necessary to have objects as vector elements.

ResampleVector class (a growable array).

Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as elements are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. Setting the capacity to what you want before inserting a large number of objects will reduce the amount of incremental reallocation. You can safely ignore the capacity and the vector will still work correctly.


Variable Index

 o capacityIncrement
The size of the increment.
 o description
The description for the vector (optional).
 o elementCount
The number of elements in the buffer.
 o elementData
The buffer where elements are stored.
 o RVDescription
 o RVEqual
 o RVGreater
 o RVGreaterEqual
 o RVLess
 o RVLessEqual

Constructor Index

 o ResampleVector()
Constructs an empty vector.
 o ResampleVector(int)
Constructs an empty vector with the specified storage capacity.
 o ResampleVector(int, int)
Constructs an empty vector with the specified storage capacity and the specified capacityIncrement.

Method Index

 o abs()
Converts all vector elements to their absolute values
 o add(double)
Adds to every vector element the given value
 o add(ResampleVector)
Adds to every vector element the corresponding parameter vector element.
 o addElement(double)
Adds the specified object as the last element of the vector.
 o capacity()
Returns the current capacity of the vector.
 o clear()
Clears the vector
 o clone()
Clones the vector.
 o concat(ResampleVector)
Appends the parameter vector to the original vector
 o contains(double)
Returns true if the specified element is a value of the collection.
 o copy()
Clones the vector.
 o copyInto(double[])
Copies the elements of this vector into the specified array.
 o count(int, double)
Returns the number of elements with a certain [value].
 o count(RVPredicate)
Returns the number of elements which fullfill [predicate].
 o divide(double)
Divides every vector element by the given value
 o divide(ResampleVector)
Divides every vector element by the corresponding parameter vector element.
 o elementAt(int)
Returns the element at the specified index.
 o elements()
Returns an enumeration of the elements.
 o ensureCapacity(int)
Ensures that the vector has at least the specified capacity.
 o filter(int, double)
Transfers only certain elements from the original vector into a new vector.
 o filter(RVPredicate)
Transfers only certain elements from the original vector into a new vector.
 o firstElement()
Returns the first element of the sequence.
 o generate(int, int, int)
Enters [count] random integer numbers into the vector starting at element 0.
 o generate(int, int, int, int)
Enters [count] random integer numbers into the vector.
 o generate(int, RVRandomProcess)
Enters [count] random numbers into the vector starting at element 0.
 o generate(int, RVRandomProcess, int)
Enters [count] random numbers into the vector .
 o getChiSquare(int, int)
Returns the chi-square-value of this vector compared to a uniform non-negative (!) integer distribution of interval [min,max].
 o getChiSquare(ResampleVector)
Compares two vectors and returns the chi-square-value.
 o getDescription()
Reads the description of the vector.
 o getPredicate(int, double)
 o indexOf(double)
Searches for the specified object, starting from the first position and returns an index to it.
 o indexOf(double, int)
Searches for the specified element, starting at the specified position and returns an index to it.
 o insertElementAt(double, int)
Inserts the specified element at the specified index.
 o isEmpty()
Returns true if the collection contains no values.
 o isSorted()
Checks if the vector is sorted
 o lastElement()
Returns the last element of the sequence.
 o lastIndexOf(double)
Searches backwards for the specified element, starting from the last position and returns an index to it.
 o lastIndexOf(double, int)
Searches backwards for the specified element, starting from the specified position and returns an index to it.
 o max()
Returns the maximum of all elements (0 if the vector is empty).
 o mean()
Returns the mean of all elements (0 if the vector is empty).
 o min()
Returns the minimum of all elements (0 if the vector is empty).
 o multiples(int, int)
Returns the number of [value]-multiples.
 o multiples(RVPredicate)
Returns the number of [predicate]-multiples.
 o multiply(double)
Multiplies every vector element by the given value
 o multiply(ResampleVector)
Multiplies every vector element with the corresponding parameter vector element.
 o numbers(int, int)
Enters ascending numbers into the vector starting at the first vector element.
 o numbers(int, int, int)
Enters ascending numbers into the vector
 o quantile(double)
Computes q'th quantile of distribution represented by ResamplingVector.
 o quantileWhere(double)
Inverse quantile: given x, computes q with quantile(q) = x.
 o read(InputStream)
Reads the vector data from an input stream (console, file or something else).
 o removeAllElements()
Removes all elements of the vector.
 o removeElement(double)
Removes the element from the vector.
 o removeElementAt(int)
Deletes the element at the specified index.
 o runs(int, int)
Returns the number of clusters of a certain length.
 o runs(RVPredicate)
Returns the number of clusters of a certain length.
 o sample(int)
Draws a sample of the vector.
 o score(double)
Adds [value] as element to the vector
 o scoreCounts(ResampleVector)
Scores for each element of the parameter vector the number of occurrences into the object vector.
 o set(double[])
Initializes the vector with a value-array starting at first vector element
 o set(double[], int)
Initializes the vector with a value-array.
 o set(int, double)
Enters [count] times [value] into the vector starting at the first vector element
 o set(int, double, int)
Enters [count] times [value] into the vector.
 o setDescription(String)
Sets the description of the vector.
 o setElementAt(double, int)
Sets the element at the specified index to be the specified object.
 o setSize(int)
Sets the size of the vector.
 o shuffle()
Shuffles the vector.
 o size()
Returns the number of elements in the vector.
 o sort()
Sorts the vector.
 o subtract(double)
Subtracts from every vector element the given value.
 o subtract(ResampleVector)
Subtracts from every vector element the corresponding parameter vector element.
 o sum()
Returns the sum over all elements (0 if the vector is empty).
 o take(int, int)
Draws the elements of interval [rangeLeft,rangeRight] of the vector.
 o toString()
Converts the vector to a string.
 o trimToSize()
Trims the vector's capacity down to size.
 o write(OutputStream)
Writes the vector data into an output stream (console, file or something else).
 o write(OutputStream, int)
See write-method above.

Variables

 o RVEqual
  public final static int RVEqual
 o RVGreater
  public final static int RVGreater
 o RVGreaterEqual
  public final static int RVGreaterEqual
 o RVLess
  public final static int RVLess
 o RVLessEqual
  public final static int RVLessEqual
 o RVDescription
  public final static String RVDescription
 o elementData
  protected double elementData[]
The buffer where elements are stored.
 o elementCount
  protected int elementCount
The number of elements in the buffer.
 o capacityIncrement
  protected int capacityIncrement
The size of the increment. If it is 0 the size of the the buffer is doubled everytime it needs to grow.
 o description
  protected String description
The description for the vector (optional). It can be used for the representation of the vector in a histogram.

Constructors

 o ResampleVector
  public ResampleVector(int initialCapacity,
                        int capacityIncrement)
Constructs an empty vector with the specified storage capacity and the specified capacityIncrement.
Parameters:
initialCapacity - the initial storage capacity of the vector
capacityIncrement - how much to increase the element's size by.
 o ResampleVector
  public ResampleVector(int initialCapacity)
Constructs an empty vector with the specified storage capacity.
Parameters:
initialCapacity - the initial storage capacity of the vector
 o ResampleVector
  public ResampleVector()
Constructs an empty vector.

Methods

 o addElement
  public final void addElement(double elem)
Adds the specified object as the last element of the vector.
Parameters:
elem - the element to be added
 o capacity
  public final int capacity()
Returns the current capacity of the vector.
 o clone
  public Object clone()
Clones the vector. All elements are cloned.
Overrides:
clone in class Object
 o contains
  public final boolean contains(double elem)
Returns true if the specified element is a value of the collection.
Parameters:
elem - the desired element
 o copyInto
  public final void copyInto(double anArray[])
Copies the elements of this vector into the specified array.
Parameters:
anArray - the array where elements get copied into
 o elementAt
  public final double elementAt(int index)
Returns the element at the specified index.
Parameters:
index - the index of the desired element
Throws: ArrayIndexOutOfBoundsException
If an invalid index was given.
 o elements
  public final Enumeration elements()
Returns an enumeration of the elements. Use the Enumeration methods on the returned object to fetch the elements sequentially.
 o ensureCapacity
  public final void ensureCapacity(int minCapacity)
Ensures that the vector has at least the specified capacity.
Parameters:
minCapacity - the desired minimum capacity
 o firstElement
  public final double firstElement()
Returns the first element of the sequence.
Throws: NoSuchElementException
if the sequence is empty.
 o indexOf
  public final int indexOf(double elem)
Searches for the specified object, starting from the first position and returns an index to it.
Parameters:
elem - the desired element
Returns:
the index of the element, or -1 if it was not found.
 o indexOf
  public final int indexOf(double elem,
                           int index)
Searches for the specified element, starting at the specified position and returns an index to it.
Parameters:
elem - the desired element
index - the index where to start searching
Returns:
the index of the element, or -1 if it was not found.
 o insertElementAt
  public final void insertElementAt(double elem,
                                    int index)
Inserts the specified element at the specified index. Elements with an index greater or equal to the current index are shifted up.
Parameters:
elem - the element to insert
index - where to insert the new element
Throws: ArrayIndexOutOfBoundsException
If the index was invalid.
 o isEmpty
  public final boolean isEmpty()
Returns true if the collection contains no values.
 o lastElement
  public final double lastElement()
Returns the last element of the sequence.
Throws: NoSuchElementException
If the sequence is empty.
 o lastIndexOf
  public final int lastIndexOf(double elem)
Searches backwards for the specified element, starting from the last position and returns an index to it.
Parameters:
elem - the desired element
Returns:
the index of the element, or -1 if it was not found.
 o lastIndexOf
  public final int lastIndexOf(double elem,
                               int index)
Searches backwards for the specified element, starting from the specified position and returns an index to it.
Parameters:
elem - the desired element
index - the index where to start searching
Returns:
the index of the element, or -1 if it was not found.
 o removeAllElements
  public final void removeAllElements()
Removes all elements of the vector. The vector becomes empty.
 o removeElement
  public final boolean removeElement(double elem)
Removes the element from the vector. If the element occurs more than once, only the first is removed. If the element is not in the vector, returns false.
Parameters:
elem - the element to be removed
Returns:
true if the element was actually removed; false otherwise.
 o removeElementAt
  public final void removeElementAt(int index)
Deletes the element at the specified index. Elements with an index greater than the current index are moved down.
Parameters:
index - the element to remove
Throws: ArrayIndexOutOfBoundsException
If the index was invalid.
 o setElementAt
  public final void setElementAt(double elem,
                                 int index)
Sets the element at the specified index to be the specified object. The previous element at that position is discarded.
Parameters:
elem - what the element is to be set to
index - the specified index
Throws: ArrayIndexOutOfBoundsException
If the index was invalid.
 o setSize
  public final void setSize(int newSize)
Sets the size of the vector. If the size shrinks, the extra elements (at the end of the vector) are lost; if the size increases, the new elements are set to 0.
Parameters:
newSize - the new size of the vector
 o size
  public final int size()
Returns the number of elements in the vector. Note that this is not the same as the vector's capacity.
 o toString
  public final String toString()
Converts the vector to a string. Useful for debugging.
Overrides:
toString in class Object
 o trimToSize
  public final void trimToSize()
Trims the vector's capacity down to size. Use this operation to minimize the storage of a vector. Subsequent insertions will cause reallocation.
 o take
  public ResampleVector take(int rangeLeft,
                             int rangeRight)
Draws the elements of interval [rangeLeft,rangeRight] of the vector. Usually used only after the vector was shuffled. Drawing without replacing. No random-process is used, just the elements of the specified range are drawn. Vector element[0] corresponds to rangeLeft = 0. The original vector is left untouched.
 o sample
  public ResampleVector sample(int count)
Draws a sample of the vector. Drawing with replacing. The original vector is left untouched.
Parameters:
count - the number of elements to be drawn
 o filter
  public ResampleVector filter(int compareMode,
                               double value)
Transfers only certain elements from the original vector into a new vector. The original vector is left untouched.
Example: Filter (RVGreaterEqual,2) transfers elements with value greater or equal 2 into a new vector.
The original vector is left untouched. Use this method if your predicate is simple like (>= 5) for example
Parameters:
compareMode - can be of the RV-constants declared in the ResampleVector class header
 o filter
  public ResampleVector filter(RVPredicate pred)
Transfers only certain elements from the original vector into a new vector. The original vector is left untouched. The method contains the simpler predicates of the preceeding method as special cases
Parameters:
pred - an object of class RVPredicate which defines a predicate against which the vector elements are sequentially matched. If the predicate is fullfilled, the element is added to the new vector .
 o divide
  public void divide(double value)
Divides every vector element by the given value
 o divide
  public void divide(ResampleVector resVec)
Divides every vector element by the corresponding parameter vector element. The vector is modified only at those positions which contain valid entries in both vectors (no error occurs if the vectors are not of the same size) !
 o multiply
  public void multiply(double value)
Multiplies every vector element by the given value
 o multiply
  public void multiply(ResampleVector resVec)
Multiplies every vector element with the corresponding parameter vector element. The vector is modified only at those positions which contain valid entries in both vectors (no error occurs if the vectors are not of the same size) !
 o add
  public void add(double value)
Adds to every vector element the given value
 o add
  public void add(ResampleVector resVec)
Adds to every vector element the corresponding parameter vector element. The vector is modified only at those positions which contain valid entries in both vectors (no error occurs if the vectors are not of the same size) !
 o subtract
  public void subtract(double value)
Subtracts from every vector element the given value.
 o subtract
  public void subtract(ResampleVector resVec)
Subtracts from every vector element the corresponding parameter vector element. The vector is modified only at those positions which contain valid entries in both vectors (no error occurs if the vectors are not of the same size) !
 o sort
  public void sort()
Sorts the vector.
 o shuffle
  public void shuffle()
Shuffles the vector.
 o abs
  public void abs()
Converts all vector elements to their absolute values
 o scoreCounts
  public void scoreCounts(ResampleVector resVecData)
Scores for each element of the parameter vector the number of occurrences into the object vector. The old value is increased, not overwritten, so that the method can be called more than once for a running program (see ResampleTest.testRandom as example). The object vector can then be used for performing a chi-square-test on the data.

Object vector element i is increased by the number of occurrences of element i in the parameter vector. Suitable only for vectors with non-negative integer values !

 o numbers
  public void numbers(int min,
                      int max)
Enters ascending numbers into the vector starting at the first vector element.
Parameters:
min - the lowest value
max - the highest value
 o numbers
  public void numbers(int min,
                      int max,
                      int index)
Enters ascending numbers into the vector
Parameters:
min - the first value
max - the last value
index - the starting position in the vector. Index must be in the range of 0 and size(), otherwise we got empty gaps of undefined values
 o set
  public void set(int count,
                  double value)
Enters [count] times [value] into the vector starting at the first vector element
 o set
  public void set(int count,
                  double value,
                  int index)
Enters [count] times [value] into the vector.
Parameters:
index - the starting position in the vector. Index must be in the range of 0 and size(), otherwise we got empty gaps of undefined values
 o set
  public void set(double valueArray[])
Initializes the vector with a value-array starting at first vector element
 o set
  public void set(double valueArray[],
                  int index)
Initializes the vector with a value-array.
Parameters:
index - the starting position in the vector. Index must be in the range of 0 and size(), otherwise we got empty gaps of undefined values
 o generate
  public void generate(int count,
                       int min,
                       int max)
Enters [count] random integer numbers into the vector starting at element 0. The numbers are in the range of [min,max]
 o generate
  public void generate(int count,
                       int min,
                       int max,
                       int index)
Enters [count] random integer numbers into the vector. The numbers are in the range of [min,max]
Parameters:
index - the starting position in the vector. Index must be in the range of 0 and size(), otherwise we got empty gaps of undefined values
 o generate
  public void generate(int count,
                       RVRandomProcess randomProcess)
Enters [count] random numbers into the vector starting at element 0. RandomProcess is an instance of a subclass of RVRandomProcess. According to the subclass-type, the corresponding nextNumber-Method is called. The range for the random-numbers was specified when creating the RandomProcess-instance
 o generate
  public void generate(int count,
                       RVRandomProcess randomProcess,
                       int index)
Enters [count] random numbers into the vector . RandomProcess is an instance of a subclass of RVRandomProcess According to the subclass-type, the corresponding nextNumber-Method is called The range for the random-numbers was specified when creating the RandomProcess-instance
Parameters:
index - the starting position in the vector. Index must be in the range of 0 and size(), otherwise we got empty gaps of undefined values
 o clear
  public void clear()
Clears the vector
 o sum
  public double sum()
Returns the sum over all elements (0 if the vector is empty).
 o mean
  public double mean()
Returns the mean of all elements (0 if the vector is empty).
 o max
  public double max()
Returns the maximum of all elements (0 if the vector is empty).
 o min
  public double min()
Returns the minimum of all elements (0 if the vector is empty).
 o multiples
  public int multiples(int compareMode,
                       int value)
Returns the number of [value]-multiples.

Example: multiples (RVEqual,3) returns the number of tripels in the vector. Vector (2 3 2 1 2 3 1) contains 2 pairs and 1 tripel. Multiples (RVGreaterEqual,2) in this case would return value 3.

Use this method if your predicate is simple like (>= 5) for example

Parameters:
compareMode - can be of the RV-constants declared in the ResampleVector class header
 o multiples
  public int multiples(RVPredicate pred)
Returns the number of [predicate]-multiples.

Example: multiples (RVPredicateEvenNumbers) would return the sum of 2-tupels, 4-tupels... Vector (1 3 2 2 2 3 1 2) contains 2 pairs and 1 quadrupel. Multiples (RVPredicateEvenNumbers) in this case would return 2+1 = 3.

The method contains the simpler predicates of the preceeding method as special cases.

 o count
  public int count(int compareMode,
                   double value)
Returns the number of elements with a certain [value].

Example: multiples (RVEqual,3) returns the number of elements with value 3. Use this method if your predicate is simple like (>= 5) for example

Parameters:
compareMode - can be of the RV-constants declared in the ResampleVector class header
 o count
  public int count(RVPredicate pred)
Returns the number of elements which fullfill [predicate].

Example: count (RVPredicateEvenNumbers) returns the number of elements with even value

Use this method for complex predicates which can't be handled by the preceeding method

 o runs
  public int runs(int compareMode,
                  int value)
Returns the number of clusters of a certain length.

Example: runs (RVGreaterEqual,3) returns the number of clusters with length >= 3. Use this method if your predicate is simple like (>= 5) for example

Parameters:
compareMode - can be of the RV-constants declared in the ResampleVector class header
 o runs
  public int runs(RVPredicate pred)
Returns the number of clusters of a certain length.

Example: runs (RVPredicateEvenNumbers) returns the number of clusters with even length. Vector (2 2 2 2 1 3 3 1 2) contains 1 cluster of length 2 and 1 cluster of length 4. Multiples (RVPredicateEvenNumbers) in this case would return 1+1 = 2.

This method contains the simpler predicates of the preceeding method as special cases

 o concat
  public void concat(ResampleVector resVec)
Appends the parameter vector to the original vector
 o score
  public void score(double value)
Adds [value] as element to the vector
 o copy
  public ResampleVector copy()
Clones the vector. Same as clone(), but you don't need to convert the result explicitly in ResampleVector
 o getPredicate
  protected static RVPredicate getPredicate(int compareMode,
                                            double value)
 o read
  public void read(InputStream input) throws IOException
Reads the vector data from an input stream (console, file or something else). One line of input = one vector. The method reads plain ascii text of the form: [data1 delimiter data2]... The delimiters can be chosen freely for example: [1/4/8] or [2,5,7,2] or [2 4 5 3]
Lines beginning with // are treated as comments and are ignored. Same with /* [..] * / C-like comment-blocks
It is possible to add a description at the end of the vector line. It has to be of the form D: description. Example:
[1,4,7,8,23] D: 5 drawn cards from a poker set
Performace warning: as no buffer can be used, the method is not very fast.
 o write
  public void write(OutputStream output) throws IOException
Writes the vector data into an output stream (console, file or something else). One line of output = one vector. The method writes plain ascii text of the form: data1, data2... If there exists a description for the vector, it is written at the end of the line in the form D description. Example:
1, 4, 7, 8, 23 D vector of 5 drawn cards from a poker set
The information about capacity and capacityIncrement is not saved to keep the output format simple. The user can specify this information in the vector-constructor before reading data in it.
A buffer size of 1024 bytes is used. If you need another buffer size, use the following method.
Performace warning: as all vector elements have to be converted to strings, the method is not very fast.
Parameters:
output - the output stream
 o write
  public void write(OutputStream output,
                    int bufSize) throws IOException
See write-method above.
Parameters:
bufSize - the size of the buffer
 o getDescription
  public String getDescription()
Reads the description of the vector.
 o setDescription
  public void setDescription(String description)
Sets the description of the vector.
 o isSorted
  public boolean isSorted()
Checks if the vector is sorted
 o getChiSquare
  public double getChiSquare(ResampleVector resVecExpect)
Compares two vectors and returns the chi-square-value. Suitable for testing whether the result vector was sufficiently random. Compared should be the two vectors containing the theoretical and the actual counts of the elements ! If the vectors are not of equal size, the rest of the greater one is ignored.

Vector element i contains the count of element i

If your distribution is uniform, use method getChiSquare (min,max) instead, you can apply that method to the data vector itself and don't have to calculate the two count-vectors.

Reference: Knuth ACP section 3.3.1 p.35-40

Parameters:
resVecExpect - the theoretical distribution
 o getChiSquare
  public double getChiSquare(int min,
                             int max)
Returns the chi-square-value of this vector compared to a uniform non-negative (!) integer distribution of interval [min,max]. Returns -1 if some of the vector data are not in the interval

Reference: Knuth ACP section 3.3.1 p.35-40

 o quantile
  public double quantile(double q)
Computes q'th quantile of distribution represented by ResamplingVector. The vector must be sorted in increasing order. The quantile is computed by a linear interpolation between the neighboring vector elements. q must be in the range 0...1.
 o quantileWhere
  public double quantileWhere(double x)
Inverse quantile: given x, computes q with quantile(q) = x. The vector must be sorted in increasing order. Will return 0 or 1 for values outside the vector's range.

All Packages  Class Hierarchy  This Package  Previous  Next  Index