You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jb...@apache.org on 2017/10/23 19:06:37 UTC

[1/2] lucene-solr:branch_7x: Solr Ref Guide: update 7.1 statistical function docs

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x 10cd93fc2 -> 2c54ee2d6


Solr Ref Guide: update 7.1 statistical function docs


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/c9c658d5
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/c9c658d5
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/c9c658d5

Branch: refs/heads/branch_7x
Commit: c9c658d5bf00770a241add617129634d4b566858
Parents: 10cd93f
Author: Joel Bernstein <jb...@apache.org>
Authored: Tue Oct 17 21:25:13 2017 -0400
Committer: Joel Bernstein <jb...@apache.org>
Committed: Mon Oct 23 14:50:40 2017 -0400

----------------------------------------------------------------------
 .../src/statistical-programming.adoc            | 77 +++++++++++++++++++-
 .../src/stream-evaluator-reference.adoc         | 57 ++++++++++++---
 2 files changed, 122 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c9c658d5/solr/solr-ref-guide/src/statistical-programming.adoc
----------------------------------------------------------------------
diff --git a/solr/solr-ref-guide/src/statistical-programming.adoc b/solr/solr-ref-guide/src/statistical-programming.adoc
index 0d461be..07fba23 100644
--- a/solr/solr-ref-guide/src/statistical-programming.adoc
+++ b/solr/solr-ref-guide/src/statistical-programming.adoc
@@ -455,10 +455,81 @@ Returns the following response:
 
 == Setting Variables with let
 
-The `let` function sets variables and runs a streaming expression that references the variables. The `let` function can be used to
-write small statistical programs.
+The `let` function sets variables and returns the last variable. The output of any statistical function can be set to a variable.
 
-A variable can be set to the output of any streaming expression. Here is a very simple example:
+Below is a simple example setting three variables `a`, `b` and `correlation`.
+
+[source,text]
+----
+let(a=array(1,2,3),
+    b=array(10, 20, 30),
+    correlation=corr(a, b))
+----
+
+Here is the output:
+
+[source,json]
+----
+{
+  "result-set": {
+    "docs": [
+      {
+        "correlation": 1
+      },
+      {
+        "EOF": true,
+        "RESPONSE_TIME": 0
+      }
+    ]
+  }
+}
+----
+
+All variables can be output by setting the `echo` variable to `true`.
+
+[source,text]
+----
+let(echo=true,
+    a=array(1,2,3),
+    b=array(10, 20, 30),
+    correlation=corr(a, b))
+----
+
+Here is the output:
+
+[source,json]
+----
+{
+  "result-set": {
+    "docs": [
+      {
+        "a": [
+          1,
+          2,
+          3
+        ],
+        "b": [
+          10,
+          20,
+          30
+        ],
+        "correlation": 1
+      },
+      {
+        "EOF": true,
+        "RESPONSE_TIME": 0
+      }
+    ]
+  }
+}
+----
+
+Streaming expressions can also be used inside of a `let` expression in the following ways:
+
+* A variable can be set to the output of any streaming expression.
+* A streaming expression can be executed after all variables have been set. The variables can then be referenced by the streaming expression that is executed. The `let` expression will stream the tuples that are emitted by the final streaming expression.
+
+Here is a very simple example:
 
 [source,text]
 ----

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c9c658d5/solr/solr-ref-guide/src/stream-evaluator-reference.adoc
----------------------------------------------------------------------
diff --git a/solr/solr-ref-guide/src/stream-evaluator-reference.adoc b/solr/solr-ref-guide/src/stream-evaluator-reference.adoc
index 392144c..257ded2 100644
--- a/solr/solr-ref-guide/src/stream-evaluator-reference.adoc
+++ b/solr/solr-ref-guide/src/stream-evaluator-reference.adoc
@@ -659,8 +659,14 @@ ebeSubtract(numericArray, numericArray)
 
 == empiricalDistribution
 
+<<<<<<< 10cd93fc248b3d24425433c68affd71fdc8f1f58
 The `empiricalDistribution` function returns https://en.wikipedia.org/wiki/Empirical_distribution_function[empirical distribution function], a continuous probability distribution function based
 on an actual data set. This function is part of the probability distribution framework and is designed to work with the `<<sample>>`, `<<kolmogorovSmirnov>>` and `<<cumulativeProbability>>` functions.
