Class Util

java.lang.Object
org.tribuo.util.Util

public final class Util extends Object
Ye olde util class.

Basically full of vector and RNG operations.

  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends Comparable<T>>
    com.oracle.labs.mlrg.olcut.util.Pair<Integer,T>
    argmax(List<T> values)
    Find the index of the maximum value in a list.
    static <T extends Comparable<T>>
    com.oracle.labs.mlrg.olcut.util.Pair<Integer,T>
    argmin(List<T> values)
    Find the index of the minimum value in a list.
    static double
    auc(double[] x, double[] y)
    Calculates the area under the curve, bounded below by the x axis.
    static <T> int
    binarySearch(List<? extends Comparable<? super T>> list, T key)
    A binary search function.
    static <T> int
    binarySearch(List<? extends Comparable<? super T>> list, T key, int low, int high)
    A binary search function.
    static <T> int
    binarySearch(List<? extends T> list, int key, ToIntFunction<T> extractionFunc)
    A binary search function.
    static int[]
    cumulativeSum(boolean[] input)
    Produces a cumulative sum array.
    static double[]
    cumulativeSum(double[] input)
    Produces a cumulative sum array.
    static int[]
    differencesIndices(double[] input)
    Returns an array containing the indices where values are different.
    static int[]
    differencesIndices(double[] input, double tolerance)
    Returns an array containing the indices where values are different.
    static String
    formatDuration(long startMillis, long stopMillis)
    Formats a duration given two times in milliseconds.
    static int[]
    generateBootstrapIndices(int size, int range, SplittableRandom rng)
    Draws a bootstrap sample of indices.
    static int[]
    Draws a bootstrap sample of indices.
    static int[]
    Draws a bootstrap sample of indices.
    static double[]
    generateCDF(double[] pmf)
    Generates a cumulative distribution function from the supplied probability mass function.
    static double[]
    generateCDF(float[] pmf)
    Generates a cumulative distribution function from the supplied probability mass function.
    static double[]
    generateCDF(long[] counts, long countSum)
    Generates a cumulative distribution function from the supplied probability mass function.
    static float[]
    generateUniformFloatVector(int length, float value)
    Generates a float vector of the specified length filled with the specified value.
    static double[]
    generateUniformVector(int length, double value)
    Generates an array of the specified length filled with the specified value.
    static float[]
    generateUniformVector(int length, float value)
    Generates an array of the specified length filled with the specified value.
    static int[]
    generateWeightedIndicesSample(int size, double[] weights, Random rng)
    Generates a sample of indices weighted by the provided weights.
    static int[]
    generateWeightedIndicesSample(int size, double[] weights, SplittableRandom rng)
    Generates a sample of indices weighted by the provided weights.
    static int[]
    generateWeightedIndicesSample(int size, float[] weights, Random rng)
    Generates a sample of indices weighted by the provided weights.
    static int[]
    generateWeightedIndicesSample(int size, float[] weights, SplittableRandom rng)
    Generates a sample of indices weighted by the provided weights.
    static int[]
    generateWeightedIndicesSampleWithoutReplacement(int size, double[] weights, Random rng)
    Generates a sample of indices weighted by the provided weights without replacement.
    static int[]
    generateWeightedIndicesSampleWithoutReplacement(int size, float[] weights, Random rng)
    Generates a sample of indices weighted by the provided weights without replacement.
    static void
    inPlaceAdd(double[] input, double[] update)
    Adds update to input in place.
    static void
    inPlaceAdd(float[] input, float[] update)
    Adds update to input in place.
    static double[]
    Normalizes the input array in place.
    static void
    Normalizes the input array in place.
    static void
    inPlaceSubtract(double[] input, double[] update)
    Subtracts update from input in place.
    static void
    inPlaceSubtract(float[] input, float[] update)
    Subtracts update from input in place.
    static void
    logVector(Logger otherLogger, Level level, double[] input)
    Logs the supplied array to the supplied logger at the specified level.
    static void
    logVector(Logger otherLogger, Level level, float[] input)
    Logs the supplied array to the supplied logger at the specified level.
    static double
    mean(double[] inputs)
    Returns the mean of the input array.
    static double
    mean(double[] array, int length)
    Computes the mean of the first length elements of array.
    static <V extends Number>
    double
    mean(Collection<V> values)
    Computes the mean of the collection.
    static com.oracle.labs.mlrg.olcut.util.Pair<Double,Double>
    meanAndVariance(double[] inputs)
    Returns the mean and variance of the input.
    static com.oracle.labs.mlrg.olcut.util.Pair<Double,Double>
    meanAndVariance(double[] inputs, int length)
    Returns the mean and variance of the input's first length elements.
    static double[]
    normalizeToDistribution(double[] input)
    Generates a normalized version of the input array.
    static double[]
    normalizeToDistribution(float[] input)
    Generates a normalized version of the input array.
    static int
    product(int[] array)
    Computes the product of this array.
    static int[]
    randperm(int size, Random rng)
    Shuffles the indices in the range [0,size).
    static int[]
    randperm(int size, SplittableRandom rng)
    Shuffles the indices in the range [0,size).
    static void
    randpermInPlace(double[] input, SplittableRandom rng)
    Shuffles the input.
    static void
    randpermInPlace(int[] input, Random rng)
    Shuffles the input.
    static void
    randpermInPlace(int[] input, SplittableRandom rng)
    Shuffles the input.
    static int
    sampleFromCDF(double[] cdf, Random rng)
    Samples an index from the supplied cdf.
    static int
    sampleFromCDF(double[] cdf, SplittableRandom rng)
    Samples an index from the supplied cdf.
    static int[]
    sampleInts(Random rng, int size, int range)
    Samples an array of ints from the supplied rng in [0,range).
    static <V extends Number>
    double
    Computes the sample standard deviation of the collection.
    static <V extends Number>
    double
    Computes the sample variance of the collection.
    static <T> void
    Shuffles an ArrayList like Collections.shuffle(java.util.List<?>) but using a SplittableRandom.
    static int[]
    sortedDifference(int[] first, int[] second)
    Expects sorted input arrays.
    static double[]
    standardize(double[] input, double mean, double variance)
    Standardizes the input so it has zero mean and unit variance, i.e., subtracts the mean and divides by the variance.
    static void
    standardizeInPlace(double[] input, double mean, double variance)
    Standardizes the input so it has zero mean and unit variance, i.e., subtracts the mean and divides by the variance.
    static double
    sum(double[] input)
    Computes the sum of the input vector.
    static double
    sum(double[] array, int length)
    Computes the sum of the input vector up to length elements.
    static float
    sum(float[] input)
    Computes the sum of the input vector.
    static float
    sum(float[] array, int length)
    Computes the sum of the input vector up to length elements.
    static float
    sum(int[] indices, float[] input)
    Computes the sum of the specified indices in the input array.
    static float
    sum(int[] indices, int indicesLength, float[] input)
    Computes the sum of the specified indices in the input array.
    static List<Float>
    toBoxedFloats(float[] input)
    Boxes an array of floats into a list.
    static double[]
    toDoubleArray(float[] floats)
    Convert an array of floats to an array of doubles.
    static float[]
    toFloatArray(double[] doubles)
    Convert an array of doubles to an array of floats.
    static double[]
    Converts a boxed list of doubles into an array of primitive doubles.
    static double[]
    Converts a boxed list of integers into an array of primitive doubles.
    static float[]
    Converts a boxed list of floats into an array of primitive floats.
    static int[]
    Converts a boxed list of integers into an array of primitive ints.
    static long[]
    Converts a boxed list of longs into an array of primitive longs.
    static double
    vectorNorm(double[] input)
    Computes the vector two-norm.
    static double
    weightedMean(double[] inputs, double[] weights)
    Returns the weighted mean of the input.
    static double
    weightedMean(double[] array, float[] weights, int length)
    Computes the weighted mean of the first length elements of the array.
    static double
    weightedSum(double[] array, float[] weights, int length)
    Computes the weighted sum of the first length elements of the array.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • argmax

      public static <T extends Comparable<T>> com.oracle.labs.mlrg.olcut.util.Pair<Integer,T> argmax(List<T> values)
      Find the index of the maximum value in a list.
      Type Parameters:
      T - the type of the values (must implement Comparable)
      Parameters:
      values - list
      Returns:
      a pair: (index of the max value, max value)
    • argmin

      public static <T extends Comparable<T>> com.oracle.labs.mlrg.olcut.util.Pair<Integer,T> argmin(List<T> values)
      Find the index of the minimum value in a list.
      Type Parameters:
      T - the type of the values (must implement Comparable)
      Parameters:
      values - list
      Returns:
      a pair: (index of the min value, min value)
    • toFloatArray

      public static float[] toFloatArray(double[] doubles)
      Convert an array of doubles to an array of floats.
      Parameters:
      doubles - The array of doubles to convert.
      Returns:
      An array of floats.
    • toDoubleArray

      public static double[] toDoubleArray(float[] floats)
      Convert an array of floats to an array of doubles.
      Parameters:
      floats - The array of floats to convert.
      Returns:
      An array of doubles.
    • randperm

      public static int[] randperm(int size, Random rng)
      Shuffles the indices in the range [0,size).
      Parameters:
      size - The number of elements.
      rng - The random number generator to use.
      Returns:
      A random permutation of the values in the range (0, size-1).
    • randperm

      public static int[] randperm(int size, SplittableRandom rng)
      Shuffles the indices in the range [0,size).
      Parameters:
      size - The number of elements.
      rng - The random number generator to use.
      Returns:
      A random permutation of the values in the range (0, size-1).
    • randpermInPlace

      public static void randpermInPlace(int[] input, Random rng)
      Shuffles the input.
      Parameters:
      input - The array to shuffle.
      rng - The random number generator to use.
    • randpermInPlace

      public static void randpermInPlace(int[] input, SplittableRandom rng)
      Shuffles the input.
      Parameters:
      input - The array to shuffle.
      rng - The random number generator to use.
    • randpermInPlace

      public static void randpermInPlace(double[] input, SplittableRandom rng)
      Shuffles the input.
      Parameters:
      input - The array to shuffle.
      rng - The random number generator to use.
    • shuffle

      public static <T> void shuffle(ArrayList<T> list, SplittableRandom rng)
      Shuffles an ArrayList like Collections.shuffle(java.util.List<?>) but using a SplittableRandom.
      Type Parameters:
      T - The type of the list.
      Parameters:
      list - The list to shuffle.
      rng - The random number generator to use.
    • generateBootstrapIndices

      public static int[] generateBootstrapIndices(int size, Random rng)
      Draws a bootstrap sample of indices.
      Parameters:
      size - Size of the sample to generate.
      rng - The RNG to use.
      Returns:
      A bootstrap sample.
    • generateBootstrapIndices

      public static int[] generateBootstrapIndices(int size, SplittableRandom rng)
      Draws a bootstrap sample of indices.
      Parameters:
      size - Size of the sample to generate.
      rng - The RNG to use.
      Returns:
      A bootstrap sample.
    • generateBootstrapIndices

      public static int[] generateBootstrapIndices(int size, int range, SplittableRandom rng)
      Draws a bootstrap sample of indices.
      Parameters:
      size - Size of the sample to generate.
      range - The range of the sample.
      rng - The RNG to use.
      Returns:
      A bootstrap sample.
    • generateWeightedIndicesSample

      public static int[] generateWeightedIndicesSample(int size, double[] weights, Random rng)
      Generates a sample of indices weighted by the provided weights.
      Parameters:
      size - Size of the sample to generate.
      weights - A probability mass function of weights.
      rng - The RNG to use.
      Returns:
      A sample with replacement from weights.
    • generateWeightedIndicesSample

      public static int[] generateWeightedIndicesSample(int size, float[] weights, Random rng)
      Generates a sample of indices weighted by the provided weights.
      Parameters:
      size - Size of the sample to generate.
      weights - A probability mass function of weights.
      rng - The RNG to use.
      Returns:
      A sample with replacement from weights.
    • generateWeightedIndicesSample

      public static int[] generateWeightedIndicesSample(int size, double[] weights, SplittableRandom rng)
      Generates a sample of indices weighted by the provided weights.
      Parameters:
      size - Size of the sample to generate.
      weights - A probability mass function of weights.
      rng - The RNG to use.
      Returns:
      A sample with replacement from weights.
    • generateWeightedIndicesSample

      public static int[] generateWeightedIndicesSample(int size, float[] weights, SplittableRandom rng)
      Generates a sample of indices weighted by the provided weights.
      Parameters:
      size - Size of the sample to generate.
      weights - A probability mass function of weights.
      rng - The RNG to use.
      Returns:
      A sample with replacement from weights.
    • generateWeightedIndicesSampleWithoutReplacement

      public static int[] generateWeightedIndicesSampleWithoutReplacement(int size, double[] weights, Random rng)
      Generates a sample of indices weighted by the provided weights without replacement. Does not recalculate proportions in-between samples. Use judiciously.
      Parameters:
      size - Size of the sample to generate
      weights - A probability mass function of weights
      rng - The RNG to use
      Returns:
      A sample without replacement from weights
    • generateWeightedIndicesSampleWithoutReplacement

      public static int[] generateWeightedIndicesSampleWithoutReplacement(int size, float[] weights, Random rng)
      Generates a sample of indices weighted by the provided weights without replacement. Does not recalculate proportions in-between samples. Use judiciously.
      Parameters:
      size - Size of the sample to generate
      weights - A probability mass function of weights
      rng - The RNG to use
      Returns:
      A sample without replacement from weights
    • generateCDF

      public static double[] generateCDF(double[] pmf)
      Generates a cumulative distribution function from the supplied probability mass function.
      Parameters:
      pmf - The probability mass function (i.e., the probability distribution).
      Returns:
      The CDF.
    • cumulativeSum

      public static double[] cumulativeSum(double[] input)
      Produces a cumulative sum array.
      Parameters:
      input - The input to sum.
      Returns:
      The cumulative sum.
    • cumulativeSum

      public static int[] cumulativeSum(boolean[] input)
      Produces a cumulative sum array.
      Parameters:
      input - The input to sum.
      Returns:
      The cumulative sum.
    • generateCDF

      public static double[] generateCDF(float[] pmf)
      Generates a cumulative distribution function from the supplied probability mass function.
      Parameters:
      pmf - The probability mass function (i.e., the probability distribution).
      Returns:
      The CDF.
    • generateCDF

      public static double[] generateCDF(long[] counts, long countSum)
      Generates a cumulative distribution function from the supplied probability mass function.
      Parameters:
      counts - The frequency counts.
      countSum - The sum of the counts.
      Returns:
      The CDF.
    • sampleFromCDF

      public static int sampleFromCDF(double[] cdf, Random rng)
      Samples an index from the supplied cdf.
      Parameters:
      cdf - The cdf to sample from.
      rng - The rng to use.
      Returns:
      A sample.
    • sampleFromCDF

      public static int sampleFromCDF(double[] cdf, SplittableRandom rng)
      Samples an index from the supplied cdf.
      Parameters:
      cdf - The cdf to sample from.
      rng - The rng to use.
      Returns:
      A sample.
    • generateUniformVector

      public static double[] generateUniformVector(int length, double value)
      Generates an array of the specified length filled with the specified value.
      Parameters:
      length - The array length.
      value - The array value.
      Returns:
      A filled array.
    • generateUniformVector

      public static float[] generateUniformVector(int length, float value)
      Generates an array of the specified length filled with the specified value.
      Parameters:
      length - The array length.
      value - The array value.
      Returns:
      A filled array.
    • normalizeToDistribution

      public static double[] normalizeToDistribution(double[] input)
      Generates a normalized version of the input array.

      Normalization in this case means the array sums to 1.0.

      Parameters:
      input - The input array.
      Returns:
      A normalized probability distribution.
    • normalizeToDistribution

      public static double[] normalizeToDistribution(float[] input)
      Generates a normalized version of the input array.

      Normalization in this case means the array sums to 1.0.

      Parameters:
      input - The input array.
      Returns:
      A normalized probability distribution.
    • inplaceNormalizeToDistribution

      public static double[] inplaceNormalizeToDistribution(double[] input)
      Normalizes the input array in place.

      Normalization in this case means the array sums to 1.0.

      Parameters:
      input - The input array.
      Returns:
      The input array after it has been normalized into a probability distribution.
    • inplaceNormalizeToDistribution

      public static void inplaceNormalizeToDistribution(float[] input)
      Normalizes the input array in place.

      Normalization in this case means the array sums to 1.0.

      Parameters:
      input - The input array.
    • logVector

      public static void logVector(Logger otherLogger, Level level, double[] input)
      Logs the supplied array to the supplied logger at the specified level.

      Prints the array as tuples of index and value.

      Parameters:
      otherLogger - The logger.
      level - The log level.
      input - The input array to log.
    • logVector

      public static void logVector(Logger otherLogger, Level level, float[] input)
      Logs the supplied array to the supplied logger at the specified level.

      Prints the array as tuples of index and value.

      Parameters:
      otherLogger - The logger.
      level - The log level.
      input - The input array to log.
    • toBoxedFloats

      public static List<Float> toBoxedFloats(float[] input)
      Boxes an array of floats into a list.
      Parameters:
      input - The array to box.
      Returns:
      The list of boxed floats.
    • toPrimitiveDoubleFromInteger

      public static double[] toPrimitiveDoubleFromInteger(List<Integer> input)
      Converts a boxed list of integers into an array of primitive doubles.
      Parameters:
      input - The input integer list.
      Returns:
      An array of doubles.
    • toPrimitiveDouble

      public static double[] toPrimitiveDouble(List<Double> input)
      Converts a boxed list of doubles into an array of primitive doubles.
      Parameters:
      input - The input double list.
      Returns:
      An array of doubles.
    • toPrimitiveFloat

      public static float[] toPrimitiveFloat(List<Float> input)
      Converts a boxed list of floats into an array of primitive floats.
      Parameters:
      input - The input float list.
      Returns:
      An array of floats.
    • toPrimitiveInt

      public static int[] toPrimitiveInt(List<Integer> input)
      Converts a boxed list of integers into an array of primitive ints.
      Parameters:
      input - The input integer list.
      Returns:
      An array of ints.
    • toPrimitiveLong

      public static long[] toPrimitiveLong(List<Long> input)
      Converts a boxed list of longs into an array of primitive longs.
      Parameters:
      input - The input long list.
      Returns:
      An array of longs.
    • sampleInts

      public static int[] sampleInts(Random rng, int size, int range)
      Samples an array of ints from the supplied rng in [0,range).
      Parameters:
      rng - The RNG to sample from.
      size - The number of samples to draw.
      range - The range of the samples.
      Returns:
      A sampled int array.
    • inPlaceAdd

      public static void inPlaceAdd(double[] input, double[] update)
      Adds update to input in place.
      Parameters:
      input - The input array.
      update - The array to add.
    • inPlaceSubtract

      public static void inPlaceSubtract(double[] input, double[] update)
      Subtracts update from input in place.
      Parameters:
      input - The input array.
      update - The array to subtract.
    • inPlaceAdd

      public static void inPlaceAdd(float[] input, float[] update)
      Adds update to input in place.
      Parameters:
      input - The input array.
      update - The array to add.
    • inPlaceSubtract

      public static void inPlaceSubtract(float[] input, float[] update)
      Subtracts update from input in place.
      Parameters:
      input - The input array.
      update - The array to subtract.
    • vectorNorm

      public static double vectorNorm(double[] input)
      Computes the vector two-norm.
      Parameters:
      input - The input vector.
      Returns:
      The euclidean norm of the vector.
    • sum

      public static double sum(double[] input)
      Computes the sum of the input vector.
      Parameters:
      input - The vector to sum.
      Returns:
      The vector sum.
    • sum

      public static float sum(float[] input)
      Computes the sum of the input vector.
      Parameters:
      input - The vector to sum.
      Returns:
      The vector sum.
    • sum

      public static double sum(double[] array, int length)
      Computes the sum of the input vector up to length elements.
      Parameters:
      array - The vector to sum.
      length - The number of elements to sum.
      Returns:
      The vector sum.
    • sum

      public static float sum(float[] array, int length)
      Computes the sum of the input vector up to length elements.
      Parameters:
      array - The vector to sum.
      length - The number of elements to sum.
      Returns:
      The vector sum.
    • sum

      public static float sum(int[] indices, int indicesLength, float[] input)
      Computes the sum of the specified indices in the input array.
      Parameters:
      indices - The indices to sum.
      indicesLength - The number of elements of indices to sum over.
      input - The input array to sum the values of.
      Returns:
      The sum of the input array.
    • sum

      public static float sum(int[] indices, float[] input)
      Computes the sum of the specified indices in the input array.
      Parameters:
      indices - The indices to sum.
      input - The input array to sum the values of.
      Returns:
      The sum of the input array.
    • generateUniformFloatVector

      public static float[] generateUniformFloatVector(int length, float value)
      Generates a float vector of the specified length filled with the specified value.
      Parameters:
      length - The vector length.
      value - The initial value.
      Returns:
      A float vector initialised with the value.
    • binarySearch

      public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
      A binary search function.
      Type Parameters:
      T - Type of the list, must implement Comparable.
      Parameters:
      list - Input list, must be ordered.
      key - Key to search for.
      Returns:
      the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size() if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
    • binarySearch

      public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key, int low, int high)
      A binary search function.
      Type Parameters:
      T - Type of the list, must implement Comparable.
      Parameters:
      list - Input list, must be ordered.
      key - Key to search for.
      low - Starting index.
      high - End index (will be searched).
      Returns:
      the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or high if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
    • binarySearch

      public static <T> int binarySearch(List<? extends T> list, int key, ToIntFunction<T> extractionFunc)
      A binary search function.
      Type Parameters:
      T - Type of the list, must implement Comparable.
      Parameters:
      list - Input list, must be ordered.
      key - Key to search for.
      extractionFunc - Takes a T and generates an int which can be used for comparison using int's natural ordering.
      Returns:
      the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or high if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
    • auc

      public static double auc(double[] x, double[] y)
      Calculates the area under the curve, bounded below by the x axis.

      Uses linear interpolation between the points on the x axis, i.e., trapezoidal integration.

      The x axis must be increasing.

      Parameters:
      x - The x points to evaluate.
      y - The corresponding heights.
      Returns:
      The AUC.
    • meanAndVariance

      public static com.oracle.labs.mlrg.olcut.util.Pair<Double,Double> meanAndVariance(double[] inputs)
      Returns the mean and variance of the input.
      Parameters:
      inputs - The input array.
      Returns:
      The mean and variance of the inputs. The mean is the first element, the variance is the second.
    • meanAndVariance

      public static com.oracle.labs.mlrg.olcut.util.Pair<Double,Double> meanAndVariance(double[] inputs, int length)
      Returns the mean and variance of the input's first length elements.
      Parameters:
      inputs - The input array.
      length - The number of elements to use.
      Returns:
      The mean and variance of the inputs. The mean is the first element, the variance is the second.
    • weightedMean

      public static double weightedMean(double[] inputs, double[] weights)
      Returns the weighted mean of the input.

      Throws IllegalArgumentException if the two arrays are not the same length.

      Parameters:
      inputs - The input array.
      weights - The weights to use.
      Returns:
      The weighted mean.
    • mean

      public static double mean(double[] inputs)
      Returns the mean of the input array.
      Parameters:
      inputs - The input array.
      Returns:
      The mean of inputs.
    • mean

      public static double mean(double[] array, int length)
      Computes the mean of the first length elements of array.
      Parameters:
      array - The array to take the mean of.
      length - The number of elements to take the mean over.
      Returns:
      The mean.
    • mean

      public static <V extends Number> double mean(Collection<V> values)
      Computes the mean of the collection.
      Type Parameters:
      V - The collection type.
      Parameters:
      values - The collection.
      Returns:
      The mean of the collection.
    • sampleVariance

      public static <V extends Number> double sampleVariance(Collection<V> values)
      Computes the sample variance of the collection.
      Type Parameters:
      V - The collection type.
      Parameters:
      values - The collection.
      Returns:
      The sample variance of the collection.
    • sampleStandardDeviation

      public static <V extends Number> double sampleStandardDeviation(Collection<V> values)
      Computes the sample standard deviation of the collection.
      Type Parameters:
      V - The collection type.
      Parameters:
      values - The collection.
      Returns:
      The sample standard deviation.
    • weightedMean

      public static double weightedMean(double[] array, float[] weights, int length)
      Computes the weighted mean of the first length elements of the array.
      Parameters:
      array - The array to mean.
      weights - The element weights.
      length - The number of elements to use.
      Returns:
      The weighted mean.
    • weightedSum

      public static double weightedSum(double[] array, float[] weights, int length)
      Computes the weighted sum of the first length elements of the array.
      Parameters:
      array - The array to sum.
      weights - The element weights.
      length - The number of elements to use.
      Returns:
      The weighted sum.
    • differencesIndices

      public static int[] differencesIndices(double[] input)
      Returns an array containing the indices where values are different. Basically a combination of np.where and np.diff.

      Stores an index if the value after it is different. Always stores the final index.

      Uses a default tolerance of 1e-12.

      Parameters:
      input - Input array.
      Returns:
      An array containing the indices where the input changes.
    • differencesIndices

      public static int[] differencesIndices(double[] input, double tolerance)
      Returns an array containing the indices where values are different. Basically a combination of np.where and np.diff.

      Stores an index if the value after it is different. Always stores the final index.

      Parameters:
      input - Input array.
      tolerance - Tolerance to determine a difference.
      Returns:
      An array containing the indices where the input changes.
    • formatDuration

      public static String formatDuration(long startMillis, long stopMillis)
      Formats a duration given two times in milliseconds.

      Format string is - (%02d:%02d:%02d:%03d) or (%d days, %02d:%02d:%02d:%03d)

      Parameters:
      startMillis - Start time in ms.
      stopMillis - End time in ms.
      Returns:
      A formatted string measuring time in hours, minutes, second and milliseconds.
    • sortedDifference

      public static int[] sortedDifference(int[] first, int[] second)
      Expects sorted input arrays. Returns an array containing all the elements in first that are not in second.
      Parameters:
      first - The first sorted array.
      second - The second sorted array.
      Returns:
      An array containing all the elements of first that aren't in second.
    • standardize

      public static double[] standardize(double[] input, double mean, double variance)
      Standardizes the input so it has zero mean and unit variance, i.e., subtracts the mean and divides by the variance.
      Parameters:
      input - The input to standardize.
      mean - The mean.
      variance - The variance.
      Returns:
      The standardized input.
    • standardizeInPlace

      public static void standardizeInPlace(double[] input, double mean, double variance)
      Standardizes the input so it has zero mean and unit variance, i.e., subtracts the mean and divides by the variance. Operates in place on the input array.
      Parameters:
      input - The input to standardize.
      mean - The mean.
      variance - The variance.
    • product

      public static int product(int[] array)
      Computes the product of this array.
      Parameters:
      array - The input array.
      Returns:
      The product of the array.