# Expectation(mean) of a discrete random variable

**URL:** <https://discourse.julialang.org/t/expectation-mean-of-a-discrete-random-variable/78359>\
**Category:** General Usage\
**Tags:** question, statistics\
**Created:** [March 23, 2022, 11:09pm UTC](https://discourse.julialang.org/t/expectation-mean-of-a-discrete-random-variable/78359 "2022-03-23T23:09:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [March 23, 2022, 11:09pm UTC](https://discourse.julialang.org/t/expectation-mean-of-a-discrete-random-variable/78359/1 "2022-03-23T23:09:53Z")

</div>

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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/e/deb332befd80f74ee84c1aefc1cd35501e8f11a0.png)

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?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 23, 2022, 11:24pm UTC](https://discourse.julialang.org/t/expectation-mean-of-a-discrete-random-variable/78359/2 "2022-03-23T23:24:51Z")

</div>

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)`.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [March 23, 2022, 11:39pm UTC](https://discourse.julialang.org/t/expectation-mean-of-a-discrete-random-variable/78359/4 "2022-03-23T23:39:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [March 23, 2022, 11:41pm UTC](https://discourse.julialang.org/t/expectation-mean-of-a-discrete-random-variable/78359/5 "2022-03-23T23:41:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [March 23, 2022, 11:42pm UTC](https://discourse.julialang.org/t/expectation-mean-of-a-discrete-random-variable/78359/6 "2022-03-23T23:42:29Z")

</div>

nice solution also

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 24, 2022, 12:25am UTC](https://discourse.julialang.org/t/expectation-mean-of-a-discrete-random-variable/78359/7 "2022-03-24T00:25:08Z")

</div>

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.
