/** * {@inheritDoc} */ public override UnivariateStatistic copy() //TODO: Supposed to be public override Mean copy() { Mean result = new Mean(); // No try-catch or advertised exception because args are guaranteed non-null copy(this, result); return(result); }
/** * Copies source to dest. * <p>Neither source nor dest can be null.</p> * * @param source Mean to copy * @param dest Mean to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(Mean source, Mean dest) { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.incMoment = source.incMoment; dest.moment = (FirstMoment)source.moment.copy(); }
/** * <p>Returns the weighted variance of the entries in the specified portion of * the input array, or <code>Double.NaN</code> if the designated subarray * is empty.</p> * <p> * Uses the formula <pre> * Σ(weights[i]*(values[i] - weightedMean)<sup>2</sup>)/(Σ(weights[i]) - 1) * </pre> * where weightedMean is the weighted mean</p> * <p> * This formula will not return the same result as the unweighted variance when all * weights are equal, unless all weights are equal to 1. The formula assumes that * weights are to be treated as "expansion values," as will be the case if for example * the weights represent frequency counts. To normalize weights so that the denominator * in the variance computation equals the length of the input vector minus one, use <pre> * <code>evaluate(values, MathArrays.normalizeArray(weights, values.length)); </code> * </pre> * <p> * Returns 0 for a single-value (i.e. length = 1) sample.</p> * <p> * Throws <code>IllegalArgumentException</code> if any of the following are true: * <ul><li>the values array is null</li> * <li>the weights array is null</li> * <li>the weights array does not have the same length as the values array</li> * <li>the weights array contains one or more infinite values</li> * <li>the weights array contains one or more NaN values</li> * <li>the weights array contains negative values</li> * <li>the start and length arguments do not determine a valid array</li> * </ul></p> * <p> * Does not change the internal state of the statistic.</p> * <p> * Throws <code>MathIllegalArgumentException</code> if either array is null.</p> * * @param values the input array * @param weights the weights array * @param begin index of the first array element to include * @param length the number of elements to include * @return the weighted variance of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the parameters are not valid * @since 2.1 */ public double evaluate(double[] values, double[] weights, int begin, int length) { double var = Double.NaN; if (test(values, weights, begin, length)) { clear(); if (length == 1) { var = 0.0; } else if (length > 1) { Mean mean = new Mean(); double m = mean.evaluate(values, weights, begin, length); var = evaluate(values, weights, m, begin, length); } } return(var); }
/** * Copy constructor, creates a new {@code Mean} identical * to the {@code original} * * @param original the {@code Mean} instance to copy * @throws NullArgumentException if original is null */ public Mean(Mean original) { copy(original, this); }