Report a bug
If you spot a problem with this page, click here to create a Github issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.random.variable

Authors:
Ilya Yaroshenko, Sebastian Wilzbach (DiscreteVariable)
enum RandomVariable;
User Defined Attribute definition for Random Variable.
template isRandomVariable(T)
Test if T is a random variable.
struct UniformVariable(T) if (isIntegral!T);
Returns:
X ~ U[a, b]
Examples:
auto gen = Random(unpredictableSeed);
auto rv = UniformVariable!int(-10, 10); // [-10, 10]
auto x = rv(gen); // random variable
assert(rv.min == -10);
assert(rv.max == 10);
enum auto isRandomVariable;
this(T a, T b);

Constraints: a <= b.

T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
@property T min();
@property T max();
struct UniformVariable(T) if (isFloatingPoint!T);
Returns:
X ~ U[a, b)
Examples:
import std.math : nextDown;
auto gen = Random(unpredictableSeed);
auto rv = UniformVariable!double(-8, 10); // [-8, 10)
auto x = rv(gen); // random variable
assert(rv.min == -8.0);
assert(rv.max == 10.0.nextDown);
enum auto isRandomVariable;
this(T a, T b);

Constraints: a < b, a and b are finite numbers.

T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
@property T min();
@property T max();
struct ExponentialVariable(T) if (isFloatingPoint!T);
Returns:
X ~ Exp(β)
Examples:
auto gen = Random(unpredictableSeed);
auto rv = ExponentialVariable!double(1);
auto x = rv(gen);
enum auto isRandomVariable;
this(T scale);
T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
enum T min;
enum T max;
struct WeibullVariable(T) if (isFloatingPoint!T);
Examples:
auto gen = Random(unpredictableSeed);
auto rv = WeibullVariable!double(3, 2);
auto x = rv(gen);
enum auto isRandomVariable;
this(T shape, T scale = 1);
T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
enum T min;
enum T max;
struct GammaVariable(T, bool Exp = false) if (isFloatingPoint!T);
Returns:
X ~ Gamma(𝝰, 𝞫)
Parameters:
T floating point type
Exp if true log-scaled values are produced. ExpGamma(𝝰, 𝞫). The flag is useful when shape parameter is small (𝝰 << 1).
Examples:
auto gen = Random(unpredictableSeed);
auto rv = GammaVariable!double(1, 1);
auto x = rv(gen);
enum auto isRandomVariable;
this(T shape, T scale = 1);
T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
enum T min;
enum T max;
struct BetaVariable(T) if (isFloatingPoint!T);
Returns:
X ~ Beta(𝝰, 𝞫)
Examples:
auto gen = Random(unpredictableSeed);
auto rv = BetaVariable!double(2, 5);
auto x = rv(gen);
enum auto isRandomVariable;
this(T a, T b);
T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
enum T min;
enum T max;
struct ChiSquaredVariable(T) if (isFloatingPoint!T);
Examples:
auto gen = Random(unpredictableSeed);
auto rv = ChiSquaredVariable!double(3);
auto x = rv(gen);
enum auto isRandomVariable;
this(size_t k);
T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
enum T min;
enum T max;
struct FisherFVariable(T) if (isFloatingPoint!T);
Examples:
auto gen = Random(unpredictableSeed);
auto rv = FisherFVariable!double(3, 4);
auto x = rv(gen);
enum auto isRandomVariable;
this(T d1, T d2);
T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
enum T min;
enum T max;
struct StudentTVariable(T) if (isFloatingPoint!T);
Examples:
auto gen = Random(unpredictableSeed);
auto rv = StudentTVariable!double(10);
auto x = rv(gen);
enum auto isRandomVariable;
this(T nu);
T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
enum T min;
enum T max;
struct NormalVariable(T) if (isFloatingPoint!T);
Returns:
X ~ N(μ, σ)
Examples:
auto gen = Random(unpredictableSeed);
auto rv = NormalVariable!double(0, 1);
auto x = rv(gen);
enum auto isRandomVariable;
this(T location, T scale = 1);
T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
enum T min;
enum T max;
struct LogNormalVariable(T) if (isFloatingPoint!T);
Examples:
auto gen = Random(unpredictableSeed);
auto rv = LogNormalVariable!double(0, 1);
auto x = rv(gen);
enum auto isRandomVariable;
this(T normalLocation, T normalScale = 1);
Parameters:
T normalLocation location of associated normal
T normalScale scale of associated normal
T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
enum T min;
enum T max;
struct CauchyVariable(T) if (isFloatingPoint!T);
Returns:
X ~ Cauchy(x, γ)
Examples:
auto gen = Random(unpredictableSeed);
auto rv = CauchyVariable!double(0, 1);
auto x = rv(gen);
enum auto isRandomVariable;
this(T location, T scale = 1);
T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
enum T min;
enum T max;
struct ExtremeValueVariable(T) if (isFloatingPoint!T);
Examples:
auto gen = Random(unpredictableSeed);
auto rv = ExtremeValueVariable!double(0, 1);
auto x = rv(gen);
enum auto isRandomVariable;
this(T location, T scale = 1);
T opCall(G)(ref G gen)
if (isSaturatedRandomEngine!G);
enum T min;
enum T max;
struct BernoulliVariable(T) if (isFloatingPoint!T);
Examples:
auto gen = Random(unpredictableSeed);
auto rv = BernoulliVariable!double(0.7);
int[2] hist;
foreach(_; 0..1000)
    hist[rv(gen)]++;
