SOLR-11697: Add geometricDistribution Stream Evaluator

This commit is contained in:
Joel Bernstein 2017-11-28 21:16:06 -05:00
parent c2f26183a7
commit 70767b109f
4 changed files with 85 additions and 1 deletions

View File

@ -279,6 +279,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("diff", TimeDifferencingEvaluator.class)
.withFunctionName("corrPValues", CorrelationSignificanceEvaluator.class)
.withFunctionName("normalizeSum", NormalizeSumEvaluator.class)
.withFunctionName("geometricDistribution", GeometricDistributionEvaluator.class)
// Boolean Stream Evaluators

View File

@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.client.solrj.io.eval;
import java.io.IOException;
import java.util.Locale;
import org.apache.commons.math3.distribution.GeometricDistribution;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class GeometricDistributionEvaluator extends RecursiveNumericEvaluator implements OneValueWorker {
private static final long serialVersionUID = 1;
public GeometricDistributionEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
}
@Override
public Object doWork(Object first) throws IOException{
if(null == first){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));
}
Number prob = (Number)first;
return new GeometricDistribution(prob.doubleValue());
}
}

View File

@ -39,6 +39,6 @@ public class PoissonDistributionEvaluator extends RecursiveNumericEvaluator impl
Number mean = (Number)first;
return new PoissonDistribution(mean.intValue());
return new PoissonDistribution(mean.doubleValue());
}
}

View File

@ -6765,6 +6765,45 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertEquals(cprob.doubleValue(), 0.5265621985303708, 0.0);
}
@Test
public void testGeometricDistribution() throws Exception {
String cexpr = "let(a=geometricDistribution(.2)," +
" b=geometricDistribution(.5)," +
" c=geometricDistribution(.8)," +
" d=sample(a, 10000)," +
" e=sample(b, 10000)," +
" f=sample(c, 10000)," +
" g=freqTable(d)," +
" h=freqTable(e)," +
" i=freqTable(f)," +
" tuple(g=g, h=h, i=i))";
ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", cexpr);
paramsLoc.set("qt", "/stream");
String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;
TupleStream solrStream = new SolrStream(url, paramsLoc);
StreamContext context = new StreamContext();
solrStream.setStreamContext(context);
List<Tuple> tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
List<Map> listg = (List<Map>)tuples.get(0).get("g");
Map mapg = listg.get(0);
double pctg = (double) mapg.get("pct");
assertEquals(pctg, .2, .02 );
List<Map> listh = (List<Map>)tuples.get(0).get("h");
Map maph = listh.get(0);
double pcth = (double)maph.get("pct");
assertEquals(pcth, .5, .02 );
List<Map> listi = (List<Map>)tuples.get(0).get("i");
Map mapi = listi.get(0);
double pcti = (double)mapi.get("pct");
assertEquals(pcti, .8, .02 );
}
@Test
public void testBinomialDistribution() throws Exception {
String cexpr = "let(a=binomialDistribution(100, .50)," +