# Expectation(mean) of a discrete random variable

Hello, good evening. I’am reading a book about statistics and got the equation the expectation or mean of a discrete random variable as:

by the book the result is 1.25
But in package Statistics or Distributions, the mean function calcs the sum(vector)/length(vector)
0,33333

Are that the correct mean for calc expectations of discrete random variables?

The `mean`, in the most general sense, is equivalent to `sum(vector)/length(vector)`. Trying to use `mean` directly to compute expectations of discrete random variables makes no sense. You have two vectors, not one, in this case, vector `x` and vector `p_x`. What you want is `sum(x .* p_x)`.

3 Likes

Yep, was a bit confusing by the name mean. But I understood is sum(x * y).

Just do `dot(x,px)` and you get what you want.

1 Like

nice solution also

The name mean makes sense. The mean of a single vector is also the expectation of discrete random variables if each element of the vector has the same chance to picked, i.e., `sum(x .* (1/length(x)) == sum(x .* p_x)` when `p_x = 1/length(x)`. So, basically, `Base.mean` gives you what you want for this specific case, but if the probability distribution is custom, then you need to do it yourself.

1 Like