//import std.stdio;
//writeln(hist);
enum auto isRandomVariable;
this(T p);
Parameters:
T p true probability
bool opCall(RNG)(ref RNG gen)
if (isSaturatedRandomEngine!RNG);
enum bool min;
enum bool max;
struct Bernoulli2Variable;
Bernoulli random variable. A fast specialization for p := 1/2.
Examples:
auto gen = Random(unpredictableSeed);
auto rv = Bernoulli2Variable.init;
int[2] hist;
foreach(_; 0..1000)
    hist[rv(gen)]++;
//import std.stdio;
//writeln(hist);
enum bool isRadomVariable;
bool opCall(RNG)(ref RNG gen)
if (isSaturatedRandomEngine!RNG);
enum bool min;
enum bool max;
struct GeometricVariable(T) if (isFloatingPoint!T);
Examples:
auto gen = Random(unpredictableSeed);
auto rv = GeometricVariable!double(0.1);
size_t[ulong] hist;
foreach(_; 0..1000)
    hist[rv(gen)]++;
//import std.stdio;
//foreach(i; 0..100)
//    if(auto count = i in hist)
//        write(*count, ", ");
//    else
//        write("0, ");
//writeln();
enum auto isRandomVariable;
this(T p, bool success = true);
Parameters:
T p probability
reverse p is success probability if true and failure probability otherwise.
ulong opCall(RNG)(ref RNG gen)
if (isSaturatedRandomEngine!RNG);
enum ulong min;
enum ulong max;
struct PoissonVariable(T) if (isFloatingPoint!T);
Examples:
import mir.random;
auto gen = Random(unpredictableSeed);
auto rv = PoissonVariable!double(10);
size_t[ulong] hist;
foreach(_; 0..1000)
    hist[rv(gen)]++;
//import std.stdio;
//foreach(i; 0..100)
//    if(auto count = i in hist)
//        write(*count, ", ");
//    else
//        write("0, ");
//writeln();
enum auto isRandomVariable;
this(T rate);
Parameters:
p probability
reverse p is success probability if true and failure probability otherwise.
ulong opCall(RNG)(ref RNG gen)
if (isSaturatedRandomEngine!RNG);
enum ulong min;
enum ulong max;
struct NegativeBinomialVariable(T) if (isFloatingPoint!T);
Examples:
import mir.random;
auto gen = Random(unpredictableSeed);
auto rv = NegativeBinomialVariable!double(30, 0.3);
size_t[ulong] hist;
foreach(_; 0..1000)
    hist[rv(gen)]++;
//import std.stdio;
//foreach(i; 0..100)
//    if(auto count = i in hist)
//        write(*count, ", ");
//    else
//        write("0, ");
//writeln();
enum auto isRandomVariable;
this(size_t r, T p);
Parameters:
size_t r r > 0; number of failures until the experiment is stopped
T p p ∈ (0,1); success probability in each experiment
ulong opCall(RNG)(ref RNG gen)
if (isSaturatedRandomEngine!RNG);
enum ulong min;
enum ulong max;
struct BinomialVariable(T) if (isFloatingPoint!T);
Examples:
import mir.random;
auto gen = Random(unpredictableSeed);
auto rv = BinomialVariable!double(20, 0.5);
int[] hist = new int[rv.max + 1];
auto cnt = 1000;
foreach(_; 0..cnt)
    hist[rv(gen)]++;
