Search in sources :

Example 1 with EwmaModel

use of org.elasticsearch.search.aggregations.pipeline.movavg.models.EwmaModel 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 2 with EwmaModel

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

the class MovAvgUnitTests method testEWMAMovAvgModel.

public void testEWMAMovAvgModel() {
    double alpha = randomDouble();
    MovAvgModel model = new EwmaModel(alpha);
    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 avg = 0;
        boolean first = true;
        for (double value : window) {
            if (first) {
                avg = value;
                first = false;
            } else {
                avg = (value * alpha) + (avg * (1 - alpha));
            }
        }
        double expected = avg;
        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) EwmaModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.EwmaModel)

Aggregations

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