6
$\begingroup$

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

$\endgroup$
3
  • 6
    $\begingroup$ Times @@@ list? $\endgroup$ – J. M.'s torpor Aug 30 '15 at 11:59
  • $\begingroup$ 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. $\endgroup$ – yohbs Aug 30 '15 at 12:06
  • 1
    $\begingroup$ Dot @@ Transpose[{{x1, y1}, {x2, y2}, ..., {xn, yn}}] ? $\endgroup$ – m_goldberg Aug 30 '15 at 12:21
5
$\begingroup$

Suppose your list is as follows

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

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

(* 5 *)
$\endgroup$
3
  • $\begingroup$ +1 Interesting use of Dot. It would be nice if you provided an explanation of the code. $\endgroup$ – DavidC Aug 30 '15 at 13:37
  • $\begingroup$ The denominator (Total[m[[All, 2]]]) yields the effective number of entities, and so division by this factor gives the expected value. $\endgroup$ – thils Aug 30 '15 at 13:42
  • 1
    $\begingroup$ Equivalent: Dot @@ MapAt[Normalize[#, Total]&, Transpose[m], 2] $\endgroup$ – J. M.'s torpor Aug 30 '15 at 14:19
5
$\begingroup$

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]

enter image description here

Equivalently,

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

(* True *)
$\endgroup$
0
3
$\begingroup$

Why not use the built-in MovingAverage directly?

enter image description here

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

enter image description here

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ The output should be the expected value $\endgroup$ – thils Aug 30 '15 at 13:05
  • 2
    $\begingroup$ This assumes (requires) that the weights have integer values $\endgroup$ – Bob Hanlon Aug 30 '15 at 13:35
  • $\begingroup$ Very good point. $\endgroup$ – DavidC Aug 30 '15 at 13:38
1
$\begingroup$

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

Given for example your data:

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]]

enter image description here

which is also more simply

Mean[myDist]
$\endgroup$
2
  • 1
    $\begingroup$ I would actually consider this the most idiomatic answer of all. :) $\endgroup$ – J. M.'s torpor Sep 1 '15 at 1:08
  • $\begingroup$ @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 ? ;)) $\endgroup$ – SquareOne Sep 1 '15 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.