Lineare Regression: Unterschied zwischen den Versionen

Aus Terragon Wiki
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „Mit der linearen Regression wird aus einer Reihe von einzelnen Messdaten die Formel für eine Gerade berechnet, die relativ mittig durch diese Messdaten führt…“)
 
Zeile 4: Zeile 4:
  
 
[[Datei:20171118_Lieare_Regression_Skizze.jpg|500px|thumb|right|Lineare Regression]]
 
[[Datei:20171118_Lieare_Regression_Skizze.jpg|500px|thumb|right|Lineare Regression]]
 +
 +
==PHP Funktion==
 +
 +
function linear_regression($x, $y) {
 +
 +
// calculate number points
 +
$n = count($x);
 +
 +
// ensure both arrays of points are the same size
 +
if ($n != count($y)) {
 +
trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
 +
echo "<h1>Fehler: Anzahl von X und Y unterschiedlich!</h1>";
 +
};
 +
 +
$x_sum = array_sum($x);// calculate sums
 +
$y_sum = array_sum($y);// calculate sums
 +
 +
$xx_sum = 0;
 +
$xy_sum = 0;
 +
 
 +
for($i = 0; $i < $n; $i++) {
 +
$xy_sum+=($x[$i]*$y[$i]);
 +
$xx_sum+=($x[$i]*$x[$i]);
 +
};
 +
 
 +
//calculate slope m
 +
$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));
 +
 
 +
//calculate intercept b
 +
$b = ($y_sum - ($m * $x_sum)) / $n;
 +
   
 +
//returns array() m=>slope, b=>intercept
 +
return array("m"=>$m, "b"=>$b);
 +
};

Version vom 7. Juli 2019, 19:02 Uhr

Mit der linearen Regression wird aus einer Reihe von einzelnen Messdaten die Formel für eine Gerade berechnet, die relativ mittig durch diese Messdaten führt.

Vorraussetzung ist, dass die Messpunkte näherungsweise auf einer Gerade liegen.

Lineare Regression

PHP Funktion

function linear_regression($x, $y) {

// calculate number points $n = count($x);

// ensure both arrays of points are the same size if ($n != count($y)) { trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);

echo "

Fehler: Anzahl von X und Y unterschiedlich!

";

};

$x_sum = array_sum($x);// calculate sums $y_sum = array_sum($y);// calculate sums

$xx_sum = 0; $xy_sum = 0;

for($i = 0; $i < $n; $i++) { $xy_sum+=($x[$i]*$y[$i]); $xx_sum+=($x[$i]*$x[$i]); };

//calculate slope m $m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));

//calculate intercept b $b = ($y_sum - ($m * $x_sum)) / $n;

//returns array() m=>slope, b=>intercept return array("m"=>$m, "b"=>$b); };