1 Star 0 Fork 2

王布衣 / ta-lib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
linear-regression.go 842 Bytes
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2023-09-13 21:38 . add Files
package linear
import (
"gitee.com/quant1x/gox/logger"
)
// LeastSquares 最简单的最小二乘法
func LeastSquares(x []float64, y []float64) (slope float64, intercept float64) {
// x是横坐标数据,y是纵坐标数据
// a是斜率,b是截距
xi := float64(0)
x2 := float64(0)
yi := float64(0)
xy := float64(0)
if len(x) != len(y) {
logger.Debugf("最小二乘时,两数组长度不一致!")
} else {
xLen := len(x)
length := float64(xLen)
window := 5
if xLen <= window {
window = xLen
}
for i := xLen - window; i < xLen; i++ {
xi += x[i]
x2 += x[i] * x[i]
yi += y[i]
xy += x[i] * y[i]
}
slope = (yi*xi - xy*length) / (xi*xi - x2*length)
intercept = (yi*x2 - xy*xi) / (x2*length - xi*xi)
}
return
}
func Predict(y, slope, intercept float64) float64 {
return y*slope + intercept
}
1
https://gitee.com/quant1x/ta-lib.git
git@gitee.com:quant1x/ta-lib.git
quant1x
ta-lib
ta-lib
v0.6.6

搜索帮助

53164aa7 5694891 3bd8fe86 5694891