Java Code Examples for org.apache.commons.math.random.RandomGenerator#setSeed()

The following examples show how to use org.apache.commons.math.random.RandomGenerator#setSeed() . 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: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 2
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 3
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 4
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 5
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 6
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 7
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 8
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 9
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 10
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 11
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 12
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 13
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 14
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example 15
Source File: DirectSearchOptimizer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/** Minimizes a cost function.
 * <p>The initial simplex is built from two vertices that are
 * considered to represent two opposite vertices of a box parallel
 * to the canonical axes of the space. The simplex is the subset of
 * vertices encountered while going from vertexA to vertexB
 * traveling along the box edges only. This can be seen as a scaled
 * regular simplex using the projected separation between the given
 * points as the scaling factor along each coordinate axis.</p>
 * <p>The optimization is performed in multi-start mode.</p>
 * @param f cost function
 * @param maxEvaluations maximal number of function calls for each
 * start (note that the number will be checked <em>after</em>
 * complete simplices have been evaluated, this means that in some
 * cases this number will be exceeded by a few units, depending on
 * the dimension of the problem)
 * @param checker object to use to check for convergence
 * @param vertexA first vertex
 * @param vertexB last vertex
 * @param starts number of starts to perform (including the
 * first one), multi-start is disabled if value is less than or
 * equal to 1
 * @param seed seed for the random vector generator
 * @return the point/cost pairs giving the minimal cost
 * @exception CostException if the cost function throws one during
 * the search
 * @exception ConvergenceException if none of the starts did
 * converge (it is not thrown if at least one start did converge)
 */
public PointCostPair minimize(CostFunction f, int maxEvaluations,
                              ConvergenceChecker checker,
                              double[] vertexA, double[] vertexB,
                              int starts, long seed)
throws CostException, ConvergenceException {

    // set up the simplex traveling around the box
    buildSimplex(vertexA, vertexB);

    // we consider the simplex could have been produced by a generator
    // having its mean value at the center of the box, the standard
    // deviation along each axe being the corresponding half size
    double[] mean              = new double[vertexA.length];
    double[] standardDeviation = new double[vertexA.length];
    for (int i = 0; i < vertexA.length; ++i) {
        mean[i]              = 0.5 * (vertexA[i] + vertexB[i]);
        standardDeviation[i] = 0.5 * Math.abs(vertexA[i] - vertexB[i]);
    }

    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(seed);
    UniformRandomGenerator urg = new UniformRandomGenerator(rg);
    RandomVectorGenerator rvg =
        new UncorrelatedRandomVectorGenerator(mean, standardDeviation, urg);
    setMultiStart(starts, rvg);

    // compute minimum
    return minimize(f, maxEvaluations, checker);

}
 
Example 16
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 17
Source File: DirectSearchOptimizer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/** Minimizes a cost function.
 * <p>The simplex is built from all its vertices.</p>
 * <p>The optimization is performed in multi-start mode.</p>
 * @param f cost function
 * @param maxEvaluations maximal number of function calls for each
 * start (note that the number will be checked <em>after</em>
 * complete simplices have been evaluated, this means that in some
 * cases this number will be exceeded by a few units, depending on
 * the dimension of the problem)
 * @param checker object to use to check for convergence
 * @param vertices array containing all vertices of the simplex
 * @param starts number of starts to perform (including the
 * first one), multi-start is disabled if value is less than or
 * equal to 1
 * @param seed seed for the random vector generator
 * @return the point/cost pairs giving the minimal cost
 * @exception NotPositiveDefiniteMatrixException if the vertices
 * array is degenerated
 * @exception CostException if the cost function throws one during
 * the search
 * @exception ConvergenceException if none of the starts did
 * converge (it is not thrown if at least one start did converge)
 */
public PointCostPair minimize(CostFunction f, int maxEvaluations,
                              ConvergenceChecker checker,
                              double[][] vertices,
                              int starts, long seed)
