1
$\begingroup$

I have a huge list with 1000 elements. Manually entering and calculating SUM of probabilities it would take a few hours.

Example:

 list = {1, 2, 3, 3, 4, 3}

Calculating probabilities:

Counts[list]/Length[list]
<|1 -> 1/6, 2 -> 1/6, 3 -> 1/2, 4 -> 1/6|>

Calculating the sum of the list elements * probabilities

1*(1/6) + 2*(1/6) + 3*(1/2) + 3*(1/2) + 4*(1/6) + 3*(1/2)= 17/3

Could you give me some tips please?

$\endgroup$
1
$\begingroup$

Updated as I miss read the question

list = {1, 2, 3, 3, 4, 3};
counts= Counts[list]
(* <|1 -> 1, 2 -> 1, 3 -> 3, 4 -> 1|> *)

Keys[counts]*vals gives you number of occurrences times the value of the occurrence. Then calculate the product with Times on making a pair with this value and its probability. Finally, Total the list of results from the multiplication.

With[{vals = Values[counts]},
 Total[Times[Sequence @@ #] & /@ 
   Transpose@{Keys[counts]*vals, vals/Length[list]}]
 ]

Hope this helps.

$\endgroup$
  • $\begingroup$ Yours answer is Wrong! Sum is 17/3 not 8/3...... $\endgroup$ – Mariusz Iwaniuk Apr 25 '15 at 15:13
  • $\begingroup$ @Mariusz Updated as I misread the question. $\endgroup$ – Edmund Apr 25 '15 at 17:21
  • $\begingroup$ Yours answer is correct and very speedly.:-) $\endgroup$ – Mariusz Iwaniuk Apr 25 '15 at 18:28
2
$\begingroup$

You can use Count and Dot to get 17/3 from your list:

list = {1, 2, 3, 3, 4, 3};
Dot[list, Count[list,#]/Length@list&/@list]
(* 17/3 *)
$\endgroup$
  • $\begingroup$ Yours answer is correct :-) $\endgroup$ – Mariusz Iwaniuk Apr 25 '15 at 18:28
1
$\begingroup$

It is easier to see what is happening if you use symbols rather than numbers.

list = {a, b, c, c, d, c};

prob = Counts[list]/Length[list];

The sum of the product of the elements and their corresponding probabilities is given by either the Dot product

elem = Union[list];

elem.prob /@ elem // Simplify

1/6 (a + b + 3 c + d)

or more simply the Mean

Mean[list]

1/6 (a + b + 3 c + d)

If you are going to include each individual element in the sum then the probability of each individual element is just 1/Length[list]

Total[list/Length[list]] // Simplify

1/6 (a + b + 3 c + d)

It is unclear what statistic your calculation represents or to what purpose it would be used.

$\endgroup$
  • $\begingroup$ .Yours answer is Wrong! Sum is 17/3 not 8/3. $\endgroup$ – Mariusz Iwaniuk Apr 25 '15 at 15:12
0
$\begingroup$

As @Bob said "it is unclear what statistic your calculation represents" ... but maybe you'll be interested in more explicit syntax (see also these docs):

Given your distribution :

list = {1, 2, 3, 3, 4, 3};

then for example, the probability to get 3 is :

Probability[x == 3, Distributed[x, list]]

1/2

Then what you ask for is :

list.(Probability[x == #, Distributed[x, list]] & /@ list)

17/3

but it looked like you were asking for :

Expectation[x, Distributed[x, list]]

8/3

$\endgroup$
  • $\begingroup$ I wanted to sum ​​it got it 17/3.English is not my native language. While translating I had something to mess with the question.Google Translator ;-) $\endgroup$ – Mariusz Iwaniuk May 1 '15 at 9:17

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.