series

package module
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: MIT Imports: 6 Imported by: 3

README

Series

test Go Report Card Go Reference

Liteweight library of series data processing functions written in pure ideomatic Go (golang). Inspired by python library Pandas.

Mostly designed for ordered time series data.

Optionally accelerated by AVX2 instructions.

Functions

  • Column/Scalar math operators:

    • Add +
    • Sub -
    • Mul *
    • Div /
    • Mod %
  • Column math functions:

    • Cos, Sin, Tan
    • Pow, Log, Log2, Exp
    • Sign
    • Min
    • Max
    • ...
    • Apply custom function
  • Rolling aggregation by functions:

    • Sum
    • Mean
    • Median (only for sorted values)
    • Min
    • Max
    • Skew
    • Variance
    • Std (standard deviation)
    • Apply custom function
  • Exponential rolling aggregation:

    • (not) adjusted Mean
  • Resampling:

    • Upsampling empty values filling:
      • Interpolate by linear method;
      • Pad known values;
      • Keep empty values.
    • Downsampling aggregation functions:
      • Sum
      • Mean
      • Max
      • Min
      • First
      • Last
      • Apply custom function
  • Series manipulations:

    • Slice, Clone
    • Sort, Reverse, Compare (indices or values)
    • Diff, Shift
    • Fill N/A values: interpolate, pad existing values or replace by the constant.
    • Delete N/A values with Shrink method.

Drawing plots

series.Data implements gonum/plot/plotter.XYer interface. You can check these plotters:

Data types

For enabling float32 use build tag series_f32, otherwise float64 will be used as data type.

Build command example:

go build -tags series_f32

Examples

financial technical indicators

drawing plots

Documentation

Index

Constants

View Source
const (
	EpsFp32 = 1e-7
	EpsFp64 = 1e-14
	Eps     = EpsFp64

	EnabledFloat32 = false
)
View Source
const EnabledAVX2 = false

Variables

This section is empty.

Functions

func Argmax added in v0.0.6

func Argmax(data Data) int

Argmax returns offset of the biggest value of series data. If the maximum is achieved in multiple locations, the first row position is returned.

func Argmin added in v0.0.6

func Argmin(data Data) int

Argmin returns offset of the smallest value of series data. If the minimum is achieved in multiple locations, the first row position is returned.

func IsNA added in v0.5.1

func IsNA(v DType) bool

Types

type AggregateFunc added in v0.1.0

type AggregateFunc func(data Data) DType

AggregateFunc is applied aggregation function.

type AlphaType added in v0.0.3

type AlphaType int
const (
	// Specify smoothing factor α directly, 0<α≤1.
	Alpha AlphaType = iota
	// Specify decay in terms of center of mass, α=1/(1+com), for com ≥ 0.
	AlphaCom
	// Specify decay in terms of span, α=2/(span+1), for span ≥ 1.
	AlphaSpan
	// Specify decay in terms of half-life, α=1−exp(−ln(2)/halflife), for halflife > 0.
	AlphaHalflife
)

type DType added in v0.3.6

type DType = float64

func First added in v0.1.0

func First(data Data) DType

func Last added in v0.1.0

func Last(data Data) DType

func Max added in v0.0.3

func Max(data Data) DType

Max returns maximum value.

func Mean

func Mean(data Data) DType

Mean returns mean of data's values.

func Median added in v0.3.7

func Median(data Data) DType

Median returns median value of series. Linear interpolation is used for odd length.

func Min added in v0.0.3

func Min(data Data) DType

Min returns minimum value.

func Skew added in v0.3.12

func Skew(data Data) DType

func Std added in v0.0.5

func Std(data Data, mean DType, ddof int) DType

Std returns standard deviation. Ddof - Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements.

func Sum

func Sum(data Data) DType

Sum returns sum of data's values.

func Variance added in v0.3.10

func Variance(data Data, mean DType, ddof int) DType

Variance returns variance of values. Ddof - Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements.

type DTypeSlice added in v0.3.7

type DTypeSlice []DType

func (DTypeSlice) Len added in v0.3.7

func (x DTypeSlice) Len() int

func (DTypeSlice) Less added in v0.3.7

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

func (DTypeSlice) Swap added in v0.3.7

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

type Data

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

Data is the series values container.

func MakeData

func MakeData(freq int64, index []int64, values []DType) Data

MakeData makes series data instance. freq is the size of values sample.

func MakeValues added in v0.3.8

func MakeValues(values []DType) Data

MakeValues makes vector of values without indices. Any manipulations with index will cause panic or incorrect results!

func (Data) Abs added in v0.0.8

func (d Data) Abs() Data

Abs replace each elemnt by their absolute value.

func (Data) Acos added in v0.3.11

func (d Data) Acos() Data

func (Data) Add

func (d Data) Add(r Data) Data

func (Data) AddScalar

func (d Data) AddScalar(s DType) Data

func (Data) Append added in v0.3.0

func (d Data) Append(r Data) Data

Append appends new values to series values.

func (Data) AppendXY added in v0.5.0

func (d Data) AppendXY(x int64, y DType) Data

AppendXY appends x to indices, y to values.

func (Data) Apply added in v0.0.5

func (d Data) Apply(fn func(DType) DType) Data

Apply applies user's function to every value of values.

func (Data) Asin added in v0.3.11

func (d Data) Asin() Data

func (Data) At added in v0.2.0

func (d Data) At(i int) DType

At returns values value at i offset. i can be negative.

func (Data) Atan added in v0.3.11

func (d Data) Atan() Data

func (Data) Ceil added in v0.0.9

func (d Data) Ceil() Data

func (Data) Clone

func (d Data) Clone() Data

Clone makes full copy of values.

func (Data) Cos added in v0.3.11

func (d Data) Cos() Data

func (Data) Cumsum added in v0.3.13

func (d Data) Cumsum() Data

Cumsum returns cumulative sum over values. NaN values are ignored.

func (Data) DataReverse added in v0.3.0

func (d Data) DataReverse() Data

Reverse reverses only values values.

func (Data) Diff added in v0.3.0

func (d Data) Diff(periods int) Data

Diff calculates the difference of a series values elements.

func (Data) Div

func (d Data) Div(r Data) Data

func (Data) DivScalar

func (d Data) DivScalar(s DType) Data

func (Data) Dot added in v0.6.2

func (d Data) Dot(r Data) DType

Dot returns scalar of vectors production.

func (Data) EWM added in v0.0.3

func (d Data) EWM(atype AlphaType, param DType, adjust bool, ignoreNA bool) ExpWindow

EWM provides exponential weighted calculations.

func (Data) Equals added in v0.3.3

func (d Data) Equals(r Data, eps DType) bool

Equals tests data searies are equal to each other. NaN values are considered to be equal.

func (Data) Exp added in v0.3.11

func (d Data) Exp() Data

Exp applies e**x, the base-e exponential of x.

func (Data) Exp2 added in v0.3.11

func (d Data) Exp2() Data

Exp2 applies 2**x, the base-2 exponential of x.

func (Data) Fillna added in v0.0.3

func (d Data) Fillna(value DType) Data

Fillna fills NaN values.

func (Data) Floor added in v0.0.9

func (d Data) Floor() Data

Floor returns the greatest integer value less than or equal to x.

func (Data) Freq added in v0.2.0

func (d Data) Freq() int64

Freq returns period length of one sample.

func (Data) HasNA added in v0.6.2

func (d Data) HasNA() bool

HasNA returns true if values has at least one n/a.

func (Data) Index

func (d Data) Index() (index []int64)

Index returns underlying index values.

func (Data) IndexAsFloat32 added in v0.0.9

func (d Data) IndexAsFloat32() (index []float32)

IndexAsFloat32 returns copy of underlying index slice converted to float32 array.

func (Data) IndexAsFloat64 added in v0.0.9

func (d Data) IndexAsFloat64() (index []float64)

IndexAsFloat64 returns copy of underlying index slice converted to float64 array.

func (Data) IndexAsInt32 added in v0.3.0

func (d Data) IndexAsInt32() (index []int32)

IndexAsInt32 returns copy of underlying index slice converted to int32 array.

func (Data) IndexAt added in v0.3.4

func (d Data) IndexAt(i int) int64

IndexAt returns index value at i offset. i can be negative.

func (Data) IndexEquals added in v0.3.4

func (d Data) IndexEquals(r Data) bool

func (Data) IndexReverse added in v0.3.4

func (d Data) IndexReverse() Data

Reverse reverses only index values.

func (Data) IndexSort added in v0.3.4

