Search in sources :

Example 1 with MovAvgModel

use of org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel in project elasticsearch by elastic.

the class SimulatedAnealingMinimizer method minimize.

/**
     * Runs the simulated annealing algorithm and produces a model with new coefficients that, theoretically
     * fit the data better and generalizes to future forecasts without overfitting.
     *
     * @param model         The MovAvgModel to be optimized for
     * @param train         A training set provided to the model, which predictions will be
     *                      generated from
     * @param test          A test set of data to compare the predictions against and derive
     *                      a cost for the model
     * @return              A new, minimized model that (theoretically) better fits the data
     */
public static MovAvgModel minimize(MovAvgModel model, EvictingQueue<Double> train, double[] test) {
    double temp = 1;
    double minTemp = 0.0001;
    int iterations = 100;
    double alpha = 0.9;
    MovAvgModel bestModel = model;
    MovAvgModel oldModel = model;
    double oldCost = cost(model, train, test);
    double bestCost = oldCost;
    while (temp > minTemp) {
        for (int i = 0; i < iterations; i++) {
            MovAvgModel newModel = oldModel.neighboringModel();
            double newCost = cost(newModel, train, test);
            double ap = acceptanceProbability(oldCost, newCost, temp);
            if (ap > Math.random()) {
                oldModel = newModel;
                oldCost = newCost;
                if (newCost < bestCost) {
                    bestCost = newCost;
                    bestModel = newModel;
                }
            }
        }
        temp *= alpha;
    }
    return bestModel;
}
Also used : MovAvgModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel)

Example 2 with MovAvgModel

use of org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel in project elasticsearch by elastic.

the class SearchModuleTests method testDoubleRegister.

public void testDoubleRegister() {
    SearchPlugin registersDupeHighlighter = new SearchPlugin() {

        @Override
        public Map<String, Highlighter> getHighlighters() {
            return singletonMap("plain", new PlainHighlighter());
        }
    };
    expectThrows(IllegalArgumentException.class, () -> new SearchModule(Settings.EMPTY, false, singletonList(registersDupeHighlighter)));
    SearchPlugin registersDupeSuggester = new SearchPlugin() {

        public List<SearchPlugin.SuggesterSpec<?>> getSuggesters() {
            return singletonList(new SuggesterSpec<>("term", TermSuggestionBuilder::new, TermSuggestionBuilder::fromXContent));
        }
    };
    expectThrows(IllegalArgumentException.class, () -> new NamedXContentRegistry(new SearchModule(Settings.EMPTY, false, singletonList(registersDupeSuggester)).getNamedXContents()));
    SearchPlugin registersDupeScoreFunction = new SearchPlugin() {

        @Override
        public List<ScoreFunctionSpec<?>> getScoreFunctions() {
            return singletonList(new ScoreFunctionSpec<>(GaussDecayFunctionBuilder.NAME, GaussDecayFunctionBuilder::new, GaussDecayFunctionBuilder.PARSER));
        }
    };
    expectThrows(IllegalArgumentException.class, () -> new NamedXContentRegistry(new SearchModule(Settings.EMPTY, false, singletonList(registersDupeScoreFunction)).getNamedXContents()));
    SearchPlugin registersDupeSignificanceHeuristic = new SearchPlugin() {

        @Override
        public List<SearchExtensionSpec<SignificanceHeuristic, SignificanceHeuristicParser>> getSignificanceHeuristics() {
            return singletonList(new SearchExtensionSpec<>(ChiSquare.NAME, ChiSquare::new, ChiSquare.PARSER));
        }
    };
    expectThrows(IllegalArgumentException.class, () -> new SearchModule(Settings.EMPTY, false, singletonList(registersDupeSignificanceHeuristic)));
    SearchPlugin registersDupeMovAvgModel = new SearchPlugin() {

        @Override
        public List<SearchExtensionSpec<MovAvgModel, MovAvgModel.AbstractModelParser>> getMovingAverageModels() {
            return singletonList(new SearchExtensionSpec<>(SimpleModel.NAME, SimpleModel::new, SimpleModel.PARSER));
        }
    };
    expectThrows(IllegalArgumentException.class, () -> new SearchModule(Settings.EMPTY, false, singletonList(registersDupeMovAvgModel)));
    SearchPlugin registersDupeFetchSubPhase = new SearchPlugin() {

        @Override
        public List<FetchSubPhase> getFetchSubPhases(FetchPhaseConstructionContext context) {
            return singletonList(new ExplainFetchSubPhase());
        }
    };
    expectThrows(IllegalArgumentException.class, () -> new SearchModule(Settings.EMPTY, false, singletonList(registersDupeFetchSubPhase)));
    SearchPlugin registersDupeQuery = new SearchPlugin() {

        public List<SearchPlugin.QuerySpec<?>> getQueries() {
            return singletonList(new QuerySpec<>(TermQueryBuilder.NAME, TermQueryBuilder::new, TermQueryBuilder::fromXContent));
        }
    };
    expectThrows(IllegalArgumentException.class, () -> new NamedXContentRegistry(new SearchModule(Settings.EMPTY, false, singletonList(registersDupeQuery)).getNamedXContents()));
    SearchPlugin registersDupeAggregation = new SearchPlugin() {

        public List<AggregationSpec> getAggregations() {
            return singletonList(new AggregationSpec(TermsAggregationBuilder.NAME, TermsAggregationBuilder::new, TermsAggregationBuilder::parse));
        }
    };
    expectThrows(IllegalArgumentException.class, () -> new NamedXContentRegistry(new SearchModule(Settings.EMPTY, false, singletonList(registersDupeAggregation)).getNamedXContents()));
    SearchPlugin registersDupePipelineAggregation = new SearchPlugin() {

        public List<PipelineAggregationSpec> getPipelineAggregations() {
            return singletonList(new PipelineAggregationSpec(DerivativePipelineAggregationBuilder.NAME, DerivativePipelineAggregationBuilder::new, DerivativePipelineAggregator::new, DerivativePipelineAggregationBuilder::parse).addResultReader(InternalDerivative::new));
        }
    };
    expectThrows(IllegalArgumentException.class, () -> new NamedXContentRegistry(new SearchModule(Settings.EMPTY, false, singletonList(registersDupePipelineAggregation)).getNamedXContents()));
}
Also used : MovAvgModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel) ExplainFetchSubPhase(org.elasticsearch.search.fetch.subphase.ExplainFetchSubPhase) SearchPlugin(org.elasticsearch.plugins.SearchPlugin) DerivativePipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.derivative.DerivativePipelineAggregationBuilder) PlainHighlighter(org.elasticsearch.search.fetch.subphase.highlight.PlainHighlighter) DerivativePipelineAggregator(org.elasticsearch.search.aggregations.pipeline.derivative.DerivativePipelineAggregator) ExplainFetchSubPhase(org.elasticsearch.search.fetch.subphase.ExplainFetchSubPhase) FetchSubPhase(org.elasticsearch.search.fetch.FetchSubPhase) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) CustomHighlighter(org.elasticsearch.search.fetch.subphase.highlight.CustomHighlighter) PlainHighlighter(org.elasticsearch.search.fetch.subphase.highlight.PlainHighlighter) FastVectorHighlighter(org.elasticsearch.search.fetch.subphase.highlight.FastVectorHighlighter) Highlighter(org.elasticsearch.search.fetch.subphase.highlight.Highlighter) PostingsHighlighter(org.elasticsearch.search.fetch.subphase.highlight.PostingsHighlighter)

