Пример #1
0
def showResults(challenger_data, model):
    ''' Show the original data, and the resulting logit-fit'''
    
    # First plot the original data
    plt.figure()
    sns.set_context('poster')
    np.set_printoptions(precision=3, suppress=True)
    
    plt.scatter(challenger_data[:, 0], challenger_data[:, 1], s=75, color="k",
                alpha=0.5)
    plt.yticks([0, 1])
    plt.ylabel("Damage Incident?")
    plt.xlabel("Outside temperature (Fahrenheit)")
    plt.title("Defects of the Space Shuttle O-Rings vs temperature")
    plt.xlim(50, 85)
    
    # Plot the fit
    x = np.arange(50, 85)
    alpha = model.params[0]
    beta = model.params[1]
    y = logistic(x, beta, alpha)
    
    plt.hold(True)
    plt.plot(x,y,'r')
    outFile = 'ChallengerPlain.png'
    mystyle.printout_plain(outFile, outDir='..\Images')
    plt.show()
Пример #2
0
def main():
    '''Demonstrate central limit theorem.'''
    # Generate data
    ndata = 1e5
    nbins = 50
    data = np.random.random(ndata)

    # Show them
    fig, axs = plt.subplots(1, 3)
    #mystyle.set(14)
    #sns.set_context('paper')
    #sns.set_style('whitegrid')

    axs[0].hist(data, bins=nbins)
    axs[0].set_title('Random data')
    axs[0].set_xticks([0, 0.5, 1])
    axs[0].set_ylabel('Counts')

    axs[1].hist(np.mean(data.reshape((ndata / 2, 2)), axis=1), bins=nbins)
    axs[1].set_xticks([0, 0.5, 1])
    axs[1].set_title(' Average over 2')

    axs[2].hist(np.mean(data.reshape((ndata / 10, 10)), axis=1), bins=nbins)
    axs[2].set_xticks([0, 0.5, 1])
    axs[2].set_title(' Average over 10')

    plt.tight_layout()
    mystyle.printout_plain('CentralLimitTheorem.png')

    plt.show()
Пример #3
0
def show_binomial():
    """Show an example of binomial distributions"""
    
    ns = [20,20,40]
    ps = [0.5, 0.7, 0.5]
    
    #markersize = 8
    for (p,n) in zip(ps, ns):
        bd = stats.binom(n,p)
        x = np.arange(n+1)
        plt.plot(x, bd.pmf(x), 'o--', label='p={0:3.1f}, n={1}'.format(p,n))
    
    plt.legend()
    #sns.set_context('poster')
    #sns.set_style('ticks')
    #mystyle.set(14)
    
    plt.title('Binomial distribuition')
    plt.xlabel('X')
    plt.ylabel('P(X)')
    #sns.despine()
    plt.annotate('Upper Limit', xy=(20,0), xytext=(27,0.04), 
                 arrowprops=dict(shrink=0.05))
    
    
    mystyle.printout_plain('Binomial_distribution_pmf.png')
    plt.show()
Пример #4
0
def showResults(challenger_data, model):
    ''' Show the original data, and the resulting logit-fit'''

    # First plot the original data
    plt.figure()
    sns.set_context('poster')
    np.set_printoptions(precision=3, suppress=True)

    plt.scatter(challenger_data[:, 0],
                challenger_data[:, 1],
                s=75,
                color="k",
                alpha=0.5)
    plt.yticks([0, 1])
    plt.ylabel("Damage Incident?")
    plt.xlabel("Outside temperature (Fahrenheit)")
    plt.title("Defects of the Space Shuttle O-Rings vs temperature")
    plt.xlim(50, 85)

    # Plot the fit
    x = np.arange(50, 85)
    alpha = model.params[0]
    beta = model.params[1]
    y = logistic(x, beta, alpha)

    plt.hold(True)
    plt.plot(x, y, 'r')
    outFile = 'ChallengerPlain.png'
    mystyle.printout_plain(outFile, outDir='..\Images')
    plt.show()
Пример #5
0
def show_binomial():
    """Show an example of binomial distributions"""

    bd1 = stats.binom(20, 0.5)
    bd2 = stats.binom(20, 0.7)
    bd3 = stats.binom(40, 0.5)

    k = np.arange(40)

    sns.set_context('paper')
    sns.set_style('ticks')
    mystyle.set(14)

    markersize = 8
    plt.plot(k, bd1.pmf(k), 'o-b', ms=markersize)
    plt.hold(True)
    plt.plot(k, bd2.pmf(k), 'd-r', ms=markersize)
    plt.plot(k, bd3.pmf(k), 's-g', ms=markersize)
    plt.title('Binomial distribuition')
    plt.legend(['p=0.5 and n=20', 'p=0.7 and n=20', 'p=0.5 and n=40'])
    plt.xlabel('X')
    plt.ylabel('P(X)')
    sns.despine()

    mystyle.printout_plain('Binomial_distribution_pmf.png')

    plt.show()
