draw_from_pdf: Draw Samples Using Inverse Transform Sampling

View source: R/core_tools.R

draw_from_pdfR Documentation

Draw Samples Using Inverse Transform Sampling

Description

draw_from_pdf generates samples from a given probability density function (PDF) using inverse transform sampling. This function takes in a numeric PDF vector and a corresponding domain vector, then returns a specified number of samples.

Usage

draw_from_pdf(a_pdf, x_def, k, seed = NULL)

Arguments

a_pdf

a numeric vector representing the PDF values.

x_def

a numeric vector defining the domain (or x-values) corresponding to the values in a_pdf. The vector x_def must be sorted in increasing order.

k

a single integer specifying the number of samples to generate.

seed

an optional single integer value used to set the seed for random number generation, allowing for reproducibility of results.

Details

This function implements inverse transform sampling by first constructing a cumulative distribution function (CDF) from the given PDF. A uniform random variable is then sampled for each of the k samples, and the corresponding value in x_def is selected by locating the appropriate interval in the CDF.

Value

A numeric vector of length k containing the sampled values from the specified PDF. If k is 0, an empty numeric vector is returned.


dRiftDM documentation built on April 3, 2025, 7:48 p.m.