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 distribution functions #22

Open
rstub opened this issue Mar 14, 2019 · 6 comments
Open

More distribution functions #22

rstub opened this issue Mar 14, 2019 · 6 comments
Labels
enhancement New feature or request

Comments

@rstub
Copy link
Member

rstub commented Mar 14, 2019

Many distribution functions are supported via the stats package and add-on packages together with Rs RNG. It would be good to support a greater selection of distributions together with the faster RNGs from dqrng.

Directly usable:

@rstub rstub added the enhancement New feature or request label Mar 14, 2019
@rstub
Copy link
Member Author

rstub commented Mar 14, 2019

Potentially usable (c.f. https://stackoverflow.com/a/54143984/8416610):

@coatless, @duckmayr: Would any of you be interested in supporting external RNGs?

Generalized Ziggurat: https://github.com/DiscreteLogarithm/Zest

@duckmayr
Copy link

@rstub Potentially. I can look into this around March 23.

@coatless
Copy link

@rstub more than content with adding new routines into rgen. That said, the rgen package was created prior to the development of @kthohr's stats as I needed R sampling routines that were not exported.

@rstub
Copy link
Member Author

rstub commented Mar 20, 2019

@coatless I don't think that additional routines would be necessary. I think it should work to add additional arguments to the existing functions. So instead of calling a fixed norm_rand or unif_rand, a function is called that has been passed in as argument and defaults to the currently used function. This way the existing functions should be usable as before but offer the possibility to change the random number source.

Concerning the stats library. One problem is that it relies on (at least) std::uniform_distribution and std::normal_distribution. This makes it unsuitable for usage in an R package, since WRE clearly says that one should not use the random header from C++11. And the implementation defined distribution functions are a good reason for this.

@rstub
Copy link
Member Author

rstub commented Apr 21, 2024

Distributions that are used in packages making use of dqrng:

  • log-normal
  • gamma

@jessekps
Copy link

Thanks for considering the gamma. For completenss sake, I actually use a couple more, but they are rather trivial extensions of a potential rgamma. Nevertheless you might be able to improve on them.

double rchisq(dqrng::xoshiro256plus& lrng, const double df)
{
	return 2 * rgamma(lrng, df/2, 1.0);
}
	
double rinvchisq(dqrng::xoshiro256plus& lrng, const double df, const double scale)
{
	double z = rchisq(lrng, df);
	if(z==0) z= 1e-100;
	return (df*scale) / z;
}

double rbeta(dqrng::xoshiro256plus& lrng, const double alpha, const double beta)
{
	double x = rgamma(lrng, alpha, 1.0);
	double y = rgamma(lrng, beta, 1.0);
	
	return x/(x+y);
}

And also a dirichlet, but the implementation is a little specific to my package and not sure how you would want to generalize it since it would need a pointer, vector or iterator of some kind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants