peersim.util
Class WeightedRandPerm

java.lang.Object
  extended by peersim.util.WeightedRandPerm
All Implemented Interfaces:
IndexIterator

public class WeightedRandPerm
extends java.lang.Object
implements IndexIterator

This class provides a weighted random permutation of indexes. Useful for weighted random sampling without replacement. The next sample is taken according to the weights given as a parameter to reset(int). The weights work as follows. The first sample is drawn according to the probability distribution defined by the (normalized) weights. After this the remaining k-1 elements and the associated k-1 (re-normalized) weights define a new probability distribution, according to which the 2nd element is drawn, and so on.


Constructor Summary
WeightedRandPerm(java.util.Random r, double[] weights)
          Set the source of randomness to use and the weights.
 
Method Summary
 boolean hasNext()
          Returns true if IndexIterator.next() can be called at least one more time.
 int next()
          The first sample is drawn according to the probability distribution defined by the (normalized) weights.
 void reset(int k)
          It initiates a random weighted permutation of the integeres from 0 to k-1.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WeightedRandPerm

public WeightedRandPerm(java.util.Random r,
                        double[] weights)
Set the source of randomness to use and the weights. You need to call reset(int) to fully initialize the object.

Parameters:
r - source of randomness
weights - The array that holds the weights for the calculation of the permutation. The length of the array will be an upper bound on the parameter reset(int) accepts. If reset(int) is called with a parameter less than the length of weights, the prefix of the same length is used. The vector elements must be positive, that is, zero is not accepted either.
Method Detail

reset

public void reset(int k)
It initiates a random weighted permutation of the integeres from 0 to k-1. It does not actually calculate the permutation. The permutation can be read using method next(). If the previous permutation was of the same length, it is more efficient. The weights set at construction time work as follows. The first sample is drawn according to the probability distribution defined by the (normalized) weights. After this the remaining k-1 elements and the associated k-1 (re-normalized) weights define a new probability distribution, according to which the 2nd element is drawn, and so on.

Specified by:
reset in interface IndexIterator
Parameters:
k - the set is defined as 0,...,k-1

next

public int next()
The first sample is drawn according to the probability distribution defined by the (normalized) weights. After this the remaining k-1 elements and the associated k-1 (re-normalized) weights define a new probability distribution, according to which the 2nd element is drawn, and so on.

Specified by:
next in interface IndexIterator
See Also:
reset(int)

hasNext

public boolean hasNext()
Description copied from interface: IndexIterator
Returns true if IndexIterator.next() can be called at least one more time. Note that IndexIterator.next() can be called k times after IndexIterator.reset(int).

Specified by:
hasNext in interface IndexIterator