Search in sources :

Example 1 with HoltLinearModel

use of org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltLinearModel 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)

Example 2 with HoltLinearModel

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

the class MovAvgUnitTests method testHoltLinearPredictionModel.

public void testHoltLinearPredictionModel() {
    double alpha = randomDouble();
    double beta = randomDouble();
    MovAvgModel model = new HoltLinearModel(alpha, beta);
    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 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;
    }
    for (int i = 0; i < numPredictions; i++) {
        expected[i] = s + (i * b);
        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) HoltLinearModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltLinearModel)

Aggregations

EvictingQueue (org.elasticsearch.common.collect.EvictingQueue)2 HoltLinearModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltLinearModel)2 MovAvgModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel)2