prometheus/prometheus · functions.go ```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 ```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 ```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 ```func LogarithmicRegression(s Series) (regressions Series, err error) {

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

var sum float64

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

f := float64(i)
a := (f*sum - sum*sum) / (f*sum - sum*sum)
b := (sum - a*sum) / 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 ```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 ```func variance(a []float64, sample bool) float64 {

if sample {
}

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 ```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 ```func ExponentialRegression(s Series) (regressions Series, err error) {

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

var sum float64

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

denominator := (sum*sum - sum*sum)
a := math.Pow(math.E, (sum*sum-sum*sum)/denominator)
b := (sum*sum - sum*sum) / 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 ```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 ```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 ```func (l *List) Sum(i int) int64 {
var sum int64
for i > 0 {
sum += l.tree[i-1]
i -= i & -i
}
return sum
}
```