srlaplace: Sampling from Laplace Distribution

View source: R/srlaplace.R

srlaplaceR Documentation

Sampling from Laplace Distribution

Description

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.

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.

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.

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)

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

# Generate 10 samples from a Laplace Distribution with mu = 2 and b = 3
samples <- srlaplace(10, mu = 2, b = 3)
print(samples)


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