I've defined the two following functions:

logit[x_] := Module[{},
   Log[x/(1 - x)]
invLogit[x_] := Module[{},
   E^x/(1 + E^x)

One is the inverse of the other. However,

In[37]:= logit[invLogit[34.55555]]

Out[37]= 34.6574

Is there a way to increase the precision of the calculations?

  • 1
    $\begingroup$ logit[invLogit[34.55555`20]]. $\endgroup$ – AccidentalFourierTransform Dec 2 '18 at 14:46
  • $\begingroup$ @AccidentalFourierTransform thanks for the comment. However, if these functions have to receive a variable, how would I do then? e.g. logit[invLogit[s]], where s is some value computed in another function. $\endgroup$ – An old man in the sea. Dec 2 '18 at 14:48
  • $\begingroup$ The inverse relationship is only valid for real values of x. Evaluate logit[invLogit[2 + 8 I]] // Simplify $\endgroup$ – Bob Hanlon Dec 2 '18 at 16:10

Something like this works

logit[x_] := Module[{}, Log[x/(1 - x)]];
invLogit[u_] := Module[{x}, x = SetPrecision[u, 30]; E^x/(1 + E^x)];

(* 34.5555500000000 *)

The issue is that the gradient of logit approaches zero for large arguments. Therefore there is a loss of precision in evaluating it. Enforcing a larger precision calculation helps get back to where you started.

  • $\begingroup$ Mikado, thanks for your answer. In the set precision command, is the $MaxExtraPrecision the best numerical approximation we can have in general terms in mathematica? $\endgroup$ – An old man in the sea. Dec 2 '18 at 15:22
  • 1
    $\begingroup$ From the help for $MaxExtraPrecision I don't think that it is relevant here. I think you can specify as many extra digits of precision as you need. $\endgroup$ – mikado Dec 2 '18 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.