What use of 'sum' in this line code?

Ben Bacarisse ben.usenet at bsb.me.uk
Sun Jan 3 19:48:29 EST 2016


Robert <rxjwg98 at gmail.com> writes:

> I find below code snippet on line:
>
> //////////
> m = 10
> theta_A = 0.8
> theta_B = 0.3
> theta_0 = [theta_A, theta_B]
>
> coin_A = bernoulli(theta_A)
> coin_B = bernoulli(theta_B)
>
> xs = map(sum, [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m),
> coin_A.rvs(m), coin_B.rvs(m)])
> /////////
>
> I see 
> [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m), coin_B.rvs(m)]
>
> is simply a list, but I don't know what use of 'sum' in this line.
> I replace the random number with a simple list:
> ///////
> yy=map(sum, [13, 22, 33, 41])
>
> In [24]: yy
> Out[24]: [13, 22, 33, 41]
> ///////
>
> I don't see 'sum' has any effect above.

map applies the first argument (sum) to the elements of the second.  You
won't see any effect unless these elements are sequences that can be
summed.  For example:

  map(sum, [[1, 2], [2, 3], [3, 4]])

bernoulli(theta_A) is a statistical distribution, with the parameter
frozen in ready to have samples drawn from it.  coin_A.rvs(10) requests
10 random variates from the distribution -- it's an array containing ten
0/1 elements.  Thus the map(sum, ...) call does have an array or array
to work with.

[Not being a Python expect I've probably got some of the terminology
wrong but I hope the gist of it clear.]

<snip>
-- 
Ben.



More information about the Python-list mailing list