Пример #6
0
def show3D():
    # imports specific to the plots in this example
    import numpy as np
    from matplotlib import cm
    from mpl_toolkits.mplot3d.axes3d import get_test_data
    
    # Twice as wide as it is tall.
    fig = plt.figure(figsize=plt.figaspect(0.5))
    
    #---- First subplot
    ax = fig.add_subplot(1, 2, 1, projection='3d')
    X = np.arange(-5, 5, 0.1)
    Y = np.arange(-5, 5, 0.1)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet,
            linewidth=0, antialiased=False)
    ax.set_zlim3d(-1.01, 1.01)
    
    fig.colorbar(surf, shrink=0.5, aspect=10)
    
    #---- Second subplot
    ax = fig.add_subplot(1, 2, 2, projection='3d')
    X, Y, Z = get_test_data(0.05)
    ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

    mystyle.printout_plain('3dGraph.png')
Пример #7
0
def show_binomial():
    """Show an example of binomial distributions"""
    
    ns = [20,20,40]
    ps = [0.5, 0.7, 0.5]
    
    #markersize = 8
    for (p,n) in zip(ps, ns):
        bd = stats.binom(n,p)
        x = np.arange(n+1)
        plt.plot(x, bd.pmf(x), 'o--', label='p={0:3.1f}, n={1}'.format(p,n))
    
    plt.legend()
    #sns.set_context('poster')
    #sns.set_style('ticks')
    #mystyle.set(14)
    
    plt.title('Binomial distribuition')
    plt.xlabel('X')
    plt.ylabel('P(X)')
    #sns.despine()
    plt.annotate('Upper Limit', xy=(20,0), xytext=(27,0.04), 
                 arrowprops=dict(shrink=0.05))
    
    
    mystyle.printout_plain('Binomial_distribution_pmf.png')
    plt.show()
Пример #8
0
def show_poisson():
    """Show an example of poisson distributions"""

    lambdas = [1, 4, 10]

    k = np.arange(20)
    markersize = 8
    for par in lambdas:
        plt.plot(k,
                 stats.poisson.pmf(k, par),
                 'o-',
                 label='$\lambda={0}$'.format(par))

    plt.legend()
    #sns.set_context('poster')
    #sns.set_style('ticks')
    #mystyle.set(14)

    plt.title('Poisson distribuition')
    plt.xlabel('X')
    plt.ylabel('P(X)')
    #sns.despine()

    mystyle.printout_plain('Poisson_distribution_pmf.png')

    plt.show()
Пример #9
0
def show_binomial():
    """Show an example of binomial distributions"""
    
    bd1 = stats.binom(20, 0.5)
    bd2 = stats.binom(20, 0.7)
    bd3 = stats.binom(40, 0.5)
    
    k = np.arange(40)
    
    sns.set_context('paper')
    sns.set_style('ticks')
    mystyle.set(14)
    
    markersize = 8
    plt.plot(k, bd1.pmf(k), 'o-b', ms=markersize)
    plt.hold(True)
    plt.plot(k, bd2.pmf(k), 'd-r', ms=markersize)
    plt.plot(k, bd3.pmf(k), 's-g', ms=markersize)
    plt.title('Binomial distribuition')
    plt.legend(['p=0.5 and n=20', 'p=0.7 and n=20', 'p=0.5 and n=40'])
    plt.xlabel('X')
    plt.ylabel('P(X)')
    sns.despine()
    
    mystyle.printout_plain('Binomial_distribution_pmf.png')
    
    plt.show()
Пример #10
0
def main():
    '''Demonstrate central limit theorem.'''
    # Generate data
    ndata = 1e5
    nbins = 50
    data = np.random.random(ndata)
    
    # Show them
    fig, axs = plt.subplots(1,3)
    #mystyle.set(14)
    #sns.set_context('paper')
    #sns.set_style('whitegrid')
    
    axs[0].hist(data,bins=nbins)
    axs[0].set_title('Random data')
    axs[0].set_xticks([0, 0.5, 1])
    axs[0].set_ylabel('Counts')
    
    axs[1].hist( np.mean(data.reshape((ndata/2,2)),  axis=1), bins=nbins)
    axs[1].set_xticks([0, 0.5, 1])
    axs[1].set_title(' Average over 2')
    
    axs[2].hist( np.mean(data.reshape((ndata/10,10)),axis=1), bins=nbins)
    axs[2].set_xticks([0, 0.5, 1])
    axs[2].set_title(' Average over 10')
    
    plt.tight_layout()
    mystyle.printout_plain('CentralLimitTheorem.png')
    
    plt.show()    