Example 3 with MovAvgModel

use of org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel in project elasticsearch by elastic.

the class MovAvgUnitTests method testEWMAPredictionModel.

public void testEWMAPredictionModel() {
    double alpha = randomDouble();
    MovAvgModel model = new EwmaModel(alpha);
    int windowSize = randomIntBetween(1, 50);
    int numPredictions = randomIntBetween(1, 50);
    EvictingQueue<Double> window = new EvictingQueue<>(windowSize);
    for (int i = 0; i < windowSize; i++) {
        window.offer(randomDouble());
    }
    double[] actual = model.predict(window, numPredictions);
    double[] expected = new double[numPredictions];
    double avg = 0;
    boolean first = true;
    for (double value : window) {
        if (first) {
            avg = value;
            first = false;
        } else {
            avg = (value * alpha) + (avg * (1 - alpha));
        }
    }
    Arrays.fill(expected, avg);
    for (int i = 0; i < numPredictions; i++) {
        assertThat(Double.compare(expected[i], actual[i]), equalTo(0));
    }
}
Also used : MovAvgModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel) EvictingQueue(org.elasticsearch.common.collect.EvictingQueue) EwmaModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.EwmaModel)

Example 4 with MovAvgModel

use of org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel in project elasticsearch by elastic.

the class MovAvgUnitTests method testHoltWintersMultiplicativePadPredictionModel.

