Cara menggunakan NP.RANDOM.BINOMIAL pada Python

Distribusi Binomial adalah Distribusi Diskrit.

Distribusi ini menggambarkan hasil dari skenario biner, misalnya melempar koin, yang akan menghasilkan kepala atau ekor.

Distribusi ini memiliki tiga parameter, yaitu:

n – jumlah percobaan.

p – kemungkinan terjadinya setiap percobaan (misalnya untuk melempar koin, peluang masing-masing 0,5).

size – Bentuk dari array yang dikembalikan.

Distribusi Diskrit: Distribusi yang ditentukan oleh rangkaian peristiwa terpisah, misalnya hasil lemparan koin adalah diskrit karena hanya menghasilkan kepala atau ekor sedangkan tinggi orang continuous seperti 170, 170.1, 170.11 dan seterusnya.

Contoh:
Berikan 10 percobaan untuk melempar koin dan menghasilkan 10 poin data

from numpy import random

x = random.binomial(n=10, p=0.5, size=10)

print(x)

Visualisasi Distribusi Binomial

Contoh:

from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot(random.binomial(n=10, p=0.5, size=1000), hist=True, kde=False)

plt.show()

Hasilnya:

Cara menggunakan NP.RANDOM.BINOMIAL pada Python

Perbedaan Antara Distribusi Normal dan Binomial

Perbedaan utamanya adalah distribusi normal kontinu sedangkan binomial diskrit, tetapi jika ada cukup titik pada distribusi binomial akan menghasilkan data yang sangat mirip dengan distribusi normal dengan lokasi dan skala tertentu.

Contoh:

from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot(random.normal(loc=50, scale=5, size=1000), hist=False, label='normal')
sns.distplot(random.binomial(n=100, p=0.5, size=1000), hist=False, label='binomial')

plt.show()

Hasilnya:

Cara menggunakan NP.RANDOM.BINOMIAL pada Python

random.binomial(n, p, size=None)#

Draw samples from a binomial distribution.

Samples are drawn from a binomial distribution with specified parameters, n trials and p probability of success where n an integer >= 0 and p is in the interval [0,1]. (n may be input as a float, but it is truncated to an integer in use)

Note

New code should use the binomial method of a default_rng() instance instead; please see the Quick Start.

Parametersnint or array_like of ints

Parameter of the distribution, >= 0. Floats are also accepted, but they will be truncated to integers.

pfloat or array_like of floats

Parameter of the distribution, >= 0 and <=1.

sizeint or tuple of ints, optional

Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. If size is None (default), a single value is returned if n and p are both scalars. Otherwise, np.broadcast(n, p).size samples are drawn.

Returnsoutndarray or scalar

Drawn samples from the parameterized binomial distribution, where each sample is equal to the number of successes over the n trials.

Notes

The probability density for the binomial distribution is

\[P(N) = \binom{n}{N}p^N(1-p)^{n-N},\]

where \(n\) is the number of trials, \(p\) is the probability of success, and \(N\) is the number of successes.

When estimating the standard error of a proportion in a population by using a random sample, the normal distribution works well unless the product p*n <=5, where p = population proportion estimate, and n = number of samples, in which case the binomial distribution is used instead. For example, a sample of 15 people shows 4 who are left handed, and 11 who are right handed. Then p = 4/15 = 27%. 0.27*15 = 4, so the binomial distribution should be used in this case.

References

1

Dalgaard, Peter, “Introductory Statistics with R”, Springer-Verlag, 2002.

2

Glantz, Stanton A. “Primer of Biostatistics.”, McGraw-Hill, Fifth Edition, 2002.

3

Lentner, Marvin, “Elementary Applied Statistics”, Bogden and Quigley, 1972.

4

Weisstein, Eric W. “Binomial Distribution.” From MathWorld–A Wolfram Web Resource. http://mathworld.wolfram.com/BinomialDistribution.html

5

Wikipedia, “Binomial distribution”, https://en.wikipedia.org/wiki/Binomial_distribution

Examples

Draw samples from the distribution:

>>> n, p = 10, .5  # number of trials, probability of each trial
>>> s = np.random.binomial(n, p, 1000)
# result of flipping a coin 10 times, tested 1000 times.

A real world example. A company drills 9 wild-cat oil exploration wells, each with an estimated probability of success of 0.1. All nine wells fail. What is the probability of that happening?

Let’s do 20,000 trials of the model, and count the number that generate zero positive results.

>>> sum(np.random.binomial(9, 0.1, 20000) == 0)/20000.
# answer = 0.38885, or 38%.