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.
-
capacityIncrement
- The size of the increment.
-
description
- The description for the vector (optional).
-
elementCount
- The number of elements in the buffer.
-
elementData
- The buffer where elements are stored.
-
RVDescription
-
-
RVEqual
-
-
RVGreater
-
-
RVGreaterEqual
-
-
RVLess
-
-
RVLessEqual
-
-
ResampleVector()
- Constructs an empty vector.
-
ResampleVector(int)
- Constructs an empty vector with the specified storage capacity.
-
ResampleVector(int, int)
- Constructs an empty vector with the specified storage
capacity and the specified capacityIncrement.
-
abs()
- Converts all vector elements to their absolute values
-
add(double)
- Adds to every vector element the given value
-
add(ResampleVector)
- Adds to every vector element the corresponding parameter vector element.
-
addElement(double)
- Adds the specified object as the last element of the vector.
-
capacity()
- Returns the current capacity of the vector.
-
clear()
- Clears the vector
-
clone()
- Clones the vector.
-
concat(ResampleVector)
- Appends the parameter vector to the original vector
-
contains(double)
- Returns true if the specified element is a value of the
collection.
-
copy()
- Clones the vector.
-
copyInto(double[])
- Copies the elements of this vector into the specified array.
-
count(int, double)
-
Returns the number of elements with a certain [value].
-
count(RVPredicate)
-
Returns the number of elements which fullfill [predicate].
-
divide(double)
- Divides every vector element by the given value
-
divide(ResampleVector)
- Divides every vector element by the corresponding parameter vector element.
-
elementAt(int)
- Returns the element at the specified index.
-
elements()
- Returns an enumeration of the elements.
-
ensureCapacity(int)
- Ensures that the vector has at least the specified capacity.
-
filter(int, double)
- Transfers only certain elements from the original vector into a new vector.
-
filter(RVPredicate)
- Transfers only certain elements from the original vector into a new vector.
-
firstElement()
- Returns the first element of the sequence.
-
generate(int, int, int)
- Enters [count] random integer numbers into the vector starting
at element 0.
-
generate(int, int, int, int)
- Enters [count] random integer numbers into the vector.
-
generate(int, RVRandomProcess)
- Enters [count] random numbers into the vector starting at element 0.
-
generate(int, RVRandomProcess, int)
- Enters [count] random numbers into the vector .
-
getChiSquare(int, int)
- Returns the chi-square-value of this vector compared to a
uniform non-negative (!) integer distribution of interval [min,max].
-
getChiSquare(ResampleVector)
- Compares two vectors and returns the chi-square-value.
-
getDescription()
- Reads the description of the vector.
-
getPredicate(int, double)
-
-
indexOf(double)
- Searches for the specified object, starting from the first position
and returns an index to it.
-
indexOf(double, int)
- Searches for the specified element, starting at the specified
position and returns an index to it.
-
insertElementAt(double, int)
- Inserts the specified element at the specified index.
-
isEmpty()
- Returns true if the collection contains no values.
-
isSorted()
- Checks if the vector is sorted
-
lastElement()
- Returns the last element of the sequence.
-
lastIndexOf(double)
- Searches backwards for the specified element, starting from the last
position and returns an index to it.
-
lastIndexOf(double, int)
- Searches backwards for the specified element, starting from the specified
position and returns an index to it.
-
max()
- Returns the maximum of all elements (0 if the vector is empty).
-
mean()
- Returns the mean of all elements (0 if the vector is empty).
-
min()
- Returns the minimum of all elements (0 if the vector is empty).
-
multiples(int, int)
-
Returns the number of [value]-multiples.
-
multiples(RVPredicate)
-
Returns the number of [predicate]-multiples.
-
multiply(double)
- Multiplies every vector element by the given value
-
multiply(ResampleVector)
- Multiplies every vector element with the corresponding parameter vector element.
-
numbers(int, int)
- Enters ascending numbers into the vector starting
at the first vector element.
-
numbers(int, int, int)
- Enters ascending numbers into the vector
-
quantile(double)
- Computes q'th quantile of distribution represented by ResamplingVector.
-
quantileWhere(double)
- Inverse quantile: given x, computes q with quantile(q) = x.
-
read(InputStream)
- Reads the vector data from an input stream (console, file or
something else).
-
removeAllElements()
- Removes all elements of the vector.
-
removeElement(double)
- Removes the element from the vector.
-
removeElementAt(int)
- Deletes the element at the specified index.
-
runs(int, int)
-
Returns the number of clusters of a certain length.
-
runs(RVPredicate)
-
Returns the number of clusters of a certain length.
-
sample(int)
- Draws a sample of the vector.
-
score(double)
- Adds [value] as element to the vector
-
scoreCounts(ResampleVector)
- Scores for each element of the parameter vector the number of
occurrences into the object vector.
-
set(double[])
-
Initializes the vector with a value-array starting at first vector element
-
set(double[], int)
-
Initializes the vector with a value-array.
-
set(int, double)
- Enters [count] times [value] into the vector starting
at the first vector element
-
set(int, double, int)
- Enters [count] times [value] into the vector.
-
setDescription(String)
- Sets the description of the vector.
-
setElementAt(double, int)
- Sets the element at the specified index to be the specified object.
-
setSize(int)
- Sets the size of the vector.
-
shuffle()
- Shuffles the vector.
-
size()
- Returns the number of elements in the vector.
-
sort()
- Sorts the vector.
-
subtract(double)
- Subtracts from every vector element the given value.
-
subtract(ResampleVector)
- Subtracts from every vector element the corresponding parameter vector element.
-
sum()
- Returns the sum over all elements (0 if the vector is empty).
-
take(int, int)
- Draws the elements of interval [rangeLeft,rangeRight] of
the vector.
-
toString()
- Converts the vector to a string.
-
trimToSize()
- Trims the vector's capacity down to size.
-
write(OutputStream)
- Writes the vector data into an output stream (console, file or
something else).
-
write(OutputStream, int)
- See write-method above.
RVEqual
public final static int RVEqual
RVGreater
public final static int RVGreater
RVGreaterEqual
public final static int RVGreaterEqual
RVLess
public final static int RVLess
RVLessEqual
public final static int RVLessEqual
RVDescription
public final static String RVDescription
elementData
protected double elementData[]
- The buffer where elements are stored.
elementCount
protected int elementCount
- The number of elements in the buffer.
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.
description
protected String description
- The description for the vector (optional). It can be used for
the representation of the vector in a histogram.
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.
ResampleVector
public ResampleVector(int initialCapacity)
- Constructs an empty vector with the specified storage capacity.
- Parameters:
- initialCapacity - the initial storage capacity of the vector
ResampleVector
public ResampleVector()
- Constructs an empty vector.
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
capacity
public final int capacity()
- Returns the current capacity of the vector.
clone
public Object clone()
- Clones the vector. All elements are cloned.
- Overrides:
- clone in class Object
contains
public final boolean contains(double elem)
- Returns true if the specified element is a value of the
collection.
- Parameters:
- elem - the desired element
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
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.
elements
public final Enumeration elements()
- Returns an enumeration of the elements. Use the Enumeration methods on
the returned object to fetch the elements sequentially.
ensureCapacity
public final void ensureCapacity(int minCapacity)
- Ensures that the vector has at least the specified capacity.
- Parameters:
- minCapacity - the desired minimum capacity
firstElement
public final double firstElement()
- Returns the first element of the sequence.
- Throws: NoSuchElementException
- if the sequence is empty.
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.
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.
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.
isEmpty
public final boolean isEmpty()
- Returns true if the collection contains no values.
lastElement
public final double lastElement()
- Returns the last element of the sequence.
- Throws: NoSuchElementException
- If the sequence is empty.
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.
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.
removeAllElements
public final void removeAllElements()
- Removes all elements of the vector. The vector becomes empty.
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.
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.
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.
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
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.
toString
public final String toString()
- Converts the vector to a string. Useful for debugging.
- Overrides:
- toString in class Object
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.
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.
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
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
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 .
divide
public void divide(double value)
- Divides every vector element by the given value
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) !
multiply
public void multiply(double value)
- Multiplies every vector element by the given value
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) !
add
public void add(double value)
- Adds to every vector element the given value
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) !
subtract
public void subtract(double value)
- Subtracts from every vector element the given value.
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) !
sort
public void sort()
- Sorts the vector.
shuffle
public void shuffle()
- Shuffles the vector.
abs
public void abs()
- Converts all vector elements to their absolute values
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 !
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
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
set
public void set(int count,
double value)
- Enters [count] times [value] into the vector starting
at the first vector element
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
set
public void set(double valueArray[])
- Initializes the vector with a value-array starting at first vector element
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
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]
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
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
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
clear
public void clear()
- Clears the vector
sum
public double sum()
- Returns the sum over all elements (0 if the vector is empty).
mean
public double mean()
- Returns the mean of all elements (0 if the vector is empty).
max
public double max()
- Returns the maximum of all elements (0 if the vector is empty).
min
public double min()
- Returns the minimum of all elements (0 if the vector is empty).
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
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.
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
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
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
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
concat
public void concat(ResampleVector resVec)
- Appends the parameter vector to the original vector
score
public void score(double value)
- Adds [value] as element to the vector
copy
public ResampleVector copy()
- Clones the vector. Same as clone(), but you don't need to
convert the result explicitly in ResampleVector
getPredicate
protected static RVPredicate getPredicate(int compareMode,
double value)
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.
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
write
public void write(OutputStream output,
int bufSize) throws IOException
- See write-method above.
- Parameters:
- bufSize - the size of the buffer
getDescription
public String getDescription()
- Reads the description of the vector.
setDescription
public void setDescription(String description)
- Sets the description of the vector.
isSorted
public boolean isSorted()
- Checks if the vector is sorted
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
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
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.
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