Google Answers Logo
View Question
 
Q: Approximating the result of a particular logarithmic expression ( Answered 5 out of 5 stars,   4 Comments )
Question  
Subject: Approximating the result of a particular logarithmic expression
Category: Science > Math
Asked by: gw-ga
List Price: $10.00
Posted: 26 Nov 2005 23:40 PST
Expires: 26 Dec 2005 23:40 PST
Question ID: 598015
In a program I'm writing, I need to evaluate an expression of the form

z = e^x / (e^x + e^y)

Because x and y may be *very* large numbers, computing e^x and e^y may
result in overflows.  Is there a way to approximate z instead of
getting an exact figure?

Alternatively, is there a way to get an exact or approximate value of
z if we use the following form instead?

e^z = e^x / (e^x + e^y)
Answer  
Subject: Re: Approximating the result of a particular logarithmic expression
Answered By: hedgie-ga on 28 Nov 2005 02:12 PST
Rated:5 out of 5 stars
 
Hello gw

 1)          Simple way would be to calculate value   of u=1/z

 1/z = 1 + e^(y-x)

 That has the advantage that y-x may be small even if each is large.
 After you have value of 1/z, you can test it's value and invert it is
 not too close to zero.

2) In the second case  

 z2 = x - lg ( e^x + e^y) 

That may work if x and y are both in range of the domain, but 1 method
looks better.

For more info, you would have to specify language (c, fortran ..)
 actual meaning of large (how large is large) and precision you need.

Hedgie
gw-ga rated this answer:5 out of 5 stars

Comments  
Subject: Re: Approximating the result of a particular logarithmic expression
From: agmpinia-ga on 27 Nov 2005 10:38 PST
 
z=e^x/(e^x-e^y) = [e^x/e^x]/[(e^x+e^y)/(e^x)] = 1/[1+e^(y-x)]

so if both become too big y-x will stay small 
This formula has problem only if y is increasing and/or x is decreasing
Subject: Re: Approximating the result of a particular logarithmic expression
From: gw-ga on 27 Nov 2005 22:05 PST
 
agmpinia-ga, thanks, that seems to be a workable solution. :)
Subject: Re: Approximating the result of a particular logarithmic expression
From: manuka-ga on 27 Nov 2005 22:38 PST
 
I would agree with simplifying to 1/[1+e^(y-x)]. However, you won't
only have problems with this in the case y increasing, x decreasing.
You will have problems any time x and y are too far apart (try it for
y = 2x at x = 1000 and you'll see what I mean). If y is more than a
little bit bigger than x the result will come back as 0, and if x is
more than a little bit bigger than y it will come back as 1.

If that's OK for you, then fine. Otherwise, if y is significantly
greater than x (and note that what counts is y-x, not y/x),
approximate as z = e^(x-y) (this is just ignoring the e^x term in the
denominator, since it's going to be immaterial). If x is significantly
greater than y, z will be very close to 1, so we need an approximation
for z - 1 = -e^y/(e^x + e^y), which is the original formula with x and
y reversed; so we approximate this as z - 1 = -e^(y-x).
Subject: Re: Approximating the result of a particular logarithmic expression
From: gw-ga on 27 Nov 2005 23:47 PST
 
For my purposes, x and y will be constrained such that the equation
e^x / (e^x + e^y) will always fall between 0 and 1.

I need to "approximately" sort some 9,000 items based on this
fraction, and I'm starting to think that I might be able to get away
with simply sorting by (y-x)...

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy