Loading problem...
In regression tasks, selecting an appropriate loss function is critical for model performance, especially when dealing with datasets containing outliers. Traditional loss functions each have their limitations:
The Robust Hybrid Regression Loss elegantly bridges these two approaches by behaving like squared error for small residuals and transitioning to linear error for larger residuals. This adaptive behavior is governed by a threshold parameter δ (delta), which defines the boundary between the two regimes.
Mathematical Formulation:
For a single prediction with residual (r = y_{\text{true}} - y_{\text{pred}}):
$$L(r) = \begin{cases} \frac{1}{2}r^2 & \text{if } |r| \leq \delta \ \delta \cdot (|r| - \frac{1}{2}\delta) & \text{if } |r| > \delta \end{cases}$$
This piecewise definition ensures:
Key Properties:
Your Task: Implement a function that computes the Robust Hybrid Regression Loss given true values, predicted values, and a delta threshold. The function should:
y_true = [2.0, 3.0, 4.0]
y_pred = [2.5, 2.0, 4.0]
delta = 1.00.2083Computing the loss for each element:
• Element 1: |2.0 - 2.5| = 0.5 ≤ δ (1.0), so use quadratic formula: 0.5 × (0.5)² = 0.125 • Element 2: |3.0 - 2.0| = 1.0 ≤ δ (1.0), so use quadratic formula: 0.5 × (1.0)² = 0.5 • Element 3: |4.0 - 4.0| = 0.0 ≤ δ (1.0), so use quadratic formula: 0.5 × (0.0)² = 0.0
Average loss = (0.125 + 0.5 + 0.0) / 3 = 0.208333...
Rounded to 4 decimal places: 0.2083
y_true = 5
y_pred = 5
delta = 1.00When the predicted value exactly matches the true value:
• Residual: |5 - 5| = 0.0 ≤ δ (1.0) • Loss: 0.5 × (0.0)² = 0.0
Perfect predictions incur zero loss, as expected from any well-designed loss function.
y_true = [1.0, 2.0, 3.0]
y_pred = [4.0, 5.0, 6.0]
delta = 1.02.5All residuals exceed the delta threshold, triggering linear loss behavior:
• Element 1: |1.0 - 4.0| = 3.0 > δ (1.0), so use linear formula: δ × (|r| - 0.5δ) = 1.0 × (3.0 - 0.5) = 2.5 • Element 2: |2.0 - 5.0| = 3.0 > δ (1.0), so use linear formula: 1.0 × (3.0 - 0.5) = 2.5 • Element 3: |3.0 - 6.0| = 3.0 > δ (1.0), so use linear formula: 1.0 × (3.0 - 0.5) = 2.5
Average loss = (2.5 + 2.5 + 2.5) / 3 = 2.5
Note: The linear penalty (2.5) is much smaller than what squared error would give (4.5), demonstrating outlier robustness.
Constraints