def poisson_mode_and_alpha(expected, alpha):
    """
    Returns mode number of expected substitutions and upper & lower limits
    within which falls `alpha` fraction of the occurrences for a poisson
    distribution with lambda=`expected` value.

    Parameters
    ----------
    expected : float
        Expected substitutions.
    alpha : float
        Probability interval containing alpha fraction of the Poisson
        distribution.

    Returns
    -------
    mode : int
        Typical number of substitutions expected.
    lower_limit, upper_limit : int
        Lower number of substitutions between which alpha fraction of the
        Poisson distribution is contained.
    upper_limit : int
        Lower number of substitutions between which alpha fraction of the
        Poisson distribution is contained.
    """
    mean, var = poisson.stats(expected, moments='mv')
    mode = math.floor(mean)  # round down to closest integer
    lower_limit, upper_limit = poisson.interval(alpha, expected, loc=0)

    return mode, lower_limit, upper_limit
Ejemplo n.º 2
0
 def test_poisson(self):
     mu = 0.6
     mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')
     self.assertEqual(mean, 0.6)
     self.assertEqual(var, 0.6)
     self.assertEqual(skew, 1.2909944487358056)
     self.assertEqual(kurt, 1.6666666666666667)
     n = np.array([0., 1., 2.])
     x = np.arange(poisson.ppf(0.01, mu), poisson.ppf(0.99, mu))
     self.assertTrue(np.array_equal(n, x))
def create_poisson_distribution():
    fig, ax = plt.subplots(1, 1)
    mu = 0.6
    mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')
    x = np.arange(poisson.ppf(0.01, mu), poisson.ppf(0.99, mu))
    ax.plot(x, poisson.pmf(x, mu), 'bo', ms=8, label='poisson pmf')
    ax.vlines(x, 0, poisson.pmf(x, mu), colors='b', lw=5, alpha=0.5)
    rv = poisson(mu)
    ax.vlines(x,
              0,
              rv.pmf(x),
              colors='k',
              linestyles='-',
              lw=1,
              label='frozen pmf')
    ax.legend(loc='best', frameon=False)
    interactive(False)
    plt.show()
Ejemplo n.º 4
0
    def get_mean_kurt_variance(self):
        """
            Function to get the mean of the data set.

            Args:
                None

            Returns:
                float: the mean value,
                float: the kurtosis value.
        """

        mean, variance, skew, kurt = poisson.stats(mu, moments='mvsk')

        self.mean = mean
        self.variance = variance
        self.kurt = kurt

        return mean, kurt, variance
def plot_poison_pmf(mean_calculated, title):
    #mu - calculate (of counts) + sum and divide by length.
    fig, ax = plt.subplots(1, 1)
    mu = mean_calculated
    mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')
    x = np.arange(poisson.ppf(0.01, mu), poisson.ppf(0.99, mu))
    ax.plot(x, poisson.pmf(x, mu), 'bo', ms=8, label='poisson pmf')
    ax.vlines(x, 0, poisson.pmf(x, mu), colors='b', lw=5, alpha=0.5)
    rv = poisson(mu)
    ax.vlines(x,
              0,
              rv.pmf(x),
              colors='k',
              linestyles='-',
              lw=1,
              label='frozen pmf')
    ax.legend(loc='best', frameon=False)
    plt.title(title)
    interactive(False)
    plt.show()
Ejemplo n.º 6
0
def testPoisson():  # {{{
    """
    Poisson Distribution (泊松分布)
    泊松分布的例子:已知某路口发生事故的比率是每天2次,那么在此处一天内发生n次事故的概率是多少?
    """
    # 准备数据: 已知lam:单位时间内发生某件事的平均次数
    # X轴: 单位时间内该事件发生的次数
    # Y轴: 次数对应的概率
    lam = 10  # p = 1/lam
    xs = np.arange(poisson.ppf(0.01, mu=lam),
                   poisson.ppf(0.99, mu=lam),
                   step=1)

    # E(X) = lam, D(X) = lam
    mean, var, skew, kurt = poisson.stats(mu=lam, loc=0, moments='mvsk')
    print("mean: %.2f, var: %.2f, skew: %.2f, kurt: %.2f" %
          (mean, var, skew, kurt))

    fig, axs = plt.subplots(1, 3)

    # 显示pmf
    ys = poisson.pmf(xs, mu=lam)
    axs[0].plot(xs, ys, 'bo', markersize=5, label='poisson pmf')
    axs[0].legend()

    # 显示cdf
    ys = poisson.cdf(xs, mu=lam)
    axs[1].plot(xs, ys, 'bo', markersize=5, label='poisson cdf')
    axs[1].legend()

    # 随机变量RVS
    data = poisson.rvs(mu=lam, size=1000)
    import sys
    sys.path.append("../../thinkstats")
    import Pmf
    pmf = Pmf.MakePmfFromList(data)
    xs, ys = pmf.Render()
    axs[2].plot(xs, ys, 'bo', markersize=5, label='rvs pmf')
    axs[2].legend()

    plt.show()
