Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7563973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:48:00+00:00 2026-05-30T13:48:00+00:00

I need logit and inverse logit functions so that logit(inv_logit(n)) == n . I

  • 0

I need logit and inverse logit functions so that logit(inv_logit(n)) == n. I use numpy and here is what I have:

import numpy as np
def logit(p):
    return np.log(p) - np.log(1 - p)

def inv_logit(p):
    return np.exp(p) / (1 + np.exp(p))

And here are the values:

print logit(inv_logit(2)) 
2.0 

print logit(inv_logit(10))
10.0 

print logit(inv_logit(20))
20.000000018 #well, pretty close

print logit(inv_logit(50))
Warning: divide by zero encountered in log
inf 

Now let’s test negative numbers

print logit(inv_logit(-10))
-10.0 
print logit(inv_logit(-20))
-20.0 
print logit(inv_logit(-200))
-200.0 
print logit(inv_logit(-500))
-500.0 
print logit(inv_logit(-2000))
Warning: divide by zero encountered in log
-inf 

So my questions are: what is the proper way to implement these functions so that the requirement logit(inv_logit(n)) == n will hold for any n in as wide a range as possible (at least [-1e4; 1e4)?

And also (and I’m sure this is connected to the first one), why are my function more stable with negative values, compared to the positive ones?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:48:01+00:00Added an answer on May 30, 2026 at 1:48 pm

    Either use

    1.
    The bigfloat package with supports arbitrary precision floating point operations.

    2.
    The SymPy symbolic math package. I’ll give examples of both:

    First, bigfloat:

    http://packages.python.org/bigfloat/

    Here’s a simple example:

    from bigfloat import *
    def logit(p):
        with precision(100000):
            return log(p)- log(1 -BigFloat(p))
    
    def inv_logit(p):
        with precision(100000):
            return exp(p) / (1 + exp(p))
    
    int(round(logit(inv_logit(12422.0))))
    # gives 12422
    int(round(logit(inv_logit(-12422.0))))
    # gives -12422
    

    This is really slow. You may want to consider restructuring your problem and do some parts analytically. Cases like these are rare in real problems – I’m curious about what kind of problem you are working on.

    Example installation:

    wget http://pypi.python.org/packages/source/b/bigfloat/bigfloat-0.3.0a2.tar.gz
    tar xvzf bigfloat-0.3.0a2.tar.gz 
    cd bigfloat-0.3.0a2
    as root:
    python setup.py install
    

    About the reason your functions wore better with negative values. Consider:

    >>> float(inv_logit(-15))
    3.059022269256247e-07
    
    >>> float(inv_logit(15))
    0.9999996940977731
    

    In the first case floating point numbers represent this value easily. The decimal point is moved so that the leading zeroes: 0.0000… does not need to be stored. In the second case all the leading 0.999 needs to be stored, so you need all that extra precision to get an exact result when later doing 1-p in logit().

    Here’s the symbolic math way (significantly faster!):

    from sympy import *
    def inv_logit(p):
        return exp(p) / (1 + exp(p))
    def logit(p):
        return log(p)- log(1 -p)
    
    x=Symbol('x')
    expr=logit(inv_logit(x))
    # expr is now:
    # -log(1 - exp(x)/(1 + exp(x))) + log(exp(x)/(1 + exp(x)))
    # rewrite it: (there are many other ways to do this. read the doc)
    # you may want to make an expansion (of some suitable kind) instead.
    expr=cancel(powsimp(expr)).expand()
    # it is now 'x'
    
    # just evaluate any expression like this:    
    result=expr.subs(x,123.231)
    
    # result is now an equation containing: 123.231
    # to get the float: 
    result.evalf()
    

    Sympy is found here http://docs.sympy.org/. In ubuntu it’s found via synaptic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple web page that till now didn't need any login. It
I have my own site that store login info only in session. I need
We have TFS installed in a US server. We need to login into VPN
For example, if I have a user model and I need to validate login
Within my logic layer I have the need to check permissions for a wide
I need syntax help with the following code logic: I have a code block
Here is the login page: http://www.ifreewind.net/iFreeWind.aspx I need content of this page which need
I need to login here. I've tried the ASIHTTPRequest and ASIFormDataRequest. None of them
I basically need to login into another domain that is using asp.net membership. If
I have a system where I need to login three user types: customers, companies,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.