//import std.stdio;
//foreach(n, e; hist)
//    writefln("p(x = %s) = %s", n, double(e) / cnt);
enum auto isRandomVariable;
this(size_t n, T p);
Parameters:
size_t n n > 0; number of trials
T p p ∈ [0,1]; success probability in each trial
size_t opCall(RNG)(ref RNG gen)
if (isSaturatedRandomEngine!RNG);
enum size_t min;
@property size_t max();
struct DiscreteVariable(T) if (isNumeric!T);
Discrete distribution sampler that draws random values from a discrete distribution given an array of the respective probability density points (weights).
Examples:
auto gen = Random(unpredictableSeed);
// 10%, 20%, 20%, 40%, 10%
auto weights = [10.0, 20, 20, 40, 10];
auto ds = DiscreteVariable!double(weights);

// weight is changed to cumulative sums
assert(weights == [10, 30, 50, 90, 100]);

// sample from the discrete distribution
auto obs = new uint[weights.length];

foreach (i; 0..1000)
    obs[ds(gen)]++;

//import std.stdio;
//writeln(obs);
Examples:
Comulative
auto gen = Random(unpredictableSeed);

auto cumulative = [10.0, 30, 40, 90, 120];
auto ds = DiscreteVariable!double(cumulative, true);

assert(cumulative == [10.0, 30, 40, 90, 120]);

// sample from the discrete distribution
auto obs = new uint[cumulative.length];
foreach (i; 0..1000)
    obs[ds(gen)]++;
enum auto isRandomVariable;
this(T[] weights, bool cumulative = false);
DiscreteVariable constructor computes cumulative density points in place without memory allocation.
Parameters:
T[] weights density points
bool cumulative optional flag indiciates if weights are already cumulative
size_t opCall(RNG)(ref RNG gen)
if (isSaturatedRandomEngine!RNG);
Samples a value from the discrete distribution using a custom random generator.

Complexity: O(log n) where n is the number of weights.

enum size_t min;
@property size_t max();
struct PiecewiseConstantVariable(T, W = T) if (isNumeric!T && isNumeric!W);
Piecewise constant variable.
Examples:
auto gen = Random(unpredictableSeed);
// 50% of the time, generate a random number between 0 and 1
// 50% of the time, generate a random number between 10 and 15
double[] i = [0,  1, 10, 15];
double[] w =   [1,  0,  1];
auto pcv = PiecewiseConstantVariable!(double, double)(i, w);
assert(w == [1, 1, 2]);

int[int] hist;
foreach(_; 0 .. 10000)
    ++hist[cast(int)pcv(gen)];

//import std.stdio, std.range;
//foreach(j; 0..cast(int)i[$-1])
//    if(auto count = j in hist)
//        writefln("%2s %s", j, '*'.repeat.take(*count / 100));

//////// output example /////////
/+
 0 **************************************************
10 *********
11 *********
12 **********
13 *********
14 **********
+/
enum auto isRandomVariable;
this(T[] intervals, W[] weights, bool cumulative = false);
PiecewiseConstantVariable constructor computes cumulative density points in place without memory allocation.
Parameters:
T[] intervals strictly increasing sequence of interval bounds.
W[] weights density points
bool cumulative optional flag indicates if weights are already cumulative
T opCall(RNG)(ref RNG gen)
if (isSaturatedRandomEngine!RNG);

Complexity: O(log n) where n is the number of weights.

@property T min();
@property T max();
struct PiecewiseLinearVariable(T) if (isFloatingPoint!T);
Piecewise constant variable.
Examples:
auto gen = Random(unpredictableSeed);
// increase the probability from 0 to 5
// remain flat from 5 to 10
// decrease from 10 to 15 at the same rate
double[] i = [0, 5, 10, 15];
double[] w = [0, 1,  1,  0];
auto pcv = PiecewiseLinearVariable!double(i, w, new double[w.length - 1]);

int[int] hist;
foreach(_; 0 .. 10000)
    ++hist[cast(int)pcv(gen)];

//import std.stdio, std.range;
//foreach(j; 0..cast(int)i[$-1]+1)
//    if(auto count = j in hist)
//        writefln("%2s %s", j, '*'.repeat.take(*count / 100));

//////// output example /////////
/+
 0 *
 1 **
 2 *****
 3 *******
 4 ********
 5 **********
 6 *********
 7 *********
 8 **********
 9 *********
10 *********
11 *******
12 ****
13 **
14 *
+/
enum auto isRandomVariable;
this(T[] points, T[] weights, T[] areas);
Parameters:
intervals strictly increasing sequence of interval bounds.
T[] weights density points
T[] areas user allocated uninitialized array

Constrains: points.length == weights.length
areas.length > 0
areas.length + 1 == weights.length

T opCall(RNG)(ref RNG gen)
if (isSaturatedRandomEngine!RNG);

Complexity: O(log n) where n is the number of weights.

@property T min();
@property T max();