Class DynamicBin1D

  • All Implemented Interfaces:
    DoubleBufferConsumer, Serializable, Cloneable

    public class DynamicBin1Dextends QuantileBin1D
    1-dimensional rebinnable bin holding double elements; Efficiently computes advanced statistics of data sequences. Technically speaking, a multiset (or bag) with efficient statistics operations defined upon. First see the package summary and javadoc tree view to get the broad picture.

    The data filled into a DynamicBin1D is internally preserved in the bin. As a consequence this bin can compute more than only basic statistics. On the other hand side, if you add huge amounts of elements, you may run out of memory (each element takes 8 bytes). If this drawbacks matter, consider to use StaticBin1D, which overcomes them at the expense of limited functionality.

    This class is fully thread safe (all public methods are synchronized). Thus, you can have one or more threads adding to the bin as well as one or more threads reading and viewing the statistics of the bin while it is filled. For high performance, add data in large chunks (buffers) via method addAllOf rather than piecewise via method add.

    If your favourite statistics measure is not directly provided by this class, check out Descriptive in combination with methods elements() and sortedElements().

    Implementation: Lazy evaluation, caching, incremental maintainance.

    See Also:
    Descriptive, Serialized Form
    • Constructor Detail

      • DynamicBin1D

        public DynamicBin1D()
        Constructs and returns an empty bin; implicitly calls setFixedOrder(false).
    • Method Detail

      • add

        public void add(double element)
        Adds the specified element to the receiver.
        add in class StaticBin1D
        element - element to be appended.
      • addAllOfFromTo

        public void addAllOfFromTo(DoubleArrayList list,                  int from,                  int to)
        Adds the part of the specified list between indexes from (inclusive) and to (inclusive) to the receiver.
        addAllOfFromTo in class QuantileBin1D
        list - the list of which elements shall be added.
        from - the index of the first element to be added (inclusive).
        to - the index of the last element to be added (inclusive).
        IndexOutOfBoundsException - if list.size()>0 && (from<0 || from>to || to>=list.size()).
      • aggregate

        public double aggregate(DoubleDoubleFunction aggr,               DoubleFunction f)
        Applies a function to each element and aggregates the results.Returns a value v such that v==a(size()) where a(i) == aggr( a(i-1), f(x(i)) ) and terminators are a(1) == f(x(0)), a(0)==Double.NaN.


        cern.jet.math.Functions F = cern.jet.math.Functions.functions;bin = 0 1 2 3 // Sum( x[i]*x[i] ) bin.aggregate(F.plus,F.square);--> 14
        For further examples, see the package doc.
        aggr - an aggregation function taking as first argument the current aggregation and as second argument the transformed current element.
        f - a function transforming the current element.
        the aggregated measure.
        See Also:
      • clear

        public void clear()
        Removes all elements from the receiver. The receiver will be empty after this call returns.
        clear in class QuantileBin1D
      • clone

        public Object clone()
        Returns a deep copy of the receiver.
        clone in class QuantileBin1D
        a deep copy of the receiver.
      • correlation

        public double correlation(DynamicBin1D other)
        Returns the correlation of two bins, which is corr(x,y) = covariance(x,y) / (stdDev(x)*stdDev(y)) (Pearson's correlation coefficient). A correlation coefficient varies between -1 (for a perfect negative relationship) to +1 (for a perfect positive relationship). See the math definition and another def.
        other - the bin to compare with.
        the correlation.
        IllegalArgumentException - if size() != other.size().
      • covariance

        public double covariance(DynamicBin1D other)
        Returns the covariance of two bins, which is cov(x,y) = (1/size()) * Sum((x[i]-mean(x)) * (y[i]-mean(y))). See the math definition.
        other - the bin to compare with.
        the covariance.
        IllegalArgumentException - if size() != other.size().
      • elements

        public DoubleArrayList elements()
        Returns a copy of the currently stored elements. Concerning the order in which elements are returned, see setFixedOrder(boolean).
        a copy of the currently stored elements.
      • equals

        public boolean equals(Object object)
        Returns whether two bins are equal. They are equal if the other object is of the same class or a subclass of this class and both have the same size, minimum, maximum, sum and sumOfSquares and have the same elements, order being irrelevant (multiset equality).

        Definition of Equality for multisets: A,B are equal <=> A is a superset of B and B is a superset of A. (Elements must occur the same number of times, order is irrelevant.)

        equals in class AbstractBin1D
      • frequencies

        public void frequencies(DoubleArrayList distinctElements,               IntArrayList frequencies)
        Computes the frequency (number of occurances, count) of each distinct element. After this call returns both distinctElements and frequencies have a new size (which is equal for both), which is the number of distinct elements currently contained.

        Distinct elements are filled into distinctElements, starting at index 0. The frequency of each distinct element is filled into frequencies, starting at index 0. Further, both distinctElements and frequencies are sorted ascending by "element" (in sync, of course). As a result, the smallest distinct element (and its frequency) can be found at index 0, the second smallest distinct element (and its frequency) at index 1, ..., the largest distinct element (and its frequency) at index distinctElements.size()-1.

        elements = (8,7,6,6,7) --> distinctElements = (6,7,8), frequencies = (2,2,1)

        distinctElements - a list to be filled with the distinct elements; can have any size.
        frequencies - a list to be filled with the frequencies; can have any size; set this parameter to null to ignore it.
      • isRebinnable

        public boolean isRebinnable()
        Returns true. Returns whether a client can obtain all elements added to the receiver. In other words, tells whether the receiver internally preserves all added elements. If the receiver is rebinnable, the elements can be obtained via elements() methods.
        isRebinnable in class StaticBin1D
      • max

        public double max()
        Returns the maximum.
        max in class StaticBin1D
      • min

        public double min()
        Returns the minimum.
        min in class StaticBin1D
      • moment

        public double moment(int k,            double c)
        Returns the moment of k-th order with value c, which is Sum( (x[i]-c)k ) / size().
        moment in class MightyStaticBin1D
        k - the order; any number - can be less than zero, zero or greater than zero.
        c - any number.
        Double.NaN if !hasSumOfPower(k).
      • quantile

        public double quantile(double phi)
        Returns the exact phi-quantile; that is, the smallest contained element elem for which holds that phi percent of elements are less than elem.
        quantile in class QuantileBin1D
        phi - must satisfy 0 < phi < 1.
        the phi quantile element.
      • quantileInverse

        public double quantileInverse(double element)
        Returns exactly how many percent of the elements contained in the receiver are <= element. Does linear interpolation if the element is not contained but lies in between two contained elements.
        quantileInverse in class QuantileBin1D
        element - the element to search for.
        the exact percentage phi of elements <= element (0.0 <= phi <= 1.0).
      • quantiles

        public DoubleArrayList quantiles(DoubleArrayList percentages)
        Returns the exact quantiles of the specified percentages.
        quantiles in class QuantileBin1D
        percentages - the percentages for which quantiles are to be computed. Each percentage must be in the interval (0.0,1.0]. percentages must be sorted ascending.
        the exact quantiles.
      • removeAllOf

        public boolean removeAllOf(DoubleArrayList list)
        Removes from the receiver all elements that are contained in the specified list.
        list - the elements to be removed.
        true if the receiver changed as a result of the call.
      • sample

        public void sample(int n,          boolean withReplacement,          RandomEngine randomGenerator,          DoubleBuffer buffer)
        Uniformly samples (chooses) n random elements with or without replacement from the contained elements and adds them to the given buffer. If the buffer is connected to a bin, the effect is that the chosen elements are added to the bin connected to the buffer. Also see buffered.
        n - the number of elements to choose.
        withReplacement - true samples with replacement, otherwise samples without replacement.
        randomGenerator - a random number generator. Set this parameter to null to use a default random number generator seeded with the current time.
        buffer - the buffer to which chosen elements will be added.
        IllegalArgumentException - if !withReplacement && n > size().
        See Also:
      • sampleBootstrap

        public DynamicBin1D sampleBootstrap(DynamicBin1D other,                           int resamples,                           RandomEngine randomGenerator,                           BinBinFunction1D function)
        Generic bootstrap resampling. Quite optimized - Don't be afraid to try it. Executes resamples resampling steps. In each resampling step does the following:
        • Uniformly samples (chooses) size() random elements with replacement from this and fills them into an auxiliary bin b1.
        • Uniformly samples (chooses) other.size() random elements with replacement from other and fills them into another auxiliary bin b2.
        • Executes the comparison function function on both auxiliary bins (function.apply(b1,b2)) and adds the result of the function to an auxiliary bootstrap bin b3.

        Finally returns the auxiliary bootstrap bin b3 from which the measure of interest can be read off.


        Also see a more in-depth discussion on bootstrapping and related randomization methods.The classical statistical test for comparing the means of two samples is the t-test. Unfortunately, this test assumes that the two samples each come from a normal distribution and that these distributions have the same standard deviation. Quite often, however, data has a distribution that is non-normal in many ways. In particular, distributions are often unsymmetric. For such data, the t-test may produce misleading results and should thus not be used. Sometimes asymmetric data can be transformed into normally distributed data by taking e.g. the logarithm and the t-test will then produce valid results, but this still requires postulation of a certain distribution underlying the data, which is often not warranted, because too little is known about the data composition.

        Bootstrap resampling of means differences (and other differences) is a robust replacement for the t-test and does not require assumptions about the actual distribution of the data. The idea of bootstrapping is quite simple: simulation. The only assumption required is that the two samples a and b are representative for the underlying distribution with respect to the statistic that is being tested - this assumption is of course implicit in all statistical tests. We can now generate lots of further samples that correspond to the two given ones, by sampling with replacement. This process is called resampling. A resample can (and usually will) have a different mean than the original one and by drawing hundreds or thousands of such resamples ar from a and br from b we can compute the so-called bootstrap distribution of all the differences "mean of ar minus mean of br". That is, a bootstrap bin filled with the differences. Now we can compute, what fraction of these differences is, say, greater than zero. Let's assume we have computed 1000 resamples of both a and b and found that only 8 of the differences were greater than zero. Then 8/1000 or 0.008 is the p-value (probability) for the hypothesis that the mean of the distribution underlying a is actually larger than the mean of the distribution underlying b. From this bootstrap test, we can clearly reject the hypothesis.

        Instead of using means differences, we can also use other differences, for example, the median differences.

        Instead of p-values we can also read arbitrary confidence intervals from the bootstrap bin. For example, 90% of all bootstrap differences are left of the value -3.5, hence a left 90% confidence interval for the difference would be (3.5,infinity); in other words: the difference is 3.5 or larger with probability 0.9.

        Sometimes we would like to compare not only means and medians, but also the variability (spread) of two samples. The conventional method of doing this is the F-test, which compares the standard deviations. It is related to the t-test and, like the latter, assumes the two samples to come from a normal distribution. The F-test is very sensitive to data with deviations from normality. Instead we can again resort to more robust bootstrap resampling and compare a measure of spread, for example the inter-quartile range. This way we compute a bootstrap resampling of inter-quartile range differences in order to arrive at a test for inequality or variability.


        // v1,v2 - the two samples to compare against each otherdouble[] v1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10,  21,  22,23,24,25,26,27,28,29,30,31};double[] v2 = {10,11,12,13,14,15,16,17,18,19,  20,  30,31,32,33,34,35,36,37,38,39};hep.aida.bin.DynamicBin1D X = new hep.aida.bin.DynamicBin1D();hep.aida.bin.DynamicBin1D Y = new hep.aida.bin.DynamicBin1D();X.addAllOf(new cern.colt.list.DoubleArrayList(v1));Y.addAllOf(new cern.colt.list.DoubleArrayList(v2));cern.jet.random.engine.RandomEngine random = new cern.jet.random.engine.MersenneTwister();// bootstrap resampling of differences of means:BinBinFunction1D diff = new BinBinFunction1D() {   public double apply(DynamicBin1D x, DynamicBin1D y) {return x.mean() - y.mean();}};// bootstrap resampling of differences of medians:BinBinFunction1D diff = new BinBinFunction1D() {   public double apply(DynamicBin1D x, DynamicBin1D y) {return x.median() - y.median();}};// bootstrap resampling of differences of inter-quartile ranges:BinBinFunction1D diff = new BinBinFunction1D() {   public double apply(DynamicBin1D x, DynamicBin1D y) {return (x.quantile(0.75)-x.quantile(0.25)) - (y.quantile(0.75)-y.quantile(0.25)); }};DynamicBin1D boot = X.sampleBootstrap(Y,1000,random,diff);cern.jet.math.Functions F = cern.jet.math.Functions.functions;System.out.println("p-value="+ (boot.aggregate(F.plus, F.greater(0)) / boot.size()));System.out.println("left 90% confidence interval = ("+boot.quantile(0.9) + ",infinity)");-->// bootstrap resampling of differences of means:p-value=0.0080left 90% confidence interval = (-3.571428571428573,infinity)// bootstrap resampling of differences of medians:p-value=0.36left 90% confidence interval = (5.0,infinity)// bootstrap resampling of differences of inter-quartile ranges:p-value=0.5699left 90% confidence interval = (5.0,infinity)
        other - the other bin to compare the receiver against.
        resamples - the number of times resampling shall be done.
        randomGenerator - a random number generator. Set this parameter to null to use a default random number generator seeded with the current time.
        function - a difference function comparing two samples; takes as first argument a sample of this and as second argument a sample of other.
        a bootstrap bin holding the results of function of each resampling step.
        See Also:
      • setFixedOrder

        public void setFixedOrder(boolean fixedOrder)
        Determines whether the receivers internally preserved elements may be reordered or not.
        • fixedOrder==false allows the order in which elements are returned by method elements() to be different from the order in which elements are added.
        • fixedOrder==true guarantees that under all circumstances the order in which elements are returned by method elements() is identical to the order in which elements are added. However, the latter consumes twice as much memory if operations involving sorting are requested. This option is usually only required if a 2-dimensional bin, formed by two 1-dimensional bins, needs to be rebinnable.

        Naturally, if fixedOrder is set to true you should not already have added elements to the receiver; it should be empty.

      • size

        public int size()
        Returns the number of elements contained in the receiver.
        size in class StaticBin1D
      • sortedElements

        public DoubleArrayList sortedElements()
        Returns a copy of the currently stored elements, sorted ascending. Concerning the memory required for operations involving sorting, see setFixedOrder(boolean).
        a copy of the currently stored elements, sorted ascending.
      • standardize

        public void standardize(double mean,               double standardDeviation)
        Modifies the receiver to be standardized. Changes each element x[i] as follows: x[i] = (x[i]-mean)/standardDeviation.
      • sum

        public double sum()
        Returns the sum of all elements, which is Sum( x[i] ).
        sum in class StaticBin1D
      • sumOfSquares

        public double sumOfSquares()
        Returns the sum of squares, which is Sum( x[i] * x[i] ).
        sumOfSquares in class StaticBin1D
      • trim

        public void trim(int s,        int l)
        Removes the s smallest and l largest elements from the receiver. The receivers size will be reduced by s + l elements.
        s - the number of smallest elements to trim away (s >= 0).
        l - the number of largest elements to trim away (l >= 0).
      • trimmedMean

        public double trimmedMean(int s,                 int l)
        Returns the trimmed mean. That is the mean of the data if the s smallest and l largest elements would be removed from the receiver (they are not removed).
        s - the number of smallest elements to trim away (s >= 0).
        l - the number of largest elements to trim away (l >= 0).
        the trimmed mean.
      • trimToSize

        public void trimToSize()
        Trims the capacity of the receiver to be the receiver's current size. (This has nothing to do with trimming away smallest and largest elements. The method name is used to be consistent with JDK practice.)

        Releases any superfluos internal memory. An application can use this operation to minimize the storage of the receiver. Does not affect functionality.

        trimToSize in class AbstractBin1D

SCaVis 2.2 © jWork.ORG