Пример #11
0
def show3D():
    # imports specific to the plots in this example
    import numpy as np
    from matplotlib import cm
    from mpl_toolkits.mplot3d.axes3d import get_test_data

    # Twice as wide as it is tall.
    fig = plt.figure(figsize=plt.figaspect(0.5))

    #---- First subplot
    ax = fig.add_subplot(1, 2, 1, projection='3d')
    X = np.arange(-5, 5, 0.1)
    Y = np.arange(-5, 5, 0.1)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    surf = ax.plot_surface(X,
                           Y,
                           Z,
                           rstride=1,
                           cstride=1,
                           cmap=cm.jet,
                           linewidth=0,
                           antialiased=False)
    ax.set_zlim3d(-1.01, 1.01)

    fig.colorbar(surf, shrink=0.5, aspect=10)

    #---- Second subplot
    ax = fig.add_subplot(1, 2, 2, projection='3d')
    X, Y, Z = get_test_data(0.05)
    ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

    mystyle.printout_plain('3dGraph.png')
Пример #12
0
def scatterplot():
    import seaborn as sns
    sns.set()
    sns.set_context('poster')

    df = sns.load_dataset("iris")
    sns.pairplot(df, hue="species", size=2.5)
    mystyle.printout_plain('multiScatterplot.png')
Пример #13
0
def scatterplot():
    import seaborn as sns
    sns.set()
    sns.set_context('poster')
    
    df = sns.load_dataset("iris")
    sns.pairplot(df, hue="species", size=2.5)    
    mystyle.printout_plain('multiScatterplot.png')
Пример #14
0
def main():
    # Calculate the PDF-curves
    x = np.linspace(-10, 15, 201)
    nd1 = stats.norm(1, 2)
    nd2 = stats.norm(6, 2)
    y1 = nd1.pdf(x)
    y2 = nd2.pdf(x)

    # Axes locations
    ROC = {'left': 0.3, 'width': 0.45, 'bottom': 0.1, 'height': 0.45}

    PDF = {'left': 0.1, 'width': 0.8, 'bottom': 0.65, 'height': 0.3}

    rect_ROC = [ROC['left'], ROC['bottom'], ROC['width'], ROC['height']]
    rect_PDF = [PDF['left'], PDF['bottom'], PDF['width'], PDF['height']]

    fig = plt.figure()

    ax1 = plt.axes(rect_PDF)
    ax2 = plt.axes(rect_ROC)

    # Plot and label the PDF-curves
    ax1.plot(x, y1)
    ax1.hold(True)
    ax1.fill_between(x, 0, y1, where=x < 3, facecolor='#CCCCCC', alpha=0.5)
    ax1.annotate('Sensitivity',
                 xy=(x[75], y1[65]),
                 xytext=(x[40], y1[75] * 1.2),
                 fontsize=14,
                 horizontalalignment='center',
                 arrowprops=dict(facecolor='#CCCCCC'))

    ax1.plot(x, y2, '#888888')
    ax1.fill_between(x, 0, y2, where=x < 3, facecolor='#888888', alpha=0.5)
    ax1.annotate('1-Specificity',
                 xy=(2.5, 0.03),
                 xytext=(6, 0.05),
                 fontsize=14,
                 horizontalalignment='center',
                 arrowprops=dict(facecolor='#888888'))

    ax1.set_ylabel('PDF')

    # Plot the ROC-curve
    ax2.plot(nd2.cdf(x), nd1.cdf(x), 'k')
    ax2.hold(True)
    ax2.plot(np.array([0, 1]), np.array([0, 1]), 'k--')

    # Format the ROC-curve
    ax2.set_xlim([0, 1])
    ax2.set_ylim([0, 1])
    #ax2.axis('equal')
    ax2.set_title('ROC-Curve')
    ax2.set_xlabel('1-specificity')
    ax2.set_ylabel('sensitivity')

    # Show the plot, and create a figure
    mystyle.printout_plain('ROC.png')