throws NotPositiveDefiniteMatrixException,
CostException, ConvergenceException {

    try {
        // store the points into the simplex
        buildSimplex(vertices);

        // compute the statistical properties of the simplex points
        VectorialMean meanStat = new VectorialMean(vertices[0].length);
        VectorialCovariance covStat = new VectorialCovariance(vertices[0].length, true);
        for (int i = 0; i < vertices.length; ++i) {
            meanStat.increment(vertices[i]);
            covStat.increment(vertices[i]);
        }
        double[] mean = meanStat.getResult();
        RealMatrix covariance = covStat.getResult();
        

        RandomGenerator rg = new JDKRandomGenerator();
        rg.setSeed(seed);
        RandomVectorGenerator rvg =
            new CorrelatedRandomVectorGenerator(mean,
                                                covariance, 1.0e-12 * covariance.getNorm(),
                                                new UniformRandomGenerator(rg));
        setMultiStart(starts, rvg);

        // compute minimum
        return minimize(f, maxEvaluations, checker);

    } catch (DimensionMismatchException dme) {
        // this should not happen
        throw new RuntimeException("internal error");
    }

}
 
Example 18
Source File: GLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generate an error covariance matrix and sample data representing models
 * with this error structure. Then verify that GLS estimated coefficients,
 * on average, perform better than OLS.
 */
@Test
public void testGLSEfficiency() throws Exception {
    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(200);  // Seed has been selected to generate non-trivial covariance
    
    // Assume model has 16 observations (will use Longley data).  Start by generating
    // non-constant variances for the 16 error terms.
    final int nObs = 16;
    double[] sigma = new double[nObs];
    for (int i = 0; i < nObs; i++) {
        sigma[i] = 10 * rg.nextDouble();
    }
    
    // Now generate 1000 error vectors to use to estimate the covariance matrix
    // Columns are draws on N(0, sigma[col])
    final int numSeeds = 1000;
    RealMatrix errorSeeds = MatrixUtils.createRealMatrix(numSeeds, nObs);
    for (int i = 0; i < numSeeds; i++) {
        for (int j = 0; j < nObs; j++) {
            errorSeeds.setEntry(i, j, rg.nextGaussian() * sigma[j]);
        }
    }
    
    // Get covariance matrix for columns
    RealMatrix cov = (new Covariance(errorSeeds)).getCovarianceMatrix();
      
    // Create a CorrelatedRandomVectorGenerator to use to generate correlated errors
    GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg);
    double[] errorMeans = new double[nObs];  // Counting on init to 0 here
    CorrelatedRandomVectorGenerator gen = new CorrelatedRandomVectorGenerator(errorMeans, cov,
     1.0e-12 * cov.getNorm(), rawGenerator);
    
    // Now start generating models.  Use Longley X matrix on LHS
    // and Longley OLS beta vector as "true" beta.  Generate
    // Y values by XB + u where u is a CorrelatedRandomVector generated
    // from cov.
    OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
    ols.newSampleData(longley, nObs, 6);
    final RealVector b = ols.calculateBeta().copy();
    final RealMatrix x = ols.X.copy();
    
    // Create a GLS model to reuse
    GLSMultipleLinearRegression gls = new GLSMultipleLinearRegression();
    gls.newSampleData(longley, nObs, 6);
    gls.newCovarianceData(cov.getData());
    
    // Create aggregators for stats measuring model performance
    DescriptiveStatistics olsBetaStats = new DescriptiveStatistics();
    DescriptiveStatistics glsBetaStats = new DescriptiveStatistics();
    
    // Generate Y vectors for 10000 models, estimate GLS and OLS and
    // Verify that OLS estimates are better
    final int nModels = 10000;
    for (int i = 0; i < nModels; i++) {
        
        // Generate y = xb + u with u cov
        RealVector u = MatrixUtils.createRealVector(gen.nextVector());
        double[] y = u.add(x.operate(b)).getData();
        
        // Estimate OLS parameters
        ols.newYSampleData(y);
        RealVector olsBeta = ols.calculateBeta();
        
        // Estimate GLS parameters
        gls.newYSampleData(y);
        RealVector glsBeta = gls.calculateBeta();
        
        // Record deviations from "true" beta
        double dist = olsBeta.getDistance(b);
        olsBetaStats.addValue(dist * dist);
        dist = glsBeta.getDistance(b);
        glsBetaStats.addValue(dist * dist);
        
    }
    
    // Verify that GLS is on average more efficient, lower variance
    assert(olsBetaStats.getMean() > 1.5 * glsBetaStats.getMean());
    assert(olsBetaStats.getStandardDeviation() > glsBetaStats.getStandardDeviation());  
}
 
