# Calculating expected value from paired values and weights

I'm very new to Mathematica. I just need a quick solution on how to multiply x with y when a data set is given as

{{x1, y1}, {x2, y2}, ..., {xn, yn}}


where xi is the value and yi is the weight. So I'm trying to calculate the expected value. How do I do it in Mathematica?

Thanks all...."Flatten" wouldn't work in this case as it's a huge data set.....thils answer is quite helpful. It's not the prob/stats that I'm new to, but I have absolutely no idea about Mathematica.....

• Times @@@ list? Commented Aug 30, 2015 at 11:59
• Welcome to Mathematica StackExchange! I edited your question for better formatting. Please click the edit button to see how it's done, or visit the help center for more details. Commented Aug 30, 2015 at 12:06
• Dot @@ Transpose[{{x1, y1}, {x2, y2}, ..., {xn, yn}}] ? Commented Aug 30, 2015 at 12:21

Suppose your list is as follows

m = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}

Dot @@ Transpose[m]/Total[m[[All, 2]]]

(* 5 *)

• +1 Interesting use of Dot. It would be nice if you provided an explanation of the code. Commented Aug 30, 2015 at 13:37
• The denominator (Total[m[[All, 2]]]) yields the effective number of entities, and so division by this factor gives the expected value. Commented Aug 30, 2015 at 13:42
• Equivalent: Dot @@ MapAt[Normalize[#, Total]&, Transpose[m], 2] Commented Aug 30, 2015 at 14:19

Using symbolic values and descriptive names for clarity.

Unless the sum of the weights is unity you will have to rescale the weights to get the expected value.

n = 4;

data = Array[{x[#1], y[#1]} &, n];

{values, weights} = data // Transpose;

expValue = values.weights/Total[weights]


Equivalently,

expValue == Total[Times @@@ data]/Total[weights]

(* True *)


Why not use the built-in MovingAverage directly?

lst = {{x1, w1}, {x2, w2}, {x3, w3}, {x4, w4}};
MovingAverage @@ Transpose[lst]


If you are just learning about expected value (as I seem to be), the following may seem like a natural way to obtain it. However, as Bob Hanlon notes, it assumes that the weights are positive integers. This is imposed by Constant Array[a, b], which gives b copies of a.

Mean[Flatten[ConstantArray @@@ {{1, 2}, {3, 4}, {5, 6}, {7, 8}}]]

5


ConstantArray @@@ {{1, 2}, {3, 4}, {5, 6}, {7, 8}}] multiplies each value by its weight, giving:

{{1, 1}, {3, 3, 3, 3}, {5, 5, 5, 5, 5, 5}, {7, 7, 7, 7, 7, 7, 7, 7}}


which, when flattened is

{1, 1, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7}


The mean of this "reconstructed set" of data will be the expected value, 5.

• The output should be the expected value Commented Aug 30, 2015 at 13:05
• This assumes (requires) that the weights have integer values Commented Aug 30, 2015 at 13:35
• Very good point. Commented Aug 30, 2015 at 13:38

Just for curiosity, this is another way using higher level built-in "stats" functions :

xw = {{x1, w1}, {x2, w2}, {x3, w3}};


the corresponding probability distribution is:

myDist = EmpiricalDistribution[xw[[All, 2]] -> xw[[All, 1]]];


then you can compute

Expectation[hello, Distributed[hello, myDist]]


which is also more simply

Mean[myDist]

• I would actually consider this the most idiomatic answer of all. :) Commented Sep 1, 2015 at 1:08
• @Guesswhoitis. Actually I would consider this the less idiomatic answer as it is likely to be understood by the broadest range of people ! Isn't it ? ;)) Commented Sep 1, 2015 at 20:21