Example usage for org.apache.commons.math.stat Frequency getPct

List of usage examples for org.apache.commons.math.stat Frequency getPct

Introduction

In this page you can find the example usage for org.apache.commons.math.stat Frequency getPct.

Prototype

public double getPct(char v) 

Source Link

Document

Returns the percentage of values that are equal to v (as a proportion between 0 and 1).

Usage

From source file:com.sse.abtester.VariationAssignerTest.java

/**
 * Test n variants./*from  w w w.  j  a v a2  s .c  o  m*/
 *
 * @param num_variants the num_variants
 */
public void testNVariants(int num_variants) {
    // make up some variants
    AbstractMap<String, IVariant<VariantBean>> coll = new HashMap<String, IVariant<VariantBean>>();

    for (int i = 0; i < num_variants; i++) {
        String name = "Bean_" + i;
        IVariant<VariantBean> vb = new VariantBean(name);
        vb.setDispatchable(true); // else non of the bean is
        // copied into normalized/weighted collections
        vb.setTargetFreq(1.0 / num_variants);
        vb.setVariationStrategy(new Default());
        coll.put(name, vb);
    }
    VariationAssigner<VariantBean> va = new VariationAssigner<VariantBean>();

    va.setIVariantCollection(coll);
    Frequency f = new Frequency();
    MockHttpServletRequest req = new MockHttpServletRequest();
    int TEST_LOOPS = 1000;
    double ALLOWABLE_DELTA = 100.0 / TEST_LOOPS; // rough check
    for (int i = 0; i < TEST_LOOPS; i++) {
        IVariant<VariantBean> assigned = va.enrollRequest(req);
        if (assigned != null)
            f.addValue(assigned.getName());
    }
    for (IVariant<VariantBean> vrb : coll.values()) {
        assertEquals(1.0 / num_variants, f.getPct(vrb.getName()), // NaN here can mean unknown name
                ALLOWABLE_DELTA);
    }
}