📄 statutils.java
字号:
public static double variance(final double[] values) { return variance.evaluate(values); } /** * Returns the variance of the entries in the specified portion of * the input array, or <code>Double.NaN</code> if the designated subarray * is empty. * <p> * See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for * details on the computing algorithm.</p> * <p> * Returns 0 for a single-value (i.e. length = 1) sample.</p> * <p> * Throws <code>IllegalArgumentException</code> if the array is null or the * array index parameters are not valid.</p> * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the variance of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double variance(final double[] values, final int begin, final int length) { return variance.evaluate(values, begin, length); } /** * Returns the variance of the entries in the specified portion of * the input array, using the precomputed mean value. Returns * <code>Double.NaN</code> if the designated subarray is empty. * <p> * See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for * details on the computing algorithm.</p> * <p> * The formula used assumes that the supplied mean value is the arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.</p> * <p> * Returns 0 for a single-value (i.e. length = 1) sample.</p> * <p> * Throws <code>IllegalArgumentException</code> if the array is null or the * array index parameters are not valid.</p> * * @param values the input array * @param mean the precomputed mean value * @param begin index of the first array element to include * @param length the number of elements to include * @return the variance of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double variance(final double[] values, final double mean, final int begin, final int length) { return variance.evaluate(values, mean, begin, length); } /** * Returns the variance of the entries in the input array, using the * precomputed mean value. Returns <code>Double.NaN</code> if the array * is empty. * <p> * See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for * details on the computing algorithm.</p> * <p> * The formula used assumes that the supplied mean value is the arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.</p> * <p> * Returns 0 for a single-value (i.e. length = 1) sample.</p> * <p> * Throws <code>IllegalArgumentException</code> if the array is null.</p> * * @param values the input array * @param mean the precomputed mean value * @return the variance of the values or Double.NaN if the array is empty * @throws IllegalArgumentException if the array is null */ public static double variance(final double[] values, final double mean) { return variance.evaluate(values, mean); } /** * Returns the maximum of the entries in the input array, or * <code>Double.NaN</code> if the array is empty. * <p> * Throws <code>IllegalArgumentException</code> if the array is null.</p> * <p> * <ul> * <li>The result is <code>NaN</code> iff all values are <code>NaN</code> * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li> * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>, * the result is <code>Double.POSITIVE_INFINITY.</code></li> * </ul></p> * * @param values the input array * @return the maximum of the values or Double.NaN if the array is empty * @throws IllegalArgumentException if the array is null */ public static double max(final double[] values) { return max.evaluate(values); } /** * Returns the maximum of the entries in the specified portion of * the input array, or <code>Double.NaN</code> if the designated subarray * is empty. * <p> * Throws <code>IllegalArgumentException</code> if the array is null or * the array index parameters are not valid.</p> * <p> * <ul> * <li>The result is <code>NaN</code> iff all values are <code>NaN</code> * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li> * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>, * the result is <code>Double.POSITIVE_INFINITY.</code></li> * </ul></p> * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the maximum of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double max(final double[] values, final int begin, final int length) { return max.evaluate(values, begin, length); } /** * Returns the minimum of the entries in the input array, or * <code>Double.NaN</code> if the array is empty. * <p> * Throws <code>IllegalArgumentException</code> if the array is null.</p> * <p> * <ul> * <li>The result is <code>NaN</code> iff all values are <code>NaN</code> * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li> * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>, * the result is <code>Double.NEGATIVE_INFINITY.</code></li> * </ul> </p> * * @param values the input array * @return the minimum of the values or Double.NaN if the array is empty * @throws IllegalArgumentException if the array is null */ public static double min(final double[] values) { return min.evaluate(values); } /** * Returns the minimum of the entries in the specified portion of * the input array, or <code>Double.NaN</code> if the designated subarray * is empty. * <p> * Throws <code>IllegalArgumentException</code> if the array is null or * the array index parameters are not valid.</p> * <p> * <ul> * <li>The result is <code>NaN</code> iff all values are <code>NaN</code> * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li> * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>, * the result is <code>Double.NEGATIVE_INFINITY.</code></li> * </ul></p> * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the minimum of the values or Double.NaN if length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double min(final double[] values, final int begin, final int length) { return min.evaluate(values, begin, length); } /** * Returns an estimate of the <code>p</code>th percentile of the values * in the <code>values</code> array. * <p> * <ul> * <li>Returns <code>Double.NaN</code> if <code>values</code> has length * <code>0</code></li></p> * <li>Returns (for any value of <code>p</code>) <code>values[0]</code> * if <code>values</code> has length <code>1</code></li> * <li>Throws <code>IllegalArgumentException</code> if <code>values</code> * is null or p is not a valid quantile value (p must be greater than 0 * and less than or equal to 100)</li> * </ul></p> * <p> * See {@link org.apache.commons.math.stat.descriptive.rank.Percentile} for * a description of the percentile estimation algorithm used.</p> * * @param values input array of values * @param p the percentile value to compute * @return the percentile value or Double.NaN if the array is empty * @throws IllegalArgumentException if <code>values</code> is null * or p is invalid */ public static double percentile(final double[] values, final double p) { return percentile.evaluate(values,p); } /** * Returns an estimate of the <code>p</code>th percentile of the values * in the <code>values</code> array, starting with the element in (0-based) * position <code>begin</code> in the array and including <code>length</code> * values. * <p> * <ul> * <li>Returns <code>Double.NaN</code> if <code>length = 0</code></li> * <li>Returns (for any value of <code>p</code>) <code>values[begin]</code> * if <code>length = 1 </code></li> * <li>Throws <code>IllegalArgumentException</code> if <code>values</code> * is null , <code>begin</code> or <code>length</code> is invalid, or * <code>p</code> is not a valid quantile value (p must be greater than 0 * and less than or equal to 100)</li> * </ul></p> * <p> * See {@link org.apache.commons.math.stat.descriptive.rank.Percentile} for * a description of the percentile estimation algorithm used.</p> * * @param values array of input values * @param p the percentile to compute * @param begin the first (0-based) element to include in the computation * @param length the number of array elements to include * @return the percentile value * @throws IllegalArgumentException if the parameters are not valid or the * input array is null */ public static double percentile(final double[] values, final int begin, final int length, final double p) { return percentile.evaluate(values, begin, length, p); } /** * Returns the sum of the (signed) differences between corresponding elements of the * input arrays -- i.e., sum(sample1[i] - sample2[i]). * * @param sample1 the first array * @param sample2 the second array * @return sum of paired differences * @throws IllegalArgumentException if the arrays do not have the same * (positive) length */ public static double sumDifference(final double[] sample1, final double[] sample2) throws IllegalArgumentException { int n = sample1.length; if (n != sample2.length || n < 1) { throw new IllegalArgumentException ("Input arrays must have the same (positive) length."); } double result = 0; for (int i = 0; i < n; i++) { result += sample1[i] - sample2[i]; } return result; } /** * Returns the mean of the (signed) differences between corresponding elements of the * input arrays -- i.e., sum(sample1[i] - sample2[i]) / sample1.length. * * @param sample1 the first array * @param sample2 the second array * @return mean of paired differences * @throws IllegalArgumentException if the arrays do not have the same * (positive) length */ public static double meanDifference(final double[] sample1, final double[] sample2) throws IllegalArgumentException { return sumDifference(sample1, sample2) / (double) sample1.length; } /** * Returns the variance of the (signed) differences between corresponding elements of the * input arrays -- i.e., var(sample1[i] - sample2[i]). * * @param sample1 the first array * @param sample2 the second array * @param meanDifference the mean difference between corresponding entries * @see #meanDifference(double[],double[]) * @return variance of paired differences * @throws IllegalArgumentException if the arrays do not have the same * length or their common length is less than 2. */ public static double varianceDifference(final double[] sample1, final double[] sample2, double meanDifference) throws IllegalArgumentException { double sum1 = 0d; double sum2 = 0d; double diff = 0d; int n = sample1.length; if (n < 2 || n != sample2.length) { throw new IllegalArgumentException("Input array lengths must be equal and at least 2."); } for (int i = 0; i < n; i++) { diff = sample1[i] - sample2[i]; sum1 += (diff - meanDifference) *(diff - meanDifference); sum2 += diff - meanDifference; } return (sum1 - (sum2 * sum2 / (double) n)) / (double) (n - 1); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -