// 如果你没有 php_trader 库,或者需要超过 3 位小数精度,
// 你可以使用这个函数
public static function linearreg_slope( $valuesIn, $period )
{
$valuesOut = array();
$startIdx = 0;
$endIdx = count($valuesIn) - 1;
$sumX = $period * ( $period - 1 ) * 0.5;
$sumXSqr = $period * ( $period - 1 ) * ( 2 * $period - 1 ) / 6;
$divisor = $sumX * $sumX - $period * $sumXSqr;
for ( $today = $startIdx, $outIdx = 0; $today <= $endIdx; $today++, $outIdx++ )
{
$sumXY = 0;
$sumY = 0;
if ( $today >= $period - 1 ) {
for( $aux = $period; $aux-- != 0; )
{
$sumY += $tempValue = $valuesIn[$today - $aux];
$sumXY += (double)$aux * $tempValue;
}
$valuesOut[$outIdx] = ( $period * $sumXY - $sumX * $sumY) / $divisor;
}
}
return $valuesOut;
}