Understanding the following line of code regarding Gaussian distribution.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello all, I am trying to plot PDF of Gaussian distribution and for that I came across the following code:
In this code, I understand every line except the one which is commented as "Not understood".
I also observed that if we omit this sentence then also code works properly.
Any help in this regard will be highly appreciated.
clc;
clear all;
close all;
% Number of samples
num_samples = 1000;
% Generate random samples from Gaussian distribution
mu_gaussian = 0; % Mean
sigma_gaussian = 1; % Standard deviation
samples_gaussian = mu_gaussian + sigma_gaussian * randn(num_samples, 1); % Not understood
x = linspace(-5, 5, 1000);
pdf_gaussian = (1 / (sigma_gaussian * sqrt(2*pi))) * exp(-(x - mu_gaussian).^2 / (2 * sigma_gaussian^2));
cdf_gaussian = 0.5 * (1 + erf((x - mu_gaussian) / (sigma_gaussian * sqrt(2))));
% Plotting PDFs
figure;
subplot(3, 1, 1);
plot(x, pdf_gaussian, 'b', 'LineWidth', 2);
title('Gaussian Distribution (PDF)');
xlabel('x');
ylabel('Probability Density');
2 Kommentare
Bruno Luong
am 3 Aug. 2023
Bearbeitet: Bruno Luong
am 3 Aug. 2023
Operations on a single normal variable
first bullet aX + b ...
Akzeptierte Antwort
Sam Chak
am 3 Aug. 2023
Hi @charu shree
This variable samples_gaussian is unused in plotting the pdf and cdf. The code runs without error.
% Number of samples
num_samples = 1000;
% Generate random samples from Gaussian distribution
mu_gaussian = 0; % Mean
sigma_gaussian = 1; % Standard deviation
samples_gaussian = mu_gaussian + sigma_gaussian*randn(num_samples, 1); % Not understood
x = linspace(-5, 5, 1000);
pdf_gaussian = (1/(sigma_gaussian*sqrt(2*pi)))*exp(- (x - mu_gaussian).^2/(2*sigma_gaussian^2));
cdf_gaussian = 0.5*(1 + erf((x - mu_gaussian)/(sigma_gaussian*sqrt(2))));
% Plotting
figure;
subplot(2, 1, 1);
plot(x, pdf_gaussian, 'b', 'LineWidth', 2); grid on
xlabel('x');
ylabel('PDF');
title('Probability Density');
subplot(2, 1, 2);
plot(x, cdf_gaussian, 'b', 'LineWidth', 2); grid on
xlabel('x');
ylabel('CDF');
title('Cumulative Density')
6 Kommentare
Sam Chak
am 3 Aug. 2023
Hi @charu shree
The "Not understood" line is actually applying a linear transformation to the random variable.
For more info:
You can visualize the generated random samples in this script:
% Number of samples
num_samples = 1000;
x = linspace(-5, 5, num_samples);
% Normal Distribution with Specific Mean and Variance
desired_mu = 0; % Mean
desired_sigma = 1; % Standard deviation
desired_pdf = (1/(desired_sigma*sqrt(2*pi)))*exp(- (x - desired_mu).^2/(2*desired_sigma^2));
plot(x, desired_pdf, 'LineWidth', 2); hold on
% Generate random samples according to the Linear Transformation of Random Variable
for j = 1:5
norm_samples = desired_mu + desired_sigma*randn(num_samples, 1);
actual_mu(j) = mean(norm_samples);
actual_sigma(j) = std(norm_samples);
pdf_gaussian = (1/(actual_sigma(j)*sqrt(2*pi)))*exp(- (x - actual_mu(j)).^2/(2*actual_sigma(j)^2));
plot(x, pdf_gaussian, '--')
hold on
end
grid on
xlabel('x');
ylabel('PDF');
title('Probability Density');
Weitere Antworten (1)
dpb
am 3 Aug. 2023
Bearbeitet: dpb
am 3 Aug. 2023
It does precisely what the comment ahead of it says it does -- generates a set of psuedo-random normally distributed values with mean mu_gaussian, std deviation sigma_gaussian. Since those are set to 0,1 in the given code, it's the same as what randn returns, but this lets one change the location and dispersion generally.
The code does the same thing whether it is/is not there because while the array was generated, it's never used for anything in the given code; the editor will point that out for you...unless it is used later, of course, but in that case then you would discover that if you were to comment it out and the code tried to reference it. So, one presumes it was "just one of those things" that the original coder had an idea of using/needing a sampled array and then changed mind/direction and never removed the superfluous definition. Or, maybe they never got done...we have no way to know.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!