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

Add triangular distribution to random #44724

miathan6 mannequin opened this issue Mar 15, 2007 · 8 comments

Add triangular distribution to random #44724

miathan6 mannequin opened this issue Mar 15, 2007 · 8 comments
stdlib Python modules in the Lib dir


Copy link

miathan6 mannequin commented Mar 15, 2007

BPO 1681432
Nosy @rhettinger, @mdickinson
  • triangular.patch: Add Triangular distribution to random module
  • 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 = ''
    closed_at = <Date 2008-03-23.13:32:58.911>
    created_at = <Date 2007-03-15.13:09:50.000>
    labels = ['library']
    title = 'Add triangular distribution to random'
    updated_at = <Date 2008-03-23.13:32:58.909>
    user = '' fields:

    activity = <Date 2008-03-23.13:32:58.909>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2008-03-23.13:32:58.911>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2007-03-15.13:09:50.000>
    creator = 'miathan6'
    dependencies = []
    files = ['7884']
    hgrepos = []
    issue_num = 1681432
    keywords = ['patch']
    message_count = 8.0
    messages = ['52238', '52239', '52240', '52241', '52242', '52243', '52244', '64356']
    nosy_count = 5.0
    nosy_names = ['collinwinter', 'rhettinger', 'mark.dickinson', 'paulhankin', 'miathan6']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = ''
    versions = ['Python 2.6']

    Copy link
    Mannequin Author

    miathan6 mannequin commented Mar 15, 2007

    This patch adds the so called Triangular distribution for random values. It is often used in simulations when an upper and lower bound is known, and a most likely value but not the exact distribution. This distribution interpolates these values linearly.

    @miathan6 miathan6 mannequin assigned rhettinger Mar 15, 2007
    @miathan6 miathan6 mannequin added the stdlib Python modules in the Lib dir label Mar 15, 2007
    Copy link

    collinwinter mannequin commented Mar 16, 2007

    Could you work up some tests (Lib/test/ and docs (Doc/lib/librandom.tex) for this? An explanation of why you think this should be included in the standard library would be helpful, too.

    Also, it seems you made this patch against Python 2.3. While this particular patch still applied (relatively) cleanly to Python 2.6a0 source, Python 2.3 is no longer supported and most patches made against it have a high probability of inapplicability. See for information on how to obtain a read-only checkout of the latest source code.

    Copy link
    Mannequin Author

    miathan6 mannequin commented Mar 16, 2007

    Yes, I will add some tests and docs, although usage is quite straightforward. That's the main reason also for adding this distribution, it has three parameters which are intuitive (left, center and right) the distribution is just a linear interpolation (P(left)=0 and P(center)=1 and P(right)=0 ).

    I am writing an event simulation and I added it because I was missing some kind of assymetric distribution for timings; in an uniform distribution t-x and t+y would be just as likely as t, in a gaussian distribution you can only set sigma but not the upper and lower bound.

    With all the other distributions in there I thought it might come in handy for other people too. I also believe it has its uses in sound generation/processing.

    Sorry for providing a patch against 2.3 and not 2.5 or higher, I cooked it up at work and it seems I have this ancient version here.

    Copy link

    paulhankin mannequin commented Mar 17, 2007

    Some minor quibbles: the distributions in random name their parameters after the most common use in math text books. Is n't that 'a, b, c' here rather than 'left, right, center' (note, different order too)? Admittedly your parameter names are clearer.

    There's not total consistency in random, but it looks like 'triangularvariate' would be the right name for the function - although my knowledge of stats is sketchy, so perhaps there's a distinction I'm not aware of.

    Indentation and spacing around operators needs fixing also.

    Copy link

    Looks correct to me, and seems to work well with correct parameters.

    The arguments to the square roots are never negative; this means that we get silent failure (that is, some distribution that isn't the triangular distribution) if the input parameters don't satisfy left <= center <= right. Maybe the inputs should be checked, and a
    ValueError raised on bad input?

    If the test "x < (center-left)/(right-left)" is replaced by "x*(right-left) < (center - left)" then the code does the right thing in the limit-case when left == center == right (that is, it gives a delta distribution at center); currently it'll raise a ZeroDivisionError in this case. It's not clear to me which behaviour should be preferred.

    Copy link

    Will get this fixe-up and applied to Py2.6. That is due out any time soon, so I'll keep it on my todo list for a bit.

    Copy link
    Mannequin Author

    miathan6 mannequin commented May 2, 2007

    I found one catch that we might want to avoid. If you input integers into this function instead of floats, it breaks. It might be a good idea to wrap the parameters in float(arg).

    Copy link

    Applied in r61796

    @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
    stdlib Python modules in the Lib dir
    None yet

    No branches or pull requests

    2 participants