Description Usage Arguments Value Author(s) See Also Examples
Brute-force algorithm for drawing random numbers from a d-dimensional distribution.
1 |
f |
function of a d-vector representing a d-dimensional distribution function. This function must be non-negative on the whole domain. It does not need to be normalized. For fast performance, this function should be vectorized, such that it returns an N-element vector if it is given an N-by-D matrix as argument. An automatic warning is produced if the function is not vectorized in this manner. |
n |
number of random numbers to be generated |
min, max |
are d-vectors specifying the domain of distribution function; the domain must be finite and should be as restrictive as possible to keep the number of random trials as low as possible. |
fmax |
maximum value of |
seed |
optional seed for random number generator. |
warn |
logical flag. If true (default), a warning is produced if the function f is not vectorized. |
Returns list of items:
x |
n-by-d matrix of n random d-vectors. |
fmax |
maximum value of the distribution function |
n |
number of random vectors (same as argument |
ntrials |
number of trials. |
Danail Obreschkow
1 2 3 4 5 6 7 8 9 10 11 | ## 1D random number generation (sin-function)
f = function(x) sin(x)
out = rng(f,1e3,0,pi)
hist(out$x,freq=FALSE,xlab='x')
curve(sin(x)/2,0,pi,add=TRUE)
## 5D random number generation (5-dimensional sphere)
f = function(x) as.numeric(sum(x^2)<=1)
out = rng(f,1e4,rep(-1,5),rep(1,5))
cat(sprintf('Number of successes over number of trials : %.4f\n',out$n/out$ntrials))
cat(sprintf('Expected ratio for n=\u221E : %.4f\n',pi^(5/2)/gamma(1+5/2)/2^5))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.