Skip to contents

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

Usage

srlaplace(n = 1, mu = 0, b = 1, x = NULL)

srlaplace_custom(n = 1, x = NULL)

Arguments

n

Integer, length 1. Number of samples to draw.

mu

Numeric, location parameter.

b

Numeric, scale parameter.

x

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

Value

A numeric vector of length n containing samples from the Laplace Distribution with the specified mu and b.

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

Details

The Laplace distribution has the probability density function (PDF): \(f(x | \mu, b) = \frac{1}{2b} \exp\left(-\frac{|x - \mu|}{b}\right),\) where:

\(\mu\)

is the location parameter (mean of the distribution).

b

is the scale parameter, which controls the spread of the distribution (b > 0).

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

By default, srlaplace() samples from a standard Laplace Distribution (mu = 0, b = 1). The proposal distribution is pre-optimized at package load time using srlaplace_optimize() with steps = 4091, creating a scalable proposal centred around the mode.

If srlaplace() is called with custom mu or b parameters, the samples are generated from the standard Laplace Distribution, then scaled and location shifted accordingly.

See also

srlaplace_optimize to optimize the custom or the scaled proposal.

Examples

# Generate 10 samples from the standard Laplace Distribution
samples <- srlaplace(10)
print(samples)
#>  [1] -0.6391839  0.2063019  0.2419238 -0.1944936 -0.6207947 -1.8125260
#>  [7] -2.1035727 -2.5515300 -0.2171045  0.2128769

# Generate 10 samples using a pre-allocated vector
x <- numeric(10)
srlaplace(10, x = x)
#>  [1]  0.52425143 -0.52716355  1.20217410 -0.01977441 -0.65687099 -0.18832695
#>  [7]  0.06994037  1.29133368 -0.35464768  1.54049519
print(x)
#>  [1]  0.52425143 -0.52716355  1.20217410 -0.01977441 -0.65687099 -0.18832695
#>  [7]  0.06994037  1.29133368 -0.35464768  1.54049519

# Generate 10 samples from a Laplace Distribution with mu = 2 and b = 3
samples <- srlaplace(10, mu = 2, b = 3)
print(samples)
#>  [1]  7.9695177  1.9647581  2.8050123  2.1736834 -0.5389744 -2.4457398
#>  [7] -3.7827723 -9.2413467 -0.7135088  7.1259799