Class ArithmeticUtils


  • public final class ArithmeticUtils
    extends Object
    Arithmetics and math utilities
    Author:
    Eyal Schneider
    • Method Detail

      • getUnionProbability

        public static double getUnionProbability​(List<Double> probabilities)

        Uses the inclusion-exclusion principle to calculate the probability of a union of independent events Ei, each having a probability Pi (P(E1-n) = P1 +P2 + Pn -P1P2 -P2P3 -P1P3 ... +P1P2P3 +... - The solution is adding all the odd combination of the probabilities and minus all the even combination of the probabilities For more info check https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle

        Note : the algorithm is exponential and not suitable for large lists
        Parameters:
        probabilities - a list of probabilities if this is an empty list 0 will return
        Returns:
        probability of the union of the given probabilities
      • getConsistentBooleanByProbability

        public static boolean getConsistentBooleanByProbability​(Object object,
                                                                float probability)
        Get consistent boolean value (accepted/rejected) by a given probability. The consistency is based on a hash value of the given object.
        Parameters:
        object - The object to either accept or reject
        probability - The probability for the object to be accepted
      • getShardByHash

        public static int getShardByHash​(Object obj,
                                         int n)
        Parameters:
        obj - A non-null object
        n - The total number of shards
        Returns:
        The (deterministic) shard the object belongs to, in the range 0 ... n - 1. Assuming a good hash function, and a large enough population of objects the shards should tend to even sizes.
      • getShardByStrongHash

        public static int getShardByStrongHash​(byte[] objBytes,
                                               int n)
        Similar to getShardByHash, but uses 64 bit hash and a stronger hashing algorithm and to achieve better balancing of shards
        Parameters:
        objBytes - The object to hash, represented as a byte array
        n - The total number of shards
        Returns:
        The (deterministic) shard the object belongs to, in the range 0 ... n - 1.