sample.distr | R Documentation |
Returns a set of values of the random variable, \bar{X}
, sampled from a user defined probability distribution f(\bar{X}, \hat{\theta})
.
Identical functionality to the runif and rnorm functions present in the base R package.
sample.distr(n, func, f.par, xmin, xmax)
n |
The no. of values of |
func |
The function |
f.par |
The specific values of the parameters, |
xmin , xmax |
Numeric values within which the returned values of |
Uses rejection sampling to arrive at the results of \bar{X}
.
Thus results for n > 10^7
could strain system resources, since rejection sampling is a monte-carlo method and needs overgeneration beyond n
in the rejection space.
The range of the function f(\bar{X},\hat{\theta})
input to func
will be normalized to f(\bar{X}, \hat{\theta}) \in [0, 1]
.
Hence it should be ensured that f(\bar{X},\hat{\theta})
is devoid of badly behaved singularities within \code{xmin} \leq \bar{X} \leq \code{xmax}
.
The reurned value is a 2 element list
random_var
: The random variable \bar{X}
which follows the distribution f(\bar{X}, \hat{\theta})
returned as a R vector
likelihood
: The density value f(\bar{X}, \hat{\theta})
for each corresponding value in \bar{X}
as a R vector
Chitran Ghosal
https://en.wikipedia.org/wiki/Rejection_sampling
####Define the probability distribution
sin.exp <- function(X, al, omeg){
y <- exp(-al*abs(X))*(sin(omeg*X))^2
return(y)
}
####define the distribution parameters
par_f <- c(2, 2)
####plot the bare distribution function
RV <- seq(-10, 10, by=0.01) #Random variable needed for the plotting
Y <- nrm(sin.exp(X=RV, al =par_f[1], omeg = par_f[2])) #The density/likelihood values against the given random variables
plot(RV, Y, type = 'l')
####retrieve random values of X out of the distribution and plot its histogram for different values of n
## for n = 10
L <- sample.distr(n = 10^1, func = sin.exp, f.par = par_f, xmin = -10, xmax = 10)
h <- hist(L$random_var, probability = T, breaks = seq(-10, 10, by = 0.25), border = NA, main = "### n = 10 ")
lines(RV, nrm(Y, min = 0, max = max(h$density)), col = 'black', lwd=2.5)
## for n = 100
L <- sample.distr(n = 10^2, func = sin.exp, f.par = par_f, xmin = -10, xmax = 10)
h <- hist(L$random_var, probability = T, breaks = seq(-10, 10, by = 0.25), border = NA, main = "### n = 100 ")
lines(RV, nrm(Y, min = 0, max = max(h$density)), col = 'black', lwd=2.5)
## for n = 1000
L <- sample.distr(n = 10^3, func = sin.exp, f.par = par_f, xmin = -10, xmax = 10)
h <- hist(L$random_var, probability = T, breaks = seq(-10, 10, by = 0.25), border = NA, main = "### n = 1000 ")
lines(RV, nrm(Y, min = 0, max = max(h$density)), col = 'black', lwd=2.5)
## for n = 10^4
L <- sample.distr(n = 10^4, func = sin.exp, f.par = par_f, xmin = -10, xmax = 10)
h <- hist(L$random_var, probability = T, breaks = seq(-10, 10, by = 0.25), border = NA, main = "### n = 10000 ")
lines(RV, nrm(Y, min = 0, max = max(h$density)), col = 'black', lwd=2.5)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.