Linear Regression Loss and Gradient
Problem statement
For this exercise, assume a batch of real-valued regression examples and the current parameters of a linear model. Implement its loss and analytic derivatives directly, without an automatic-differentiation or machine-learning library.
features is an n-by-d matrix; targets has one value per sample.
For sample i, let prediction[i] = bias + sum_j(features[i][j] * weights[j]). The loss is mean squared error: L = sum_i((prediction[i] - targets[i])^2) / n.
Return [L, dL/dweights[0], ..., dL/dweights[d-1], dL/dbias]. Differentiate with respect to the current weights and bias; do not update them.
No regularization is used. The loss has no factor of one half.
Function
linearLossGradient(features: double[][], targets: double[], weights: double[], bias: double) → double[]Examples
Example 1
features = [[1.0],[2.0]]targets = [2.0,4.0]weights = [1.0]bias = 0.0return = [2.5,-5.0,-3.0]Predictions are 1 and 2, so errors are -1 and -2. Mean loss is 2.5; the weight and bias derivatives are -5 and -3.
Example 2
features = [[1.0,2.0],[3.0,4.0]]targets = [1.0,2.0]weights = [0.0,0.0]bias = 0.0return = [2.5,-7.0,-10.0,-3.0]At zero parameters the two errors are -1 and -2, giving loss 2.5 and gradients -7, -10, -3.
Example 3
features = [[0.0]]targets = [3.0]weights = [5.0]bias = 1.0return = [4.0,0.0,-4.0]A zero feature leaves the weight derivative zero. The error is -2, so loss is 4 and the bias derivative is -4.
Constraints
1 <= n <= 1000and1 <= d <= 50.- All arrays have the compatible shapes described above; all input numbers are finite.
- Feature values, weights and bias are in
[-10,10]. - Targets are in
[-10,10]. - Each returned number is compared with absolute or relative tolerance
10^-6. - Use analytic derivatives; finite-difference estimates are outside the exercise contract.