prometheus/prometheus · functions.go
go logo
func linearRegression(samples []Point, interceptTime int64) (slope, intercept float64) {
	var (
		n            float64
		sumX, sumY   float64
		sumXY, sumX2 float64
	)
	for _, sample := range samples {
		x := float64(sample.T-interceptTime) / 1e3
		n += 1.0
		sumY += sample.V
		sumX += x
		sumXY += x * sample.V
		sumX2 += x * x
	}
	covXY := sumXY - sumX*sumY/n
	varX := sumX2 - sumX*sumX/n

	slope = covXY / varX
	intercept = sumY/n - slope*sumX/n
	return slope, intercept
}
Similar code snippets
1.
control-center/serviced · ols.go
Match rating: 60.94% · See similar code snippets
go logo
func Mean(series []float64) float64 {
	var n, x, sum float64
	for _, x = range series {
		sum += x
		n++
	}
	return sum / n
}
2.
aclements/go-moremath · vec.go
Match rating: 60.45% · See similar code snippets
go logo
func Sum(xs []float64) float64 {
	sum := 0.0
	for _, x := range xs {
		sum += x
	}
	return sum
}
3.
montanaflynn/stats · regression.go
Match rating: 57.42% · See similar code snippets
go logo
func LogarithmicRegression(s Series) (regressions Series, err error) {

	if len(s) == 0 {
		return nil, EmptyInputErr
	}

	var sum [4]float64

	i := 0
	for ; i < len(s); i++ {
		sum[0] += math.Log(s[i].X)
		sum[1] += s[i].Y * math.Log(s[i].X)
		sum[2] += s[i].Y
		sum[3] += math.Pow(math.Log(s[i].X), 2)
	}

	f := float64(i)
	a := (f*sum[1] - sum[2]*sum[0]) / (f*sum[3] - sum[0]*sum[0])
	b := (sum[2] - a*sum[0]) / f

	for j := 0; j < len(s); j++ {
		regressions = append(regressions, Coordinate{
			X: s[j].X,
			Y: b + a*math.Log(s[j].X),
		})
	}

	return regressions, nil
}
4.
golang/tour · named-results.go
Match rating: 57.38% · See similar code snippets
go logo
func split(sum int) (x, y int) {
	x = sum * 4 / 9
	y = sum - x
	return
}
5.
dgryski/go-onlinestats · basics.go
Match rating: 57.23% · See similar code snippets
go logo
func variance(a []float64, sample bool) float64 {

	var adjustN float64

	if sample {
		adjustN = -1
	}

	m := Mean(a)
	var sum2 float64
	var sum3 float64
	for _, aa := range a {
		sum2 += (aa - m) * (aa - m)
		sum3 += (aa - m)
	}
	n := float64(len(a))
	return (sum2 - (sum3*sum3)/n) / (n + adjustN)
}
6.
control-center/serviced · ols.go
Match rating: 56.93% · See similar code snippets
go logo
func LeastSquares(xs, ys []float64) (m, b float64, err error) {

	lx, ly := len(xs), len(ys)

	// If the arrays are not of equal length, we can't do anything
	if lx != ly {
		err = ErrUnequalArrays
		return
	}

	// If we don't have at least two points, we can't do anything
	if lx < 2 {
		err = ErrInsufficientData
		return
	}

	var (
		x, y, meanx, meany, sumxx, sumxy float64
		i                                int
	)

	// Since we're dealing with sums of squares, we'll need to shift the data
	// to be centered around the origin to prevent potential overflow or
	// numerical instability. We'll do this by subtracting the mean of x from
	// each x value, and the mean of y from each y value. This takes an extra
	// pass in each case, but will be much more accurate, since we won't
	// compound rounding errors produced by summing squares of large floats.
	meanx = Mean(xs)
	meany = Mean(ys)

	for i, x = range xs {
		y = ys[i] - meany
		x = x - meanx
		sumxx += x * x
		sumxy += x * y
	}

	// Least squares gets a lot simpler when you subtract the means, because
	// sum of x-values and sum of y-values are both zero.
	m = sumxy / sumxx

	// Shift our y-intercept back to account for our mean subtraction
	b = meany - (m * meanx)

	return
}
7.
montanaflynn/stats · regression.go
Match rating: 56.23% · See similar code snippets
go logo
func ExponentialRegression(s Series) (regressions Series, err error) {

	if len(s) == 0 {
		return nil, EmptyInputErr
	}

	var sum [6]float64

	for i := 0; i < len(s); i++ {
		if s[i].Y < 0 {
			return nil, YCoordErr
		}
		sum[0] += s[i].X
		sum[1] += s[i].Y
		sum[2] += s[i].X * s[i].X * s[i].Y
		sum[3] += s[i].Y * math.Log(s[i].Y)
		sum[4] += s[i].X * s[i].Y * math.Log(s[i].Y)
		sum[5] += s[i].X * s[i].Y
	}

	denominator := (sum[1]*sum[2] - sum[5]*sum[5])
	a := math.Pow(math.E, (sum[2]*sum[3]-sum[5]*sum[4])/denominator)
	b := (sum[1]*sum[4] - sum[5]*sum[3]) / denominator

	for j := 0; j < len(s); j++ {
		regressions = append(regressions, Coordinate{
			X: s[j].X,
			Y: a * math.Exp(b*s[j].X),
		})
	}

	return regressions, nil
}
8.
neurosnap/sentences · frequency_dist.go
Match rating: 56.2% · See similar code snippets
go logo
func (f *FreqDist) N() float64 {
	sum := 0.0
	for _, val := range f.Samples {
		sum += float64(val)
	}
	return sum
}
9.
gizak/termui · utils.go
Match rating: 55.59% · See similar code snippets
go logo
func SumFloat64Slice(data []float64) float64 {
	sum := 0.0
	for _, v := range data {
		sum += v
	}
	return sum
}
10.
yourbasic/fenwick · list.go
Match rating: 55.16% · See similar code snippets
go logo
func (l *List) Sum(i int) int64 {
	var sum int64
	for i > 0 {
		sum += l.tree[i-1]
		i -= i & -i
	}
	return sum
}