/** * Generates a random value from the {@link ZipfDistribution Zipf Distribution}. * * @param numberOfElements the number of elements of the ZipfDistribution * @param exponent the exponent of the ZipfDistribution * @return random value sampled from the Zipf(numberOfElements, exponent) distribution * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0} * or {@code exponent <= 0}. */ public int nextZipf(int numberOfElements, double exponent) throws NotStrictlyPositiveException { return new ZipfDistribution(getRandomGenerator(), numberOfElements, exponent).sample(); }
/** * Used by {@link #getNumericalVariance()}. * * @return the variance of this distribution */ protected double calculateNumericalVariance() { final int N = getNumberOfElements(); final double s = getExponent(); final double Hs2 = generalizedHarmonic(N, s - 2); final double Hs1 = generalizedHarmonic(N, s - 1); final double Hs = generalizedHarmonic(N, s); return (Hs2 / Hs) - ((Hs1 * Hs1) / (Hs * Hs)); }
/** * {@inheritDoc} * * For number of elements {@code N} and exponent {@code s}, the mean is * {@code Hs1 / Hs}, where * <ul> * <li>{@code Hs1 = generalizedHarmonic(N, s - 1)},</li> * <li>{@code Hs = generalizedHarmonic(N, s)}.</li> * </ul> */ public double getNumericalMean() { if (!numericalMeanIsCalculated) { numericalMean = calculateNumericalMean(); numericalMeanIsCalculated = true; } return numericalMean; }
public ZipfianStringKeyGenerator(boolean preLoadKeys, int numKeys, double exponent) { super(numKeys, preLoadKeys); this.zipf = new ZipfDistribution(numKeys, exponent); }
/** * @param param * 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 ZipfDistribution(param, param2); } return zipf; }
Integer startInt = schema.getStartInt(); cardinality = schema.getEndInt() - startInt; ZipfDistribution zipf = new ZipfDistribution(cardinality, schema.getZipfExponent()); for (int i = 0; i < cardinality; i++) { probabilities.add(new Pair<>((Object) (i + startInt), zipf.probability(i))); ZipfDistribution zipf = new ZipfDistribution(enumeratedValues.size(), schema.getZipfExponent()); for (int i = 0; i < cardinality; i++) { probabilities.add(new Pair<>(enumeratedValues.get(i), zipf.probability(i)));
ZipfDistribution za = (ZipfDistribution) a; ZipfDistribution zb = (ZipfDistribution) b; return za.getNumberOfElements() == zb.getNumberOfElements() && za.getExponent() == zb.getNumberOfElements(); } else { throw new UnsupportedOperationException("Unknown or not supported IntegerDistribution: " + c);
ZipfDistribution zdiBanks = new ZipfDistribution((Integer) state.get("numBanks"), 1); String bank = Utils.getBank(zdiBanks.inverseCumulativeProbability(rand.nextDouble())); ZipfDistribution zdiAccts = new ZipfDistribution(numAccts, 1); String acct1 = Utils.getAccount(zdiAccts.inverseCumulativeProbability(rand.nextDouble())); String acct2 = Utils.getAccount(zdiAccts.inverseCumulativeProbability(rand.nextDouble())); while (acct2.equals(acct1)) {
private int getNextVal(int mult) { int rval; if (zipfian) { rval = zipf.sample(); } else { rval = (int) Math.abs(Math.floor(rng.nextDouble() * mult)); } return rval; }
/** {@inheritDoc} */ public double cumulativeProbability(final int x) { if (x <= 0) { return 0.0; } else if (x >= numberOfElements) { return 1.0; } return generalizedHarmonic(x, exponent) / generalizedHarmonic(numberOfElements, exponent); }
/** * {@inheritDoc} * * The upper bound of the support is the number of elements. * * @return upper bound of the support */ public int getSupportUpperBound() { return getNumberOfElements(); }
/** * {@inheritDoc} * * For number of elements {@code N} and exponent {@code s}, the mean is * {@code (Hs2 / Hs) - (Hs1^2 / Hs^2)}, where * <ul> * <li>{@code Hs2 = generalizedHarmonic(N, s - 2)},</li> * <li>{@code Hs1 = generalizedHarmonic(N, s - 1)},</li> * <li>{@code Hs = generalizedHarmonic(N, s)}.</li> * </ul> */ public double getNumericalVariance() { if (!numericalVarianceIsCalculated) { numericalVariance = calculateNumericalVariance(); numericalVarianceIsCalculated = true; } return numericalVariance; }
public ZipfianStringKeyGenerator(boolean preLoadKeys, int numKeys, double exponent) { super(numKeys, preLoadKeys); this.zipf = new ZipfDistribution(numKeys, exponent); }
ZipfDistribution za = (ZipfDistribution) a; ZipfDistribution zb = (ZipfDistribution) b; return za.getNumberOfElements() == zb.getNumberOfElements() && za.getExponent() == zb.getNumberOfElements(); } else { throw new UnsupportedOperationException("Unknown or not supported IntegerDistribution: " + c);
@Override public String getNextKey() { int keyIndex = zipf.sample(); if (isPreLoadKeys()) { return keys.get(keyIndex); } else { return "T" + keyIndex; } } }
/** {@inheritDoc} */ public double probability(final int x) { if (x <= 0 || x > numberOfElements) { return 0.0; } return (1.0 / FastMath.pow(x, exponent)) / generalizedHarmonic(numberOfElements, exponent); }
/** * {@inheritDoc} * * The upper bound of the support is the number of elements. * * @return upper bound of the support */ public int getSupportUpperBound() { return getNumberOfElements(); }
/** * {@inheritDoc} * * For number of elements {@code N} and exponent {@code s}, the mean is * {@code (Hs2 / Hs) - (Hs1^2 / Hs^2)}, where * <ul> * <li>{@code Hs2 = generalizedHarmonic(N, s - 2)},</li> * <li>{@code Hs1 = generalizedHarmonic(N, s - 1)},</li> * <li>{@code Hs = generalizedHarmonic(N, s)}.</li> * </ul> */ public double getNumericalVariance() { if (!numericalVarianceIsCalculated) { numericalVariance = calculateNumericalVariance(); numericalVarianceIsCalculated = true; } return numericalVariance; }
protected String prepareTestDate() throws IOException { String[] allKeys = new String[KEY_SPACE]; for (int i = 0; i < KEY_SPACE; i++) { allKeys[i] = RandomStringUtils.randomAlphabetic(10); } outputMsg("Start to create test random data..."); long startTime = System.currentTimeMillis(); ZipfDistribution zipf = new ZipfDistribution(KEY_SPACE, 0.5); int keyIndex; File tempFile = File.createTempFile("ZipfDistribution", ".txt"); if (tempFile.exists()) FileUtils.forceDelete(tempFile); Writer fw = new OutputStreamWriter(new FileOutputStream(tempFile), StandardCharsets.UTF_8); try { for (int i = 0; i < TOTAL_RECORDS; i++) { keyIndex = zipf.sample() - 1; fw.write(allKeys[keyIndex]); fw.write('\n'); } } finally { if (fw != null) fw.close(); } outputMsg("Create test data takes : " + (System.currentTimeMillis() - startTime) / 1000 + " seconds."); outputMsg("Test data in : " + tempFile.getAbsolutePath()); return tempFile.getAbsolutePath(); }
@Override public Object doWork(Object first, Object second) throws IOException{ if(null == first){ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory))); } if(null == second){ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory))); } Number size = (Number)first; Number exp = (Number)second; return new ZipfDistribution(size.intValue(), exp.doubleValue()); } }