trader_linearreg_slope

(PECL trader >= 0.2.0)

trader_linearreg_slopePente de régression linéaire

Description

trader_linearreg_slope(array $real, int $timePeriod = ?): array

Liste de paramètres

real

Tableau de valeurs réelles.

timePeriod

Nombre de période. Intervalle valide : 2 à 100000.

Valeurs de retour

Retourne un tableau contenant les données calculées, ou false si une erreur survient.

add a note add a note

User Contributed Notes 2 notes

up
10
Angel J. Salinas
8 years ago
// If you don't have the php_trader library or needs more than 3 precision digits,
// you can use this function:
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;
}
up
0
Angel J. Salinas
5 years ago
Default value of precision in trader library is 3, but can be increased easily with something like this:

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