Ejemplo n.º 7
0
    def test_poisson(self):
        from scipy.stats import poisson
        import matplotlib.pyplot as plt
        fig, ax = plt.subplots(1, 1)

        mu = 0.6
        mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')

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

        rv = poisson(mu)
        ax.vlines(x,
                  0,
                  rv.pmf(x),
                  colors='k',
                  linestyles='-',
                  lw=1,
                  label='frozen pmf')
        ax.legend(loc='best', frameon=False)
        self.assertEqual(str(ax), "AxesSubplot(0.125,0.11;0.775x0.77)")
Ejemplo n.º 8
0
 def poisson_distribution(*args):
     try:
         a = float(args[2])
     except (ValueError, TypeError):
         return {'is_valid': False}
     if not 0 < a < 1:
         return {'is_valid': False}
     t = Symbol('t')
     mean, var = poisson.stats(a)
     cf = exp(a * (exp(I * t) - 1))
     d_cf = diff(cf, t)
     dd_cf = diff(d_cf, t)
     mean2 = round(float(lambdify(t, -dd_cf)(0)), 2)
     return {
         'a': a,
         'mean': mean,
         'mean2': mean2,
         'var': var,
         'g': latex(nsimplify(cf, tolerance=0.1)),
         'g1': latex(nsimplify(d_cf, tolerance=0.1)),
         'g2': latex(nsimplify(dd_cf, tolerance=0.1)),
         'type': type,
         'is_valid': True,
     }
Ejemplo n.º 9
0
#from https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.stats.poisson.html#scipy.stats.poisson
from scipy.stats import poisson
import matplotlib.pyplot as plt
import numpy as np

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

mu = 0.6

mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')

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

rv = poisson(mu)
ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1,label='frozen pmf')
ax.legend(loc='best', frameon=False)
plt.show()
Ejemplo n.º 10
0
from scipy.stats import poisson
import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1)
mu = 5.0
mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')
x = np.arange(poisson.ppf(0.01, mu), poisson.ppf(0.99, mu))
ax.plot(x, poisson.pmf(x, mu), 'bo', ms=8, label='poisson pmf')
ax.vlines(x, 0, poisson.pmf(x, mu), colors='b', lw=5, alpha=0.5)
plt.show()
Ejemplo n.º 11
0
data = [pkt_segs, time_segs]

#print(poisson.pmf(test, mean))
#print((mean**test)*np.exp(-mean))#/np.math.factorial(int(np.round(test))))

#calculate mean of samples - max likelyhood estimator for poisson distribution
pp = PdfPages(
    '/home/francesco/Documents/Thesis_project/Results/Trace_packet_dist.pdf')
ks_res = np.zeros((len(pkt_segs), 2))
for i, metric in enumerate(data):

    for j, seg in enumerate(metric):
        fig, ax = plt.subplots(1, 1)
        psn_param = np.mean(seg)

        mean, var, skew, kurt = poisson.stats(psn_param, moments='mvsk')
        #fig, ax = plt.subplots(1, 1)

        ax.plot(np.sort(seg),
                poisson.pmf(np.sort(seg), psn_param),
                ms=8,
                label='trace seg: ' + str(j + 1) + ', ' + r'$\lambda$: ' +
                str(round(psn_param, 3)))
        n, x, _ = ax.hist(seg,
                          histtype='step',
                          density=True,
                          label='Emperical Distribution')
        density = stats.gaussian_kde(np.sort(seg))
        ax.plot(x, density(x), 'r--', label='Gaussian Kernel Smoothing ')

        if i == 0:
