srexp: Sampling from Exponential Distribution

View source: R/srexp.R

srexpR Documentation

Sampling from Exponential Distribution

Description

The srexp() function generates random samples from a Exponential Distribution using the STORS algorithm. It employs an optimized proposal distribution around the mode and Inverse Transform (IT) method for the tails.

Usage

srexp(n = 1, rate = 1, x = NULL)

srexp_custom(n = 1, x = NULL)

Arguments

n

Integer, length 1. Number of samples to draw.

rate

Numeric. is the rate parameter of the Exponential Distribution.

x

(optional) Numeric vector of length n. If provided, this vector is over written in place to avoid any memory allocation.

Details

The Exponential distribution has the probability density function (PDF): f(x | \lambda) = \lambda \exp(-\lambda x), \quad x \geq 0, where:

\lambda

is the rate parameter (\lambda > 0), which determines the rate of decay of the distribution.

The Exponential distribution is commonly used to model the time between independent events that occur at a constant average rate.

These two functions are for sampling using the STORS algorithm based on the proposal that has been constructed using srexp_optimize.

By default, srexp() samples from a standard Exponential Distribution rate = 1. The proposal distribution is pre-optimized at package load time using srexp_optimize() with steps = 4091, creating a scalable proposal centred around the mode.

If srexp() is called with custom rate parameter, the samples are generated from the standard Exponential Distribution, then scaled accordingly.

Value

A numeric vector of length n containing samples from the Exponential Distribution with the specified rate.

NOTE: When the x parameter is specified, it is updated in-place with the simulation for performance reasons.

See Also

srexp_optimize to optimize the custom or the scaled proposal.

Examples

# Generate 10 samples from the standard Exponential Distribution
samples <- srexp(10)
print(samples)

# Generate 10 samples using a pre-allocated vector
x <- numeric(10)
srexp(10, x = x)
print(x)

# Generate 10 samples from a Exponential Distribution with rate = 4
samples <- srexp(10, rate = 4)
print(samples)


stors documentation built on April 3, 2025, 6:16 p.m.