public void testHoltWintersMultiplicativePadPredictionModel() {
    double alpha = randomDouble();
    double beta = randomDouble();
    double gamma = randomDouble();
    int period = randomIntBetween(1, 10);
    MovAvgModel model = new HoltWintersModel(alpha, beta, gamma, period, HoltWintersModel.SeasonalityType.MULTIPLICATIVE, true);
    // HW requires at least two periods of data
    int windowSize = randomIntBetween(period * 2, 50);
    int numPredictions = randomIntBetween(1, 50);
    EvictingQueue<Double> window = new EvictingQueue<>(windowSize);
    for (int i = 0; i < windowSize; i++) {
        window.offer(randomDouble());
    }
    double[] actual = model.predict(window, numPredictions);
    double[] expected = new double[numPredictions];
    // Smoothed value
    double s = 0;
    double last_s = 0;
    // Trend value
    double b = 0;
    double last_b = 0;
    // Seasonal value
    double[] seasonal = new double[windowSize];
    int counter = 0;
    double[] vs = new double[windowSize];
    for (double v : window) {
        vs[counter] = v + 0.0000000001;
        counter += 1;
    }
    // Calculate the slopes between first and second season for each period
    for (int i = 0; i < period; i++) {
        s += vs[i];
        b += (vs[i + period] - vs[i]) / period;
    }
    s /= period;
    b /= period;
    last_s = s;
    // Calculate first seasonal
    if (Double.compare(s, 0.0) == 0 || Double.compare(s, -0.0) == 0) {
        Arrays.fill(seasonal, 0.0);
    } else {
        for (int i = 0; i < period; i++) {
            seasonal[i] = vs[i] / s;
        }
    }
    for (int i = period; i < vs.length; i++) {
        s = alpha * (vs[i] / seasonal[i - period]) + (1.0d - alpha) * (last_s + last_b);
        b = beta * (s - last_s) + (1 - beta) * last_b;
        seasonal[i] = gamma * (vs[i] / (last_s + last_b)) + (1 - gamma) * seasonal[i - period];
        last_s = s;
        last_b = b;
    }
    for (int i = 1; i <= numPredictions; i++) {
        int idx = window.size() - period + ((i - 1) % period);
        expected[i - 1] = (s + (i * b)) * seasonal[idx];
        assertThat(Double.compare(expected[i - 1], actual[i - 1]), equalTo(0));
    }
}
Also used : MovAvgModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel) HoltWintersModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltWintersModel) EvictingQueue(org.elasticsearch.common.collect.EvictingQueue)

Example 5 with MovAvgModel

use of org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel in project elasticsearch by elastic.

the class MovAvgUnitTests method testHoltLinearMovAvgModel.

public void testHoltLinearMovAvgModel() {
    double alpha = randomDouble();
    double beta = randomDouble();
    MovAvgModel model = new HoltLinearModel(alpha, beta);
    int numValues = randomIntBetween(1, 100);
    int windowSize = randomIntBetween(1, 50);
    EvictingQueue<Double> window = new EvictingQueue<>(windowSize);
    for (int i = 0; i < numValues; i++) {
        double randValue = randomDouble();
        if (i == 0) {
            window.offer(randValue);
            continue;
        }
        double s = 0;
        double last_s = 0;
        // Trend value
        double b = 0;
        double last_b = 0;
        int counter = 0;
        double last;
        for (double value : window) {
            last = value;
            if (counter == 1) {
                s = value;
                b = value - last;
            } else {
                s = alpha * value + (1.0d - alpha) * (last_s + last_b);
                b = beta * (s - last_s) + (1 - beta) * last_b;
            }
            counter += 1;
            last_s = s;
            last_b = b;
        }
        double expected = s + (0 * b);
        double actual = model.next(window);
        assertThat(Double.compare(expected, actual), equalTo(0));
        window.offer(randValue);
    }
}
Also used : MovAvgModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel) EvictingQueue(org.elasticsearch.common.collect.EvictingQueue) HoltLinearModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltLinearModel)

Aggregations

MovAvgModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel)16 EvictingQueue (org.elasticsearch.common.collect.EvictingQueue)12 HoltLinearModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltLinearModel)4 HoltWintersModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltWintersModel)4 EwmaModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.EwmaModel)2 LinearModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.LinearModel)2 SimpleModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.SimpleModel)2 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ParsingException (org.elasticsearch.common.ParsingException)1 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)1 Entry (org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry)1 NamedXContentRegistry (org.elasticsearch.common.xcontent.NamedXContentRegistry)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1 SearchPlugin (org.elasticsearch.plugins.SearchPlugin)1 GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)1 DerivativePipelineAggregationBuilder (org.elasticsearch.search.aggregations.pipeline.derivative.DerivativePipelineAggregationBuilder)1 DerivativePipelineAggregator (org.elasticsearch.search.aggregations.pipeline.derivative.DerivativePipelineAggregator)1 FetchSubPhase (org.elasticsearch.search.fetch.FetchSubPhase)1