Пример #15
0
def main():
    # generate the data
    x = np.arange(10)
    np.random.seed(10)
    y = 3*x+2+20*np.random.rand(len(x))
    
    # determine the line-fit
    k,d = np.polyfit(x,y,1)
    yfit = k*x+d
    
    # plot the data
    plt.scatter(x,y)
    plt.hold(True)
    plt.plot(x, yfit, 'r')
    for ii in range(len(x)):
        plt.plot([x[ii], x[ii]], [yfit[ii], y[ii]], 'k')
        
    plt.xlim((-0.1, 9.1))
    plt.xlabel('X')
    plt.ylabel('Y')
    
    mystyle.printout_plain('residuals.png') 
Пример #16
0
def show_poisson():
    """Show an example of poisson distributions"""
    
    lambdas = [1,4,10]
    
    k = np.arange(20)
    markersize = 8
    for par in lambdas:
        plt.plot(k, stats.poisson.pmf(k, par), 'o--', label='$\lambda={0}$'.format(par))
    
    plt.legend()
    #sns.set_context('poster')
    #sns.set_style('ticks')
    #mystyle.set(14)
    
    plt.title('Poisson distribuition')
    plt.xlabel('X')
    plt.ylabel('P(X)')
    #sns.despine()
    
    mystyle.printout_plain('Poisson_distribution_pmf.png')
    
    plt.show()
Пример #17
0
# additional packages
import mystyle


# Get the data, and fit the normal distribution
weight = np.array([2784, 2632, 2771, 2495, 2435, 2513, 2633, 2737, 2687, 2647], dtype=np.float32)
(md, sd) = stats.norm.fit(weight)
nd = stats.norm(md, sd)

# Plot the data
sns.set_context(context='poster')

x = np.linspace(2300, 3000)
y = nd.pdf(x)

checkVal = 2460
print('p = {0:5.3f}'.format(nd.cdf(checkVal)))

x1 = np.linspace(2300, checkVal)
y1 = nd.pdf(x1)

sns.rugplot(weight, height=0.0005)
plt.hold(True)
plt.plot(x,y)
plt.fill_between(x1, y1, alpha=0.3)

mystyle.printout_plain('pdf_checkMean.png')

plt.show()
Пример #18
0
# Arrange subplots
sns.set_context('paper')
sns.set_style('white')
mystyle.set(11)
fig, axs = plt.subplots(1, 2)

# Plot distribution
axs[0].plot(x, y)
axs[0].set_xlabel('X')
axs[0].set_ylabel('PDF(X)')
axs[0].set_title('chi2(x), k=3')
sns.set_style('white')

x0, x1 = axs[0].get_xlim()
y0, y1 = axs[0].get_ylim()
axs[0].set_aspect((x1 - x0) / (y1 - y0))
#sns.despine()

# Plot probplot
plt.axes(axs[1])
stats.probplot(data, plot=plt)

x0, x1 = axs[1].get_xlim()
y0, y1 = axs[1].get_ylim()
axs[1].set_aspect((x1 - x0) / (y1 - y0))
#sns.despine()

mystyle.printout_plain('chi2pp.png')

plt.show()
Пример #19
0
                                facecolor='black'))
    ax.annotate('',
                xy=(90, groupMean[1]),
                xytext=(90, groupMean[1] + 0.2),
                arrowprops=dict(arrowstyle='<->, head_width=0.1',
                                facecolor='black'))
    ax.text(210, (grandMean + groupMean[1]) / 2.,
            '$SS_{Treatment}$',
            fontsize=24)
    ax.text(90, groupMean[1] + 0.1, '$SS_{Error}$', ha='right', fontsize=24)


if __name__ == '__main__':
    centers = [5, 5.3, 4.7]
    colors = 'brg'

    #sns.set_context('paper')
    #sns.set_style('white')
    np.random.seed(123)
    mystyle.set(18)

    fig = plt.figure()
    ax = fig.add_subplot(111)
    std = 0.1
    numData = 100
    show_fig(0.1, ax, 'Sum-Squares')

    mystyle.printout_plain('anova_annotated.png')

    plt.show()
Пример #20
0
def show_fig(std, ax, title):
    '''Create plot of 3 different, normally distributed data groups'''
    for ii in range(3):
        data = stats.norm(centers[ii], std).rvs(numData)
        offset = ii*numData
        ax.plot( offset+np.arange(numData), data, '.', color=colors[ii], ms=10)
        
    ax.xaxis.set_ticks([50,150,250])
    ax.set_xticklabels(['Group1', 'Group2', 'Group3'])
    ax.set_title(title)
    sns.despine()

