Пример #1
0
 def __init__(self, T, delta, optimal_arm, dist_type):
     self.optimal_arm = optimal_arm
     self.base_p =  0.5
     self.delta = delta
     self.rewards_list=[[],[]]
     self.t=0
     self.T = T 
     self.return_num_batches = False
         
     if dist_type == 'bernoulli':
         self.optimal_sampler = bernoulli(self.base_p + delta)
         self.dagger_sampler = bernoulli(self.base_p)
     elif dist_type == 'norm':
         self.optimal_sampler = norm(self.base_p + delta,1)
         self.dagger_sampler = norm(self.base_p,1)
     elif dist_type == 'poisson':
         self.optimal_sampler = poisson(self.base_p + delta)
         self.dagger_sampler = poisson(self.base_p)  
     elif dist_type == 'student_t':
         self.optimal_sampler = student_t(2, loc=self.base_p+delta)
         self.dagger_sampler = student_t(2,loc=self.base_p)                       
Пример #2
0
def estimate_and_plot(s, path='coding_3a.png'):

    # Reference
    # https://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html#broyden-fletcher-goldfarb-shanno-algorithm-method-bfgs
    # If we do not supply our gradient, it is taken by finite difference methods
    # order [mu, sigma]
    x0 = np.array([0, 1.0])
    param_Gaussian = minimize(MLE_Gaussian,
                              x0,
                              method='BFGS',
                              options={'disp': True})
    print(param_Gaussian.x)

    # order [k, mu, sigma]
    x0 = np.array([10, 0.0, 1.0])
    param_Student_t = minimize(MLE_Student_t,
                               x0,
                               method='BFGS',
                               options={'disp': True})
    print(param_Student_t.x)

    x = np.linspace(-5, 5, num_pts)
    plt.figure(figsize=(8, 6), dpi=DPI)
    # Plot points
    plt.hist(s, bins=np.arange(-10, 10 + binwidth, binwidth), density=True)
    # Plot Gaussian
    dist = gaussian(loc=param_Gaussian.x[0], scale=param_Gaussian.x[1])
    plt.plot(x, dist.pdf(x), lw=lw, c='blue', label='Gaussian')
    # Plot Student t
    dist = student_t(df=param_Student_t.x[0],
                     loc=param_Student_t.x[1],
                     scale=param_Student_t.x[2])
    plt.plot(x, dist.pdf(x), lw=lw, c='red', label='Student_t')

    plt.ylim(0, 0.55)
    plt.xlim(-10, 10)
    plt.xlabel('$x$')
    plt.ylabel(r'$p(x)$')
    plt.title(r'Estimated Gaussian and Students $t$ Distribution')
    plt.legend(loc="upper right")
    plt.grid(True)
    print("Saving to " + path)
    plt.savefig(path)
    plt.close()
Пример #3
0
def main():

    mu = 0
    variance = 1
    sigma = np.sqrt(variance)
    x = np.linspace(-10, 10, 1000)
    linestyles = ['-', '--', ':', '-.']
    dofs = [1, 2, 4, 30]

    # plot the different student t distributions
    for dof, ls in zip(dofs, linestyles):
        dist = student_t(dof, mu)
        label = r'$\mathrm{t}(dof=%1.f, \mu=%1.f)$' % (dof, mu)
        plt.plot(x, dist.pdf(x), ls=ls, color="black", label=label)

    plt.plot(x,
             norm.pdf(x, mu, sigma),
             color="green",
             linewidth=3,
             label=r'$\mathrm{N}(\mu=%1.f,\sigma=%1.f)$' % (mu, sigma))

    plt.xlim(-5, 5)
    plt.xlabel('$x$')
    plt.ylabel(r'$p(x|k)$')
    plt.title("Student's $t$ Distribution approximates Normal")

    plt.legend()
    plt.show()

    print "The 90%% confidence interval is = (%0.2f %0.2f)" % (norm.interval(
        0.9, loc=0, scale=1))
    download_rate_estimate = 0.02
    sigma_s = download_rate_estimate * (1. - download_rate_estimate)
    N = 5.3792 * sigma_s / (0.1 * download_rate_estimate)**2
    print "estimate of N = %d" % round(N)

    # Calculate the t value given the measured download fractions
    download_fractions = [0.0187, 0.0210]
    print "t_A = %0.2f (for a measured download rate of 2.1%%)" % t_test(
        N, download_fractions[1], N, download_fractions[0])

    return
#   The figure produced by this code is published in the textbook
#   "Statistics, Data Mining, and Machine Learning in Astronomy" (2013)
#   For more information, see http://astroML.github.com
import numpy as np
from scipy.stats import t as student_t
from matplotlib import pyplot as plt

#------------------------------------------------------------
# Define the distribution parameters to be plotted
mu = 0
k_values = [1E10, 2, 1, 0.5]
linestyles = ['-', '--', ':', '-.']
x = np.linspace(-10, 10, 1000)

