2024 年 PHP Conference Japan

trader_linearreg_slope

(PECL trader >= 0.2.0)

trader_linearreg_slope線性迴歸斜率

說明

trader_linearreg_slope(陣列 $real, 整數 $timePeriod = ?): 陣列

參數

real

實數值陣列。

timePeriod

期數。有效範圍從 2 到 100000。

回傳值

傳回一個包含計算後數據的陣列,若失敗則傳回 false。

新增註解

使用者貢獻的註解 2 則註解

Angel J. Salinas
8 年前
// 如果你沒有 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;
}
Angel J. Salinas
5 年前
trader 函式庫預設的精確度是小數點後 3 位,但可以很容易地用以下方法增加:

ini_set( 'trader.real_precision', '6' );
To Top