/** * Gets the Median Absolute Deviation (MAD) from a array of numbers. * * @param data the array of numbers * @return the mad */ public static double mad(final double... data) { if (data.length == 0) { return 0; } final double median = median(data); final double[] deviationSum = new double[data.length]; for (int i = 0; i < data.length; i++) { deviationSum[i] = Math.abs(median - data[i]); } return median(deviationSum); }
/** * Gets the Variance from a list of numbers. * * @param list the list of numbers * @return the variance */ public static double variance(final List<Double> list) { long n = 0; double mean = mean(list); double s = 0.0; for (double x : list) { n++; double delta = x - mean; mean += delta / n; s += delta * (x - mean); } // if you want to calculate std deviation // of a sample change this to (s/(n-1)) return s / (n - 1); }
/** * Gets the tricube bisquare weigths. * * @param residuals the residuals array * @return the tricube bisquare weigths */ public static double[] getTricubeBisquareWeights(final double... residuals) { final double[] weights = getTricubeWeights(residuals.length); final double[] weights2 = new double[residuals.length]; final double s6 = median(abs(residuals)) * 6; for (int i = 2; i < residuals.length; i++) { final double k = Math.pow(1 - Math.pow(residuals[i] / s6, 2), 2); weights2[i] = k > 0 ? (1 / k) * weights[i] : Double.MAX_VALUE; } weights2[0] = weights2[1] = weights2[2]; return weights2; }
/** * Gets the host utilization MAD. * * @param host the host * @return the host utilization MAD */ protected double getHostUtilizationMad(PowerHostUtilizationHistory host) throws IllegalArgumentException { double[] data = host.getUtilizationHistory(); if (MathUtil.countNonZeroBeginning(data) >= 12) { // 12 has been suggested as a safe value return MathUtil.mad(data); } throw new IllegalArgumentException(); }
/** * Gets the host CPU utilization percentage IQR. * * @param host the host * @return the host CPU utilization percentage IQR */ protected double getHostUtilizationIqr(PowerHostUtilizationHistory host) throws IllegalArgumentException { double[] data = host.getUtilizationHistory(); if (MathUtil.countNonZeroBeginning(data) >= 12) { // 12 has been suggested as a safe value return MathUtil.iqr(data); } throw new IllegalArgumentException(); }
/** * Gets the tricube bisquare weigths. * * @param residuals the residuals array * @return the tricube bisquare weigths * @todo The word "weight" is misspelled in the method name. */ public static double[] getTricubeBisquareWeigts(final double[] residuals) { int n = residuals.length; double[] weights = getTricubeWeigts(n); double[] weights2 = new double[n]; double s6 = median(abs(residuals)) * 6; for (int i = 2; i < n; i++) { double k = Math.pow(1 - Math.pow(residuals[i] / s6, 2), 2); if (k > 0) { weights2[i] = (1 / k) * weights[i]; } else { weights2[i] = Double.MAX_VALUE; } } weights2[0] = weights2[1] = weights2[2]; return weights2; }
double medTimeBeforeHostShutdown = Double.NaN; if (!timeBeforeHostShutdown.isEmpty()) { meanTimeBeforeHostShutdown = MathUtil.mean(timeBeforeHostShutdown); stDevTimeBeforeHostShutdown = MathUtil.stDev(timeBeforeHostShutdown); medTimeBeforeHostShutdown = MathUtil.median(timeBeforeHostShutdown); double medTimeBeforeContainerMigration = Double.NaN; if (!timeBeforeContainerMigration.isEmpty()) { meanTimeBeforeContainerMigration = MathUtil.mean(timeBeforeContainerMigration); stDevTimeBeforeContainerMigration = MathUtil.stDev(timeBeforeContainerMigration); medTimeBeforeContainerMigration = MathUtil.median(timeBeforeContainerMigration); double medActiveVm = Double.NaN; if (!activeVm.isEmpty()) { meanActiveVm = MathUtil.mean(activeVm); stDevActiveVm = MathUtil.stDev(activeVm); medActiveVm = MathUtil.median(activeVm); double medActiveHosts = Double.NaN; if (!activeHost.isEmpty()) { meanActiveHosts = MathUtil.mean(activeHost); stDevActiveHosts = MathUtil.stDev(activeHost); medActiveHosts = MathUtil.median(activeHost); double medNumberOfContainerMigrations = Double.NaN; if (!numberOfContainerMigrations.isEmpty()) { meanNumberOfContainerMigrations = MathUtil.mean(numberOfContainerMigrations); stDevNumberOfContainerMigrations = MathUtil.stDev(numberOfContainerMigrations); medNumberOfContainerMigrations = MathUtil.median(numberOfContainerMigrations);
double stDevTimeBeforeHostShutdown = Double.NaN; if (!timeBeforeHostShutdown.isEmpty()) { meanTimeBeforeHostShutdown = MathUtil.mean(timeBeforeHostShutdown); stDevTimeBeforeHostShutdown = MathUtil.stDev(timeBeforeHostShutdown); double stDevTimeBeforeVmMigration = Double.NaN; if (!timeBeforeVmMigration.isEmpty()) { meanTimeBeforeVmMigration = MathUtil.mean(timeBeforeVmMigration); stDevTimeBeforeVmMigration = MathUtil.stDev(timeBeforeVmMigration); double executionTimeVmSelectionMean = MathUtil.mean(vmAllocationPolicy .getExecutionTimeHistoryVmSelection()); double executionTimeVmSelectionStDev = MathUtil.stDev(vmAllocationPolicy .getExecutionTimeHistoryVmSelection()); double executionTimeHostSelectionMean = MathUtil.mean(vmAllocationPolicy .getExecutionTimeHistoryHostSelection()); double executionTimeHostSelectionStDev = MathUtil.stDev(vmAllocationPolicy .getExecutionTimeHistoryHostSelection()); double executionTimeVmReallocationMean = MathUtil.mean(vmAllocationPolicy .getExecutionTimeHistoryVmReallocation()); double executionTimeVmReallocationStDev = MathUtil.stDev(vmAllocationPolicy .getExecutionTimeHistoryVmReallocation()); double executionTimeTotalMean = MathUtil.mean(vmAllocationPolicy .getExecutionTimeHistoryTotal()); double executionTimeTotalStDev = MathUtil.stDev(vmAllocationPolicy .getExecutionTimeHistoryTotal()); double executionTimeVmSelectionMean = MathUtil.mean(vmAllocationPolicy .getExecutionTimeHistoryVmSelection());
public static SimpleRegression createWeigthedLinearRegression( final double[] x, final double[] y, final double[] weigths) { double[] xW = new double[x.length]; double[] yW = new double[y.length]; int numZeroWeigths = 0; for (int i = 0; i < weigths.length; i++) { if (weigths[i] <= 0) { numZeroWeigths++; } } for (int i = 0; i < x.length; i++) { if (numZeroWeigths >= 0.4 * weigths.length) { // See: http://www.ncsu.edu/crsc/events/ugw07/Presentations/Crooks_Qiao/Crooks_Qiao_Alt_Presentation.pdf xW[i] = Math.sqrt(weigths[i]) * x[i]; yW[i] = Math.sqrt(weigths[i]) * y[i]; } else { xW[i] = x[i]; yW[i] = y[i]; } } return createLinearRegression(xW, yW); }
/** * Gets the median from a list of numbers. * * @param list the list of numbers * @return the median */ public static double median(final List<Double> list) { return getStatistics(list).getPercentile(50); }
/** * Gets the utilization estimates. * * @param utilizationHistoryReversed the utilization history reversed * @return the utilization estimates */ @Override protected double[] getParameterEstimates(double[] utilizationHistoryReversed) { return MathUtil.getRobustLoessParameterEstimates(utilizationHistoryReversed); }
/** * Gets utilization estimates. * * @param utilizationHistoryReversed the utilization history in reverse order * @return the utilization estimates */ protected double[] getParameterEstimates(double[] utilizationHistoryReversed) { return MathUtil.getLoessParameterEstimates(utilizationHistoryReversed); }
/** * Trims zeros at the end of an array. * * @param data the data array * @return the trimmed array */ public static double[] trimZeroTail(final double[] data) { return Arrays.copyOfRange(data, 0, countNonZeroBeginning(data)); }
/** * Computes the host utilization MAD used for generating the host over utilization threshold. * * @param host the host * @return the host utilization MAD * @throws {@inheritDoc} */ @Override public double computeHostUtilizationMeasure(final Host host) throws IllegalArgumentException { final double[] cpuUsageArray = getHostCpuUsageArray(host); if (MathUtil.countNonZeroBeginning(cpuUsageArray) >= MIN_HISTORY_ENTRIES_FOR_MAD) { return MathUtil.mad(cpuUsageArray); } throw new IllegalArgumentException("There is not enough Host history to compute Host utilization MAD"); }
/** * Computes the host utilization IRQ used for generating the host over utilization threshold. * * @param host the host * @return the host CPU utilization percentage IQR */ @Override public double computeHostUtilizationMeasure(final Host host) throws IllegalArgumentException { final double[] cpuUsageArray = getHostCpuUsageArray(host); if (MathUtil.countNonZeroBeginning(cpuUsageArray) >= MIN_HISTORY_ENTRIES_FOR_IRQ) { return MathUtil.iqr(cpuUsageArray); } throw new IllegalArgumentException("There is not enough Host history to compute Host utilization IRQ"); }
double stDevTimeBeforeHostShutdown = Double.NaN; if (!timeBeforeHostShutdown.isEmpty()) { meanTimeBeforeHostShutdown = MathUtil.mean(timeBeforeHostShutdown); stDevTimeBeforeHostShutdown = MathUtil.stDev(timeBeforeHostShutdown); double stDevTimeBeforeVmMigration = Double.NaN; if (!timeBeforeVmMigration.isEmpty()) { meanTimeBeforeVmMigration = MathUtil.mean(timeBeforeVmMigration); stDevTimeBeforeVmMigration = MathUtil.stDev(timeBeforeVmMigration); double stDevTimeBeforeContainerMigration = Double.NaN; if (!timeBeforeContainerMigration.isEmpty()) { meanTimeBeforeContainerMigration = MathUtil.mean(timeBeforeContainerMigration); stDevTimeBeforeContainerMigration = MathUtil.stDev(timeBeforeContainerMigration); double executionTimeVmSelectionMean = MathUtil.mean(vmAllocationPolicy .getExecutionTimeHistoryVmSelection()); double executionTimeVmSelectionStDev = MathUtil.stDev(vmAllocationPolicy .getExecutionTimeHistoryVmSelection()); double executionTimeHostSelectionMean = MathUtil.mean(vmAllocationPolicy .getExecutionTimeHistoryHostSelection()); double executionTimeHostSelectionStDev = MathUtil.stDev(vmAllocationPolicy .getExecutionTimeHistoryHostSelection()); double executionTimeVmReallocationMean = MathUtil.mean(vmAllocationPolicy .getExecutionTimeHistoryVmReallocation()); double executionTimeVmReallocationStDev = MathUtil.stDev(vmAllocationPolicy .getExecutionTimeHistoryVmReallocation()); double executionTimeTotalMean = MathUtil.mean(vmAllocationPolicy .getExecutionTimeHistoryTotal());
/** * Creates a a weighted linear regression. * @param x the independent variable * @param y the dependent variable * @param weights the weights to apply to x and y * @return */ private static SimpleRegression createWeightedLinearRegression( final double[] x, final double[] y, final double[] weights) { final double[] weightedX = new double[x.length]; final double[] weightedY = new double[y.length]; final long numZeroWeights = Arrays.stream(weights).filter(weight -> weight <= 0).count(); for (int i = 0; i < x.length; i++) { if (numZeroWeights >= 0.4 * weights.length) { // See: http://www.ncsu.edu/crsc/events/ugw07/Presentations/Crooks_Qiao/Crooks_Qiao_Alt_Presentation.pdf weightedX[i] = Math.sqrt(weights[i]) * x[i]; weightedY[i] = Math.sqrt(weights[i]) * y[i]; } else { weightedX[i] = x[i]; weightedY[i] = y[i]; } } return createLinearRegression(weightedX, weightedY); }
/** * Gets the median from an array of numbers. * * @param list the array of numbers * * @return the median */ public static double median(final double[] list) { return getStatistics(list).getPercentile(50); }
/** * Gets the utilization estimates. * * @param reversedUsageHistory the utilization history in reverse order * @return the utilization estimates */ @Override protected double[] getParameterEstimates(final double[] reversedUsageHistory) { return MathUtil.getRobustLoessParameterEstimates(reversedUsageHistory); }
/** * Gets utilization estimates. * * @param utilizationHistoryReversed the utilization history in reverse order * @return the utilization estimates */ protected double[] getParameterEstimates(final double... utilizationHistoryReversed) { return MathUtil.getLoessParameterEstimates(utilizationHistoryReversed); }