Discrete and continuous random variables distribution

I want to plot normal Gaussian distribution

9 commentaires

randi() can be used to generate the random values. However I do not understand what you are plotting.
A coin toss is NOT Gaussian. However, the law of large numbers tells you that the sum of MANY coin tosses will be effectively the sum of many iid binomial random variables, which in aggregate, will be approximately and asymptotically normally distributed.
Sometimes for this purpose you want to generate a 2d array of random values 0 1, and sum them along one dimension and divide by the length of the dimension. There result is an experimental probability that the coin was in a particular state after that many tosses. The other dimension corresponds to repeating the experiment.
The OP searches for the PDF and CDF for a random variable which gives 0 with probability 0.5 and a number drawn from the standard normal distribution also with probability 0.5.
I do not agree. The question specifically mentions discrete random variables. That is not the same as "a random variable which gives 0 with probability 0.5".
Consider a symmetric beta distribtution over to . Then there is a value of α for which the peak PDF would be 0.5 at x = 0 -- but it would not be a discrete distribution.
Torsten
Torsten le 27 Fév 2022
Modifié(e) : Torsten le 27 Fév 2022
In my opinion, the OP searches for the PDF and CDF of a "mixture" random variable (i.e. a random variable that is neither discrete nor continuous). There will be a discontinuity at x=0 for the PDF as well as for the CDF. In all other points, it will be continuous.
Unfortunately the poster edited away the original question. It referred to coin tosses -- and coin tosses are discrete.
I believe they were being asked to simulate coin tosses and determine an imperical PDF and CDF.
Torsten
Torsten le 27 Fév 2022
Modifié(e) : Torsten le 27 Fév 2022
I only remember the OP's name: Mohamed Wnis.
The problem was that a random variable was defined to give 0 if the coin toss gave tail and to give a number randomly drawn from the standard normal distribution if the coin toss gave head.
Oh... maybe! The wording of it was rather confusing!

Connectez-vous pour commenter.

 Réponse acceptée

Torsten
Torsten le 26 Fév 2022
Modifié(e) : Torsten le 26 Fév 2022
n = 10000;
x = rand(n,1);
n1 = numel(x(x<=0.5));
n2 = numel(x(x>0.5));
y1 = randn(n1,1);
y2 = zeros(n2,1);
y = [y1;y2];
histogram(y,'Normalization','pdf')
hold on
cdfplot(y)

5 commentaires

thank you for your efforts, can you clearify the steps of the code please and i also need plot of the PDF.
Torsten
Torsten le 26 Fév 2022
Modifié(e) : Torsten le 26 Fév 2022
I can't use MATLAB at the moment, but there should be two curves in the plot: PDF and CDF.
I think the code should be self-explaining.
thank you and I apricate your cooperation but could you clarify n1 = numel(x(x<=0.5)) meaning please ?
Torsten
Torsten le 26 Fév 2022
Modifié(e) : Torsten le 26 Fév 2022
n coin tosses are simulated by producing n uniformly distributed random numbers over [0:1]
(x = rand(n,1)).
We assume that the i-th coin toss gave head if x(i) <= 0.5, else if x(i) > 0.5, it gave tail.
We count the number of heads (n1 = numel(x(x<=0.5))) and the number of tails (n2 = numel(x(x>0.5))).
For the n1 heads, we generate random numbers drawn from the standard normal distribution.
For the n2 tails, we set the outcome to 0.
Now we mix the random numbers (y=[y1;y2]) and compute empirical PDF (histogram(...)) and CDF (cdfplot(...)) of the mixed distribution.
And now it's up to you to compute the explicit formulae for the theoretical PDF and CDF :-)
n1 = numel(x(x<=0.5));
can also be written
n1 = nnz(x<=0.5);

Connectez-vous pour commenter.

Plus de réponses (2)

"tosses a coin and the result is head, and if the result is tail plot 0 in that point,"
Here's a start
numTosses = 30;
tosses = randi([0,1], numTosses, 1);
bar(tosses);
grid on;
xlabel('Toss Number');

2 commentaires

thank you for your help, but i need to plot normal gaussian distribution random variable if the result is head, means the plot represnts mixed random variable the first one is discrete (tossing the coin) and the second is continous (normal gaussian random varriable), could you help me with this?
Looks like you're all set now since you accepted the other answer.

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 27 Fév 2022

0 votes

For this purpose you want to generate a 2d array of random values 0 1, and sum them along one dimension and divide by the length of the dimension. There result is an experimental probability that the coin was in a particular state after that many tosses. The other dimension corresponds to repeating the experiment. Histogram the experimental probabilities to get pdf. cumsum the pdf to get the cdf.

Community Treasure Hunt

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

Start Hunting!

Translated by