Lasso (L1) Regularized Cost
Ardwick Mutual's pricing model has more features than anyone can defend, and the modelling desk would rather it used fewer. So the trainer is charged for the weights it uses, but on a different scale from the one the last review tried. Where that charge grew faster the larger a weight got, this one is flat: every unit of weight costs the same wherever it sits, which turns out to make switching a weight off entirely worth doing. Lambda again sets how heavy the charge is.
Task: Print the model's cost with the size charge included.
Input
Three lines. The first holds two numbers separated by a single space: the model's mean squared error, then lambda. The second holds n, the number of weights. The third holds the n weights, separated by single spaces, and some of them are negative.
Output
One line holding the charged cost rounded to 4 decimal places. The charge is the size of each weight ignoring its sign — a weight of -2 costs exactly what a weight of 2 costs — added up across all n weights, that total scaled by lambda, and the result added on top of the error. The charge is a plain total, not an average.
Example:
Input:
2.0 0.1
3
1 -2 3
Output:
2.6000
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in