Weird behavior of random numbers in Python

275
December 14, 2017, at 04:16 AM

I stumbled upon some peculiar behavior of random numbers in Python , specifically I use the module numpy.random.

Consider the following expression:

n = 50
N = 1000
np.histogram(np.sum(np.random.randint(0, 2, size=(n, N)), axis=0), bins=n+1)[0]

In the limit of large N I would expect a binomial distribution (for the interested reader, this simulates the Ehrenfest model) and for large n a normal distribution. A typical output however, looks like this:

array([
1, 0, 0, 1, 0, 2, 0, 1, 0, 15, 0,
12, 0, 18, 0, 39, 0, 64, 0, 62, 0, 109,
0, 97, 0, 107, 0, 114, 0, 102, 0, 92, 0,
55, 0, 46, 0, 35, 0, 10, 0, 9, 0, 4,
0, 0, 0, 3, 0, 1, 1
])

With the statement from above, I can't explain the occurrence of the zeros in the histogram - am I missing something obvious here?

Answer 1

You're using histogram wrong. The bins aren't where you think they are. They don't go from 0 to 50; they go from the minimum input value to the maximum input value. The 0s represent bins that lie entirely between two integers.

Try it with numpy.bincount:

In [31]: n = 50
In [32]: N = 5000
In [33]: np.bincount(np.sum(np.random.randint(0, 2, size=(n, N)), axis=0))
Out[33]: 
array([  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   7,  13,  22,  46,  75, 126, 220, 305, 367, 461, 550, 578,
       517, 471, 438, 314, 189, 146,  76,  50,  17,   9,   2,   1])
READ ALSO
Python plot amount of x values in data

Python plot amount of x values in data

I have a huge csv file of data, it looks like this:

281
How to get data received in Flask request

How to get data received in Flask request

I want to be able to get the data sent to my Flask appI've tried accessing request

423
When setting the value of a dictionary to an index i + 1 in a list, item is set to the the item after the last occurance of the value i

When setting the value of a dictionary to an index i + 1 in a list, item is set to the the item after the last occurance of the value i

I am currently creating a markov chain-esque projectWhen I ran this code, I had expected for the dictionary to look as follows:

163
pandas splitting letter and number mix column based on pattern

pandas splitting letter and number mix column based on pattern

I have a sample database with one column:

267