Example usage for org.apache.commons.math.distribution ZipfDistributionImpl ZipfDistributionImpl

List of usage examples for org.apache.commons.math.distribution ZipfDistributionImpl ZipfDistributionImpl

Introduction

In this page you can find the example usage for org.apache.commons.math.distribution ZipfDistributionImpl ZipfDistributionImpl.

Prototype

public ZipfDistributionImpl(final int numberOfElements, final double exponent) throws IllegalArgumentException 

Source Link

Document

Create a new Zipf distribution with the given number of elements and exponent.

Usage

From source file:geogebra.kernel.statistics.AlgoDistribution.java

ZipfDistribution getZipfDistribution(int param, double param2) {
    if (zipf == null)
        zipf = new ZipfDistributionImpl(param, param2);
    else {/*from w w w . j  a  va2  s  .  c o  m*/
        zipf.setNumberOfElements(param);
        zipf.setExponent(param2);
    }
    return zipf;
}

From source file:geogebra.common.kernel.statistics.AlgoDistribution.java

/**
 * @param param//from   w ww .  j  a v  a  2  s . c om
 *            number of elements
 * @param param2
 *            exponent
 * @return Zipf distribution
 */
ZipfDistribution getZipfDistribution(int param, double param2) {
    if (zipf == null || zipf.getNumberOfElements() != param || zipf.getExponent() != param2)
        zipf = new ZipfDistributionImpl(param, param2);
    return zipf;
}

From source file:geogebra.common.kernel.algos.AlgoBarChart.java

/**
 * Prepares list1 and list2 for use with probability distribution bar charts
 *///from w  ww. j  av  a 2s.  com
private boolean prepareDistributionLists() {
    IntegerDistribution dist = null;
    int first = 0, last = 0;
    try {
        // get the distribution and the first, last list values for given
        // distribution type
        switch (type) {
        case TYPE_BARCHART_BINOMIAL:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            int n = (int) Math.round(p1.getDouble());
            double p = p2.getDouble();
            dist = new BinomialDistributionImpl(n, p);
            first = 0;
            last = n;
            break;

        case TYPE_BARCHART_PASCAL:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            n = (int) Math.round(p1.getDouble());
            p = p2.getDouble();
            dist = new PascalDistributionImpl(n, p);

            first = 0;
            last = (int) Math.max(1, (kernel).getXmax() + 1);
            break;
        case TYPE_BARCHART_ZIPF:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            n = (int) Math.round(p1.getDouble());
            p = p2.getDouble();
            dist = new ZipfDistributionImpl(n, p);

            first = 0;
            last = n;
            break;
        case TYPE_BARCHART_POISSON:
            if (!p1geo.isDefined())
                return false;
            double lambda = p1.getDouble();
            dist = new PoissonDistributionImpl(lambda);
            first = 0;
            last = (int) Math.max(1, kernel.getXmax() + 1);
            break;

        case TYPE_BARCHART_HYPERGEOMETRIC:
            if (!(p1geo.isDefined() && p2geo.isDefined() && p3geo.isDefined()))
                return false;
            int pop = (int) p1.getDouble();
            int successes = (int) p2.getDouble();
            int sample = (int) p3.getDouble();
            dist = new HypergeometricDistributionImpl(pop, successes, sample);
            first = Math.max(0, successes + sample - pop);
            last = Math.min(successes, sample);
            break;
        }

        // load class list and probability list
        loadDistributionLists(first, last, dist);
    }

    catch (Exception e) {
        App.debug(e.getMessage());
        return false;
    }

    return true;
}

From source file:geogebra.kernel.AlgoFunctionAreaSums.java

/**
 * Prepares list1 and list2 for use with probability distribution bar charts
 */// w  ww.  ja  v a  2s  .c om
private boolean prepareDistributionLists() {
    IntegerDistribution dist = null;
    int first = 0, last = 0;
    try {
        // get the distribution and the first, last list values for given distribution type
        switch (type) {
        case TYPE_BARCHART_BINOMIAL:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            int n = (int) Math.round(p1.getDouble());
            double p = p2.getDouble();
            dist = new BinomialDistributionImpl(n, p);
            first = 0;
            last = n;
            break;

        case TYPE_BARCHART_PASCAL:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            n = (int) Math.round(p1.getDouble());
            p = p2.getDouble();
            dist = new PascalDistributionImpl(n, p);

            first = 0;
            last = (int) Math.max(1, kernel.getXmax() + 1);
            break;
        case TYPE_BARCHART_ZIPF:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            n = (int) Math.round(p1.getDouble());
            p = p2.getDouble();
            dist = new ZipfDistributionImpl(n, p);

            first = 0;
            last = n;
            break;
        case TYPE_BARCHART_POISSON:
            if (!p1geo.isDefined())
                return false;
            double lambda = p1.getDouble();
            dist = new PoissonDistributionImpl(lambda);
            first = 0;
            last = (int) Math.max(1, kernel.getXmax() + 1);
            break;

        case TYPE_BARCHART_HYPERGEOMETRIC:
            if (!(p1geo.isDefined() && p2geo.isDefined() && p3geo.isDefined()))
                return false;
            int pop = (int) p1.getDouble();
            int successes = (int) p2.getDouble();
            int sample = (int) p3.getDouble();
            dist = new HypergeometricDistributionImpl(pop, successes, sample);
            first = Math.max(0, successes + sample - pop);
            last = Math.min(successes, sample);
            break;
        }

        // load class list and probability list
        loadDistributionLists(first, last, dist);
    }

    catch (Exception e) {
        Application.debug(e.getMessage());
        return false;
    }

    return true;
}

