TaLib的代码:https://sourceforge.net/p/ta-lib/code/HEAD/tree/trunk/ta-lib/c/src/ta_func/ta_LINEARREG_SLOPE.c#l331
我想要这样的JavaScript函数:
functuin linearRegressionSlope(arr, range) {
return [...slopeVal]
}
我试图根据上面的源代码实现一个,但是发现结果与它不一致:
const data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
let today = 0
const optInTimePeriod = 5
const SumX = optInTimePeriod * (optInTimePeriod - 1) * 0.5
const SumXSqr =
(optInTimePeriod * (optInTimePeriod - 1) * (2 * optInTimePeriod - 1)) / 6
const Divisor = SumX * SumX - optInTimePeriod * SumXSqr
const outReal = []
while (today <= data.length - 1) {
let SumXY = 0
let SumY = 0
for (let i = optInTimePeriod; i > 0; i--) {
SumY += data[today - i]
SumXY += i * data[today - i]
}
outReal.push((optInTimePeriod * SumXY - SumX * SumY) / Divisor)
today++
}
console.log(outReal)
您可以通过talib-binding对其进行测试
const {LINEARREG_SLOPE} = require('talib-binding')
console.log(LINEARREG_SLOPE(data, optInTimePeriod))
最佳答案
原始代码有for
循环就像
for(int i = 5; i-- != 0; )
printf("%d ", i);
打印出来:4 3 2 1 0
并且您的循环迭代5 4 3 2 1。
也许是原因。
关于javascript - 如何用js实现talib的LINEARREG_SLOPE函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57737778/