Skip to content

Instantly share code, notes, and snippets.

@arogozhnikov
Created September 8, 2015 20:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arogozhnikov/45440f0b9a06be6774b0 to your computer and use it in GitHub Desktop.
Save arogozhnikov/45440f0b9a06be6774b0 to your computer and use it in GitHub Desktop.
Shortest isotonic transform function in the world. Generates increasing formula.
def isotonic_transform(x, y, alpha=0.001):
target = y + 0.
output = numpy.zeros(len(x))
steps = 2 ** numpy.arange(int(numpy.log2(len(x)) - 1))[::-1]
print steps
for i in range(100):
for step in steps:
penalty = 2 * (target - output)
diffs = - (output[step:] - output[:-step])
diffs = diffs.clip(0)
misorder_penalty = numpy.zeros(len(x))
misorder_penalty[step:] += diffs / 2.
misorder_penalty[:-step] -= diffs / 2.
output += alpha * penalty + misorder_penalty
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment