Kernel functions for distance weighting.

Overview

Weight functions (kernels) determine how neighboring points contribute to each local fit. Points closer to the target receive higher weights.

Weight Functions


Available Kernels

KernelEfficiencySmoothnessSupport
Tricube0.998Very smoothCompact
Epanechnikov1.000SmoothCompact
Gaussian0.961InfiniteUnbounded
Biweight0.995Very smoothCompact
Cosine0.999SmoothCompact
Triangle0.989ModerateCompact
Uniform0.943NoneCompact

Efficiency = AMISE relative to Epanechnikov (1.0 = optimal)


Tricube (Default)

Cleveland’s original choice. Best all-around performance.

$$w(u) = (1 - |u|^3)^3$$

Use when: Default choice for most applications.

package main

import (
 "fmt"
 "log"
 "math"

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

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

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

 result, err := model.Fit(x, y)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Println("First smoothed value (tricube kernel):", result.Y[0])
}
First smoothed value (tricube kernel): 0.38088390025381585

Epanechnikov

Theoretically optimal for kernel density estimation.

$$w(u) = \frac{3}{4}(1 - u^2)$$

Use when: Optimal MSE properties desired.

package main

import (
 "fmt"
 "log"
 "math"

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

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

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

 result, err := model.Fit(x, y)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Println("First smoothed value (epanechnikov kernel):", result.Y[0])
}
First smoothed value (epanechnikov kernel): 0.406923278722846

Gaussian

Infinitely smooth. No boundary effects.

$$w(u) = \exp(-u^2/2)$$

Use when: Maximum smoothness needed, computational cost acceptable.

package main

import (
 "fmt"
 "log"
 "math"

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

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

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

 result, err := model.Fit(x, y)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Println("First smoothed value (gaussian kernel):", result.Y[0])
}
First smoothed value (gaussian kernel): 0.35401405464559677

Biweight

Good balance of efficiency and smoothness.

$$w(u) = (1 - u^2)^2$$

Use when: Alternative to Tricube with slightly different properties.

package main

import (
 "fmt"
 "log"
 "math"

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

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

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

 result, err := model.Fit(x, y)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Println("First smoothed value (biweight kernel):", result.Y[0])
}
First smoothed value (biweight kernel): 0.3743954563102442

Cosine

Smooth and computationally efficient.

$$w(u) = \cos(\pi u / 2)$$

Use when: Want smooth kernel with simple form.

package main

import (
 "fmt"
 "log"
 "math"

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

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

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

 result, err := model.Fit(x, y)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Println("First smoothed value (cosine kernel):", result.Y[0])
}
First smoothed value (cosine kernel): 0.4007691120197535

Triangle

Simple linear taper.

$$w(u) = 1 - |u|$$

Use when: Simple, interpretable weights.

package main

import (
 "fmt"
 "log"
 "math"

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

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

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

 result, err := model.Fit(x, y)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Println("First smoothed value (triangle kernel):", result.Y[0])
}
First smoothed value (triangle kernel): 0.38102907107175976

Uniform

Equal weights within window. Fastest but least smooth.

$$w(u) = 1$$

Use when: Speed is critical, smoothness less important.

package main

import (
 "fmt"
 "log"
 "math"

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

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

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

 result, err := model.Fit(x, y)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Println("First smoothed value (uniform kernel):", result.Y[0])
}
First smoothed value (uniform kernel): 0.45455195196938875

Choosing a Kernel

Choose the first row below whose condition applies:

ConditionKernel
Need maximum smoothness"gaussian"
Default is acceptable"tricube"
Need optimal asymptotic MSE"epanechnikov"
Speed is critical"uniform"
None of the above"biweight"

Recommendation: Stick with Tricube (default) unless you have specific requirements. The differences between kernels are usually small in practice.