Clear Filters
Clear Filters

How can I plot the probability density function of this generated cumulative distribution function curve?

12 views (last 30 days)
I am writing code to generate a cumulative distribution function (CDF) curve. Now, I want to generate a probability density function (PDF) curve. Please help me create the PDF curve.
clc; clear all; close all;
m = 10^5;
A = 22;
A_T = 10^(A/10);
B = 5;
B_E = 10^(B/10);
exp = 3.5;
dt = 2;
t = dt^exp;
h = exprnd(1/t, [1, m]);
ee = 10^(110/10);
R1 = 4;
R2 = 5;
g_R = (2^R2) - 1;
g_E = (2^(R2-R1)) - 1;
n_2 = 1;
g_d = g_E / (1 + g_E);
x2= 0:1:100;
b = 1 + ((t/ee)*(B_E/A_T));
a = g_d/g_R;
for r = 1:length(x2)
c11= 0;
for it = 1:m
if 1/(a * ( b + (n_2 / ( A_T * h(1, it) ) ) ) ) < x2(r)
c11 = c11+1;
end
end
p1(r) = c11/m;
end
grid on
plot(x2,p1,'r');

Accepted Answer

Shubham
Shubham on 26 Aug 2024
Hi Abhishek,
To generate a Probability Density Function (PDF) from a given Cumulative Distribution Function (CDF), you can take the derivative of the CDF. In discrete terms, this is often approximated by calculating the difference between consecutive values of the CDF and dividing by the difference in the corresponding x values.
Let's modify your MATLAB code to compute and plot the PDF based on the CDF you've already calculated:
clc; clear all; close all;
% Parameters
m = 10^5;
A = 22;
A_T = 10^(A/10);
B = 5;
B_E = 10^(B/10);
exp = 3.5;
dt = 2;
t = dt^exp;
h = exprnd(1/t, [1, m]);
ee = 10^(110/10);
R1 = 4;
R2 = 5;
g_R = (2^R2) - 1;
g_E = (2^(R2-R1)) - 1;
n_2 = 1;
g_d = g_E / (1 + g_E);
x2 = 0:1:100;
b = 1 + ((t/ee)*(B_E/A_T));
a = g_d/g_R;
% Calculate CDF
p1 = zeros(1, length(x2));
for r = 1:length(x2)
c11 = 0;
for it = 1:m
if 1/(a * ( b + (n_2 / ( A_T * h(1, it) ) ) ) ) < x2(r)
c11 = c11 + 1;
end
end
p1(r) = c11 / m;
end
% Calculate PDF from CDF
pdf = diff(p1) ./ diff(x2);
% Plot CDF
figure;
subplot(2, 1, 1);
plot(x2, p1, 'r');
title('Cumulative Distribution Function (CDF)');
xlabel('x');
ylabel('F(x)');
grid on;
% Plot PDF
subplot(2, 1, 2);
plot(x2(1:end-1), pdf, 'b'); % Note: PDF is one element shorter
title('Probability Density Function (PDF)');
xlabel('x');
ylabel('f(x)');
grid on;
Explanation
  • CDF Calculation: The code calculates the CDF by counting the number of samples that fall below each value in x2 and normalizing by the total number of samples m.
  • PDF Calculation: The PDF is calculated by taking the difference between consecutive CDF values (diff(p1)) and dividing by the difference in the corresponding x values (diff(x2)). This gives an approximation of the derivative of the CDF.
  • Plotting: The CDF and PDF are plotted in two separate subplots for comparison.
This approach gives you a basic way to derive and visualize the PDF from the CDF using numerical differentiation.

More Answers (1)

Sam Chak
Sam Chak on 26 Aug 2024
Add these lines at the end of the script to visualize the estimated PDF using the gradient() method.
cdf = p1;
pdf = gradient(p1)./gradient(x2);
pdf = smoothdata(pdf, "sgolay", 5);
plot(x2, cdf), hold on
plot(x2, pdf), grid on
xlabel('x')
legend('CDF', 'PDF', 'location', 'east'), ylim([-0.2, 1.2])

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!