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.

package main

import (
 "fmt"
 "log"
 "math"

 "github.com/thisisamirv/lowess-project/bindings/go/fastlowess/v5"
)

func main() {
 n := 500
 t := make([]float64, n)
 y := make([]float64, n)
 for i := 0; i < n; i++ {
  t[i] = float64(i) * 100.0 / float64(n-1)
  y[i] = 10.0 + 0.5*t[i] + 3.0*math.Sin(t[i]/10.0) + (math.Mod(float64(i*7+3), 1.7)-0.85)*3.0
 }

 opts := fastlowess.DefaultOptions()
 opts.Fraction = 0.1
 opts.Iterations = 3

 model, err := fastlowess.NewLowess(opts)
 if err != nil {
  log.Fatal(err)
 }
 defer model.Close()

 result, err := model.Fit(t, y)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Println("y[0]:", result.Y[0])
}
y[0]: 11.311602696688704

Detrending

Remove trend to analyze residual patterns.

Setting Outputs = []string{"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.

package main

import (
 "fmt"
 "log"
 "math"

 "github.com/thisisamirv/lowess-project/bindings/go/fastlowess/v5"
)

func main() {
 n := 100
 t := make([]float64, n)
 y := make([]float64, n)
 for i := 0; i < n; i++ {
  t[i] = float64(i) * 2 * math.Pi / float64(n-1)
  y[i] = math.Sin(t[i]) + 0.1
 }

 opts := fastlowess.DefaultOptions()
 opts.Fraction = 0.3
 opts.Iterations = 3
 opts.Outputs = []string{"residuals"}

 model, err := fastlowess.NewLowess(opts)
 if err != nil {
  log.Fatal(err)
 }
 defer model.Close()

 result, err := model.Fit(t, y)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Println("residuals[0]:", result.Residuals[0])
}
residuals[0]: -0.1582360248782261

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.

package main

import (
 "fmt"
 "log"
 "math"

 "github.com/thisisamirv/lowess-project/bindings/go/fastlowess/v5"
)

func main() {
 n := 100
 t := make([]float64, n)
 y := make([]float64, n)
 for i := 0; i < n; i++ {
  t[i] = float64(i) * 2 * math.Pi / float64(n-1)
  y[i] = math.Sin(t[i]) + 0.1
 }

 opts := fastlowess.DefaultOptions()
 opts.Fraction = 0.2
 opts.Iterations = 3
 ci := 0.95
 pi := 0.95
 opts.Intervals = &fastlowess.IntervalsOptions{Confidence: &ci, Prediction: &pi}

 model, err := fastlowess.NewLowess(opts)
 if err != nil {
  log.Fatal(err)
 }
 defer model.Close()

 result, err := model.Fit(t, y)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Printf("95%% PI: [%v, %v]\n", result.PredictionLower[0], result.PredictionUpper[0])
}
95% PI: [0.1561443787744995, 0.29890448860240026]

Handling Missing Data

LOWESS naturally handles irregular time sampling:

package main

import (
 "fmt"
 "log"
 "math"

 "github.com/thisisamirv/lowess-project/bindings/go/fastlowess/v5"
)

func main() {
 n := 100
 tIrregular := make([]float64, n)
 yIrregular := make([]float64, n)
 for i := 0; i < n; i++ {
  tIrregular[i] = float64(i)*1.0 + float64((i*31)%10)*0.1
  yIrregular[i] = 10.0 + 0.3*tIrregular[i] + 2.0*math.Sin(tIrregular[i]*0.1)
 }

 opts := fastlowess.DefaultOptions()
 opts.Fraction = 0.2

 model, err := fastlowess.NewLowess(opts)
 if err != nil {
  log.Fatal(err)
 }
 defer model.Close()

 result, err := model.Fit(tIrregular, yIrregular)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Println("y[0]:", result.Y[0])
}
y[0]: 11.32705234623955

Multi-Scale Analysis

Use different fractions to extract features at different scales:

package main

import (
 "fmt"
 "log"
 "math"

 "github.com/thisisamirv/lowess-project/bindings/go/fastlowess/v5"
)

func main() {
 n := 100
 t := make([]float64, n)
 y := make([]float64, n)
 for i := 0; i < n; i++ {
  t[i] = float64(i) * 2 * math.Pi / float64(n-1)
  y[i] = math.Sin(t[i]) + 0.1
 }

 for _, f := range []float64{0.05, 0.2, 0.5} {
  opts := fastlowess.DefaultOptions()
  opts.Fraction = f
  model, err := fastlowess.NewLowess(opts)
  if err != nil {
   log.Fatal(err)
  }
  result, err := model.Fit(t, y)
  model.Close()
  if err != nil {
   log.Fatal(err)
  }
  fmt.Printf("fraction=%v: y[0] = %v\n", f, result.Y[0])
 }
}
fraction=0.05: y[0] = 0.13171195982828227
fraction=0.2: y[0] = 0.22752443368844988
fraction=0.5: y[0] = 0.3325413792904887

Gene Expression Time Course

Biological application:

package main

import (
 "fmt"
 "log"
 "math"

 "github.com/thisisamirv/lowess-project/bindings/go/fastlowess/v5"
)

func main() {
 n := 49
 hours := make([]float64, n)
 expression := make([]float64, n)
 for i := 0; i < n; i++ {
  hours[i] = float64(i) * 0.5
  expression[i] = 100.0*(1.0+0.5*math.Sin(hours[i]*math.Pi/12.0)) + (math.Mod(float64(i*7+3), 1.7)-0.85)*10.0
 }

 opts := fastlowess.DefaultOptions()
 opts.Fraction = 0.3
 opts.Iterations = 3
 opts.Outputs = []string{"diagnostics"}
 ci := 0.95
 opts.Intervals = &fastlowess.IntervalsOptions{Confidence: &ci}

 model, err := fastlowess.NewLowess(opts)
 if err != nil {
  log.Fatal(err)
 }
 defer model.Close()

 result, err := model.Fit(hours, expression)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Printf("R2: %.3f\n", result.Diagnostics.RSquared)
}
R2: 0.976

Choosing Fraction for Time Series

Data TypeRecommended FractionRationale
Daily data (years)0.3–0.5Capture annual trends
Hourly data (days)0.1–0.2Capture daily patterns
Sensor data (minutes)0.05–0.1Preserve short-term features
Noisy dataHigherReduce noise impact
Clean dataLowerPreserve detail

See Also