Merge Strategies

How overlapping chunk boundaries are reconciled in Streaming mode.

Overview

StreamingLowess processes data in fixed-size chunks with a configurable overlap. Points inside the overlap zone are fitted twice — once by the left chunk and once by the right chunk. mergeStrategy decides how those two estimates are combined into a single output value.

Chunk A:   [=========|=====]
Chunk B:            [=====|=========]
Overlap:            [=====]
                      ^
                mergeStrategy
                applied here
Strategy Method Robustness Speed

"average"

Simple mean of both estimates

Low

Fastest

"take_first"

Left-chunk estimate only

Low

Fastest

"take_last"

Right-chunk estimate only

Low

Fastest

"weighted_average"

Distance-weighted mean

High

Moderate

Merge Strategies

Average

Takes the arithmetic mean of the left-chunk and right-chunk estimates in the overlap region. Fast and sufficient when both chunks have similar smoothing quality.

Use when: Chunks are large and the overlap region has uniform data density.

import fastlowess.Result;
import fastlowess.StreamingLowess;
import fastlowess.StreamingOptions;

public class Example {
    public static void main(String[] args) {
        int n = 100;
        double[] x = new double[n];
        double[] y = new double[n];
        for (int i = 0; i < n; i++) {
            x[i] = i * 2 * Math.PI / (n - 1);
            y[i] = Math.sin(x[i]) + 0.1;
        }

        StreamingOptions options = StreamingOptions.builder().overlap(10).mergeStrategy("average").build();

        try (StreamingLowess model = new StreamingLowess(options)) {
            model.processChunk(java.util.Arrays.copyOfRange(x, 0, 50), java.util.Arrays.copyOfRange(y, 0, 50));
            model.processChunk(java.util.Arrays.copyOfRange(x, 50, x.length), java.util.Arrays.copyOfRange(y, 50, y.length));

            Result result = model.finish();
            System.out.println("First smoothed value (average merge): " + result.y()[0]);
        }
    }
}
First smoothed value (average merge): -0.41219870679813103

Take First

Keeps only the left-chunk estimate in the overlap zone and discards the right-chunk estimate. Produces a definitive, non-revised output as soon as the right boundary of each chunk is reached.

Use when: You need final output values immediately after each chunk (no look-ahead revision); left-chunk data quality is higher.

import fastlowess.Result;
import fastlowess.StreamingLowess;
import fastlowess.StreamingOptions;

public class Example {
    public static void main(String[] args) {
        int n = 100;
        double[] x = new double[n];
        double[] y = new double[n];
        for (int i = 0; i < n; i++) {
            x[i] = i * 2 * Math.PI / (n - 1);
            y[i] = Math.sin(x[i]) + 0.1;
        }

        StreamingOptions options = StreamingOptions.builder().overlap(10).mergeStrategy("take_first").build();

        try (StreamingLowess model = new StreamingLowess(options)) {
            model.processChunk(java.util.Arrays.copyOfRange(x, 0, 50), java.util.Arrays.copyOfRange(y, 0, 50));
            model.processChunk(java.util.Arrays.copyOfRange(x, 50, x.length), java.util.Arrays.copyOfRange(y, 50, y.length));

            Result result = model.finish();
            System.out.println("First smoothed value (take_first merge): " + result.y()[0]);
        }
    }
}
First smoothed value (take_first merge): -0.41219870679813103

Take Last

Keeps only the right-chunk estimate in the overlap zone. The right chunk sees more of the surrounding data, so its fit can be more accurate near the left boundary of the new chunk.

Use when: Right-chunk context improves overlap quality; you are post-processing complete data rather than streaming live.

import fastlowess.Result;
import fastlowess.StreamingLowess;
import fastlowess.StreamingOptions;

public class Example {
    public static void main(String[] args) {
        int n = 100;
        double[] x = new double[n];
        double[] y = new double[n];
        for (int i = 0; i < n; i++) {
            x[i] = i * 2 * Math.PI / (n - 1);
            y[i] = Math.sin(x[i]) + 0.1;
        }

        StreamingOptions options = StreamingOptions.builder().overlap(10).mergeStrategy("take_last").build();

        try (StreamingLowess model = new StreamingLowess(options)) {
            model.processChunk(java.util.Arrays.copyOfRange(x, 0, 50), java.util.Arrays.copyOfRange(y, 0, 50));
            model.processChunk(java.util.Arrays.copyOfRange(x, 50, x.length), java.util.Arrays.copyOfRange(y, 50, y.length));

            Result result = model.finish();
            System.out.println("First smoothed value (take_last merge): " + result.y()[0]);
        }
    }
}
First smoothed value (take_last merge): -0.41219870679813103

Weighted Average

Assigns each overlap point a weight proportional to its proximity to the centre of its respective chunk: points near the left-chunk centre get higher left weight; points near the right-chunk centre get higher right weight. This produces the smoothest transition across chunk boundaries.

\[\hat{y} = \frac{w_L \hat{y}_L + w_R \hat{y}_R}{w_L + w_R}\]

where \(w_L\) and \(w_R\) are linear distance weights from the chunk centres.

Use when: Minimising boundary artefacts is more important than speed; moderate overlap (10–20% of chunk size).

import fastlowess.Result;
import fastlowess.StreamingLowess;
import fastlowess.StreamingOptions;

public class Example {
    public static void main(String[] args) {
        int n = 100;
        double[] x = new double[n];
        double[] y = new double[n];
        for (int i = 0; i < n; i++) {
            x[i] = i * 2 * Math.PI / (n - 1);
            y[i] = Math.sin(x[i]) + 0.1;
        }

        StreamingOptions options = StreamingOptions.builder().overlap(10).mergeStrategy("weighted_average").build(); // Default merge strategy

        try (StreamingLowess model = new StreamingLowess(options)) {
            model.processChunk(java.util.Arrays.copyOfRange(x, 0, 50), java.util.Arrays.copyOfRange(y, 0, 50));
            model.processChunk(java.util.Arrays.copyOfRange(x, 50, x.length), java.util.Arrays.copyOfRange(y, 50, y.length));

            Result result = model.finish();
            System.out.println("First smoothed value (weighted_average merge): " + result.y()[0]);
        }
    }
}
First smoothed value (weighted_average merge): -0.41219870679813103

Choosing a Strategy

Situation Recommended Strategy

General purpose

"weighted_average"

Maximum throughput

"average"

Immediate finalised output

"take_first"

Post-processing, right context better

"take_last"

Minimising boundary artefacts

"weighted_average"

Overlap size matters: A larger overlap gives the merge strategy more room to blend, reducing boundary artefacts regardless of the strategy chosen. A good starting point is 10% of chunkSize.

All examples above use the same 100-point noisy sine wave (see Weight Functions for the generation code).