我正在为我正在处理的网站编写一些分析模块,我需要估算当前小时后的总观看次数。我有每分钟的数据直到当前分钟,所以如果时间是12:28,我将有一个看起来像这样的数组:
0: "21410"
1: "21886"
2: "21837"
3: "21895"
4: "21564"
5: "21714"
6: "21571"
7: "21324"
8: "21310"
9: "21390"
10: "21764"
11: "21598"
12: "21493"
13: "21352"
14: "21478"
15: "21058"
16: "20942"
17: "20825"
18: "21321"
19: "20950"
20: "21039"
21: "21117"
22: "20733"
23: "20773"
24: "20929"
25: "20900"
26: "20687"
27: "20999"
目前我正在预测小时的价值:
(60/minsSoFar)*totalSoFar
这种方法运作得相当好,但我宁愿在数学上做得更多。我想计算出迄今为止我所拥有的数据的最佳拟合线,并计算到第60分钟。这将考虑加速和减速。
根据我目前正在使用的方法,我实际上假设趋势是一条直线。我如何计算多项式或幂次数的公式?
我在NodeJS中写这个,所以JavaScript是理想的,但我也会采用伪代码!
如果需要,可以使用更简单的格式:
[21410, 21886, 21837, 21895, 21564, 21714, 21571, 21324, 21310, 21390, 21764, 21598, 21493, 21352, 21478, 21058, 20942, 20825, 21321, 20950, 21039, 21117, 20733, 20773, 20929, 20900, 20687, 20999]
感谢您的帮助!
答案 0 :(得分:9)
你可以least-squares fit行。
function LineFitter()
{
this.count = 0;
this.sumX = 0;
this.sumX2 = 0;
this.sumXY = 0;
this.sumY = 0;
}
LineFitter.prototype = {
'add': function(x, y)
{
this.count++;
this.sumX += x;
this.sumX2 += x*x;
this.sumXY += x*y;
this.sumY += y;
},
'project': function(x)
{
var det = this.count * this.sumX2 - this.sumX * this.sumX;
var offset = (this.sumX2 * this.sumY - this.sumX * this.sumXY) / det;
var scale = (this.count * this.sumXY - this.sumX * this.sumY) / det;
return offset + x * scale;
}
};
function linearProject(data, x)
{
var fitter = new LineFitter();
for (var i = 0; i < data.length; i++)
{
fitter.add(i, data[i]);
}
return fitter.project(x);
}
示例:强>
>>> linearProject([
21410, 21886, 21837, 21895, 21564, 21714, 21571, 21324, 21310, 21390,
21764, 21598, 21493, 21352, 21478, 21058, 20942, 20825, 21321, 20950,
21039, 21117, 20733, 20773, 20929, 20900, 20687, 20999
], 60);
19489.614121510676
对方形多项式做类似的事情有点复杂:
function SquareFitter()
{
this.count = 0;
this.sumX = 0;
this.sumX2 = 0;
this.sumX3 = 0;
this.sumX4 = 0;
this.sumY = 0;
this.sumXY = 0;
this.sumX2Y = 0;
}
SquareFitter.prototype = {
'add': function(x, y)
{
this.count++;
this.sumX += x;
this.sumX2 += x*x;
this.sumX3 += x*x*x;
this.sumX4 += x*x*x*x;
this.sumY += y;
this.sumXY += x*y;
this.sumX2Y += x*x*y;
},
'project': function(x)
{
var det = this.count*this.sumX2*this.sumX4 - this.count*this.sumX3*this.sumX3 - this.sumX*this.sumX*this.sumX4 + 2*this.sumX*this.sumX2*this.sumX3 - this.sumX2*this.sumX2*this.sumX2;
var offset = this.sumX*this.sumX2Y*this.sumX3 - this.sumX*this.sumX4*this.sumXY - this.sumX2*this.sumX2*this.sumX2Y + this.sumX2*this.sumX3*this.sumXY + this.sumX2*this.sumX4*this.sumY - this.sumX3*this.sumX3*this.sumY;
var scale = -this.count*this.sumX2Y*this.sumX3 + this.count*this.sumX4*this.sumXY + this.sumX*this.sumX2*this.sumX2Y - this.sumX*this.sumX4*this.sumY - this.sumX2*this.sumX2*this.sumXY + this.sumX2*this.sumX3*this.sumY;
var accel = this.sumY*this.sumX*this.sumX3 - this.sumY*this.sumX2*this.sumX2 - this.sumXY*this.count*this.sumX3 + this.sumXY*this.sumX2*this.sumX - this.sumX2Y*this.sumX*this.sumX + this.sumX2Y*this.count*this.sumX2;
return (offset + x*scale + x*x*accel)/det;
}
};
function squareProject(data)
{
var fitter = new SquareFitter();
for (var i = 0; i < data.length; i++)
{
fitter.add(i, data[i]);
}
return fitter.project(60);
}
<强>例2:强>
>>> squareProject([
21410, 21886, 21837, 21895, 21564, 21714, 21571, 21324, 21310, 21390,
21764, 21598, 21493, 21352, 21478, 21058, 20942, 20825, 21321, 20950,
21039, 21117, 20733, 20773, 20929, 20900, 20687, 20999
], 60);
19282.85862700518
我可以为更高次多项式做到这一点,但表达式会变得更长。对于任意程度,您必须查看矩阵。