Clear Filters
Clear Filters

Multiply two probability plots (CDF/PDF)

77 views (last 30 days)
Deepayan Bhadra
Deepayan Bhadra on 19 Sep 2024 at 14:38
Edited: Umar on 27 Sep 2024 at 4:23
I have two probability plots, one generated as a CDF, and one as a pdf. The exact mathematics is not important for my purpose, I only want to extract the qualitative idea.
This is the code I used:
figure()
ax1 = subplot(1,1,1);
cdfplot(temp);
% plot(DE,y)
ax1.XDir = 'reverse';
set(gca, 'YScale', 'log')
figure();
pd_HOLT = fitdist(total_HOLT,'Normal');
DE_HOLT = bingroups_HOLT;
y_HOLT = pdf(pd_HOLT,DE_HOLT);
ax1 = subplot(1,1,1);
plot(DE_HOLT,y_HOLT)
ax1.XDir = 'reverse';
set(gca, 'YScale', 'log')
The x-axis is the same. How can I multiply these plots to convey a (qualitative) idea? Thanks.
  4 Comments
Deepayan Bhadra
Deepayan Bhadra on 20 Sep 2024 at 8:09
Hi @Malay Agarwal, I have uploaded the data (note: changed A.var7 -> temp)
Deepayan Bhadra
Deepayan Bhadra on 20 Sep 2024 at 8:10
Hi @Jeff Miller: Since I am trying to do a point-wise multiplication, the idea I am trying to convey is about the (combined) decreasing trend, as we proceed towards -100

Sign in to comment.

Accepted Answer

Umar
Umar on 20 Sep 2024 at 3:24

Hi @Deepayan Bhadra,

You mentioned, “How can I multiply these plots to convey a (qualitative) idea? “

Please see my response to your comments below.

My suggestion to achieve your goal of combining these two plots while maintaining clarity would be overlaying them in a single figure. This would allow you to visualize both the cumulative probabilities and the density of occurrences simultaneously. After analyzing your code, here is example code snippet to help you out.

% Generate some generic data for demonstration
data = randn(1000, 1); % Normal distributed data
% Create CDF plot
figure();
ax1 = subplot(1,1,1);
cdfplot(data);
hold on;
% Fit a normal distribution to the data
pd = fitdist(data,'Normal');
% Generate x values for PDF
x_values = linspace(min(data), max(data), 100);
y_values = pdf(pd, x_values);
% Plot PDF on the same axes
plot(x_values, y_values, 'r-', 'LineWidth', 2);
% Reverse x-axis and set log scale for y-axis
ax1.XDir = 'reverse';
set(gca, 'YScale', 'log');
% Add legends and labels
legend('CDF', 'PDF');
xlabel('Value');
ylabel('Probability');
title('Combined CDF and PDF Plot');
hold off;

So, in above example code, I used randn(1000, 1) to create a sample dataset that follows a normal distribution. The cdfplot(data) function creates the cumulative distribution function plot. You will see that the example code fits a normal distribution to the data and calculates its PDF over a range of x-values. The hold on command allows you to overlay the PDF plot on top of the CDF plot in the same figure and the x-axis is reversed, for more information on this command, please refer to

https://www.mathworks.com/help/matlab/ref/hold.html#

and a logarithmic scale is applied to the y-axis for better visibility of both plots. Make sure when you visualize both plots together, see how the probability density (PDF) at each point contributes to the cumulative probability (CDF). This dual representation will help you understanding both local behavior (PDF) and global behavior (CDF) of your data distribution. Feel free to adjust line styles, colors, and markers according to your preferences for better visual distinction between CDF and PDF.

Please see attached.

If you have any further questions, please let me know.

  11 Comments
Deepayan Bhadra
Deepayan Bhadra on 25 Sep 2024 at 14:55
Edited: Deepayan Bhadra on 25 Sep 2024 at 14:56
@Umar: A follow up question: If you look at the vectors total_HOLT and bingroups_HOLT in the data, how can I plot a simple discrete probability?
>> a = [70,23,3,0,0,0,0,0,0,0];
>> b = [-90,-80,-70,-60,-50,-40,-30,-20,-10,0];
For example, I want to show that if we have a 'b' value somewhere between (-90,-70), then we have a high probability dictated by corresponding values in 'a' but a negligible probability otherwise for other values in 'b'
Umar
Umar on 26 Sep 2024 at 1:38
Edited: Umar on 27 Sep 2024 at 4:23

Hi @ Deepayan Bhadra,

After reviewing your comments second time, I do apologize for misunderstanding your comments. I have edited comments in above post. Your goal is to visualize how the values in vector a correspond to the ranges of values in vector b, specifically showing high probabilities within a certain range of b. Also, you want to represent discrete probabilities derived from two vectors, where:

  • Vector a contains probability values.
  • Vector b defines corresponding ranges.

In your given example, you are interested in showing that a value in vector b between -90 and -70 corresponds to a high probability from vector a. You already defined the data by representing them in two vectors:

   a = [70, 23, 3, 0, 0, 0, 0, 0, 0, 0];
   b = [-90, -80, -70, -60, -50, -40, -30, -20, -10, 0];

It will be helpful to normalize your probabilities so that they sum to 1 if you are treating them as probabilities. In this case:

     total_probability = sum(a);
     normalized_a = a / total_probability; % Normalize 'a'

Then use a bar plot to represent the probabilities clearly. Here’s how you can implement this in MATLAB:

   % Create the bar plot for probabilities
   figure();
   bar(b, normalized_a); % Use 'b' for x-axis and normalized    probabilities for 
   y-axis
   xlabel('Value (b)');
   ylabel('Probability');
   title('Discrete Probability Distribution');
   % Highlight the range of interest
   hold on;
   xline(-90,'r--','Start    Range','LabelHorizontalAlignment','left');
   xline(-70,'g--','End    Range','LabelHorizontalAlignment','right');
   % Customize y-axis limits if necessary
   ylim([0 max(normalized_a) * 1.1]);
   % Add grid for better visibility
   grid on;
   hold off;

As you can see in example code snippet, the bar function creates a bar plot where each bar represents the probability associated with each value in vector b. The xline function is used to add vertical lines at -90 and -70 to visually indicate the range of interest and normalization makes sure that your representation is consistent with probability principles. Finally, grid enhances readability by allowing viewers to gauge values more easily. This visualization will clearly show that values in vector a corresponding to b values between -90 and -70 are significantly higher than those outside this range. This can be crucial for interpreting data distributions or making decisions based on these probabilities.

Hope this answers your question. If you have further questions or need additional modifications, feel free to ask!

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!