func (d Data) IndexSort() Data

IndexSort sorts data's index.

func (Data) IndexSortStable added in v0.3.4

func (d Data) IndexSortStable() Data

IndexSortStable sorts data's index using stable sort algorithm.

func (Data) Len

func (d Data) Len() int

Len returns size of series values.

func (Data) Lerp added in v0.3.2

func (d Data) Lerp() Data

Lerp fills NaNs between known values by linear interpolation method.

func (Data) Log added in v0.0.3

func (d Data) Log() Data

Log applies natural logarithm function to values of values.

func (Data) Log10 added in v0.6.0

func (d Data) Log10() Data

Log10 applies Log10(x).

func (Data) Log2 added in v0.3.11

func (d Data) Log2() Data

Log2 applies Log2(x).

func (Data) Max added in v0.3.11

func (d Data) Max(r Data) Data

func (Data) Min added in v0.3.11

func (d Data) Min(r Data) Data

func (Data) Mod added in v0.3.11

func (d Data) Mod(r Data) Data

func (Data) Mul

func (d Data) Mul(r Data) Data

func (Data) MulScalar

func (d Data) MulScalar(s DType) Data

func (Data) Pad added in v0.3.0

func (d Data) Pad() Data

Pad fills NaNs by previous values.

If series starts with NaN, it will be filled by the first non-NaN value.

func (Data) Pow added in v0.3.11

func (d Data) Pow(exp DType) Data

Pow applies x**y, the base-x exponential of y.

func (Data) Pow10 added in v0.3.11

func (d Data) Pow10() Data

Pow10 applies 10**e, the base-10 exponential of e.

func (Data) Resample

func (d Data) Resample(freq int64, origin ResampleOrigin) Resampler

func (Data) Resize added in v0.3.0

func (d Data) Resize(newLen int) Data

Resize resizes underlying arrays.

New index values are filled by MaxInt64. New values values are filled by NaN.

func (Data) Reverse added in v0.3.0

func (d Data) Reverse() Data

Reverse reverses index and values values.

func (Data) RollData

func (d Data) RollData(window int, cb func(l int, r int))

RollData applies custom function to rolling window of values. Function accepts window bounds.

func (Data) Rolling

func (d Data) Rolling(window int) Window

Rolling provides rolling window calculations.

func (Data) Round added in v0.0.9

func (d Data) Round() Data

Round returns the nearest integer, rounding half away from zero.

func (Data) RoundToEven added in v0.0.9

func (d Data) RoundToEven() Data

RoundToEven returns the nearest integer, rounding ties to even.

func (Data) Set added in v0.5.0

func (d Data) Set(i int, v DType)

Set sets new value at i position. i can be negative.

func (Data) SetXY added in v0.5.0

func (d Data) SetXY(i int, x int64, y DType)

SetXY x to index, y to values at position i. i can be negative.

func (Data) Shift added in v0.3.0

func (d Data) Shift(periods int) Data

Shift shifts values by specified periods count.

func (Data) Shrink added in v0.6.1

func (d Data) Shrink() Data

Shrink removes na values.

New Data instance will be returned. Old and new have the same internal arrays. No additional memory is used.

Safe for the empty index.

func (Data) Sign added in v0.3.11

func (d Data) Sign() Data

func (Data) Sin added in v0.3.11

func (d Data) Sin() Data

func (Data) Slice

func (d Data) Slice(l, r int) Data

Slice makes valuesice of values. l and r can be negatvie values.

func (Data) Sort added in v0.2.0

func (d Data) Sort() Data

Sort sorts data.

func (Data) SortStable added in v0.2.0

func (d Data) SortStable() Data

SortStable sorts data's index using stable sort algorithm.

func (Data) Sqr added in v0.3.11

func (d Data) Sqr() Data

Sqr applies x**2, the base-x exponential of 2.

func (Data) String added in v0.4.1

func (d Data) String() string

String converts time series columns to string. Index values are rendered as time.Duration.

func (Data) Sub

func (d Data) Sub(r Data) Data

func (Data) SubScalar

func (d Data) SubScalar(s DType) Data

func (Data) Tan added in v0.3.11

func (d Data) Tan() Data

func (Data) Trunc added in v0.0.9

func (d Data) Trunc() Data

Trunc returns the integer value of x.

func (Data) Values added in v0.3.4

func (d Data) Values() (values []DType)

