Package org.apache.commons.math3.random

Examples of org.apache.commons.math3.random.RandomGenerator


  
  @Test
  public void testVersusHashMap() {
    FastByIDFloatMap actual = new FastByIDFloatMap();
    Map<Long,Float> expected = Maps.newHashMapWithExpectedSize(1000000);
    RandomGenerator r = RandomManager.getRandom();
    for (int i = 0; i < 1000000; i++) {
      double d = r.nextDouble();
      Long key = (long) r.nextInt(100);
      if (d < 0.4) {
        Number expectedValue = expected.get(key);
        float actualValue = actual.get(key);
        if (expectedValue == null) {
          assertNaN(actualValue);
View Full Code Here


    assertEquals(-1, bitSet.nextSetBit(NUM_BITS - 1));
  }

  @Test
  public void testNextBitSetRandom() {
    RandomGenerator random = RandomManager.getRandom();
    for (int i = 0; i < 100; i++) {
      BitSet bitSet = new BitSet(NUM_BITS);
      for (int j = 0; j < 20 + random.nextInt(50); j++) {
        bitSet.set(random.nextInt(NUM_BITS));
      }
      int from = random.nextInt(NUM_BITS);
      int nextSet = bitSet.nextSetBit(from);
      if (nextSet == -1) {
        for (int j = from; j < NUM_BITS; j++) {
          assertFalse(bitSet.get(j));
        }
View Full Code Here

  
  @Test
  public void testVersusHashMap() {
    FastByIDMap<String> actual = new FastByIDMap<String>();
    Map<Long, String> expected = Maps.newHashMapWithExpectedSize(1000000);
    RandomGenerator r = RandomManager.getRandom();
    for (int i = 0; i < 1000000; i++) {
      double d = r.nextDouble();
      Long key = (long) r.nextInt(100);
      if (d < 0.4) {
        assertEquals(expected.get(key), actual.get(key));
      } else {
        if (d < 0.7) {
          assertEquals(expected.put(key, "bang"), actual.put(key, "bang"));
View Full Code Here

    }
  }

  private static void assignVectorsParallel(FastByIDMap<float[]> vectors, double samplingRate, Node[][] map) {
    boolean doSample = samplingRate < 1.0;
    RandomGenerator random = RandomManager.getRandom();
    for (FastByIDMap.MapEntry<float[]> entry : vectors.entrySet()) {
      if (doSample && random.nextDouble() > samplingRate) {
        continue;
      }
      float[] V = entry.getValue();
      int[] bmuCoordinates = findBestMatchingUnit(V, map);
      if (bmuCoordinates != null) {
View Full Code Here

        add(mapRow[j].getCenter(), mean);
      }
    }
    divide(mean, mapSize * mapSize);

    RandomGenerator random = RandomManager.getRandom();
    float[] rBasis = RandomUtils.randomUnitVector(numFeatures, random);
    float[] gBasis = RandomUtils.randomUnitVector(numFeatures, random);
    float[] bBasis = RandomUtils.randomUnitVector(numFeatures, random);

    for (Node[] mapRow : map) {
View Full Code Here

    log.info("{}ms elapsed", elapsedMS);
    assertTrue(elapsedMS < 300 * iterations);
  }
 
  private static RealMatrix randomSymmetricMatrix(int dimension) {
    RandomGenerator random = RandomManager.getRandom();
    RealMatrix symmetric = new Array2DRowRealMatrix(dimension, dimension);
    for (int j = 0; j < dimension; j++) {
      // Diagonal
      symmetric.setEntry(j, j, random.nextDouble());
      for (int k = j + 1; k < dimension; k++) {
        // Off-diagonal
        double d = random.nextDouble();
        symmetric.setEntry(j, k, d);
        symmetric.setEntry(k, j, d);         
      }
    }
    return symmetric;
View Full Code Here

    maxBitsDiffering = bitsDiffering - 1;
    log.info("Max bits differing: {}", maxBitsDiffering);

    int features = Y.entrySet().iterator().next().getValue().length;

    RandomGenerator random = RandomManager.getRandom();
    randomVectors = new boolean[NUM_HASHES][features];
    for (boolean[] randomVector : randomVectors) {
      for (int j = 0; j < features; j++) {
        randomVector[j] = random.nextBoolean();
      }
    }

    meanVector = findMean(Y, features);
View Full Code Here

  @Test
  public void testLSH() {
    System.setProperty("model.lsh.sampleRatio", "0.1");
    System.setProperty("model.lsh.numHashes", "20");
    RandomGenerator random = RandomManager.getRandom();

    Mean avgPercentTopRecsConsidered = new Mean();
    Mean avgNDCG = new Mean();
    Mean avgPercentAllItemsConsidered= new Mean();
View Full Code Here

        ExecutorUtils.shutdownNowAndAwait(executor);       
      }
      return null;     
    }

    RandomGenerator random = RandomManager.getRandom();
    long[] testUserIDs = RandomUtils.chooseAboutNFromStream(NUM_USER_ITEMS_TO_TEST_CONVERGENCE,
                                                            RbyRow.keySetIterator(),
                                                            RbyRow.size(),
                                                            random);
    long[] testItemIDs = RandomUtils.chooseAboutNFromStream(NUM_USER_ITEMS_TO_TEST_CONVERGENCE,
View Full Code Here

    return null;
  }

  private FastByIDMap<float[]> constructInitialY(FastByIDMap<float[]> previousY) {

    RandomGenerator random = RandomManager.getRandom();
   
    FastByIDMap<float[]> randomY;
    if (previousY == null || previousY.isEmpty()) {
      // Common case: have to start from scratch
      log.info("Starting from new, random Y matrix");     
      randomY = new FastByIDMap<float[]>(RbyColumn.size());
     
    } else {
     
      int oldFeatureCount = previousY.entrySet().iterator().next().getValue().length;
      if (oldFeatureCount > features) {
        // Fewer features, use some dimensions from prior larger number of features as-is
        log.info("Feature count has decreased to {}, projecting down previous generation's Y matrix", features);               
        randomY = new FastByIDMap<float[]>(previousY.size());
        for (FastByIDMap.MapEntry<float[]> entry : previousY.entrySet()) {
          float[] oldLargerVector = entry.getValue();
          float[] newSmallerVector = new float[features];
          System.arraycopy(oldLargerVector, 0, newSmallerVector, 0, newSmallerVector.length);
          SimpleVectorMath.normalize(newSmallerVector);
          randomY.put(entry.getKey(), newSmallerVector);
        }
       
      } else if (oldFeatureCount < features) {
        log.info("Feature count has increased to {}, using previous generation's Y matrix as subspace", features);       
        randomY = new FastByIDMap<float[]>(previousY.size());
        for (FastByIDMap.MapEntry<float[]> entry : previousY.entrySet()) {
          float[] oldSmallerVector = entry.getValue();
          float[] newLargerVector = new float[features];
          System.arraycopy(oldSmallerVector, 0, newLargerVector, 0, oldSmallerVector.length);
          // Fill in new dimensions with random values
          for (int i = oldSmallerVector.length; i < newLargerVector.length; i++) {
            newLargerVector[i] = (float) random.nextGaussian();
          }
          SimpleVectorMath.normalize(newLargerVector);         
          randomY.put(entry.getKey(), newLargerVector);
        }
       
View Full Code Here

TOP

Related Classes of org.apache.commons.math3.random.RandomGenerator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.