Skip to content
DeepForgeby svx
All problems
MediumCalculusca-211

Proximal Operator of Elastic Net

Problem

The elastic net penalty is lambda1 * ||v||_1 + (lambda2 / 2) * ||v||_2^2, combining sparsity with shrinkage.

Its proximal operator applies soft-thresholding and then divides by (1 + lambda2): prox(v_i) = soft(v_i, lambda1) / (1 + lambda2). Given the values and both penalties, return the result.

Hint

The L2 part rescales all surviving coefficients uniformly.

Test cases

case 1
in: [[3,-2],0.5,1]
expected: [1.25,-0.75]
case 2
in: [[1],1,0]
expected: [0]
case 3
in: [[4],0,2]
expected: [1.3333333333333333]
case 4
in: [[0.3],0.5,1]
expected: [0]
Starter codePython
def prox_elastic_net(values, lam1, lam2):
    # Your code here
    pass

4 test cases · run your code against them in the editor