I want know how can I calculate variance & expectation in Matlab for 10 sample

6 Ansichten (letzte 30 Tage)
how can I calculate variance & expectation in Matlab for 10 sample for random variable (x)
pmf of X is :
s=(-1:0.5:1);
p=[0.5*ones(1,1);0.25*ones(1,1);1/8*ones(1,1);1/16*ones(2,1)];

Akzeptierte Antwort

John D'Errico
John D'Errico am 27 Okt. 2022
Bearbeitet: John D'Errico am 27 Okt. 2022
It would seem your question defines a discrete random variable on the set s, where you have given the probability at each point.
Now you want to compute the mean and variance of what? Are you asking to compute the population mean and variance? (That is implied when you use the word EXPECTATION.
Or are you asking how to compute the mean and variance of a sample, of size 10? These are subtly different things, but are often confused. But you seem to be mixing up your question in a way that says you are not aware of the difference.
s=(-1:0.5:1);
p=[0.5*ones(1,1);0.25*ones(1,1);1/8*ones(1,1);1/16*ones(2,1)];
bar(s,p)
Given that set of weights for a discrete random sample, you could now use randsample to generate a random set of 10 deviates from that distribution. And then you could compute the mean and variance, OF THAT SAMPLE.
X = randsample(s,10,true,p)
X = 1×10
0.5000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 0 -1.0000 -0.5000 0.5000
mean(X)
ans = -0.5500
var(X)
ans = 0.4139
Of course, that is not the same thing as the population mean and variance, since the sampel mean and variance will checnge every time you create a new sample. Since your weights are already unit normalized (checking that claim)
sum(p)
ans = 1
Then the population mean is simply
PopMean = dot(s,p)
PopMean = -0.5313
And the population variance is as easily written as:
PopVar = dot((s - PopMean).^2,p)
PopVar = 0.3584
  1 Kommentar
S.M
S.M am 27 Okt. 2022
Yes, that's what I meant, thank you. I just had one question: I use the finitepmf code to get PMF, but it gives me an error. Is there another command? To get the pmf and plot it?
y=finitepmf(s,p,X);
eror :Unrecognized function or variable 'finitepmf'.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by