Search in sources :

Example 1 with EvictingQueue

use of org.elasticsearch.common.collect.EvictingQueue 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 EvictingQueue

use of org.elasticsearch.common.collect.EvictingQueue 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 3 with EvictingQueue

use of org.elasticsearch.common.collect.EvictingQueue 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 4 with EvictingQueue

use of org.elasticsearch.common.collect.EvictingQueue 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 5 with EvictingQueue

use of org.elasticsearch.common.collect.EvictingQueue 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)14 MovAvgModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel)13 HoltLinearModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltLinearModel)4 HoltWintersModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltWintersModel)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 StreamSupport (java.util.stream.StreamSupport)2 StreamInput (org.elasticsearch.common.io.stream.StreamInput)2 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)2 DocValueFormat (org.elasticsearch.search.DocValueFormat)2 InternalAggregation (org.elasticsearch.search.aggregations.InternalAggregation)2 ReduceContext (org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext)2 InternalAggregations (org.elasticsearch.search.aggregations.InternalAggregations)2 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)2 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)2 HistogramFactory (org.elasticsearch.search.aggregations.bucket.histogram.HistogramFactory)2 GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)2