Skip to content

Instantly share code, notes, and snippets.

@0x0L
Created July 23, 2017 14:26
Show Gist options
  • Save 0x0L/a81c624bd1b4ca8384869091b7e33367 to your computer and use it in GitHub Desktop.
Save 0x0L/a81c624bd1b4ca8384869091b7e33367 to your computer and use it in GitHub Desktop.
l1 projection
def l1_simplex_proj(x, radius=1.0):
z = np.abs(x)
mu = np.sort(z)[::-1]
cmu = np.cumsum(mu)
f = mu * np.arange(1, 1 + len(x)) - (cmu - radius)
rho = np.where(f > 0)[0][-1]
theta = (cmu[rho] - radius) / (1.0 + rho)
y = np.sign(x) * np.maximum(z - theta, 0)
return y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment