Skip to content

Comments

Add std.random.discrete: Discrete sampler#234

Merged
9il merged 10 commits intolibmir:masterfrom
wilzbach:mixture
Jun 26, 2016
Merged

Add std.random.discrete: Discrete sampler#234
9il merged 10 commits intolibmir:masterfrom
wilzbach:mixture

Conversation

@wilzbach
Copy link
Member

@wilzbach wilzbach commented Jun 25, 2016

@wilzbach wilzbach force-pushed the mixture branch 3 times, most recently from e0da7ef to d0c322e Compare June 25, 2016 22:33
size_t opCall(RNG)(ref RNG gen) const
{
import std.random : uniform;
T v = uniform!"[]"(0, _cdPoints[$-1], gen);
Copy link
Member

Choose a reason for hiding this comment

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

it can be just uniform

Copy link
Member

Choose a reason for hiding this comment

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

without "[]"

@codecov-io
Copy link

codecov-io commented Jun 25, 2016

Current coverage is 96.52%

Merging #234 into master will increase coverage by 0.02%

@@             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          

Sunburst

Powered by Codecov. Last updated by 03c87f6...c2f8930

size_t opCall(RNG)(ref RNG gen) const
{
import std.random : uniform;
T v = uniform(0, _cdPoints[$-1], gen);
Copy link
Member

Choose a reason for hiding this comment

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

uniform!T please

Copy link
Member Author

Choose a reason for hiding this comment

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

doesn't work - the prototype is auto uniform(string boundaries = "[)", T1, T2)(T1 a, T2 b)

https://dlang.org/phobos/std_random.html#.uniform

Copy link
Member

Choose a reason for hiding this comment

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

uniform!("[)", T, T) then

@9il
Copy link
Member

9il commented Jun 25, 2016

ping @joseph-wakeling-sociomantic

wilzbach added 2 commits June 26, 2016 01:25
- uint array instead of associative array
- named Random variable
- renamed probs -> cdPoints
- reduced iteration size to 1000
@wilzbach
Copy link
Member Author

wilzbach commented Jun 25, 2016

Docs can be previewed at preview.mir.rocks.

ping @joseph-wakeling-sociomantic

@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)
Copy link
Member

Choose a reason for hiding this comment

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

Struct declaration should be after function. And add a small separate comment please

Copy link
Member Author

Choose a reason for hiding this comment

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

Struct declaration should be after function. And add a small separate comment please

ok for the order, but ditto would group them:

image

@wilzbach wilzbach changed the title Add std.random.mixture: Discrete sampler - fixes #233 Add std.random.discrete: Discrete sampler Jun 26, 2016
return Discrete!T(cdPoints);
}

/// ditto
Copy link
Member

Choose a reason for hiding this comment

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

Please do not compose function and struct declarations

@9il 9il merged commit b6c255f into libmir:master Jun 26, 2016
{
import std.random : uniform;
T v = uniform!("[)", T, T)(0, _cdPoints[$-1], gen);
return (cast(SortedRange!(const(T)[], "a <= b")) r).lowerBound(v).length;

Choose a reason for hiding this comment

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

Can't you just use assumeSorted here ... ?

Copy link
Member

@9il 9il Jun 26, 2016

Choose a reason for hiding this comment

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

SortedRange constructor always check that range is sorted in debug mode. This will affect complexity

Choose a reason for hiding this comment

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

... actually, isn't r already a sorted range? Why the cast at all?

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

Choose a reason for hiding this comment

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

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

Copy link
Member

Choose a reason for hiding this comment

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

opCall is const

Choose a reason for hiding this comment

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

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

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 :/

Copy link
Member

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

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.

wilzbach added a commit to wilzbach/mir that referenced this pull request Jun 26, 2016
- renamed unittest variable rndGen -> gen
- renamed r -> _cdSortedRange
wilzbach added a commit to wilzbach/mir that referenced this pull request Jun 26, 2016
- renamed unittest variable rndGen -> gen
- renamed r -> _cdSortedRange
wilzbach added a commit to wilzbach/mir that referenced this pull request Jun 26, 2016
- renamed unittest variable rndGen -> gen
- renamed r -> _cdSortedRange
- use assumeSorted instead of directly constructing the SortedRange
wilzbach added a commit to wilzbach/mir that referenced this pull request Jun 26, 2016
- renamed unittest variable rndGen -> gen
- removed r -> in favor of using assumeSorted directly in opCall
9il pushed a commit that referenced this pull request Jun 26, 2016
* Adress comments from #234

- renamed unittest variable rndGen -> gen
- removed r -> in favor of using assumeSorted directly in opCall

* remove cdPoints API
@wilzbach wilzbach deleted the mixture branch June 26, 2016 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants