float linearRegessionSlope(std::list<PointItem> & data)
{
if (data.empty())
return 0.0f;
float Lxx = 0.0f, Lxy = 0.0f, xa = 0.0f, ya = 0.0f;
std::list<PointItem>::iterator it;
//求平均数
for (it = data.begin(); it != data.end(); it++)
{
xa += it->timeX;
ya += it->valueY;
}
xa /= data.size(); // X平均值
ya /= data.size(); // Y平均值
for (it = data.begin(); it != data.end(); it++)
{
Lxx += ((it->timeX - xa) * (it->timeX - xa)); // Lxx = Sum((X - Xa)平方)
Lxy += ((it->valueY - xa) * (it->valueY - ya)); // Lxy = Sum((X - Xa)(Y - Ya))
}
if(Lxx > 0.0f || Lxx < 0.0f)
return Lxy / Lxx; // b = Lxy / Lxx
else
return 10.0f;//够大了吧
}