Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random.expovariate(0.0) #49119

Closed
kbriggs mannequin opened this issue Jan 7, 2009 · 7 comments
Closed

random.expovariate(0.0) #49119

kbriggs mannequin opened this issue Jan 7, 2009 · 7 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@kbriggs
Copy link
Mannequin

kbriggs mannequin commented Jan 7, 2009

BPO 4869
Nosy @rhettinger, @mdickinson

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2009-01-07.17:54:52.680>
created_at = <Date 2009-01-07.16:17:00.329>
labels = ['type-feature', 'library']
title = 'random.expovariate(0.0)'
updated_at = <Date 2009-01-07.19:10:35.193>
user = 'https://bugs.python.org/kbriggs'

bugs.python.org fields:

activity = <Date 2009-01-07.19:10:35.193>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = <Date 2009-01-07.17:54:52.680>
closer = 'mark.dickinson'
components = ['Library (Lib)']
creation = <Date 2009-01-07.16:17:00.329>
creator = 'kbriggs'
dependencies = []
files = []
hgrepos = []
issue_num = 4869
keywords = []
message_count = 7.0
messages = ['79346', '79348', '79349', '79350', '79351', '79354', '79363']
nosy_count = 3.0
nosy_names = ['rhettinger', 'mark.dickinson', 'kbriggs']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue4869'
versions = ['Python 3.1', 'Python 2.7']

@kbriggs
Copy link
Mannequin Author

kbriggs mannequin commented Jan 7, 2009

random.expovariate(lambd) should handle lambd=0 cleanly, and probably
return a FP infinity.

At the moment it gives:

ZeroDivisionError: float division

@kbriggs kbriggs mannequin added the extension-modules C modules in the Modules dir label Jan 7, 2009
@mdickinson
Copy link
Member

An exponential distribution with parameter 0 isn't an exponential distribution any more. On the real line, there isn't
even a limiting distribution as the parameter approaches 0.

Is there really any use for having expovariate degenerate this way?
It seems much more likely that a call to random.expovariate(0) is
caused by a bug somewhere, so should raise an exception. Which is exactly
what it does already.

The proposed change would also not be in keeping with the philosophy
behind most of Python's mathematics, which is to raise exceptions rather
than returning exceptional (nan, inf) results. (Think of it as IEEE 754
will the invalid, overflow and division-by-zero signals all being
trapped.)

@mdickinson mdickinson added stdlib Python modules in the Lib dir type-feature A feature request or enhancement and removed extension-modules C modules in the Modules dir labels Jan 7, 2009
@mdickinson
Copy link
Member

I would suggest adding a note to the documentation stating clearly that
the parameter should be nonzero. The documentation is also unclear on
whether a negative parameter value is permitted, although the current code
works exactly the way that I'd expect for a negative parameter.

If a negative parameter is permitted then the last sentence of the
docstring:

"""... Returned values range from 0 to positive infinity."""

is inaccurate, since values range from 0 to negative infinity in that
case. If it's not supposed to be permitted then perhaps the docstring
should mention that the parameter should be positive?

@kbriggs
Copy link
Mannequin Author

kbriggs mannequin commented Jan 7, 2009

That's all true - the only thing in favour of my suggestion is that to
allow lambda=0 is sometimes useful in simulations, meaning that it's
infinite time to the next event, i.e. it never occurs. A FP infinity
would do this without requiring a special case.

At least the documentation should state the restrictions on lambda.

@mdickinson
Copy link
Member

to allow lambda=0 is sometimes useful in simulations, meaning that it's
infinite time to the next event, i.e. it never occurs.

Thanks; that makes sense. If 1./0. returned inf then expovariate would
already do what you want. But rightly or wrongly, Python just doesn't
work that way.

Incidentally, if expovariate had been written to take the mean as its
argument (instead of the reciprocal of the mean) then this would be easy:
just provide a parameter of float('inf') and everything would work. (And
a parameter of 0 would give a point distribution at 0, which again seems
perfectly sound.)

@mdickinson
Copy link
Member

Doc fixes in r68378. Closing.

@rhettinger
Copy link
Contributor

I concur with Mark.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants