如何为MATLAB的robustfit或fitlm提供自定义代价函数?
Great question—let's break this down clearly to resolve your confusion:
1. Are Cost Functions and Weight Functions the Same in MATLAB?
No, they are distinct concepts:
- A cost function is the objective we aim to minimize during fitting (e.g., OLS minimizes the sum of squared residuals, MSE minimizes the mean of squared residuals, RMSE is just the square root of MSE). Minimizing RMSE is mathematically equivalent to minimizing MSE or OLS (since scaling the cost by a constant doesn't change the optimal coefficients).
- A weight function adjusts the influence of individual data points on the cost function. For example, in
robustfit(), after an initial OLS fit, it calculates weights based on residuals (e.g., downweighting outliers) and then performs weighted OLS—here, the cost function is still weighted squared residuals, and the weight function just modifies how much each residual contributes to that cost.
2. How to Use a Custom Cost Function (Like MSE) in MATLAB Fitting
Most built-in fitting functions (fitlm(), robustfit()) use fixed cost functions under the hood. To use a custom cost function, you'll need to pair a generic optimization solver (like fminunc, lsqnonlin, or fminsearch) with your own cost function definition.
Example: Implementing MSE as a Custom Cost Function
MSE is actually equivalent to OLS (scaled by 1/n), but here's how you'd define and use it explicitly:
% Define your custom MSE cost function function cost = custom_mse(beta, X, y) y_pred = X * beta; % X = design matrix with intercept column residuals = y - y_pred; cost = mean(residuals.^2); % MSE = average of squared residuals end % Example usage % Sample data x_data = linspace(0, 10, 100); y = 2*x_data + 3 + randn(size(x_data))*0.5; % Linear data with noise % Construct design matrix (add intercept term) X = [ones(length(y), 1), x_data]; % Initial guess for coefficients [intercept, slope] beta0 = [0; 0]; % Use fminunc to minimize the custom MSE cost options = optimoptions('fminunc', 'Display', 'iter', 'MaxIterations', 100); beta_hat = fminunc(@(b) custom_mse(b, X, y), beta0, options); % Print results fprintf('Estimated intercept: %.4f, Estimated slope: %.4f\n', beta_hat(1), beta_hat(2));
If you wanted a different custom cost (e.g., L1 loss/MAE), you'd just modify the cost calculation in the function to mean(abs(residuals)).
3. What About robustfit()'s Custom Weight Function?
When you pass a custom weight function to robustfit(), it is not treated as a cost function. Instead:
robustfit()first runs an initial OLS fit to get residuals.- It uses your custom weight function to compute weights for each data point (the function takes residuals as input and returns corresponding weights).
- It then performs weighted OLS, minimizing the sum of (weight * residual²)—so the core cost function is still weighted squared residuals, and your weight function only adjusts the contribution of each data point to this cost.
If you wanted to use a completely different cost function for robust fitting (e.g., Huber loss directly instead of weighted OLS), you'd need to implement it manually with an optimizer, similar to the MSE example above.
内容的提问来源于stack exchange,提问作者Nuhil Mehdy

