MathUtil

public final class MathUtil

A class containing multiple convenient math functions.

Author:Anton Beloglazov

Fields

HUNDRED_PERCENT

public static final double HUNDRED_PERCENT

100% represented in scale [0 .. 1].

Methods

abs

public static double[] abs(double... data)

Gets the absolute values of an array of values

Parameters:
  • data – the array of values
Returns:

a new array with the absolute value of each element in the given array.

correlationCoefficients

public static List<Double> correlationCoefficients(double[][] data)

Computes correlation coefficients for a set of data.

Parameters:
  • data – the data to compute the correlation coefficients
Returns:

the correlation coefficients

countNonZeroBeginning

public static int countNonZeroBeginning(double... data)

Counts the number of values different of zero at the beginning of an array.

Parameters:
  • data – the array of numbers
Returns:

the number of values different of zero at the beginning of the array

createLinearRegression

public static SimpleRegression createLinearRegression(double[] x, double[] y)

Creates a a simple linear regression.

Parameters:
  • x – the independent variable
  • y – the dependent variable

createLinearRegression

public static OLSMultipleLinearRegression createLinearRegression(double[][] x, double[] y)

Creates a a multiple linear regression.

Parameters:
  • x – the independent variable
  • y – the dependent variable

doubleToInt

public static int doubleToInt(double value)

Converts a double value to an int, using an appropriate rounding function. If the double is negative, it applies Math.floor(double) to round the number down. If it’ a positive value, it applies Math.ceil(double) to round the number up. This way, a negative double will be converted to a negative int and a positive double will be converted to a positive int.

It’s different from using: Math.round(double) which always rounds to the next positive integer; Math.floor(double) which always rounds down; or Math.ceil(double) which always rounds up. It applies floor for negative values and ceil for positive ones.

This method is useful to be used by Comparators which rely on a double attribute to compare a list of objects. Since the Comparator.compare(Object,Object) method must return an int, the method being implemented here converts a double to an int value which can be used by a Comparator.

Parameters:
  • value – the double value to convert
Returns:

zero if the double value is zero, a negative int if the double is negative, or a positive int if the double is positive.

getLoessParameterEstimates

public static double[] getLoessParameterEstimates(double... y)

Gets the Local Regression (Loess) parameter estimates.

Parameters:
  • y – the dependent variable
Returns:

the Loess parameter estimates

getRobustLoessParameterEstimates

public static double[] getRobustLoessParameterEstimates(double... y)

Gets the robust loess parameter estimates.

Parameters:
  • y – the dependent variable
Returns:

the robust loess parameter estimates

getStatistics

public static DescriptiveStatistics getStatistics(Collection<Double> list)

Gets an object to compute descriptive statistics for an list of numbers.

Parameters:
  • list – the list of numbers. Must not be null.
Returns:

descriptive statistics for the list of numbers.

getStatistics

public static DescriptiveStatistics getStatistics(double... list)

Gets an object to compute descriptive statistics for an array of numbers.

Parameters:
  • list – the array of numbers. Must not be null.
Returns:

descriptive statistics for the array of numbers.

getTricubeBisquareWeights

public static double[] getTricubeBisquareWeights(double... residuals)

Gets the tricube bisquare weigths.

Parameters:
  • residuals – the residuals array
Returns:

the tricube bisquare weigths

getTricubeWeights

public static double[] getTricubeWeights(int weightsNumber)

Gets the tricube weigths.

Parameters:
  • weightsNumber – the number of weights
Returns:

an array of tricube weigths with n elements

iqr

public static double iqr(double... data)

Gets the Interquartile Range (IQR) from an array of numbers.

Parameters:
  • data – the array of numbers
Returns:

the IQR

mad

public static double mad(double... data)

Gets the Median Absolute Deviation (MAD) from a array of numbers.

Parameters:
  • data – the array of numbers
Returns:

the mad

mean

public static double mean(List<Double> list)

Gets the average from a list of numbers. If the list is empty or contains just zeros, returns 0.

Parameters:
  • list – the list of numbers
Returns:

the average

median

public static double median(Collection<Double> list)

Gets the median from a list of numbers.

Parameters:
  • list – the list of numbers
Returns:

the median

median

public static double median(double... list)

Gets the median from an array of numbers.

Parameters:
  • list – the array of numbers
Returns:

the median

same

public static boolean same(double first, double second)

Checks if two double numbers are equals, considering a precision error or 0.01. That is, if the different between the two numbers are lower or equal to 0.01, they are considered equal.

Parameters:
  • first – the first number to check
  • second – the second number to check
Returns:

true if the numbers are equal considering the precision error

same

public static boolean same(double first, double second, double precisionError)

Checks if two double numbers are equals, considering a given precision error. That is, if the different between the two numbers are lower or equal to the precision error, they are considered equal.

Parameters:
  • first – the first number to check
  • second – the second number to check
  • precisionError – the precision error used to compare the numbers
Returns:

true if the numbers are equal considering the precision error

stDev

public static double stDev(List<Double> list)

Gets the Standard Deviation from a list of numbers.

Parameters:
  • list – the list of numbers
Returns:

the standard deviation

sum

public static double sum(List<? extends Number> list)

Sums a list of numbers.

Parameters:
  • list – the list of numbers
Returns:

the double

variance

public static double variance(List<Double> list)

Gets the Variance from a list of numbers.

Parameters:
  • list – the list of numbers
Returns:

the variance