Time Series Analysis

LOWESS for trend extraction and temporal smoothing.

Overview

Time series data often contains noise, seasonality, and trends. LOWESS provides flexible trend extraction without parametric assumptions.


Basic Trend Extraction

fraction = 0.1 sizes the neighbourhood as 10% of the data at each evaluation point — narrow enough to follow a slowly varying trend without smearing periodic variation. Three robustness iterations down-weight noise spikes so they cannot bias the fitted curve; this is especially important when the signal-to-noise ratio is low or when occasional outliers are expected.

import fastlowess.Lowess;
import fastlowess.Options;
import fastlowess.Result;

public class Example {
    public static void main(String[] args) {
        int n = 500;
        double[] t = new double[n];
        double[] y = new double[n];
        for (int i = 0; i < n; i++) {
            t[i] = i * 100.0 / (n - 1);
            y[i] = 10.0 + 0.5 * t[i] + 3.0 * Math.sin(t[i] / 10.0) + (((i * 7 + 3) % 1.7) - 0.85) * 3.0;
        }

        Options options = Options.builder().fraction(0.1).iterations(3).build();

        try (Lowess model = new Lowess(options)) {
            Result result = model.fit(t, y);
            System.out.println("y[0]: " + result.y()[0]);
        }
    }
}
y[0]: 11.311602696688702

Detrending

Remove trend to analyze residual patterns.

Setting .outputs("residuals") stores observed − smoothed alongside the smooth. A slightly wider fraction = 0.3 produces a smoother baseline trend, so short-duration oscillations end up in the residuals rather than being absorbed into the trend component. The residual series is then ready for spectral analysis, seasonality detection, or change-point methods.

import fastlowess.Lowess;
import fastlowess.Options;
import fastlowess.Result;

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

        Options options = Options.builder()
                .fraction(0.3)
                .iterations(3)
                .outputs("residuals")
                .build();

        try (Lowess model = new Lowess(options)) {
            Result result = model.fit(t, y);
            double[] residuals = result.residuals().orElseThrow();
            System.out.println("residuals[0]: " + residuals[0]);
        }
    }
}
residuals[0]: -0.158236024878226

Forecasting with Prediction Intervals

Prediction intervals widen the uncertainty band to include both the uncertainty in the fitted curve (confidence interval) and the expected scatter of new observations around it. fraction = 0.2 offers a balance between local detail and stable interval width — too small a fraction produces jagged interval edges; too large a fraction underestimates local variance near turning points.

import fastlowess.IntervalsOptions;
import fastlowess.Lowess;
import fastlowess.Options;
import fastlowess.Result;

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

        Options options = Options.builder()
                .fraction(0.2)
                .iterations(3)
                .intervals(IntervalsOptions.builder().confidence(0.95).prediction(0.95).build())
                .build();

        try (Lowess model = new Lowess(options)) {
            Result result = model.fit(t, y);
            double[] lower = result.predictionLower().orElseThrow();
            double[] upper = result.predictionUpper().orElseThrow();
            System.out.printf("95%% PI: [%s, %s]%n", lower[0], upper[0]);
        }
    }
}
95% PI: [0.15614437877449788, 0.29890448860239904]

Handling Missing Data

LOWESS naturally handles irregular time sampling:

import fastlowess.Lowess;
import fastlowess.Options;
import fastlowess.Result;

public class Example {
    public static void main(String[] args) {
        int n = 100;
        double[] tIrregular = new double[n];
        double[] yIrregular = new double[n];
        for (int i = 0; i < n; i++) {
            tIrregular[i] = i * 1.0 + (i * 31 % 10) * 0.1;
            yIrregular[i] = 10.0 + 0.3 * tIrregular[i] + 2.0 * Math.sin(tIrregular[i] * 0.1);
        }

        Options options = Options.builder().fraction(0.2).build();

        try (Lowess model = new Lowess(options)) {
            Result result = model.fit(tIrregular, yIrregular);
            System.out.println("y[0]: " + result.y()[0]);
        }
    }
}
y[0]: 11.32705234623955

Multi-Scale Analysis

Use different fractions to extract features at different scales:

import fastlowess.Lowess;
import fastlowess.Options;
import fastlowess.Result;

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

        for (double f : new double[] {0.05, 0.2, 0.5}) {
            Options options = Options.builder().fraction(f).build();
            try (Lowess model = new Lowess(options)) {
                Result result = model.fit(t, y);
                System.out.printf("fraction=%s: y[0] = %s%n", f, result.y()[0]);
            }
        }
    }
}
fraction=0.05: y[0] = 0.13171195982828227
fraction=0.2: y[0] = 0.22752443368844846
fraction=0.5: y[0] = 0.33254137929048877

Gene Expression Time Course

Biological application:

import fastlowess.Lowess;
import fastlowess.Options;
import fastlowess.Result;

public class Example {
    public static void main(String[] args) {
        int n = 49;
        double[] hours = new double[n];
        double[] expression = new double[n];
        for (int i = 0; i < n; i++) {
            hours[i] = i * 0.5;
            expression[i] = 100.0 * (1.0 + 0.5 * Math.sin(hours[i] * Math.PI / 12.0)) + (((i * 7 + 3) % 1.7) - 0.85) * 10.0;
        }

        Options options = Options.builder()
                .fraction(0.3)
                .iterations(3)
                .outputs("diagnostics")
                .build();

        try (Lowess model = new Lowess(options)) {
            Result result = model.fit(hours, expression);
            double rSquared = result.diagnostics().orElseThrow().rSquared();
            System.out.printf("R2: %.3f%n", rSquared);
        }
    }
}
R2: 0.976

Choosing Fraction for Time Series

Data Type Recommended Fraction Rationale

Daily data (years)

0.3–0.5

Capture annual trends

Hourly data (days)

0.1–0.2

Capture daily patterns

Sensor data (minutes)

0.05–0.1

Preserve short-term features

Noisy data

Higher

Reduce noise impact

Clean data

Lower

Preserve detail


See Also