From source file:geogebra.common.kernel.algos.AlgoFunctionAreaSums.java

/**
 * Prepares list1 and list2 for use with probability distribution bar charts
 *///ww w.j  a  v  a2s.  c  o m
private boolean prepareDistributionLists() {
    IntegerDistribution dist = null;
    int first = 0, last = 0;
    try {
        // get the distribution and the first, last list values for given
        // distribution type
        switch (type) {
        case BARCHART_BINOMIAL:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            int n = (int) Math.round(p1.getDouble());
            double p = p2.getDouble();
            dist = new BinomialDistributionImpl(n, p);
            first = 0;
            last = n;
            break;

        case BARCHART_PASCAL:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            n = (int) Math.round(p1.getDouble());
            p = p2.getDouble();
            dist = new PascalDistributionImpl(n, p);

            first = 0;
            last = (int) Math.max(1, (kernel).getXmax() + 1);
            break;
        case BARCHART_ZIPF:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            n = (int) Math.round(p1.getDouble());
            p = p2.getDouble();
            dist = new ZipfDistributionImpl(n, p);

            first = 0;
            last = n;
            break;
        case BARCHART_POISSON:
            if (!p1geo.isDefined())
                return false;
            double lambda = p1.getDouble();
            dist = new PoissonDistributionImpl(lambda);
            first = 0;
            last = (int) Math.max(1, kernel.getXmax() + 1);
            break;

        case BARCHART_HYPERGEOMETRIC:
            if (!(p1geo.isDefined() && p2geo.isDefined() && p3geo.isDefined()))
                return false;
            int pop = (int) p1.getDouble();
            int successes = (int) p2.getDouble();
            int sample = (int) p3.getDouble();
            dist = new HypergeometricDistributionImpl(pop, successes, sample);
            first = Math.max(0, successes + sample - pop);
            last = Math.min(successes, sample);
            break;
        }

        // load class list and probability list
        loadDistributionLists(first, last, dist);
    }

    catch (Exception e) {
        App.debug(e.getMessage());
        return false;
    }

    return true;
}

From source file:org.apache.accumulo.test.randomwalk.conditional.Transfer.java

@Override
public void visit(State state, Environment env, Properties props) throws Exception {
    String table = state.getString("tableName");
    Random rand = (Random) state.get("rand");
    Connector conn = env.getConnector();

    int numAccts = (Integer) state.get("numAccts");
    // note: non integer exponents are slow

    ZipfDistributionImpl zdiBanks = new ZipfDistributionImpl((Integer) state.get("numBanks"), 1);
    String bank = Utils.getBank(zdiBanks.inverseCumulativeProbability(rand.nextDouble()));
    ZipfDistributionImpl zdiAccts = new ZipfDistributionImpl(numAccts, 1);
    String acct1 = Utils.getAccount(zdiAccts.inverseCumulativeProbability(rand.nextDouble()));
    String acct2 = Utils.getAccount(zdiAccts.inverseCumulativeProbability(rand.nextDouble()));
    while (acct2.equals(acct1)) {
        // intentionally not using zipf distribution to pick on retry
        acct2 = Utils.getAccount(rand.nextInt(numAccts));
    }// w  w w. ja v a2  s  .  c  o m

    // TODO document how data should be read when using ConditionalWriter
    Scanner scanner = new IsolatedScanner(conn.createScanner(table, Authorizations.EMPTY));

    scanner.setRange(new Range(bank));
    scanner.fetchColumnFamily(new Text(acct1));
    scanner.fetchColumnFamily(new Text(acct2));

    Account a1 = new Account();
    Account a2 = new Account();
    Account a;

    for (Entry<Key, Value> entry : scanner) {
        String cf = entry.getKey().getColumnFamilyData().toString();
        String cq = entry.getKey().getColumnQualifierData().toString();

        if (cf.equals(acct1))
            a = a1;
        else if (cf.equals(acct2))
            a = a2;
        else
            throw new Exception("Unexpected column fam: " + cf);

        if (cq.equals("bal"))
            a.setBal(entry.getValue().toString());
        else if (cq.equals("seq"))
            a.setSeq(entry.getValue().toString());
        else
            throw new Exception("Unexpected column qual: " + cq);
    }

    int amt = rand.nextInt(50);

    log.debug("transfer req " + bank + " " + amt + " " + acct1 + " " + a1 + " " + acct2 + " " + a2);

    if (a1.bal >= amt) {
        ConditionalMutation cm = new ConditionalMutation(bank,
                new Condition(acct1, "seq").setValue(Utils.getSeq(a1.seq)),
                new Condition(acct2, "seq").setValue(Utils.getSeq(a2.seq)));
        cm.put(acct1, "bal", (a1.bal - amt) + "");
        cm.put(acct2, "bal", (a2.bal + amt) + "");
        cm.put(acct1, "seq", Utils.getSeq(a1.seq + 1));
        cm.put(acct2, "seq", Utils.getSeq(a2.seq + 1));

        ConditionalWriter cw = (ConditionalWriter) state.get("cw");
        Status status = cw.write(cm).getStatus();
        while (status == Status.UNKNOWN) {
            log.debug("retrying transfer " + status);
            status = cw.write(cm).getStatus();
        }
        log.debug("transfer result " + bank + " " + status + " " + a1 + " " + a2);
    }

}