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?

=================

=================

4 Answers

4

=================

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.

Yours answer is Wrong! Sum is 17/3 not 8/3……

– Mariusz Iwaniuk

Apr 25 ’15 at 15:13

@Mariusz Updated as I misread the question.

– Edmund

Apr 25 ’15 at 17:21

Yours answer is correct and very speedly.:-)

– Mariusz Iwaniuk

Apr 25 ’15 at 18:28

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 *)

Yours answer is correct 🙂

– Mariusz Iwaniuk

Apr 25 ’15 at 18:28

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.

.Yours answer is Wrong! Sum is 17/3 not 8/3.

– Mariusz Iwaniuk

Apr 25 ’15 at 15:12

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

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 😉

– Mariusz Iwaniuk

May 1 ’15 at 9:17