Effacer les filtres
Effacer les filtres

Generate Random Samples Using Latin Hypercube Sampling Method

44 vues (au cours des 30 derniers jours)
Mark
Mark le 7 Fév 2023
I want my MATLAB code to generate the random samples of uncertainties in the three parameters (155, 189, 232) using Latin Hypercube sampling which has 15 %, 20 % and 30 % relative uncertainties. The maximum and minimum uncertainity should cover more than 90 % of the of the distribution probability that is two standard derivation. Below is my MATLAB code, please check if I am on the right path.
% Define the variables
p1 = 155;
p2 = 189;
p3 = 232;
% Define the error
rel_unc1 = 0.15;
rel_unc2 = 0.20;
rel_unc3 = 0.30;
% Calculate the parameter ranges
unc_range1 = p1 * rel_unc1;
unc_range2 = p2 * rel_unc2;
unc_range3 = p3 * rel_unc3;
% Set the number of samples to generate
n_samples = 100;
% Generate the random samples using Latin Hypercube Sampling
lhs1 = lhsdesign(n_samples, 1);
lhs2 = lhsdesign(n_samples, 1);
lhs3 = lhsdesign(n_samples, 1);
% Scale the samples to cover more than 90% of the distribution
lhs1 = p1 - unc_range1 + 2 * unc_range1 * lhs1;
lhs2 = p2 - unc_range2 + 2 * unc_range2 * lhs2;
lhs3 = p3 - unc_range3 + 2 * unc_range3 * lhs3;
% Concatenate the samples into a matrix
lhs = [lhs1, lhs2, lhs3];
% Write the results to a CSV file
header = {'p1', 'p2', 'p3'};
fid = fopen('p_samples.csv', 'w');
fprintf(fid, '%s,%s,%s\n', header{:});
fclose(fid);
dlmwrite('p_samples.csv', lhs, '-append');

Réponses (1)

Paras Gupta
Paras Gupta le 22 Juin 2024
Hi Mark,
I understand you want to generate random samples of uncertainties in three parameters using Latin Hypercube Sampling (LHS) with specified relative uncertainties.
Your MATLAB code is correct, but it implicitly assumes a uniform distribution for the uncertainties. This is evident from how the Latin Hypercube Sampling (LHS) is scaled.
However, if we are to cover 90% of the distribution probability and we don't have a specific distribution in mind, the normal distribution is a reasonable assumption due to its common usage in uncertainty analysis.The folllowing code models the uncertainties as normally distributed:
% Define the variables
p1 = 155;
p2 = 189;
p3 = 232;
% Define the relative uncertainties
rel_unc1 = 0.15;
rel_unc2 = 0.20;
rel_unc3 = 0.30;
% Calculate the standard deviations
std_dev1 = p1 * rel_unc1 / 2;
std_dev2 = p2 * rel_unc2 / 2;
std_dev3 = p3 * rel_unc3 / 2;
% Set the number of samples to generate
n_samples = 100;
% Generate the random samples using Latin Hypercube Sampling
lhs = lhsdesign(n_samples, 3);
% Scale the samples to cover more than 90% of the distribution
lhs1 = p1 + std_dev1 * norminv(lhs(:, 1), 0, 1);
lhs2 = p2 + std_dev2 * norminv(lhs(:, 2), 0, 1);
lhs3 = p3 + std_dev3 * norminv(lhs(:, 3), 0, 1);
% Concatenate the samples into a matrix
lhs_samples = [lhs1, lhs2, lhs3];
% Write the results to a CSV file
header = {'p1', 'p2', 'p3'};
fid = fopen('p_samples.csv', 'w');
fprintf(fid, '%s,%s,%s\n', header{:});
fclose(fid);
dlmwrite('p_samples.csv', lhs_samples, '-append');
The above code has the following necessary changes to model the normal distribution:
  1. Standard Deviation Calculation: We calculate the standard deviation 'std_dev' for each parameter to ensure that the range covers more than 90% of the distribution, which corresponds to approximately two standard deviations for a normal distribution.
  2. Latin Hypercube Sampling: We generate Latin Hypercube samples for three parameters simultaneously to ensure the samples are properly correlated.
  3. Scaling Samples: We scale the samples using the 'norminv' function to transform the uniformly distributed Latin Hypercube samples into normally distributed samples with the specified standard deviations.
You can refer to the following references for more information on the functions used in the code above:
Hope this helps you with your work.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by