if __name__ == '__main__':
    centers = [5, 5.3, 4.7]
    colors = 'brg'
    
    sns.set_context('paper')
    sns.set_style('whitegrid')
    mystyle.set(14)
    
    fig, axs = plt.subplots(1, 2)
    stds = [0.1, 2]
    numData = 100
    show_fig(0.1, axs[0], 'SD=0.1')
    show_fig(2,   axs[1], 'SD=2.0')
    
    mystyle.printout_plain('anova_oneway.png')
    
    plt.show()

Пример #21
0
y1 = nd.pdf(x1)
y2 = nd.pdf(x2)
y3 = nd.pdf(x3)

sns.set(context='poster')
sns.set_style('ticks')
fig, axs = plt.subplots(1, 3, sharey=True)


def show_SD(axis, xi, yi, text):
    '''Show the area covered by 1/2/3 SDs'''

    fc = '#DDDDDD'
    axis.plot(x, y)
    axis.fill_between(xi, yi, facecolor=fc)

    axis.text(0, 0.05, text, horizontalalignment='center', fontsize=25)
    axis.set_xlim([-3.5, 3.5])
    axis.set_ylim([-0.0, 0.5])
    sns.despine(ax=axis, left=True)
    axis.set_yticks([])


show_SD(axs[0], x1, y1, '68.3%')
show_SD(axs[1], x2, y2, '95.4%')
show_SD(axs[2], x3, y3, '99.7%')

plt.tight_layout()

mystyle.printout_plain('area_SDs.png')
Пример #22
0
nd = stats.norm(md, sd)

# Plot the data
sns.set_context(context='poster')
sns.set_style('ticks')

limits = (md-3*sd, md+3*sd)
x = np.linspace(limits[0], limits[1])
y = nd.pdf(x)

checkVal = 2.6
print('p = {0:5.3f}'.format(nd.cdf(checkVal)))

x1 = np.linspace(limits[0], checkVal)
y1 = nd.pdf(x1)
x2 = np.linspace(md + (md-checkVal), limits[1])
y2 = nd.pdf(x2)

plt.plot(x,y)
plt.fill_between(x1, y1, alpha=0.5)
plt.fill_between(x2, y2, alpha=0.2)
plt.xlabel('Weight')
plt.ylabel('P(Weight)')
plt.text(2.1, 0.05, '11.8%', fontsize=20)

sns.despine()

mystyle.printout_plain('pdf_checkValue.png')

plt.show()
Пример #23
0
nd = stats.norm(md, sd)

# Plot the data
sns.set_context(context="poster")
sns.set_style("ticks")

limits = (md - 3 * sd, md + 3 * sd)
x = np.linspace(limits[0], limits[1])
y = nd.pdf(x)

checkVal = 2.6
print("p = {0:5.3f}".format(nd.cdf(checkVal)))

x1 = np.linspace(limits[0], checkVal)
y1 = nd.pdf(x1)
x2 = np.linspace(md + (md - checkVal), limits[1])
y2 = nd.pdf(x2)

plt.plot(x, y)
plt.fill_between(x1, y1, alpha=0.5)
plt.fill_between(x2, y2, alpha=0.2)
plt.xlabel("Weight")
plt.ylabel("P(Weight)")
plt.text(2.1, 0.05, "11.8%", fontsize=20)

sns.despine()

mystyle.printout_plain("pdf_checkValue.png")

plt.show()
Пример #24
0
# Arrange subplots
sns.set_context('paper')
sns.set_style('white')
mystyle.set(11)
fig, axs = plt.subplots(1,2)

# Plot distribution
axs[0].plot(x,y)
axs[0].set_xlabel('X')
axs[0].set_ylabel('PDF(X)')
axs[0].set_title('chi2(x), k=3')
sns.set_style('white')

x0, x1 = axs[0].get_xlim()
y0, y1 = axs[0].get_ylim()
axs[0].set_aspect((x1-x0)/(y1-y0))
#sns.despine()


# Plot probplot
plt.axes(axs[1])
stats.probplot(data, plot=plt)

x0, x1 = axs[1].get_xlim()
y0, y1 = axs[1].get_ylim()
axs[1].set_aspect((x1-x0)/(y1-y0))
#sns.despine()

mystyle.printout_plain('chi2pp.png')

plt.show()
Пример #25
0
nd = stats.norm(md, sd)

# Plot the data
sns.set_context(context='poster')
sns.set_style('ticks')

limits = (md - 3 * sd, md + 3 * sd)
x = np.linspace(limits[0], limits[1])
y = nd.pdf(x)

checkVal = 2.6
print('p = {0:5.3f}'.format(nd.cdf(checkVal)))

x1 = np.linspace(limits[0], checkVal)
y1 = nd.pdf(x1)
x2 = np.linspace(md + (md - checkVal), limits[1])
y2 = nd.pdf(x2)