fig.suptitle("Distribución Binomial Negativa")
plt.show()

# DISTRIBUCIÓN POISSON

from scipy.stats import poisson

poisson.pmf(0, mu=3)

poisson.cdf(0, mu=3)

sum(poisson.pmf(range(0, 10), mu=3))

poisson.cdf(10, mu=3)

poisson.stats(mu=3, moments='mv')  # Tonteria

mu = 10  # mu = lambda
x = np.arange(poisson.ppf(0.01, mu), poisson.ppf(0.99, mu))
fig = plt.figure(figsize=(5, 2.7))
ax = fig.add_subplot(1, 2, 1)
ax.plot(x, poisson.pmf(x, mu), 'bo', ms=5, label='poisson pmf')
ax.vlines(x, 0, poisson.pmf(x, mu), colors='b', lw=2, alpha=0.5)
for tick in ax.xaxis.get_major_ticks():
    tick.label.set_fontsize(5)
for tick in ax.yaxis.get_major_ticks():
    tick.label.set_fontsize(5)
ax = fig.add_subplot(1, 2, 2)
ax.plot(x, poisson.cdf(x, mu), 'bo', ms=5, label='poisson cdf')
ax.vlines(x, 0, poisson.cdf(x, mu), colors='b', lw=2, alpha=0.5)
for tick in ax.xaxis.get_major_ticks():
Ejemplo n.º 13
0
# ちょうど7人来る確率を計算したいので、k=7です。
k = 7

# 確率質量関数をつかって確率を計算します。
prob = (lamb**k) * exp(-lamb) / factorial(k)

print(' 昼のピーク時にお客さんが7人である確率は、{:0.2f}%です。'.format(100 * prob))
"""
確率質量関数を手作りできました。scipyを使うともう少し楽です。
"""

# 平均は10です。
mu = 10

# 平均と分散を計算できます。
mean, var = poisson.stats(mu)

# 確率質量関数を使って、特定の確率を計算することも可能です。
odds_seven = poisson.pmf(7, mu)

print('ピーク時に7人の確率は{:0.2f}%'.format(odds_seven * 100))

print('平均={}'.format(mean))
"""
ピーク時に7人の確率は9.01%
平均=10.0
分布の全体を見ておくことにします。これは、10人よりお客が多い確率を求めるのに必要です。
"""

# 確率質量関数をプロットしてみましょう。
from scipy.stats import binom, geom, hypergeom, poisson, expon
import numpy as np

#Utilizamos una distribución de poisson

mu = 2

mean, var = poisson.stats(mu, moments = 'mv')
print("La esperanza es de: %f"%mean)
print("La varianza es de: %f"%var)

prob_0 = poisson.pmf(0,mu)
print("La probabilidad de que haya 0 accidentes es de un %f %s"%((round(prob_0*100,2),"%")))
Ejemplo n.º 15
0
plt.legend(loc='upper left', shadow=True)

plt.show()

# ### Poisson Distribution

# In[5]:

#Poisson Distribution
from scipy.stats import poisson

loc, mu = 0, 10  # Mu is basically Lambda
x = np.arange(poisson.ppf(0.01, mu, loc), poisson.ppf(
    0.99, mu, loc))  #Percent Point Function (inverse of cdf — percentiles)

print("Mean              : ", poisson.stats(mu, loc, moments='m'))
print("Variance          : ", poisson.stats(mu, loc, moments='v'))
print("Prob. Dens. Func. : ", poisson.pmf(x, mu, loc))
print("Cum. Density Func.: ", poisson.cdf(x, mu, loc))

CDF = poisson.cdf(x, mu, loc)

fig = plt.figure(figsize=(20, 10))
plt.subplot(221)
plt.plot(x, poisson.pmf(x, mu, loc), 'go', ms=8, label='PMF')
plt.vlines(x, 0, poisson.pmf(x, mu, loc), colors='g', lw=5, alpha=0.5)
plt.xlabel("Sample Space of Poisson Distribution", fontsize=14)
plt.ylabel("PMF", fontsize=14)
plt.title("Probability Distribution of Poisson(λ=10) Distribution",
          fontsize=16)
plt.xticks(np.arange(0, 20, 1))
Ejemplo n.º 16
0
def histogram(data, x_axis, y, name):
    # Create the regular raw data figure
    plt.clf()
    plt.bar(x_axis, y)
    plt.xlabel("Count")
    plt.ylabel("Frequency")
    plt.savefig(name + "_count_histo.png")
    plt.clf()

    # Graphing expected values from the poisson probability mass function
    fig, ax = plt.subplots(1, 1)
    # Caluclating first moments below
    mu = statistics.mean(data)  # Get the average from all the data
    print("average is ", mu)  # Print it
    mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')

    ax.plot(data, poisson.pmf(data, mu), 'bo', ms=8, label='poisson pmf')
    ax.vlines(data, 0, poisson.pmf(data, mu), colors='b', lw=5, alpha=0.5)
    plt.xlabel("Count")
    plt.ylabel("Probability")
    plt.savefig(name + "_poisson.png")
    plt.clf()

    poisson_pts = poisson.pmf(data, mu)
    print("Chi Square is for poisson before multiplication ",
          chisquare(poisson_pts))
    poisson_pts = [i * len(data) for i in poisson_pts]
    print("Chi Square is for poisson after multiplication ",
          chisquare(poisson_pts))

    #print("Poisson stuff is ", poisson_pts) # Printing poisson points we see that they match hand calculated numbers

    # Graph with histogram and poisson fitted values
    #plt.bar(x_axis,y, yerr = statistics.stdev(data))
    plt.hist(data)
    plt.plot(
        data,
        poisson_pts,
        marker='o',
        linestyle='',
        label='Fit result',
    )
    plt.legend()
    plt.xlabel("Count")
    plt.ylabel("Frequency")
    plt.savefig(name + "_poisson2.png")
    plt.clf()

    # Fit a normal distribution to the data:
    mu, std = norm.fit(data)  #Gets mean and standard_dev with scipy statistics
    plt.hist(data)  # create histogram from our data
    xmin, xmax = plt.xlim(
    )  # Set limits on x axis for gaussian distribution to be over
    x = np.linspace(
        xmin, xmax,
        100)  #Return evenly spaced numbers over a specified interval.
    p = norm.pdf(x, mu, std)
    print("Chi Square of the normal distribution before multiplication is ",
          chisquare(p))

    list = p
    list = [i * len(data) for i in p]
    print(
        "Chi Square of the normal distribution after multiplication by len(data) ",
        chisquare(list))

    #print("p is ", list) Output the gaussian distribution numbers
    plt.plot(x, list, 'k', linewidth=2)
    title = "Fit results: mu = %.2f,  std = %.2f" % (mu, std)
    plt.title(title)
    plt.xlabel("Count")
    plt.ylabel("Probability")

    plt.savefig(name + "_normal.png")
    plt.clf()

    # Now normal with poisson and bar graph
    #print(x)
    #print(y)
    plt.hist(data)
    plt.plot(data, poisson_pts, marker='o', linestyle='', label='Fit result')
    plt.plot(x, list, 'k', linewidth=2)
    plt.xlabel("Count")
    plt.ylabel("Frequency")
    plt.savefig(name + "normal_poisson.png")
#Generate four histogram plots, one for each data set, and display them all
#simultaneously

#The first plot will be in the top left
plt.figure(1)
plt.subplot(2,2,1) #(number of rows, number of colums, figure number)
count1, bins1, ignored1 = plt.hist(samples1, 12, facecolor = 'blue',
     normed=False)
#Label the axes and title the plot
plt.xlabel('Value')
plt.ylabel('Counts')
plt.title('N=10')

#Calculate the mean, mode, variance, skew, and kurtosis, then print them
mean1, var1, skew1, kurt1 = poisson.stats(5, moments='mvsk')
mode1 = np.bincount(samples1)
print'For N=10, the average value is ', samples1.mean(), ', the mode is ',\
    np.argmax(mode1), ', the variance is ', samples1.var(), ', the skew is ', skew1, \
    ', and the kurtosis is ', kurt1
print 


#The second plot will be on the top right
plt.subplot(2,2,2)
count2, bins2, ignored2 = plt.hist(samples2, 12, facecolor = 'green',
        normed = False)
#Label the axes and title the plot
plt.xlabel('Value')
plt.ylabel('Counts')
plt.title('N=100')