Values returns data data values.

func (Data) ValuesAsFloat32 added in v0.6.1

func (d Data) ValuesAsFloat32() (values []float32)

ValuesAsFloat32 returns copy of underlying values slice converted to float32 array.

func (Data) ValuesAsFloat64 added in v0.6.1

func (d Data) ValuesAsFloat64() (values []float64)

DataAsFloat64 returns copy of underlying values slice converted to float64 array.

func (Data) ValuesAsInt32 added in v0.6.1

func (d Data) ValuesAsInt32() (values []int32)

ValuesAsInt32 returns copy of underlying values slice converted to int32 array.

func (Data) ValuesAsInt64 added in v0.6.1

func (d Data) ValuesAsInt64() (values []int64)

ValuesAsInt64 returns copy of underlying values slice converted to float32 array.

func (Data) ValuesEquals added in v0.3.4

func (d Data) ValuesEquals(r Data, eps DType) bool

func (Data) XY added in v0.4.0

func (d Data) XY(i int) (x, y float64)

XY returns index and value as x, y tuple. Required for gonum's plotter.XYer interface.

index values must be nanoseconds since 1970 1st Jan. x will be converted to seconds.

i can be negative.

type ExpWindow added in v0.0.3

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

func (ExpWindow) Mean added in v0.0.3

func (w ExpWindow) Mean() Data

type InterpolationMethod added in v0.3.0

type InterpolationMethod int

InterpolationMethod is the method of filling NaN values.

const (
	// InterpolationLinear fills NaNs by linear interpolation method.
	InterpolationLinear InterpolationMethod = iota
	// InterpolationPad fills NaNs by existing values.
	InterpolationPad
	// InterpolationNone doesn't fill NaNs.
	InterpolationNone
)

type ResampleOrigin added in v0.1.0

type ResampleOrigin int

ResampleOrigin is the timestamp (milliseconds) on which to adjust the grouping. The timezone of origin must match the timezone of the index.

const (
	// OriginEpoch is 1970-01-01.
	OriginEpoch ResampleOrigin = iota
	// OriginStart is the first value of the timeseries.
	OriginStart
	// OriginStartDay is the first day at midnight of the timeseries.
	OriginStartDay
)

type Resampler added in v0.1.0

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

Resampler resamples time-series data. Not full groups will are filled by NaNs.

func (Resampler) Apply added in v0.1.0

func (res Resampler) Apply(agg AggregateFunc) Data

Apply applies custom function to sample group.

func (Resampler) First added in v0.1.0

func (res Resampler) First() Data

First applies first function to sample group.

func (Resampler) Interpolate added in v0.3.2

func (res Resampler) Interpolate(method InterpolationMethod) Data

Interpolate fills all NaNs between known values after applied upsamping.

func (Resampler) Last added in v0.1.0

func (res Resampler) Last() Data

Last applies last function to sample group.

func (Resampler) Max added in v0.1.0

func (res Resampler) Max() Data

Max applies max function to sample group.

func (Resampler) Mean added in v0.1.0

func (res Resampler) Mean() Data

Mean applies mean function to sample group.

func (Resampler) Median added in v0.3.7

func (res Resampler) Median() Data

Median applies median function to sample group.

func (Resampler) Min added in v0.1.0

func (res Resampler) Min() Data

Min applies min function to sample group.

func (Resampler) Sum added in v0.1.0

func (res Resampler) Sum() Data

Sum applies sum function to sample group.

type Window

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

func (Window) Apply added in v0.0.6

func (w Window) Apply(agg AggregateFunc) Data

func (Window) Max added in v0.0.3

func (w Window) Max() Data

func (Window) Mean

func (w Window) Mean() Data

func (Window) Median added in v0.3.7

func (w Window) Median() Data

func (Window) Min added in v0.0.3

func (w Window) Min() Data

func (Window) Skew added in v0.3.12

func (w Window) Skew(ma Data) Data

func (Window) Std added in v0.0.7

func (w Window) Std(ma Data, ddof int) Data

func (Window) Sum

func (w Window) Sum() Data

func (Window) Variance added in v0.3.10

func (w Window) Variance(ma Data, ddof int) Data

Directories

Path Synopsis
Package math provides basic constants and mathematical functions.
Package math provides basic constants and mathematical functions.

Jump to

Keyboard shortcuts

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