plt.plot(x, y)
plt.fill_between(x1, y1, alpha=0.5)
plt.fill_between(x2, y2, alpha=0.2)
plt.xlabel('Weight')
plt.ylabel('P(Weight)')
plt.text(2.1, 0.05, '11.8%', fontsize=20)

sns.despine()

mystyle.printout_plain('pdf_checkValue.png')

plt.show()
Пример #26
0
    # Cumulative curve
    xcr = np.round(xcum * 10)
    xir = np.round(x * 10)
    for ii in range(len(xir)):
        ycum[xcr == xir[ii]] += y[ii]
    return ycum


# Right plot
plt.subplot(122)

# Format
plt.hold(True)
plt.xlim(-6, 11)
plt.ylim(-0.005, 0.18)
plt.xlabel('x')
plt.axhline(0)

# Rugplot & individual Gaussians
for ii in range(len(x)):
    plt.plot([x, x], [0, -0.005], 'b')
    ycum = plotNorm(x[ii], sd, xcum, ycum)

# Plot cumulative curve
ycum /= np.sum(ycum) / 10
plt.plot(xcum, ycum)

mystyle.printout_plain('KDEexplained.png')
plt.show()
Пример #27
0
x = np.linspace(-3,3,100)
yp = nd.pdf(x)
y = nd.cdf(x)
x1 = np.linspace(-3, 1)
y1 = nd.pdf(x1)

# Make the plot
sns.set_context('paper')
sns.set_style('white')
mystyle.set(12)

figs, axs = plt.subplots(1,2)

axs[0].plot(x,yp, 'k')
axs[0].fill_between(x1, y1, facecolor='#CCCCCC')
axs[0].text(0, 0.1, 'CDF(x)', family='cursive', fontsize=14, horizontalalignment='center', style='italic')
axs[0].set_xlabel('x')
axs[0].set_ylabel('PDF(x)')
sns.despine()

axs[1].plot(x, y, '#999999', lw=3)
axs[1].set_xlabel('x')
axs[1].set_ylabel('CDF(x)')
plt.vlines(0, 0, 1, linestyles='--')
sns.despine()

mystyle.printout_plain('PDF_CDF.png')

plt.show()
Пример #28
0
x = np.linspace(-3,3,100)
yp = nd.pdf(x)
y = nd.cdf(x)
x1 = np.linspace(-3, 1)
y1 = nd.pdf(x1)

# Make the plot
sns.set_context('paper')
sns.set_style('white')
mystyle.set(12)

figs, axs = plt.subplots(1,2)

axs[0].plot(x,yp, 'k')
axs[0].fill_between(x1, y1, facecolor='#CCCCCC')
axs[0].text(0, 0.1, 'CDF(x)', family='cursive', fontsize=14, horizontalalignment='center', style='italic')
axs[0].set_xlabel('x')
axs[0].set_ylabel('PDF(x)')
sns.despine()

axs[1].plot(x, y, '#999999', lw=3)
axs[1].set_xlabel('x')
axs[1].set_ylabel('CDF(x)')
plt.vlines(0, 0, 1, linestyles='--')
sns.despine()

mystyle.printout_plain('PDF_CDF.png')

plt.show()
Пример #29
0
y = nd.pdf(x)
y1 = nd.pdf(x1)
y2 = nd.pdf(x2)
y3 = nd.pdf(x3)

sns.set(context='poster')
sns.set_style('ticks')
fig, axs = plt.subplots(1,3, sharey=True)

def show_SD(axis, xi, yi, text):
    '''Show the area covered by 1/2/3 SDs'''
    
    fc = '#DDDDDD'
    axis.plot(x,y)
    axis.fill_between(xi, yi, facecolor=fc)
    
    axis.text(0, 0.05, text, horizontalalignment='center', fontsize=25)
    axis.set_xlim([-3.5, 3.5])
    axis.set_ylim([-0.0, 0.5])
    sns.despine(ax=axis, left=True)
    axis.set_yticks([])

show_SD(axs[0], x1, y1, '68.3%')    
show_SD(axs[1], x2, y2, '95.4%')    
show_SD(axs[2], x3, y3, '99.7%')    

plt.tight_layout()

mystyle.printout_plain('area_SDs.png')
Пример #30
0
data = nd.rvs(100)

x = np.linspace(-5, 5, 101)
pdf = nd.pdf(x)

# Calculate the KDE
sd = np.std(data, ddof=1)
h = (4 / (3 * 100))**0.2
h_str = '{0:4.2f}'.format(h)