for k, ls in zip(k_values, linestyles):
    dist = student_t(k, 0)

    if k >= 1E10:
        label = r'$\mathrm{t}(k=\infty)$'
    else:
        label = r'$\mathrm{t}(k=%.1f)$' % k

    plt.plot(x, dist.pdf(x), ls=ls, c='black', label=label)

plt.xlim(-5, 5)
plt.ylim(0.0, 0.5)

plt.xlabel('$x$', fontsize=14)
plt.ylabel(r'$P(x|k)$', fontsize=14)
plt.title("Student's t Distribution")
Пример #5
0
# from astroML.plotting import setup_text_plots
# setup_text_plots(fontsize=8, usetex=True)

#------------------------------------------------------------
# Define the distribution parameters to be plotted
mu = 0
k_values = [1E10, 2, 1, 0.5]
linestyles = ['-', '--', ':', '-.']
x = np.linspace(-10, 10, 1000)

#------------------------------------------------------------
# plot the distributions
fig, ax = plt.subplots(figsize=(5, 3.75))

for k, ls in zip(k_values, linestyles):
    dist = student_t(k, 0)

    if k >= 1E10:
        label = r'$\mathrm{t}(k=\infty)$'
    else:
        label = r'$\mathrm{t}(k=%.1f)$' % k

    plt.plot(x, dist.pdf(x), ls=ls, c='black', label=label)

plt.xlim(-5, 5)
plt.ylim(0.0, 0.45)

plt.xlabel('$x$')
plt.ylabel(r'$p(x|k)$')
plt.title("Student's $t$ Distribution")
	if(flaDeb): print "tmpSE: ",tmpSE
	tmpAv = np.array(muestreo1).mean()
	muestreo1SE.append(tmpSE)
	muestreo1Av.append(tmpAv)
	muestroY.append((tmpAv-mean)/tmpSE)
	if(flaDeb): print "muestroY: ", muestroY
	
if(flaDeb): print "mean muestreo: ",muestroY,"std muestreo: ",np.array(muestreo1).std()*math.sqrt(tomaMuestreo)/math.sqrt(tomaMuestreo-1)

muestreoHist1 = []
muestreobins1 = []
muestreoHist1, muestreobins1 = np.histogram(muestroY,  bins=x, density=True)
muestreobins1 = np.array(muestreobins1)
muestreoHist1 = np.array(muestreoHist1)
plt.subplot(2, 1, 1)
plt.bar(muestreobins1[0:(len(muestreoHist1)-0)] ,  muestreoHist1, align='center', width = width*2, color = 'red', label='Muestreo de %d'%(tomaMuestreo))
dist = student_t(tomaMuestreo-1, 0)
plt.plot(x, dist.pdf(x), lw=2, c='blue', label="t-Student df: (%d-1)"%(tomaMuestreo))
normalDistrib = stats.norm.pdf(x, loc=0, scale=1)
plt.plot(x,normalDistrib,color='red',lw=2,label="normal (${\mu=}0,{\sigma=}1$)")
plt.legend( loc='upper left', numpoints = 1 )
plt.xlabel("Desviacion estandarizada ")
plt.ylabel("Probabilidad")
plt.title("Distribuciones de $<Y>$ para muestras de  %d"%(tomaMuestreo))
plt.xlim(-4, 4)

plt.subplot(2, 1, 2)
stats.probplot(muestroY, plot=plt)
plt.show()

import numpy as np
from scipy.stats import t as student_t
import plotly.plotly as py
from plotly.graph_objs import *

mu = 0
x = np.linspace(-10, 10, 1000)
dist = student_t(1e10, 0)
y = dist.pdf(x)
data = Data([
    Scatter(
        x = x,
        y = y
    )
])
layout = Layout(title="Student's t-distribution")
fig = Figure(data=data,layout=layout)
py.plot(fig,file="Student Distribution")
Пример #8
0
def Student_t(input, k, mu, sigma):
    dist = student_t(df=k, loc=mu, scale=sigma)
    return dist.pdf(input)
Пример #9
0
print("\n================ Question 1 ===================")
xmax = 10
xmin = -xmax
x = np.linspace(xmin, xmax, num_pts)

# Define the distribution parameters to be plotted
k_values = [0.1, 1, 10, 100, 10E6]
color_values = ['r', 'magenta', 'k', 'g', 'c']

# Plot the student's t distributions
plt.figure(figsize=(8, 6), dpi=DPI)

for k, col in zip(k_values, color_values):
    # Reference
    # https://www.astroml.org/book_figures/chapter3/fig_student_t_distribution.html
    dist = student_t(df=k, loc=mu, scale=scale)
    label = r't ($\mathrm{\nu}=%.1f$)' % k
    plt.plot(x, dist.pdf(x), lw=lw, c=col, label=label)

# Finally plot Gaussian
dist = gaussian(loc=mu, scale=scale)
plt.plot(x, dist.pdf(x), lw=lw, c='blue', label='Gaussian')

plt.xlim(-xmax, xmax)
plt.ylim(0.0, 0.4)
plt.xlabel('$x$')
plt.ylabel(r'$p(x)$')
plt.title(r'Students $t$ and Gaussian Distribution')

plt.legend()
plt.grid(True)