如何从概率密度函数抽取变量样本?MATLAB高斯采样实现问询
Sampling from a PDF depends on the distribution you’re working with, but here are the most practical approaches:
- Inverse Transform Sampling: The go-to method for distributions with a closed-form cumulative distribution function (CDF) and its inverse. Generate uniform random samples between 0 and 1, then map each sample through the inverse CDF to get a sample from your target PDF. Works great for uniform, exponential, or logistic distributions.
- Rejection Sampling: Useful when the inverse CDF isn’t easy to compute. Generate candidate samples from a simpler "proposal" distribution, then accept each candidate with a probability equal to the ratio of your target PDF to the proposal PDF (scaled to ensure the ratio never exceeds 1).
- Built-in Library Functions: For common distributions like Gaussian, Poisson, or binomial, use optimized built-in functions (like MATLAB’s
normrndorrandn). This avoids manual implementation errors and is far more efficient for standard distributions.
Let’s walk through exactly how to generate 200 Gaussian samples per sensor using your 13×200 data matrix. Here’s a step-by-step solution:
Step 1: Calculate Mean and Variance for Each Sensor
First, compute the mean and variance for each row (sensor) in your data. We’ll use MATLAB’s built-in mean and var functions, specifying we want to calculate these values along the columns (dimension 2):
Step 2: Generate Gaussian Samples
You have two straightforward options—one using the Statistics and Machine Learning Toolbox, and another using core MATLAB (no toolbox required):
Option 1: Using normrnd (Toolbox Required)
% Replace this with your actual 13x200 sensor data matrix sensor_data = randn(13, 200); % Calculate mean for each sensor (row-wise) sensor_means = mean(sensor_data, 2); % Results in a 13x1 vector % Calculate unbiased variance for each sensor (row-wise) % Use 1 instead of 0 for biased variance (divides by N instead of N-1) sensor_vars = var(sensor_data, 0, 2); sensor_stds = sqrt(sensor_vars); % Convert variance to standard deviation % Generate 200 samples per sensor num_samples = 200; % normrnd takes mu, sigma, and output dimensions; transpose to match original shape sampled_data = normrnd(sensor_means, sensor_stds, num_samples, 13)';
Option 2: Using randn (Core MATLAB)
If you don’t have the Statistics Toolbox, generate standard normal samples and scale/shift them to match your desired mean and standard deviation:
% Same initial steps to get means and standard deviations sensor_data = randn(13, 200); sensor_means = mean(sensor_data, 2); sensor_vars = var(sensor_data, 0, 2); sensor_stds = sqrt(sensor_vars); num_samples = 200; % Generate scaled/shifted Gaussian samples sampled_data = sensor_means + sensor_stds .* randn(13, num_samples);
Key Notes
- Variance Estimator: The
varfunction uses an unbiased estimator (divides by N-1) by default. Change the second argument to1if you want a biased estimator (divides by N). - Output Shape: Both methods produce a 13×200 matrix where each row contains 200 samples from the Gaussian distribution fitted to that sensor’s data—matching your original data’s structure.
内容的提问来源于stack exchange,提问作者Manel