# Calculate the smoothed plots, with 3 different parameters
kde_small = stats.kde.gaussian_kde(data, 0.1)
kde = stats.kde.gaussian_kde(data, h)
kde_large = stats.kde.gaussian_kde(data, 1)

# Generate two plots: one KDE with rug-plot, and one with different parameters
sns.set_context('poster')
sns.set_style('ticks')
fig, axs = plt.subplots(1, 2)
sns.distplot(data, rug=True, ax=axs[0])

axs[1].plot(x, pdf)
axs[1].plot(x, kde.evaluate(x), 'r')
axs[1].plot(x, kde_small.evaluate(x), '--', color=[0.8, 0.8, 0.8])
axs[1].plot(x, kde_large.evaluate(x), '--')
axs[1].legend(['exact', h_str, '0.1', '1.0'])
axs[1].set_ylim(0, 0.40)

mystyle.printout_plain('kdePlot.png')
plt.show()
Пример #31
0
np.random.seed(12345)
data = nd.rvs(100)

x = np.linspace(-5, 5, 101)
pdf = nd.pdf(x)

# Calculate the KDE
sd = np.std(data, ddof=1)
h = (4/(3*100))**0.2
h_str = '{0:4.2f}'.format(h)

# Calculate the smoothed plots, with 3 different parameters
kde_small = stats.kde.gaussian_kde(data, 0.1)
kde = stats.kde.gaussian_kde(data, h)
kde_large = stats.kde.gaussian_kde(data, 1)

# Generate two plots: one KDE with rug-plot, and one with different parameters
sns.set_context('poster')
sns.set_style('ticks')
fig, axs = plt.subplots(1,2)
sns.distplot(data, rug=True, ax=axs[0])

axs[1].plot(x, pdf)
axs[1].plot(x, kde.evaluate(x), 'r')
axs[1].plot(x,kde_small.evaluate(x),'--', color=[0.8, 0.8, 0.8])
axs[1].plot(x,kde_large.evaluate(x),'--')
axs[1].legend(['exact', h_str, '0.1', '1.0'])
axs[1].set_ylim(0, 0.40)

mystyle.printout_plain('kdePlot.png')
plt.show()
Пример #32
0
# Import standard packages
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import seaborn as sns

# additional packages
import mystyle

sns.set_context('poster')
sns.set_style('ticks')

# Generate the data
np.random.seed(1234)
nd = stats.norm(100, 20)
scores = nd.rvs(10)

# Make the plot
plt.plot(scores, 'o')
plt.axhline(110, ls='--')
plt.axhline(np.mean(scores), ls='-.')
plt.xlim(-0.2, 9.2)
plt.ylim(50, 130)
plt.xlabel('Student-Nr')
plt.ylabel('Score')

outFile = 'fig_ExampleTtest.png'
mystyle.printout_plain(outFile)
plt.show()
Пример #33
0
def main():
    # Calculate the PDF-curves
    x = np.linspace(-10, 15, 201)
    nd1 = stats.norm(1,2)
    nd2 = stats.norm(6,2)
    y1 = nd1.pdf(x)
    y2 = nd2.pdf(x)
    
    # Axes locations
    ROC = {'left': 0.3,
           'width': 0.45,
           'bottom': 0.1,
           'height': 0.45}
    
    PDF = {'left': 0.1,
           'width': 0.8,
           'bottom': 0.65,
           'height': 0.3}
           
    rect_ROC = [ROC['left'], ROC['bottom'], ROC['width'], ROC['height']]
    rect_PDF = [PDF['left'], PDF['bottom'], PDF['width'], PDF['height']]
    
    fig = plt.figure()
    
    ax1 = plt.axes(rect_PDF)
    ax2 = plt.axes(rect_ROC)
    
    # Plot and label the PDF-curves
    ax1.plot(x,y1)
    ax1.hold(True)
    ax1.fill_between(x,0,y1, where=x<3, facecolor='#CCCCCC', alpha=0.5)
    ax1.annotate('Sensitivity',
                 xy=(x[75], y1[65]),
                 xytext=(x[40], y1[75]*1.2), 
                 fontsize=14,
                 horizontalalignment='center',
                 arrowprops=dict(facecolor='#CCCCCC'))
    
    ax1.plot(x,y2,'#888888')
    ax1.fill_between(x,0,y2, where=x<3, facecolor='#888888', alpha=0.5)
    ax1.annotate('1-Specificity',
                 xy=(2.5, 0.03),
                 xytext=(6,0.05), 
                 fontsize=14,
                 horizontalalignment='center',
                 arrowprops=dict(facecolor='#888888'))
    
    ax1.set_ylabel('PDF')
    
    # Plot the ROC-curve
    ax2.plot(nd2.cdf(x), nd1.cdf(x), 'k')
    ax2.hold(True)
    ax2.plot(np.array([0,1]), np.array([0,1]), 'k--')
    
    # Format the ROC-curve
    ax2.set_xlim([0, 1])
    ax2.set_ylim([0, 1])
    #ax2.axis('equal')
    ax2.set_title('ROC-Curve')
    ax2.set_xlabel('1-specificity')
    ax2.set_ylabel('sensitivity')
    
    # Show the plot, and create a figure
    mystyle.printout_plain('ROC.png')    
    grandMean = np.mean(groupMean)
    ax.axhline(grandMean)
    ax.plot([80, 220], [groupMean[1], groupMean[1]], 'b')
    ax.plot([80, 120], [groupMean[1]+0.2, groupMean[1]+0.2], 'b')
    ax.annotate('', xy=(210, grandMean), xytext=(210,groupMean[1]), 
            arrowprops=dict(arrowstyle='<->, head_width=0.1', facecolor='black'))
    ax.annotate('', xy=(90, groupMean[1]), xytext=(90,groupMean[1]+0.2), 
            arrowprops=dict(arrowstyle='<->, head_width=0.1', facecolor='black'))
    ax.text(210, (grandMean + groupMean[1])/2., '$SS_{Treatment}$', fontsize=24)
    ax.text(90, groupMean[1]+0.1, '$SS_{Error}$', ha='right', fontsize=24)