Example 19
Source File: GLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generate an error covariance matrix and sample data representing models
 * with this error structure. Then verify that GLS estimated coefficients,
 * on average, perform better than OLS.
 */
@Test
public void testGLSEfficiency() throws Exception {
    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(200);  // Seed has been selected to generate non-trivial covariance
    
    // Assume model has 16 observations (will use Longley data).  Start by generating
    // non-constant variances for the 16 error terms.
    final int nObs = 16;
    double[] sigma = new double[nObs];
    for (int i = 0; i < nObs; i++) {
        sigma[i] = 10 * rg.nextDouble();
    }
    
    // Now generate 1000 error vectors to use to estimate the covariance matrix
    // Columns are draws on N(0, sigma[col])
    final int numSeeds = 1000;
    RealMatrix errorSeeds = MatrixUtils.createRealMatrix(numSeeds, nObs);
    for (int i = 0; i < numSeeds; i++) {
        for (int j = 0; j < nObs; j++) {
            errorSeeds.setEntry(i, j, rg.nextGaussian() * sigma[j]);
        }
    }
    
    // Get covariance matrix for columns
    RealMatrix cov = (new Covariance(errorSeeds)).getCovarianceMatrix();
      
    // Create a CorrelatedRandomVectorGenerator to use to generate correlated errors
    GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg);
    double[] errorMeans = new double[nObs];  // Counting on init to 0 here
    CorrelatedRandomVectorGenerator gen = new CorrelatedRandomVectorGenerator(errorMeans, cov,
     1.0e-12 * cov.getNorm(), rawGenerator);
    
    // Now start generating models.  Use Longley X matrix on LHS
    // and Longley OLS beta vector as "true" beta.  Generate
    // Y values by XB + u where u is a CorrelatedRandomVector generated
    // from cov.
    OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
    ols.newSampleData(longley, nObs, 6);
    final RealVector b = ols.calculateBeta().copy();
    final RealMatrix x = ols.X.copy();
    
    // Create a GLS model to reuse
    GLSMultipleLinearRegression gls = new GLSMultipleLinearRegression();
    gls.newSampleData(longley, nObs, 6);
    gls.newCovarianceData(cov.getData());
    
    // Create aggregators for stats measuring model performance
    DescriptiveStatistics olsBetaStats = new DescriptiveStatistics();
    DescriptiveStatistics glsBetaStats = new DescriptiveStatistics();
    
    // Generate Y vectors for 10000 models, estimate GLS and OLS and
    // Verify that OLS estimates are better
    final int nModels = 10000;
    for (int i = 0; i < nModels; i++) {
        
        // Generate y = xb + u with u cov
        RealVector u = MatrixUtils.createRealVector(gen.nextVector());
        double[] y = u.add(x.operate(b)).getData();
        
        // Estimate OLS parameters
        ols.newYSampleData(y);
        RealVector olsBeta = ols.calculateBeta();
        
        // Estimate GLS parameters
        gls.newYSampleData(y);
        RealVector glsBeta = gls.calculateBeta();
        
        // Record deviations from "true" beta
        double dist = olsBeta.getDistance(b);
        olsBetaStats.addValue(dist * dist);
        dist = glsBeta.getDistance(b);
        glsBetaStats.addValue(dist * dist);
        
    }
    
    // Verify that GLS is on average more efficient, lower variance
    assert(olsBetaStats.getMean() > 1.5 * glsBetaStats.getMean());
    assert(olsBetaStats.getStandardDeviation() > glsBetaStats.getStandardDeviation());  
}
 
Example 20
Source File: LSHCreator.java    From datafu with Apache License 2.0 4 votes vote down vote up
public RandomGenerator createGenerator() 
{
  RandomGenerator rg = new JDKRandomGenerator();
  rg.setSeed(seed);
  return rg;
}