Skip to content

Instantly share code, notes, and snippets.

@miho
Created December 13, 2024 11:22
Show Gist options
  • Save miho/857d30f4be5db5d23fc78bb3b691a3d7 to your computer and use it in GitHub Desktop.
Save miho/857d30f4be5db5d23fc78bb3b691a3d7 to your computer and use it in GitHub Desktop.
public class StatisticsUtils {
/**
* Computes the Coefficient of Variation (CV) from an array of doubles.
* Handles cases where the mean is zero by returning a modified CV using the absolute mean.
* @param data An array of doubles.
* @return The Coefficient of Variation (CV).
*/
public static double computeCV(double[] data) {
if (data == null || data.length == 0) {
throw new IllegalArgumentException("Data array is null or empty");
}
double mean = computeMean(data);
double absMean = Math.abs(mean);
// If mean is zero, return Double.POSITIVE_INFINITY
if (absMean == 0) {
return Double.POSITIVE_INFINITY; // Extreme variability
}
double stdDev = computeStandardDeviation(data, mean);
return stdDev / absMean;
}
/**
* Computes a range-based variability measure using standard deviation over the actual data range.
* @param data An array of doubles.
* @return The variability relative to the range.
*/
public static double computeRangeBasedVariability(double[] data) {
if (data == null || data.length == 0) {
throw new IllegalArgumentException("Data array is null or empty");
}
double min = findMin(data);
double max = findMax(data);
double range = max - min;
if (range == 0) {
return 0.0; // No variability if all values are the same
}
double mean = computeMean(data);
double stdDev = computeStandardDeviation(data, mean);
return stdDev / range;
}
/**
* Computes a range-based variability measure using standard deviation over a user-specified range.
* @param data An array of doubles.
* @param specifiedMin The minimum value of the range.
* @param specifiedMax The maximum value of the range.
* @return The variability relative to the specified range.
*/
public static double computeRangeBasedVariability(double[] data, double specifiedMin, double specifiedMax) {
if (data == null || data.length == 0) {
throw new IllegalArgumentException("Data array is null or empty");
}
double range = specifiedMax - specifiedMin;
if (range <= 0) {
throw new IllegalArgumentException("Specified range must be greater than zero");
}
double mean = computeMean(data);
double stdDev = computeStandardDeviation(data, mean);
return stdDev / range;
}
/**
* Helper method to compute the mean for an array.
*/
private static double computeMean(double[] data) {
double sum = 0.0;
for (double num : data) {
sum += num;
}
return sum / data.length;
}
/**
* Helper method to compute the standard deviation for an array.
*/
private static double computeStandardDeviation(double[] data, double mean) {
double varianceSum = 0.0;
for (double num : data) {
varianceSum += Math.pow(num - mean, 2);
}
return Math.sqrt(varianceSum / data.length);
}
/**
* Helper method to find the minimum value in an array.
*/
private static double findMin(double[] data) {
double min = Double.MAX_VALUE;
for (double num : data) {
if (num < min) {
min = num;
}
}
return min;
}
/**
* Helper method to find the maximum value in an array.
*/
private static double findMax(double[] data) {
double max = -Double.MAX_VALUE;
for (double num : data) {
if (num > max) {
max = num;
}
}
return max;
}
public static void main(String[] args) {
// Example data: temperature readings
double[] dataArray = {-5.0, -2.5, 0.0, 2.5, 5.0};
// Compute Coefficient of Variation (CV)
try {
double cv = computeCV(dataArray);
System.out.printf("CV (array): %.4f%n", cv);
} catch (ArithmeticException | IllegalArgumentException e) {
System.out.println("Error (CV): " + e.getMessage());
}
// Compute range-based variability (actual range)
try {
double rangeVar = computeRangeBasedVariability(dataArray);
System.out.printf("Range-Based Variability (actual range): %.4f%n", rangeVar);
} catch (IllegalArgumentException e) {
System.out.println("Error (Range-Based Variability): " + e.getMessage());
}
// Compute range-based variability (specified range)
try {
double rangeVarSpecified = computeRangeBasedVariability(dataArray, -10.0, 10.0);
System.out.printf("Range-Based Variability (specified range): %.4f%n", rangeVarSpecified);
} catch (IllegalArgumentException e) {
System.out.println("Error (Range-Based Variability with Specified Range): " + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment