Skip to content

Instantly share code, notes, and snippets.

@mtholder
Created May 29, 2012 16:19
Show Gist options
  • Save mtholder/2829324 to your computer and use it in GitHub Desktop.
Save mtholder/2829324 to your computer and use it in GitHub Desktop.
Log of the mean of the exp of values from standard input
#!/usr/bin/env python
import sys, math
v_list = [float(value) for value in sys.stdin]
offset = max(v_list)
n = len(v_list)
sum_exps = sum([math.exp(v - offset) for v in v_list])
mean_exps = sum_exps/n
sys.stdout.write('Log of mean of the exp of %d values is:\n' % n)
sys.stdout.write('%8.7f\n' % (math.log(mean_exps) + offset))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment