What is a model in the mathematical sense

Learning as minimizing error

If we can measure error (loss), then model training becomes very simple: we change the model parameters so that this number goes down. The model itself does not “understand” the task — it just minimizes the loss function we chose.

Below is a minimal example: two observations and a linear model $ŷ = w x + b$. First the parameters are bad and the loss is large. Then we adjust $w$ so that predictions become closer to reality and the loss decreases noticeably.

Example of use:

 
<?php

/**
 * A minimal 1D linear regression model.
 *
 * The model represents a straight line:
 *  ŷ = w * x + b
 */
class LinearModel {
    
/**
     * Slope (weight) of the linear function.
     */
    
public float $w;

    
/**
     * Intercept (bias) of the linear function.
     */
    
public float $b;

    
/**
     * @param float $w Slope (weight)
     * @param float $b Intercept (bias)
     */
    
public function __construct(float $wfloat $b) {
        
$this->$w;
        
$this->$b;
    }

    
/**
     * Predict the target value for a single input.
     *
     * @param float $x Input feature value
     * @return float Predicted value ŷ
     */
    
public function predict(float $x): float {
        return 
$this->$x $this->b;
    }
}

/**
 * Squared error loss for a single sample.
 *
 * SE = (ŷ - y)^2
 *
 * @param float $yTrue Ground-truth value y
 * @param float $yPredicted Model prediction ŷ
 * @return float Non-negative loss value
 */
function squaredError(float $yTruefloat $yPredicted): float {
    return (
$yPredicted $yTrue) ** 2;
}