Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions source/mir/random/discrete.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ unittest

// sample from the discrete distribution
auto obs = new uint[cdPoints.length];
foreach (i; 0..1000)
foreach (i; 0..10_000)
Copy link
Member Author

@wilzbach wilzbach Jun 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the split looks too random with 1000

obs[ds()]++;
}

Expand Down Expand Up @@ -81,34 +81,57 @@ struct Discrete(T)
import std.range : assumeSorted;

T v = uniform!("[)", T, T)(0, cdPoints[$-1], gen);
return cdPoints.assumeSorted!"a <= b".lowerBound(v).length;
return cdPoints.length - cdPoints.assumeSorted!"a < b".upperBound(v).length;
}
}

// test with cumulative probs
unittest
{
import std.random : Random;
auto gen = Random(42);
import std.random : Mt19937;
auto gen = Mt19937(42);

// 10%, 20%, 20%, 40%, 10%
auto cdPoints = [0.1, 0.3, 0.5, 0.9, 1];
auto ds = discrete(cdPoints);

auto obs = new uint[cdPoints.length];
foreach (i; 0..1000)
foreach (i; 0..10_000)
obs[ds(gen)]++;

assert(obs == [1030, 1964, 1968, 4087, 951]);
}

// test with cumulative count
unittest
{
import std.random : Random;
auto gen = Random(42);
import std.random : Mt19937;
auto gen = Mt19937(42);

// 1, 2, 1
auto cdPoints = [1, 3, 4];
auto ds = discrete(cdPoints);

auto obs = new uint[cdPoints.length];
foreach (i; 0..1000)
foreach (i; 0..10_000)
obs[ds(gen)]++;

assert(obs == [2536, 4963, 2501]);
}

// test with zero probabilities
unittest
{
import std.random : Mt19937;
auto gen = Mt19937(42);

// 0, 1, 2, 0, 1
auto cdPoints = [0, 1, 3, 3, 4];
auto ds = discrete(cdPoints);

auto obs = new uint[cdPoints.length];
foreach (i; 0..10_000)
obs[ds(gen)]++;

assert(obs == [0, 2536, 4963, 0, 2501]);
}