govector

package module
v0.0.0-...-b958ac0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2022 License: Apache-2.0 Imports: 7 Imported by: 18

README

govector

Build Status GoDoc

Provide nice vector API for handling numeric types in Go.

Usage

// create a Vector type from an int array
x, err := AsVector([]int{1, 2, 3, 4, 6, 5})

// create a Vector type from a float64 array, to be used for weights
w, _ := AsVector([]float64{1.0, 1.0, 1.0, 1.0, 1.0, 4.0})

// find the differences of the Vector x
d_x := x.Diff()

// Generate the empirical CDF function for x
empirical := x.Ecdf()

// Calculate the percentile from the empirical CDF of x
percentile = empirical(2.4)

// Calculate the weighted mean of x using weights w
wm, _ = x.WeightedMean(w)

// Calculate the 5% and 95% quantiles of x
q, _ := AsVector([]float64{0.05, 0.95})
quantiles, _ = x.Quantiles(q)

// cumulative sum
s = x.Cumsum()

// shuffle x
shuffled := x.Shuffle()

// Apply arbitrary functions to vectors
_ = x.Apply(empirical)
_ = x.Apply(math.Sqrt)

Documentation

Overview

Package govector provides a nice vector API for handling numeric types in Go.

Index

Constants

Variables

This section is empty.

Functions

func Cor

func Cor(x, y Vector) (float64, error)

Cor returns the Pearson correlation between two vectors.

func Cosine

func Cosine(x, y Vector) (float64, error)

Cosine returns the cosine similarity between two vectors.

func DotProduct

func DotProduct(x, y Vector) (float64, error)

DotProduct returns the dot product of two vectors.

func Norm

func Norm(x Vector, pow float64) float64

Norm returns the vector norm. Use pow = 2.0 for Euclidean.

Types

type Vector

type Vector []float64

func AsVector

func AsVector(any interface{}) (Vector, error)

AsVector converts slices of numeric types into a Vector.

func Join

func Join(vectors ...Vector) Vector

Join returns an (efficiently joined) vector of the input vectors.

func Product

func Product(x, y Vector) (Vector, error)

Product returns a vector of element-wise products of two input vectors.

func (Vector) Abs

func (x Vector) Abs() Vector

Abs returns the absolute values of the vector elements.

func (Vector) Apply

func (x Vector) Apply(f func(float64) float64) Vector

Apply returns the values of the vector applied to an arbitrary function, which must return a float64, since a Vector will be returned.

func (Vector) Copy

func (x Vector) Copy() Vector

Copy returns a copy the input vector. This is useful for functions that perform modification and shuffling on the order of the input vector.

func (Vector) Cumsum

func (x Vector) Cumsum() Vector

Cumsum returns the cumulative sum of the vector.

func (Vector) Diff

func (x Vector) Diff() Vector

Diff returns a vector of length (n - 1) of the differences in the input vector

func (Vector) Ecdf

func (x Vector) Ecdf() func(float64) float64

Ecdf returns the empirical cumulative distribution function. The ECDF function will return the percentile of a given value relative to the vector.

func (Vector) Filter

func (x Vector) Filter(f func(float64) bool) Vector

Filter returns the values that match the filter function. Vector elements with return values of TRUE are filtered/removed.

func (Vector) Len

func (x Vector) Len() int

Len, Swap, and Less are implemented to allow for direct sorting on Vector types.

func (Vector) Less

func (x Vector) Less(i, j int) bool

func (Vector) Max

func (x Vector) Max() float64

Max returns the maximum value of the vector

func (Vector) Mean

func (x Vector) Mean() float64

Mean returns the mean of the vector.

func (Vector) MeanVar

func (x Vector) MeanVar() (float64, float64)

MeanVar returns both the mean and the variance of the vector

func (Vector) Min

func (x Vector) Min() float64

Min returns the minimum value of the vector

func (Vector) Order

func (x Vector) Order() Vector

Order returns a vector of untied ranks of the input vector.

func (*Vector) Push

func (x *Vector) Push(y float64)

Push appends the input vector with the value to be pushed.

func (*Vector) PushFixed

func (x *Vector) PushFixed(y float64) error

Append values to an array. Array size will not grow if unnecessary. It will grow if the cap has been extended by external modification.

func (Vector) Quantiles

func (x Vector) Quantiles(q Vector) Vector

Quantiles returns the quantiles of a vector corresponding to input quantiles using a weighted average approach for index interpolation.

func (Vector) Rank

func (x Vector) Rank() Vector

Rank returns a vector of the ranked values of the input vector.

func (Vector) RelDiff

func (x Vector) RelDiff() Vector

RelDiff returns a vector of the relative differences of the input vector

func (Vector) Round

func (x Vector) Round(precision uint) Vector

Round returns a vector with all values rounded to a specified precision (decimal places)

func (Vector) Sample

func (x Vector) Sample(n int) Vector

Sample returns a sample of n elements of the original input vector.

func (Vector) Sd

func (x Vector) Sd() float64

Sd calculates the standard deviation of the vector

func (Vector) Shuffle

func (x Vector) Shuffle() Vector

Shuffle returns a shuffled copy of the original input vector.

func (Vector) Smooth

func (x Vector) Smooth(left, right uint) Vector

Smooth takes a sliding window average of vector. Indices i and j refer to the the number of points you'd like to consider before and after a point in the average.

func (Vector) Sort

func (x Vector) Sort()

func (Vector) Subtract

func (x Vector) Subtract(y Vector) (Vector, error)

Subtract returns a vector with the difference between x and some y Vector

func (Vector) SubtractConst

func (x Vector) SubtractConst(e float64) Vector

SubtractConst returns a vector with the differencs between all the values of x and a constant e

func (Vector) Sum

func (x Vector) Sum() float64

Sum returns the sum of the vector.

func (Vector) Swap

func (x Vector) Swap(i, j int)

func (Vector) Unique

func (x Vector) Unique() Vector

Unique returns a vector with only the unique values

func (Vector) Variance

func (x Vector) Variance() float64

Variance caclulates the variance of the vector

func (Vector) WeightedMean

func (x Vector) WeightedMean(w Vector) (float64, error)

WeightedMean returns the weighted mean of the vector for a given vector of weights.

Jump to

Keyboard shortcuts

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