All problems

Ridge (L2) Regularized Cost

easyPythonEnsembles & Regularization

Ardwick Mutual's pricing model fits its historical quotes well, yet behaves erratically on new ones. Inspecting it, the weights on a handful of features have grown enormous and are cancelling each other out — one feature pushing a price up by thousands while its neighbour pulls it back down. The trainer is therefore given a second thing to care about: the number it minimises is no longer the fit error alone, but the fit error with a charge added for how large the weights have become. A dial called lambda decides how heavy that 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.

Output

One line holding the charged cost rounded to 4 decimal places. The charge is each weight squared, added up across all n weights, that total scaled by lambda, and the result added on top of the error. Squaring means a weight's sign never matters, and one large weight costs far more than several small ones adding to the same size. The charge is a plain total, not an average, so it grows as weights are added.

Example:

Input:
2.0 0.1
3
1 2 3

Output:
3.4000

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

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…