Connection to specific algorithms
Linear model with feature vector
Below is a minimal linear model example: multiply each feature by its corresponding weight, sum everything, and add bias.
Example of use
<?php
function linearModel(array $x, array $w, float $b): float {
$n = count($x);
if ($n !== count($w)) {
throw new InvalidArgumentException('Arguments x and w must have the same length');
}
$sum = $b;
for ($i = 0; $i < $n; $i++) {
$sum += $x[$i] * $w[$i];
}
return $sum;
}
$x = [2, 3];
$w = [0.5, 1.5];
$b = 1.0;
$result = linearModel($x, $w, $b);
echo $result;
// 6.5
// b + (x[0] * w[0]) + (x[1] * w[1]) = 1.0 + (2 * 0.5) + (3 * 1.5) = 6.5
Result:
Memory: 0.002 Mb
Time running: < 0.001 sec.
6.5
The key idea is simple: this pattern underlies linear regression and appears in many other models as a basic building block.
Explanation:
$b + (x[0] * w[0]) + (x[1] * w[1]) = 1.0 + (2 * 0.5) + (3 * 1.5) = 6.5$