+=======
+The `empiricalDistribution` function returns a continuous probability distribution function based
+on an actual data set (https://en.wikipedia.org/wiki/Empirical_distribution_function). This function is part of the probability distribution framework and is
+designed to work with the `sample`, `kolmogorovSmirnov` and `cumulativeProbability` functions.
+>>>>>>> Solr Ref Guide: update 7.1 statistical function docs
 
 This function is designed to work with continuous data. To build a distribution from
 a discrete data set use the `<<enumeratedDistribution>>`.
@@ -1049,7 +1055,11 @@ The supported distribution functions are: `<<empiricalDistribution>>`, `<<normal
 
 === kolmogorovSmirnov Returns
 
+<<<<<<< 10cd93fc248b3d24425433c68affd71fdc8f1f58
 A result tuple: A tuple containing the p-value and d-statistic for the test result.
+=======
+result tuple : A tuple containing the p-value and d-statistic for the test result.
+>>>>>>> Solr Ref Guide: update 7.1 statistical function docs
 
 === kolmogorovSmirnov Syntax
 
@@ -1158,8 +1168,13 @@ if(gt(fieldA,fieldB),mod(fieldA,fieldB),mod(fieldB,fieldA)) // if fieldA > field
 
 == monteCarlo
 
+<<<<<<< 10cd93fc248b3d24425433c68affd71fdc8f1f58
 The `monteCarlo` function performs a https://en.wikipedia.org/wiki/Monte_Carlo_method[Monte Carlo simulation]
 based on its parameters. The `monteCarlo` function runs another function a specified number of times and returns the results.
+=======
+The `monteCarlo` function performs a Monte Carlo simulation (https://en.wikipedia.org/wiki/Monte_Carlo_method)
+based on its parameters. The monteCarlo function runs another function a specified number of times and returns the results.
+>>>>>>> Solr Ref Guide: update 7.1 statistical function docs
 The function being run typically has one or more variables that are drawn from probability
 distributions on each run. The `<<sample>>` function is used in the function to draw the samples.
 
@@ -1325,9 +1340,15 @@ or(fieldA,fieldB,fieldC,and(fieldD,fieldE),fieldF)
 
 == poissonDistribution
 
+<<<<<<< 10cd93fc248b3d24425433c68affd71fdc8f1f58
 The `poissonDistribution` function returns a https://en.wikipedia.org/wiki/Poisson_distribution[poisson probability distribution]
 based on its parameter. This function is part of the probability distribution framework and is designed to
 work with the `<<sample>>`, `<<probability>>` and `<<cumulativeProbability>>` functions.
+=======
+The `poissonDistribution` function returns a poisson probability distribution (https://en.wikipedia.org/wiki/Poisson_distribution)
+based on its parameter. This function is part of the probability distribution framework and is designed to
+work with the `sample`, `probability` and `cumulativeProbability` functions.
+>>>>>>> Solr Ref Guide: update 7.1 statistical function docs
 
 === poissonDistribution Parameters
 
@@ -1348,9 +1369,15 @@ The `polyFit` function performs https://en.wikipedia.org/wiki/Curve_fitting#Fitt
 
 === polyFit Parameters
 
+<<<<<<< 10cd93fc248b3d24425433c68affd71fdc8f1f58
 * `numeric array`: (Optional) x values. If omitted a sequence will be created for the x values.
 * `numeric array`: y values
 * `integer`: (Optional) polynomial degree. Defaults to 3.
+=======
+* `numeric array` : (Optional) x values. If omitted a sequence will be created for the x values.
+* `numeric array` : y values
+* `integer` : (Optional) polynomial degree. Defaults to 3.
+>>>>>>> Solr Ref Guide: update 7.1 statistical function docs
 
 === polyFit Returns
 
@@ -1359,7 +1386,8 @@ A numeric array: curve that was fit to the data points.
 === polyFit Syntax
 
 [source,text]
-polyFit(yValues) // This creates the xValues automatically and fits a curve through the data points using a the default 3 degree polynomial.
+polyFit(yValues) // This creates the xValues automatically and fits a curve through the data points using the default 3 degree polynomial.
+polyFit(yValues, 5) // This creates the xValues automatically and fits a curve through the data points using a 5 degree polynomial.
 polyFit(xValues, yValues, 5) // This will fit a curve through the data points using a 5 degree polynomial.
 
 == polyfitDerivative
@@ -1368,9 +1396,15 @@ The `polyfitDerivative` function returns the derivative of the curve created by
 
 === polyfitDerivative Parameters
 
+<<<<<<< 10cd93fc248b3d24425433c68affd71fdc8f1f58
 * `numeric array`: (Optional) x values. If omitted a sequence will be created for the x values.
 * `numeric array`: y values
 * `integer`: (Optional) polynomial degree. Defaults to 3.
+=======
+* `numeric array` : (Optional) x values. If omitted a sequence will be created for the x values.
+* `numeric array` : y values
+* `integer` : (Optional) polynomial degree. Defaults to 3.
+>>>>>>> Solr Ref Guide: update 7.1 statistical function docs
 
 === polyfitDerivative Returns
 
@@ -1380,6 +1414,7 @@ A numeric array: The curve for the derivative created by the polynomial curve fi
 
 [source,text]
 polyfitDerivative(yValues) // This creates the xValues automatically and returns the polyfit derivative
+polyfitDerivative(yValues, 5) //  This creates the xValues automatically and fits a curve through the data points using a 5 degree polynomial and returns the polyfit derivative.
 polyfitDerivative(xValues, yValues, 5) // This will fit a curve through the data points using a 5 degree polynomial and returns the polyfit derivative.
 
 == pow
@@ -1439,13 +1474,17 @@ primes(100, 2000) // returns 100 primes starting from 2000
 
 == probability
 
-The `probability` function returns the probability of encountering a random variable within a discrete
-probability distribution.
+The `probability` function returns the probability of a random variable within a discrete probability distribution.
 
 === probability Parameters
 
+<<<<<<< 10cd93fc248b3d24425433c68affd71fdc8f1f58
 * `discrete probability distribution`: poissonDistribution | binomialDistribution | uniformDistribution | enumeratedDistribution
 * `integer`: Value of the random variable to compute the probability for.
+=======
+* `discrete probability distribution` : poissonDistribution | binomialDistribution | uniformDistribution | enumeratedDistribution
+* `integer` : Value of the random variable to compute the probability for.
+>>>>>>> Solr Ref Guide: update 7.1 statistical function docs
 
 === probability Returns
 
@@ -1454,7 +1493,7 @@ A double: the probability.
 === probability Syntax
 
 [source,text]
-probability(poissonDistribution(10), 7) // Returns the probability of encountering a random sample if 7 in a poisson distribution with a mean of 10.
+probability(poissonDistribution(10), 7) // Returns the probability of a random sample of 7 in a poisson distribution with a mean of 10.
 
 == rank
 
@@ -1493,7 +1532,7 @@ eq(raw(fieldA), fieldA) // true if the value of fieldA equals the string "fieldA
 
 == regress
 
-The `regress` function performs a simple regression on two numeric arrays.
+The `regress` function performs a simple regression of two numeric arrays.
 
 The result of this expression is also used by the `<<predict>>` and `<<residuals>>` functions.
 
@@ -1512,8 +1551,8 @@ regress(numericArray1, numericArray2)
 
 The `residuals` function takes three parameters: a simple regression model, an array of predictor values
 and an array of actual values. The residuals function applies the simple regression model to the
-array of predictor values and computes a predictions array. The actual values array is then
-subtracted from the predictions array to compute the residuals array.
+array of predictor values and computes a predictions array. The predicted values array is then
+subtracted from the actual value array to compute the residuals array.
 
 === residuals Parameters
 
@@ -1576,8 +1615,8 @@ Either a single numeric random sample, or a numeric array depending on the sampl
 === sample Syntax
 
 [source,text]
-sample(normalDistribution(50, 5)) // Return a single random sample from a normalDistribution with mean of 50 and standard deviation of 5.
-sample(poissonDistribution(5), 1000) // Return 1000 random samples from poissonDistribution with a mean of 5.
+sample(poissonDistribution(5)) // Returns a single random sample from a poissonDistribution with mean of 5.
+sample(poissonDistribution(5), 1000) // Returns 1000 random samples from poissonDistribution with a mean of 5.
 
 == scale
 


[2/2] lucene-solr:branch_7x: SOLR-11429: Add loess Stream Evaluator to support Local Regression interpolation

Posted by jb...@apache.org.
SOLR-11429: Add loess Stream Evaluator to support Local Regression interpolation


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/2c54ee2d
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/2c54ee2d
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/2c54ee2d

Branch: refs/heads/branch_7x
Commit: 2c54ee2d6da489b73cff4891f9a96765787925eb
Parents: c9c658d
Author: Joel Bernstein <jb...@apache.org>
Authored: Mon Oct 23 14:44:50 2017 -0400
Committer: Joel Bernstein <jb...@apache.org>
Committed: Mon Oct 23 15:03:18 2017 -0400

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |  1 +
 .../client/solrj/io/eval/LoessEvaluator.java    | 83 ++++++++++++++++++++
 .../solrj/io/eval/RecursiveEvaluator.java       |  3 +-
 .../solrj/io/stream/StreamExpressionTest.java   | 29 +++++++
 4 files changed, 115 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/2c54ee2d/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
index ac3fc1a..51e0575 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -267,6 +267,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
         .withFunctionName("polyfit", PolyFitEvaluator.class)
         .withFunctionName("polyfitDerivative", PolyFitDerivativeEvaluator.class)
         .withFunctionName("harmonicFit", HarmonicFitEvaluator.class)
+        .withFunctionName("loess", LoessEvaluator.class)
 
         // Boolean Stream Evaluators
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/2c54ee2d/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LoessEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LoessEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LoessEvaluator.java
new file mode 100644
index 0000000..5824f61
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LoessEvaluator.java
@@ -0,0 +1,83 @@
+/*
+ * 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.math.BigDecimal;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.commons.math3.analysis.interpolation.LoessInterpolator;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionNamedParameter;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class LoessEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker {
+  protected static final long serialVersionUID = 1L;
+
+  private double bandwidth = .25;
+  private int robustIterations = 2;
+
+  public LoessEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+    super(expression, factory);
+
+    List<StreamExpressionNamedParameter> namedParams = factory.getNamedOperands(expression);
+
+    for(StreamExpressionNamedParameter namedParam : namedParams){
+      if(namedParam.getName().equals("bandwidth")){
+        this.bandwidth = Double.parseDouble(namedParam.getParameter().toString().trim());
+      } else if(namedParam.getName().equals("robustIterations")) {
+        this.robustIterations = Integer.parseInt(namedParam.getParameter().toString().trim());
+      } else {
+        throw new IOException("Unexpected named parameter:"+namedParam.getName());
+      }
+    }
+
+  }
+
+  @Override
+  public Object doWork(Object... objects) throws IOException{
+
+    Object first = objects[0];
+
+    double[] x = null;
+    double[] y = null;
+
+    if(objects.length == 1) {
+      //Only the y values passed
+      y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+      x = new double[y.length];
+      for(int i=0; i<y.length; i++) {
+        x[i] = i;
+      }
+    } else if(objects.length == 2) {
+      Object second = objects[1];
+      x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+      y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+    }
+
+    LoessInterpolator interpolator = new LoessInterpolator(bandwidth, robustIterations);
+    double[] smooth = interpolator.smooth(x, y);
+
+    List list = new ArrayList();
+    for(double yvalue : smooth) {
+      list.add(yvalue);
+    }
+
+    return list;
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/2c54ee2d/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java
index 8df46b1..6c5ae41 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java
@@ -171,7 +171,7 @@ public abstract class RecursiveEvaluator implements StreamEvaluator, ValueWorker
     
     Set<String> namedParameters = factory.getNamedOperands(expression).stream().map(param -> param.getName()).collect(Collectors.toSet());
     long ignorableCount = ignoredNamedParameters.stream().filter(name -> namedParameters.contains(name)).count();
-    
+    /*
     if(0 != expression.getParameters().size() - containedEvaluators.size() - ignorableCount){
       if(namedParameters.isEmpty()){
         throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - unknown operands found - expecting only StreamEvaluators or field names", expression));
@@ -180,6 +180,7 @@ public abstract class RecursiveEvaluator implements StreamEvaluator, ValueWorker
         throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - unknown operands found - expecting only StreamEvaluators, field names, or named parameters [%s]", expression, namedParameters.stream().collect(Collectors.joining(","))));
       }
     }
+    */
   }
   
   @Override

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/2c54ee2d/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
index 18f4068..ac06b58 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
@@ -6635,6 +6635,35 @@ public class StreamExpressionTest extends SolrCloudTestCase {
     assertTrue(out.get(7).intValue() == 1);
   }
 
+
+  @Test
+  public void testLoess() throws Exception {
+    String cexpr = "let(echo=true," +
+                   "    a=array(0,1,2,3,4,5,6,7)," +
+                   "    fit=loess(a))";
+
+    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<Number> out = (List<Number>)tuples.get(0).get("fit");
+    assertTrue(out.size() == 8);
+    assertTrue(out.get(0).intValue() == 0);
+    assertTrue(out.get(1).intValue() == 1);
+    assertTrue(out.get(2).intValue() == 2);
+    assertTrue(out.get(3).intValue() == 3);
+    assertTrue(out.get(4).intValue() == 4);
+    assertTrue(out.get(5).intValue() == 5);
+    assertTrue(out.get(6).intValue() == 6);
+    assertTrue(out.get(7).intValue() == 7);
+  }
+
+
   @Test
   public void testHarmonicFit() throws Exception {
     String cexpr = "let(a=sin(sequence(100, 1, 6)), b=harmonicFit(a), s=ebeSubtract(a, b))";