Add std.random.discrete: Discrete sampler#234
Conversation
e0da7ef to
d0c322e
Compare
source/mir/random/mixture.d
Outdated
| size_t opCall(RNG)(ref RNG gen) const | ||
| { | ||
| import std.random : uniform; | ||
| T v = uniform!"[]"(0, _cdPoints[$-1], gen); |
Current coverage is 96.52%@@ master #234 diff @@
==========================================
Files 18 19 +1
Lines 3230 3256 +26
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 3117 3143 +26
Misses 113 113
Partials 0 0
|
source/mir/random/mixture.d
Outdated
| size_t opCall(RNG)(ref RNG gen) const | ||
| { | ||
| import std.random : uniform; | ||
| T v = uniform(0, _cdPoints[$-1], gen); |
There was a problem hiding this comment.
doesn't work - the prototype is auto uniform(string boundaries = "[)", T1, T2)(T1 a, T2 b)
- uint array instead of associative array - named Random variable - renamed probs -> cdPoints - reduced iteration size to 1000
|
Docs can be previewed at preview.mir.rocks. @WebDrake you told me to ping both accounts? |
| A sampler that can be called to sample from the distribution. | ||
| */ | ||
| struct Discrete(T) | ||
| if (isNumeric!T) |
There was a problem hiding this comment.
Struct declaration should be after function. And add a small separate comment please
source/mir/random/discrete.d
Outdated
| return Discrete!T(cdPoints); | ||
| } | ||
|
|
||
| /// ditto |
There was a problem hiding this comment.
Please do not compose function and struct declarations
| { | ||
| import std.random : uniform; | ||
| T v = uniform!("[)", T, T)(0, _cdPoints[$-1], gen); | ||
| return (cast(SortedRange!(const(T)[], "a <= b")) r).lowerBound(v).length; |
There was a problem hiding this comment.
Can't you just use assumeSorted here ... ?
There was a problem hiding this comment.
SortedRange constructor always check that range is sorted in debug mode. This will affect complexity
There was a problem hiding this comment.
... actually, isn't r already a sorted range? Why the cast at all?
There was a problem hiding this comment.
Why the cast at all?
because the internal variable r has type const SortedRange!(const(T)[], "a <= b") - I didn't know another way to get rid of the const.
There was a problem hiding this comment.
I would repeat my suggestion that you look at hap.random's DiscreteDistribution -- I did not need any casts under the hood:
https://github.com/WebDrake/hap/blob/e25fc15a17e4e439333740d2a9894fc7d3c94012/source/hap/random/distribution.d#L323-L329
There was a problem hiding this comment.
Right, but that would mean you can't use lowerBound with a const SortedRange here, right? Surely that should be fixed in SortedRange, rather than covering up the non-const-ness with a cast?
There was a problem hiding this comment.
Surely that should be fixed in SortedRange, rather than covering up the non-const-ness with a cast?
Yes I will try to fix Phobos, but that won't be released before 2.073 :/
There was a problem hiding this comment.
Yes I will try to fix Phobos, but that won't be released before 2.073 :/
You have not time to fix Phobos templates, please consider on related issues
There was a problem hiding this comment.
I'm not sure that it's appropriate to prioritize putting const on opCall above having clean and easy-to-follow code. Things like the cast and the two different internal variables for the cumulative distribution points create unnecessary complication in the code, and the cast in any case risks breaking the const guarantee (certainly if e.g. at some point you generalize the type which can be provided as input to the constructor).
This is a case where the fix needs to be upstream, and where the guarantee wanted here can easily be added after that fix is complete.
- renamed unittest variable rndGen -> gen - renamed r -> _cdSortedRange
- renamed unittest variable rndGen -> gen - renamed r -> _cdSortedRange
- renamed unittest variable rndGen -> gen - renamed r -> _cdSortedRange - use assumeSorted instead of directly constructing the SortedRange
- renamed unittest variable rndGen -> gen - removed r -> in favor of using assumeSorted directly in opCall

preview docs