libmir / mir-random

Advanced Random Number Generators
http://mir-random.libmir.org/
32 stars 15 forks source link

ExponentialVariable seems broken #96

Closed luismarques closed 6 years ago

luismarques commented 6 years ago

Maybe, I'm doing something wrong (help would be appreciated) but I'm getting nonsensical results when using an ExponentialVariable (EV).

Take an EV with lambda 0.5. We can confirm that it has a median of around 1.38629: http://www.wolframalpha.com/input/?i=median+of+exponential+distribution+with+lambda+0.5.

Therefore, I would expect that a ExponentialVariable!double(0.5) range would return approximately half of its values < 1.38629, and half > 1.38629. That doesn't seem to be the case.

import std.stdio;
import mir.random.algorithm;
import mir.random.variable;

void main()
{
    enum lambda = 0.5;
    enum median = 1.38629;
    enum n = 1000;
    auto ev = ExponentialVariable!double(lambda).range;

    int smaller;
    int equal;
    int larger;

    foreach(i; 0 .. n)
    {
        auto v = ev.front();
        ev.popFront();

        if(v < median)
            ++smaller;
        else if(v == median)
            ++equal;
        else
            ++larger;
    }

    auto nf = double(n);
    writefln("%s %s %s", smaller/nf, equal/nf, larger/nf);
}

(I used an explicit loop, instead of ranges, just to be sure I wasn't doing something unexpected).

Running this (https://run.dlang.io/is/r1p4vE) I get about 95% of values are smaller than the median.

9il commented 6 years ago

Another one Mir Random user is detected. ExponentialVariable is correct (at least it is not a bug). It accepts scale parameter, which is inverse of lambda. https://run.dlang.io/is/nmRSSx

jmh530 commented 6 years ago

@9il Not a bug, but the documentation still needs improvement. It just has a link to wikipedia, which emphasizes lambdas. Many users might be confused by this. It should at least say somewhere that scale is the inverse of lambda. This is not unique to the exponential distribution.

9il commented 6 years ago

@9il Not a bug, but the documentation still needs improvement. It just has a link to wikipedia, which emphasizes lambdas. Many users might be confused by this. It should at least say somewhere that scale is the inverse of lambda. This is not unique to the exponential distribution.

Agreed