def get_bernoulli(p, size):
    rv = bernoulli(p)
    x = np.arange(0, np.minimum(rv.dist.b, 3))
    h = plt.vlines(x, 0, rv.pmf(x), lw=2)
    prb = bernoulli.cdf(x, p)
    h = plt.semilogy(np.abs(x - bernoulli.ppf(prb, p)) + 1e-20)
    labels = bernoulli.rvs(p, size=size).tolist()
    return labels
Example #2
0
    def test_bernoulli_draw_list(self, n, mu):
        array_c = covid19.intArray(n)
        covid19.bernoulli_draw_list(array_c, n, mu)
        array_c = c_array_as_python_list(array_c, n)

        a = int(np.floor(mu))
        array_np = a + bernoulli.ppf(
            (np.arange(n) + 1) / (n + 1), mu - a, loc=0)

        np.testing.assert_equal(np.sort(array_c), array_np)
Example #3
0
    def test_bernoulli(self):
        fig, ax = plt.subplots(1, 1)

        p = 0.3
        mean, var, skew, kurt = bernoulli.stats(p, moments='mvsk')

        x = np.arange(bernoulli.ppf(0.01, p), bernoulli.ppf(0.99, p))

        ax.plot(x, bernoulli.pmf(x, p), 'bo', ms=8, label='bernoulli pmf')
        ax.vlines(x, 0, bernoulli.pmf(x, p), colors='b', lw=5, alpha=0.5)

        rv = bernoulli(p)

        ax.vlines(x,
                  0,
                  rv.pmf(x),
                  colors='k',
                  linestyles='-',
                  lw=1,
                  label='frozen pmf')
        ax.legend(loc='best', frameon=False)

        self.assertEqual("AxesSubplot(0.125,0.11;0.775x0.77)", str(ax))
Example #4
0
from scipy.stats import bernoulli
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1, 1)

# Calculate a few first moments:
p = 0.3
mean, var, skew, kurt = bernoulli.stats(p, moments='mvsk')

# Display the probability mass function (pmf):
x = np.arange(bernoulli.ppf(0.01, p),
              bernoulli.ppf(0.99, p))
ax.plot(x, bernoulli.pmf(x, p), 'bo', ms=8, label='bernoulli pmf')
ax.vlines(x, 0, bernoulli.pmf(x, p), colors='b', lw=5, alpha=0.5)

# Freeze the distribution and display the frozen pmf:
rv = bernoulli(p)
ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1,
        label='frozen pmf')
ax.legend(loc='best', frameon=False)
plt.show()

# Check accuracy of cdf and ppf:
prob = bernoulli.cdf(x, p)
np.allclose(x, bernoulli.ppf(prob, p))

# Generate random numbers:
r = bernoulli.rvs(p, size=1000)
Example #5
0
File: td1.py Project: linkzl/insa
ax.hist(r, normed=True, histtype='stepfilled', alpha=0.2)
ax.legend(loc='best', frameon=False)




# ============================================= #
# ================== BERNOULLI ================ #
# ============================================= #
fig, ax = plt.subplots(1, 1)

p = 0.3
mean, var, skew, kurt = bernoulli.stats(p, moments='mvsk')

x = np.arange(bernoulli.ppf(0.01, p), bernoulli.ppf(0.99, p))
ax.plot(x, bernoulli.pmf(x, p), 'bo', ms=8, label='bernoulli pmf')
ax.vlines(x, 0, bernoulli.pmf(x, p), colors='b', lw=5, alpha=0.5)

rv = bernoulli(p)
ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf')
ax.legend(loc='best', frameon=False)




# ============================================= #
# ================== BINOMIALE ================ #
# ============================================= #
fig, ax = plt.subplots(1, 1)
Example #6
0
# In[1]:

#Import Common Libraries
import numpy as np
from matplotlib import pyplot as plt
plt.style.use('seaborn')
import seaborn as sns

# ### Bernoulli Distribution

# In[2]:

#Bernoulli Distribution
from scipy.stats import bernoulli
p = 0.7
x = np.arange(bernoulli.ppf(0.01, p), bernoulli.ppf(
    0.99, p))  #Percent Point Function (inverse of cdf — percentiles)

print("Mean              : ", bernoulli.stats(p, moments='m'))
print("Variance          : ", bernoulli.stats(p, moments='v'))
print("Prob. Mass Func.  : ", bernoulli.pmf(x, p).item())
print("Cum. Density Func.: ", bernoulli.cdf(x, p).item())

fig = plt.figure(figsize=(20, 10))
plt.subplot(221)
plt.plot(x, bernoulli.pmf(x, p), 'ro', ms=8, label='PMF=(1-p)')
plt.plot(1 - x, 1 - bernoulli.pmf(x, p), 'go', ms=8, label='PMF=p')
plt.vlines(x, 0, bernoulli.pmf(x, p), colors='r', lw=5, alpha=0.5)
plt.vlines(1 - x, 0, 1 - bernoulli.pmf(x, p), colors='g', lw=5, alpha=0.5)
plt.xlabel("Sample Space of Bernoulli Distribution", fontsize=14)
plt.ylabel("PMF", fontsize=14)