Search in sources :

Example 1 with LinearModel

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

the class MovAvgUnitTests method testLinearMovAvgModel.

public void testLinearMovAvgModel() {
    MovAvgModel model = new LinearModel();
    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;
        long totalWeight = 1;
        long current = 1;
        for (double value : window) {
            avg += value * current;
            totalWeight += current;
            current += 1;
        }
        double expected = avg / totalWeight;
        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) LinearModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.LinearModel)

Example 2 with LinearModel

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

the class MovAvgUnitTests method testLinearPredictionModel.

public void testLinearPredictionModel() {
    MovAvgModel model = new LinearModel();
    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;
    long totalWeight = 1;
    long current = 1;
    for (double value : window) {
        avg += value * current;
        totalWeight += current;
        current += 1;
    }
    avg = avg / totalWeight;
    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) HoltLinearModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltLinearModel) LinearModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.LinearModel)

Aggregations

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