org.apache.commons.math.random.RandomData Java Examples

The following examples show how to use org.apache.commons.math.random.RandomData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testWithInitialCapacity() {

    ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
    Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());

    RandomData randomData = new RandomDataImpl();
    int iterations = randomData.nextInt(100, 1000);

    for( int i = 0; i < iterations; i++) {
        eDA2.addElement( i );
    }

    Assert.assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements());

    eDA2.addElement( 2.0 );

    Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations + 1 , eDA2.getNumElements() );
}
 
Example #2
Source File: MathUtilTest.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Run nextInt range for 10000. Time:9, Heap:0.0M
 	0: 44.8%
	1: 31.42%
	2: 16.7%
	3: 5.54%
	4: 1.31%
	5: 0.2%
	6: 0.03%
	7: 0.0%
	8: 0.0%
	9: 0.0%
	
 * @throws Exception
 */
@Test
public void testNextInt() throws Exception {
	int max = 10000;
	final int[] ratio1 = new int[10];
	
	final RandomData commonRandom = new RandomDataImpl();
	TestUtil.doPerform(new Runnable() {
		public void run() {
			int r = MathUtil.nextGaussionInt(0, 10);
			ratio1[r]++;
		}
	}, "nextInt range", max);
	
	printRatio(ratio1, max);
}
 
Example #3
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testWithInitialCapacity() {

    ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
    Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());

    RandomData randomData = new RandomDataImpl();
    int iterations = randomData.nextInt(100, 1000);

    for( int i = 0; i < iterations; i++) {
        eDA2.addElement( i );
    }

    Assert.assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements());

    eDA2.addElement( 2.0 );

    Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations + 1 , eDA2.getNumElements() );
}
 
Example #4
Source File: MathUtilTest.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Run Uniform Random for 100000000. Time:29752, Heap:0.2576065M
 * Normal Distribution [1.9831936257114444E-9, 0.958216890674393]
 * Run Uniform Random for 100000. Time:25, Heap:0.0M
 * 
 * @throws Exception
 */
@Test
public void testNextGaussianDouble() throws Exception {
	int max = 100000;
	
	final RandomData commonRandom = new RandomDataImpl();
	final double[] result = new double[2];
	result[0] = Double.MAX_VALUE;
	result[1] = Double.MIN_NORMAL;
	TestUtil.doPerform(new Runnable() {
		public void run() {
			double r  = MathUtil.nextGaussionDouble();
			if ( result[0]>r ) {
				result[0] = r;
			}
			if ( result[1]<r ) {
				result[1] = r;
			}
		}
	}, "Uniform Random", max);
	
	System.out.println("Normal Distribution ["+result[0]+", "+result[1]+"]");
}
 
Example #5
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 *
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example #6
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example #7
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example #8
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 * 
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example #9
Source File: LSHPigTest.java    From datafu with Apache License 2.0 5 votes vote down vote up
@Test
public void testL1UDF() throws Exception
{
  setMemorySettings();
  RandomGenerator rg = new JDKRandomGenerator();
  rg.setSeed(0);
  RandomData rd = new RandomDataImpl(rg);
  int n = 1000;
  List<RealVector> vectors = LSHTest.getVectors(rd, 1000, n);
  PigTest test = createPigTestFromString(l1Test);
  writeLinesToFile("input", getLines(vectors));
  List<RealVector> queries = LSHTest.getVectors(rd, 1000, 10);
  writeLinesToFile("queries", getLines(queries));
  test.runScript();
  List<Tuple> neighbors = this.getLinesForAlias(test, "NEIGHBOR_CNT");
  Assert.assertEquals( queries.size(), neighbors.size() );
  for(long cnt : getCounts(neighbors))
  {
    Assert.assertTrue(cnt >= 3);
  }
  Distance d = new Distance()
  {

    @Override
    public double distance(RealVector v1, RealVector v2) {
      return L1.distance(v1, v2);
    }

  };
  verifyPoints(neighbors, d, 1000);
}
 
Example #10
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example #11
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example #12
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 *
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example #13
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example #14
Source File: DescriptiveStatisticsImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testGetSortedValues() {
    double[] test1 = {5,4,3,2,1};
    double[] test2 = {5,2,1,3,4,0};
    double[] test3 = {1};
    int[] testi = null;
    double[] test4 = null;
    RandomData rd = new RandomDataImpl();
    tstGetSortedValues(test1);
    tstGetSortedValues(test2);
    tstGetSortedValues(test3);
    for (int i = 0; i < 10; i++) {
        testi = rd.nextPermutation(10,6);
        test4 = new double[6];
        for (int j = 0; j < testi.length; j++) {
            test4[j] = (double) testi[j];
        }
        tstGetSortedValues(test4);
    }
    for (int i = 0; i < 10; i++) {
        testi = rd.nextPermutation(10,5);
        test4 = new double[5];
        for (int j = 0; j < testi.length; j++) {
            test4[j] = (double) testi[j];
        }
        tstGetSortedValues(test4);
    }        
}
 
