title: log-sum-exp trick why not recursive
body: <p>I have been researching the log-sum-exp problem. I have a list of numbers stored as logarithms which I would like to sum and store in a logarithm.</p>
  
  <p>the naive algorithm is</p>
  
  <pre><code>def naive(listOfLogs):
      return math.log10(sum(10**x for x in listOfLogs))
  </code></pre>
  
  <p>many websites including:
  <a href="https://stackoverflow.com/questions/4169981/">logsumexp implementation in C?</a>
  and
  <a href="http://machineintelligence.tumblr.com/post/4998477107/" rel="nofollow noreferrer">http://machineintelligence.tumblr.com/post/4998477107/</a>
  recommend using</p>
  
  <pre><code>def recommend(listOfLogs):
      maxLog = max(listOfLogs)
      return maxLog + math.log10(sum(10**(x-maxLog) for x in listOfLogs))
  </code></pre>
  
  <p>aka</p>
  
  <pre><code>def recommend(listOfLogs):
      maxLog = max(listOfLogs)
      return maxLog + naive((x-maxLog) for x in listOfLogs)
  </code></pre>
  
  <p>what I don't understand is if recommended algorithm is better why should we call it recursively?
  would that provide even more benefit?</p>
  
  <pre><code>def recursive(listOfLogs):
      maxLog = max(listOfLogs)
      return maxLog + recursive((x-maxLog) for x in listOfLogs)
  </code></pre>
  
  <p>while I'm asking are there other tricks to make this calculation more numerically stable?</p>