Example usage for org.apache.mahout.math.random MultiNormal sample

List of usage examples for org.apache.mahout.math.random MultiNormal sample

Introduction

In this page you can find the example usage for org.apache.mahout.math.random MultiNormal sample.

Prototype

@Override
    public Vector sample() 

Source Link

Usage

From source file:zx.soft.mahout.knn.search.AbstractSearchTest.java

License:Apache License

protected static Matrix randomData() {
    Matrix data = new DenseMatrix(1000, 20);
    MultiNormal gen = new MultiNormal(20);
    for (MatrixSlice slice : data) {
        slice.vector().assign(gen.sample());
    }//from   www.  j  a v  a2 s  . c o m
    return data;
}

From source file:zx.soft.mahout.knn.search.AbstractSearchTest.java

License:Apache License

@Test
public void testNearMatch() {
    List<MatrixSlice> queries = Lists.newArrayList(Iterables.limit(testData(), 100));
    Searcher s = getSearch(20);/*from w  w w . java2 s  .co m*/
    s.addAllMatrixSlicesAsWeightedVectors(testData());

    MultiNormal noise = new MultiNormal(0.01, new DenseVector(20));
    for (MatrixSlice slice : queries) {
        Vector query = slice.vector();
        final Vector epsilon = noise.sample();
        //         List<WeightedThing<Vector>> r0 = s.search(query, 2);
        query = query.plus(epsilon);
        List<WeightedThing<Vector>> r = s.search(query, 2);
        r = s.search(query, 2);
        assertEquals("Distance has to be small", epsilon.norm(2), r.get(0).getWeight(), 1e-5);
        assertEquals("Answer must be substantially the same as query", epsilon.norm(2),
                r.get(0).getValue().minus(query).norm(2), 1e-5);
        assertTrue("Wrong answer must be further away", r.get(1).getWeight() > r.get(0).getWeight());
    }
}

From source file:zx.soft.mahout.knn.search.AbstractSearchTest.java

License:Apache License

@Test
public void testOrdering() {
    Matrix queries = new DenseMatrix(100, 20);
    MultiNormal gen = new MultiNormal(20);
    for (int i = 0; i < 100; i++) {
        queries.viewRow(i).assign(gen.sample());
    }/*from   www .j ava2  s.c o  m*/

    Searcher s = getSearch(20);
    // s.setSearchSize(200);
    s.addAllMatrixSlices(testData());

    for (MatrixSlice query : queries) {
        List<WeightedThing<Vector>> r = s.search(query.vector(), 200);
        double x = 0;
        for (WeightedThing<Vector> thing : r) {
            assertTrue("Scores must be monotonic increasing", thing.getWeight() > x);
            x = thing.getWeight();
        }
    }
}