Example #15
Source File: LSHPigTest.java    From datafu with Apache License 2.0 5 votes vote down vote up
@Test
public void testCosineUDF() throws Exception
{
  setMemorySettings();
  RandomGenerator rg = new JDKRandomGenerator();
  rg.setSeed(0);
  RandomData rd = new RandomDataImpl(rg);
  int n = 1000;
  List<RealVector> vectors = LSHTest.getVectors(rd, 1000, n);
  PigTest test = createPigTestFromString(cosTest);
  writeLinesToFile("input", getLines(vectors));
  List<RealVector> queries = LSHTest.getVectors(rd, 1000, 10);
  writeLinesToFile("queries", getLines(queries));
  test.runScript();
  List<Tuple> neighbors = this.getLinesForAlias(test, "NEIGHBOR_CNT");
  Assert.assertEquals( queries.size(), neighbors.size() );
  for(long cnt : getCounts(neighbors))
  {
    Assert.assertTrue(cnt >= 2);
  }
  Distance d = new Distance()
  {

    @Override
    public double distance(RealVector v1, RealVector v2) {
      return Cosine.distance(v1, v2);
    }

  };
  verifyPoints(neighbors, d, .001);
}
 
Example #16
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example #17
Source File: LSHPigTest.java    From datafu with Apache License 2.0 5 votes vote down vote up
@Test
public void testL1UDFSparse() throws Exception
{

  setMemorySettings();
  RandomGenerator rg = new JDKRandomGenerator();
  rg.setSeed(0);
  RandomData rd = new RandomDataImpl(rg);
  int n = 1000;
  List<RealVector> vectors = LSHTest.getVectors(rd, 1000, n);
  PigTest test = createPigTestFromString(l1SparseTest);
  writeLinesToFile("input", getSparseLines(vectors));
  List<RealVector> queries = LSHTest.getVectors(rd, 1000, 10);
  writeLinesToFile("queries", getSparseLines(queries));
  test.runScript();
  List<Tuple> neighbors = this.getLinesForAlias(test, "NEIGHBOR_CNT");
  Assert.assertEquals( queries.size(), neighbors.size() );
  for(long cnt : getCounts(neighbors))
  {
    Assert.assertTrue(cnt >= 3);
  }
  Distance d = new Distance()
  {

    @Override
    public double distance(RealVector v1, RealVector v2) {
      return L1.distance(v1, v2);
    }

  };
  verifyPoints(neighbors, d, 1000);
}
 
Example #18
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are 
 * uniformly distributed over [-100, 100].
 * 
 * @return array of random double values
 */
private double[] generateSample() {
    final RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;     
}
 
Example #19
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 *
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example #20
Source File: LSHTest.java    From datafu with Apache License 2.0 5 votes vote down vote up
static List<RealVector> getVectors(RandomData rd, double stddev, int n)
{
  ArrayList<RealVector> vecs = new ArrayList<RealVector>();
  
  for(int i = 0;i < n;++i)
  {
    vecs.add(getRandomVector(rd, stddev, 3));
  }
  return vecs;
}
 
Example #21
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 * 
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example #22
Source File: LSHPigTest.java    From datafu with Apache License 2.0 5 votes vote down vote up
@Test
public void testSparseVectors() throws IOException, ParseException
{
  RandomGenerator rg = new JDKRandomGenerator();
  rg.setSeed(0);
  RandomData rd = new RandomDataImpl(rg);
  int n = 20;
  List<RealVector> vectors = LSHTest.getVectors(rd, 1000, n);
  PigTest test = createPigTestFromString(sparseVectorTest);
  writeLinesToFile("input", getSparseLines(vectors));
  test.runScript();
  List<Tuple> neighbors = this.getLinesForAlias(test, "PTS");
  Assert.assertEquals(neighbors.size(), n);
  int idx = 0;
  for(Tuple t : neighbors)
  {
    Assert.assertTrue(t.get(0) instanceof DataBag);
    Assert.assertEquals(t.size(), 1);
    RealVector interpreted = DataTypeUtil.INSTANCE.convert(t, 3);
    RealVector original = vectors.get(idx);
    Assert.assertEquals(original.getDimension(), interpreted.getDimension());
    for(int i = 0;i < interpreted.getDimension();++i)
    {
      double originalField = original.getEntry(i);
      double interpretedField = interpreted.getEntry(i);
      Assert.assertTrue(Math.abs(originalField - interpretedField) < 1e-5);
    }

    idx++;
  }
}
 
Example #23
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are 
 * uniformly distributed over [-100, 100].
 * 
 * @return array of random double values
 */
private double[] generateSample() {
    final RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;     
}
 
Example #24
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 *
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example #25
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 *
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example #26
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 *
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example #27
Source File: LSHPigTest.java    From datafu with Apache License 2.0 5 votes vote down vote up
@Test
public void testL2UDF() throws Exception
{
  setMemorySettings();
  RandomGenerator rg = new JDKRandomGenerator();
  rg.setSeed(0);
  RandomData rd = new RandomDataImpl(rg);
  int n = 1000;
  List<RealVector> vectors = LSHTest.getVectors(rd, 1000, n);
  PigTest test = createPigTestFromString(l2Test);
  writeLinesToFile("input", getLines(vectors));
  List<RealVector> queries = LSHTest.getVectors(rd, 1000, 10);
  writeLinesToFile("queries", getLines(queries));
  test.runScript();
  List<Tuple> neighbors = this.getLinesForAlias(test, "NEIGHBOR_CNT");
  Assert.assertEquals( queries.size(), neighbors.size() );
  for(long cnt : getCounts(neighbors))
  {
    Assert.assertTrue(cnt >= 3);
  }
  Distance d = new Distance()
  {

    @Override
    public double distance(RealVector v1, RealVector v2) {
      return L2.distance(v1, v2);
    }

  };
  verifyPoints(neighbors, d, 1000);
}
 
Example #28
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 *
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example #29
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example #30
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 *
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}