public final class Util extends Object
Basically full of vector and RNG operations.
Modifier and Type | Method and Description |
---|---|
static <T extends Comparable<T>> |
argmax(List<T> values)
Find the index of the maximum value in a list.
|
static <T extends Comparable<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,
Random rng)
Draws a bootstrap sample of indices.
|
static int[] |
generateBootstrapIndices(int size,
SplittableRandom rng)
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) |
static double[] |
generateUniformVector(int length,
double value) |
static float[] |
generateUniformVector(int length,
float 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) |
static void |
inPlaceAdd(float[] input,
float[] update) |
static double[] |
inplaceNormalizeToDistribution(double[] input) |
static void |
inplaceNormalizeToDistribution(float[] input) |
static void |
inPlaceSubtract(double[] input,
double[] update) |
static void |
inPlaceSubtract(float[] input,
float[] update) |
static void |
logVector(Logger otherLogger,
Level level,
double[] input) |
static void |
logVector(Logger otherLogger,
Level level,
float[] input) |
static <V extends Number> |
mean(Collection<V> values) |
static double |
mean(double[] inputs)
Returns the mean of the input array.
|
static double |
mean(double[] array,
int length) |
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) |
static double[] |
normalizeToDistribution(float[] input) |
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(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) |
static <V extends Number> |
sampleStandardDeviation(Collection<V> values) |
static <V extends Number> |
sampleVariance(Collection<V> values) |
static int[] |
sortedDifference(int[] first,
int[] second)
Expects sorted input arrays.
|
static double |
sum(double[] input) |
static double |
sum(double[] array,
int length) |
static float |
sum(float[] input) |
static float |
sum(float[] array,
int length) |
static float |
sum(int[] indices,
float[] input) |
static float |
sum(int[] indices,
int indicesLength,
float[] input) |
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[] |
toPrimitiveDouble(List<Double> input) |
static double[] |
toPrimitiveDoubleFromInteger(List<Integer> input) |
static float[] |
toPrimitiveFloat(List<Float> input) |
static int[] |
toPrimitiveInt(List<Integer> input) |
static long[] |
toPrimitiveLong(List<Long> input) |
static double |
vectorNorm(double[] input) |
static double |
weightedMean(double[] inputs,
double[] weights)
Returns the weighted mean of the input.
|
static double |
weightedMean(double[] array,
float[] weights,
int length) |
static double |
weightedSum(double[] array,
float[] weights,
int length) |
public static <T extends Comparable<T>> com.oracle.labs.mlrg.olcut.util.Pair<Integer,T> argmax(List<T> values)
T
- the type of the values (must implement Comparable)values
- listpublic static <T extends Comparable<T>> com.oracle.labs.mlrg.olcut.util.Pair<Integer,T> argmin(List<T> values)
T
- the type of the values (must implement Comparable)values
- listpublic static float[] toFloatArray(double[] doubles)
doubles
- The array of doubles to convert.public static double[] toDoubleArray(float[] floats)
floats
- The array of floats to convert.public static int[] randperm(int size, Random rng)
size
- The number of elements.rng
- The random number generator to use.public static int[] randperm(int size, SplittableRandom rng)
size
- The number of elements.rng
- The random number generator to use.public static void randpermInPlace(int[] input, Random rng)
input
- The array to shuffle.rng
- The random number generator to use.public static void randpermInPlace(int[] input, SplittableRandom rng)
input
- The array to shuffle.rng
- The random number generator to use.public static int[] generateBootstrapIndices(int size, Random rng)
size
- Size of the sample to generate.rng
- The RNG to use.public static int[] generateBootstrapIndices(int size, SplittableRandom rng)
size
- Size of the sample to generate.rng
- The RNG to use.public static int[] generateWeightedIndicesSample(int size, double[] weights, Random rng)
size
- Size of the sample to generate.weights
- A probability mass function of weights.rng
- The RNG to use.public static int[] generateWeightedIndicesSample(int size, float[] weights, Random rng)
size
- Size of the sample to generate.weights
- A probability mass function of weights.rng
- The RNG to use.public static int[] generateWeightedIndicesSample(int size, double[] weights, SplittableRandom rng)
size
- Size of the sample to generate.weights
- A probability mass function of weights.rng
- The RNG to use.public static int[] generateWeightedIndicesSample(int size, float[] weights, SplittableRandom rng)
size
- Size of the sample to generate.weights
- A probability mass function of weights.rng
- The RNG to use.public static int[] generateWeightedIndicesSampleWithoutReplacement(int size, double[] weights, Random rng)
size
- Size of the sample to generateweights
- A probability mass function of weightsrng
- The RNG to usepublic static int[] generateWeightedIndicesSampleWithoutReplacement(int size, float[] weights, Random rng)
size
- Size of the sample to generateweights
- A probability mass function of weightsrng
- The RNG to usepublic static double[] generateCDF(double[] pmf)
pmf
- The probability mass function (i.e., the probability distribution).public static double[] cumulativeSum(double[] input)
input
- The input to sum.public static int[] cumulativeSum(boolean[] input)
input
- The input to sum.public static double[] generateCDF(float[] pmf)
pmf
- The probability mass function (i.e., the probability distribution).public static double[] generateCDF(long[] counts, long countSum)
counts
- The frequency counts.countSum
- The sum of the counts.public static int sampleFromCDF(double[] cdf, Random rng)
cdf
- The cdf to sample from.rng
- The rng to use.public static int sampleFromCDF(double[] cdf, SplittableRandom rng)
cdf
- The cdf to sample from.rng
- The rng to use.public static double[] generateUniformVector(int length, double value)
public static float[] generateUniformVector(int length, float value)
public static double[] normalizeToDistribution(double[] input)
public static double[] normalizeToDistribution(float[] input)
public static double[] inplaceNormalizeToDistribution(double[] input)
public static void inplaceNormalizeToDistribution(float[] input)
public static double[] toPrimitiveDoubleFromInteger(List<Integer> input)
public static int[] sampleInts(Random rng, int size, int range)
public static void inPlaceAdd(double[] input, double[] update)
public static void inPlaceSubtract(double[] input, double[] update)
public static void inPlaceAdd(float[] input, float[] update)
public static void inPlaceSubtract(float[] input, float[] update)
public static double vectorNorm(double[] input)
public static double sum(double[] input)
public static float sum(float[] input)
public static double sum(double[] array, int length)
public static float sum(float[] array, int length)
public static float sum(int[] indices, int indicesLength, float[] input)
public static float sum(int[] indices, float[] input)
public static float[] generateUniformFloatVector(int length, float value)
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
T
- Type of the list, must implement Comparable.list
- Input list, must be ordered.key
- Key to search for.public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key, int low, int high)
T
- Type of the list, must implement Comparable.list
- Input list, must be ordered.key
- Key to search for.low
- Starting index.high
- End index (will be searched).public static <T> int binarySearch(List<? extends T> list, int key, ToIntFunction<T> extractionFunc)
T
- Type of the list, must implement Comparable.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.public static double auc(double[] x, double[] y)
Uses linear interpolation between the points on the x axis, i.e., trapezoidal integration.
The x axis must be increasing.
x
- The x points to evaluate.y
- The corresponding heights.public static com.oracle.labs.mlrg.olcut.util.Pair<Double,Double> meanAndVariance(double[] inputs)
inputs
- The input array.public static com.oracle.labs.mlrg.olcut.util.Pair<Double,Double> meanAndVariance(double[] inputs, int length)
inputs
- The input array.length
- The number of elements to use.public static double weightedMean(double[] inputs, double[] weights)
Throws IllegalArgumentException if the two arrays are not the same length.
inputs
- The input array.weights
- The weights to use.public static double mean(double[] inputs)
inputs
- The input array.public static double mean(double[] array, int length)
public static <V extends Number> double mean(Collection<V> values)
public static <V extends Number> double sampleVariance(Collection<V> values)
public static <V extends Number> double sampleStandardDeviation(Collection<V> values)
public static double weightedMean(double[] array, float[] weights, int length)
public static double weightedSum(double[] array, float[] weights, int length)
public static int[] differencesIndices(double[] input)
Stores an index if the value after it is different. Always stores the final index.
Uses a default tolerance of 1e-12.
input
- Input array.public static int[] differencesIndices(double[] input, double tolerance)
Stores an index if the value after it is different. Always stores the final index.
input
- Input array.tolerance
- Tolerance to determine a difference.public static String formatDuration(long startMillis, long stopMillis)
Format string is - (%02d:%02d:%02d:%03d) or (%d days, %02d:%02d:%02d:%03d)
startMillis
- Start time in ms.stopMillis
- End time in ms.public static int[] sortedDifference(int[] first, int[] second)
first
- The first sorted array.second
- The second sorted array.Copyright © 2015–2021 Oracle and/or its affiliates. All rights reserved.