loess

package
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2018 License: MIT Imports: 2 Imported by: 4

Documentation

Overview

Example (ExternalWeights)
a := []float64{
	5, 6.0, 2.0, 4.5, 5,
	5, 6.5, 3.5, 4.0, 5,
	5, 5.5, 3.5, 5.0, 5,
	5, 6.5, 2.5, 4.5, 5,
}
w := []float64{
	1, 0, 1, 0, 1,
	1, 0, 1, 0, 1,
	1, 0, 1, 0, 1,
	1, 0, 1, 0, 1,
}

width := 5
state := NewWithExternal(width, a, w)
retVal := make([]float64, len(a))

if smoothed, err := UnsafeSmooth(state, width, 1, Linear, retVal); err == nil {
	fmt.Printf("Smoothed %1.2f\n", smoothed)
} else {
	fmt.Printf("ERR %v", err)
}
Output:

Smoothed [5.00 3.50 2.00 3.50 5.00 5.00 4.25 3.50 4.25 5.00 5.00 4.25 3.50 4.25 5.00 5.00 3.75 2.50 3.75 5.00]
Example (Smooth)
a := []float64{
	5, 6.0, 2.0, 4.5, 5,
	5, 6.5, 3.5, 4.0, 5,
	5, 5.5, 3.5, 5.0, 5,
	5, 6.5, 2.5, 4.5, 5,
}

// Smooth on a seasonality width of 1, and period of 5 (i.e. look ahead 5 and regress on it)
if smoothed, err := Smooth(a, 1, 5, Linear); err == nil {
	fmt.Printf("Smoothed %1.2f\n", smoothed)
}

// Smoothed on a periodic window of of 5
if smoothed, err := Smooth(a, 5, 1, Linear); err == nil {
	fmt.Printf("Smoothed %1.2f\n", smoothed)
}

// if smoothed, err := Smooth(a, 5, 1, Quadratic); err == nil {
// 	fmt.Printf("Smoothed %1.1v\n", smoothed)
// }

// Smoothed [1e+01 1e+01 1e+01 9 7 5 5 5 5 5 5 3 1 -0.7 -3 -5 0.1 5 9 1e+01]
Output:

Smoothed [5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00]
Smoothed [5.50 4.60 3.86 3.93 4.86 5.43 5.21 4.50 4.14 4.71 5.14 4.78 4.50 4.57 5.00 5.43 4.93 4.22 4.35 4.78]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Linear

func Linear(s *State, x, left, right float64) error

Linear performs linear regression, but constrained to left and right values. The implementation is slightly different from the typical Y = (X'X)^-1(X'Y)

func Quadratic

func Quadratic(s *State, x, left, right float64) error

Quadratic performs quadratic regression, constrained to left. and right.

func Regress

func Regress(s *State, fn WeightUpdate, x, left, right float64) (retVal float64, err error)

Regress performs local regression of x between left and right

func Smooth

func Smooth(x []float64, width, jump int, fn WeightUpdate) ([]float64, error)

Smooth smooths a slice of float64, with the provided width, jumps and update functions

func UnsafeSmooth

func UnsafeSmooth(regression *State, width, jump int, fn WeightUpdate, retVal []float64) ([]float64, error)

UnsafeSmooth is like Smooth, except the state is passed in, as well as a optional return value to be mutated.

Types

type State

type State struct {
	// contains filtered or unexported fields
}

State represents a state for regression. Every field will be mutated by the Regress and Smooth functions

func New

func New(width int, x []float64) *State

New creates a new LOESS state

func NewWithExternal

func NewWithExternal(width int, x, e []float64) *State

NewWithExternal creates a new LOESS state with externally provided weights

func (State) E

func (s State) E() []float64

W returns the externally defined weights in the state.

func (State) W

func (s State) W() []float64

W returns the weights in the state

func (State) X

func (s State) X() []float64

X returns the Xs in the state.

type WeightUpdate

type WeightUpdate func(s *State, x, left, right float64) error

WeightUpdate is a function that modifies the State.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL