Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more random functions #4145

Merged
merged 7 commits into from
Aug 15, 2018
Merged

Conversation

catoverdrive
Copy link
Contributor

(mostly the ones that balding nichols uses.)


Parameters
----------
a : float or :class:`.Expression` of type :py:data:`.tfloat64`
Copy link
Contributor

Choose a reason for hiding this comment

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

you can simplify this here and elsewhere:

float or :class`Float64Expression`

max=nullable(expr_float64),
seed=nullable(int))
def rand_beta(a, b, min=None, max=None, seed=None) -> Float64Expression:
"""Returns a random floating-point number drawn from a beta distribution
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd shorten to "Returns a random draw from a beta distribution." and link beta distribution to: https://en.wikipedia.org/wiki/Beta_distribution

----------
a : float or :class:`.Expression` of type :py:data:`.tfloat64`
b : float or :class:`.Expression` of type :py:data:`.tfloat64`
min : float or :class:`.Expression` of type :py:data:`.tfloat64`
Copy link
Contributor

Choose a reason for hiding this comment

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

convention is:

float or :class`Float64Expression`, optional

(Optional) Left boundary of truncated beta distribution.
max : float or :class:`.Expression` of type :py:data:`.tfloat64`
(Optional) Right boundary of truncated beta distribution.
seed : :obj:`int` or `None`
Copy link
Contributor

@jbloom22 jbloom22 Aug 14, 2018

Choose a reason for hiding this comment

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

:obj:`int`, optional

make this change below too (and check that all these random functions are consistently documented)

max : float or :class:`.Expression` of type :py:data:`.tfloat64`
(Optional) Right boundary of truncated beta distribution.
seed : :obj:`int` or `None`
If not `None`, function will be seeded with provided seed.
Copy link
Contributor

@jbloom22 jbloom22 Aug 14, 2018

Choose a reason for hiding this comment

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

Just put "Random seed".

@@ -2207,6 +2204,7 @@ def balding_nichols_model(n_populations, n_samples, n_variants, n_partitions=Non
mixture)
return MatrixTable(jmt)


Copy link
Contributor

Choose a reason for hiding this comment

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

remove line


def rgamma(a: Double, scale: Double): Double = Gamma.random(a, scale, random)

def rdraw(a: Array[Double]): Int = {
Copy link
Contributor

Choose a reason for hiding this comment

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

rcat(prob)


def rdraw(a: Array[Double]): Int = {
val sum = a.sum
val probs = Array.tabulate(a.length)(i => a(i) / sum)
Copy link
Contributor

Choose a reason for hiding this comment

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

no need for rescaling. Just set draw = random.nextDouble() * sum


Warning
-------
This function may be slow over very large arrays.
Copy link
Contributor

Choose a reason for hiding this comment

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

This function may be slow when the number of categories is large.

Examples
--------

>>> hl.rand_dirichlet([1, 1, 1]).value
Copy link
Contributor

Choose a reason for hiding this comment

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

what behavior do you get if a value is negative?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

>>> hl.rand_dirichlet([-1, -1, 1]).value
[nan, nan, nan]

Copy link
Contributor

@jbloom22 jbloom22 left a comment

Choose a reason for hiding this comment

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

great!

"""
return _seeded_func("rand_norm", tfloat64, seed, mean, sd)


@typecheck(lamb=expr_float64, seed=nullable(int))
def rand_pois(lamb, seed=None) -> Float64Expression:
"""Samples from a Poisson distribution with rate parameter `lamb` (RNG).
"""Samples from a Poisson distribution with rate parameter `lamb`.
Copy link
Contributor

Choose a reason for hiding this comment

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

please change parameter name to lambda and link to https://en.wikipedia.org/wiki/Poisson_distribution

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lambda is a reserved word in python, unfortunately

Copy link
Contributor

Choose a reason for hiding this comment

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

oh right, that's why we used lamb. ok

"""Returns a random index from an array of unnormalized probabilities.
def rand_cat(prob, seed=None) -> Int32Expression:
"""Samples from a `categorical distribution
<https://en.wikipedia.org/wiki/Categorical_distribution>`__
Copy link
Contributor

Choose a reason for hiding this comment

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

add period

def rand_beta(a, b, lower=None, upper=None, seed=None) -> Float64Expression:
"""Samples from a `beta distribution
<https://en.wikipedia.org/wiki/Beta_distribution>`__ with parameters `a`
(alpha) and `b` (beta).
Copy link
Contributor

Choose a reason for hiding this comment

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

nice, very clear

@jbloom22
Copy link
Contributor

don't forget to make a discuss post on the breaking change on the parameter names (lower/upper)

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.

3 participants