if __name__ == '__main__':
    centers = [5, 5.3, 4.7]
    colors = 'brg'
    
    #sns.set_context('paper')
    #sns.set_style('white')
    np.random.seed(123)
    mystyle.set(18)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    std = 0.1
    numData = 100
    show_fig(0.1, ax, 'Sum-Squares')
    
    mystyle.printout_plain('anova_annotated.png')
    
    plt.show()

Пример #35
0
    plt.plot(x,y/10, 'r--')
    
    # Cumulative curve
    xcr = np.round(xcum*10)
    xir = np.round(x*10)
    for ii in range(len(xir)):
        ycum[xcr==xir[ii]] += y[ii]
    return ycum
    
# Right plot
plt.subplot(122)

# Format
plt.hold(True)
plt.xlim(-6, 11)
plt.ylim(-0.005, 0.18)
plt.xlabel('x')
plt.axhline(0)

# Rugplot & individual Gaussians
for ii in range(len(x)):
    plt.plot([x,x], [0, -0.005], 'b')    
    ycum = plotNorm(x[ii], sd, xcum, ycum)

# Plot cumulative curve
ycum /= np.sum(ycum)/10
plt.plot(xcum, ycum)

mystyle.printout_plain('KDEexplained.png')
plt.show()
Пример #36
0
# additional packages
import mystyle

# Get the data, and fit the normal distribution
weight = np.array([2784, 2632, 2771, 2495, 2435, 2513, 2633, 2737, 2687, 2647],
                  dtype=np.float32)
(md, sd) = stats.norm.fit(weight)
nd = stats.norm(md, sd)

# Plot the data
sns.set_context(context='poster')

x = np.linspace(2300, 3000)
y = nd.pdf(x)

checkVal = 2460
print('p = {0:5.3f}'.format(nd.cdf(checkVal)))

x1 = np.linspace(2300, checkVal)
y1 = nd.pdf(x1)

sns.rugplot(weight, height=0.0005)
plt.hold(True)
plt.plot(x, y)
plt.fill_between(x1, y1, alpha=0.3)

mystyle.printout_plain('pdf_checkMean.png')

plt.show()
Пример #37
0
        offset = ii * numData
        ax.plot(offset + np.arange(numData),
                data,
                '.',
                color=colors[ii],
                ms=10)

    ax.xaxis.set_ticks([50, 150, 250])
    ax.set_xticklabels(['Group1', 'Group2', 'Group3'])
    ax.set_title(title)
    sns.despine()


if __name__ == '__main__':
    centers = [5, 5.3, 4.7]
    colors = 'brg'

    sns.set_context('paper')
    sns.set_style('whitegrid')
    mystyle.set(14)

    fig, axs = plt.subplots(1, 2)
    stds = [0.1, 2]
    numData = 100
    show_fig(0.1, axs[0], 'SD=0.1')
    show_fig(2, axs[1], 'SD=2.0')

    mystyle.printout_plain('anova_oneway.png')

    plt.show()