Exemple #1
0
pdf, xs, kde_best = kde_1d.kde(data,G,bbox,details=True)

sigma = kde_best.bandwidth
plt.figure()
xs_fine = sp.linspace(bbox[0],bbox[1],1E4)
kernels = sp.zeros([len(xs_fine),num_samples])
for i, datum in enumerate(data):
	kernels[:,i] = gaussian(x=xs_fine,mu=datum,sigma=sigma)
plt.plot(xs_fine,kernels,'-b',alpha=0.2)
plt.plot(xs,pdf,'-b',linewidth=2)
plt.show()



# Do density estimation
results = deft_1d.run(data, G=G, alpha=alpha, bbox=bbox)
xs_hist = results.bin_edges[:-1]
xs = results.bin_centers
bin_centers = results.bin_centers
R = results.R
Q_star = results.Q_star
h = results.h
i_star = results.map_curve.i_star

# Plot histogram with density estimate on top
plt.figure(figsize=[3, 8])

# Set Qs along MAP curve to plot
Qs = [[], 
	  results.map_curve.points[0].Q,
	  results.map_curve.points[i_star - 20].Q,
orange = [1., 0.5, 0.]
gray = [0.6, 0.6, 0.6]

# Simulate data and get default deft settings
data, defaults = simulate_data_1d.run(data_type, N)

# Get bounding box
bbox = [defaults['box_min'], defaults['box_max']]

#
# Do DEFT density estimation
#

results = deft_1d.run(data, G=G, alpha=alpha, \
    bbox=bbox, \
    periodic=False,
    num_samples=100,
    print_t=True)

# Provide feedback
print '\n%s, alpha=%d' % (data_type, alpha)

# Compute true density
xs = results.bin_centers
Q_true = np.zeros(G)
for i, x in enumerate(xs):
    Q_true[i] = eval(defaults['pdf_py'])
Q_true /= results.h * sum(Q_true)

#
# Estimate entropy
for d, data_type in enumerate(data_types):

    # Simulate data and get default deft settings
    data, defaults = simulate_data_1d.run(data_type, N)

    # Iterate over value sfor alpha
    for a, alpha in enumerate(alphas):

        # Specify subplot
        ax = plt.subplot(num_rows, num_cols, num_cols * d + a + 1)

        bbox = [defaults['box_min'], defaults['box_max']]

        # Do density estimation
        results = deft_1d.run(data, G=G, alpha=alpha, \
            bbox=bbox, \
            periodic=defaults['periodic'])

        # Plot histogram density
        left_bin_edges = results.bin_edges[:-1]
        plt.bar(left_bin_edges, results.R, \
            width=results.h, linewidth=0, color=gray, zorder=0)

        # Plot error bars on density estimate
        plt.fill_between(results.bin_centers,
                         results.Q_ub,
                         results.Q_lb,
                         color=lightblue,
                         zorder=1,
                         alpha=0.5,
                         linewidth=0)
Exemple #4
0
        mis[i] = mutual_information(Q_xy)

    # Compute mean and variance of the differential entropy
    mi_mean = np.mean(mis)
    mi_std = np.std(mis)
    results.mi_compute_time = time.clock() - entropy_start_time

# Estimate distribution of mutual information estimates
# Now do density estimation for the entropies
mi_G = 100
mi_box = [max(0., mi_mean - 5 * mi_std), mi_mean + 5 * mi_std]
mi_box[0] = min(mi_box[0], mi_true)
mi_box[1] = max(mi_box[1], mi_true)
mi_results = deft_1d.run(mis,
                         G=mi_G,
                         bbox=mi_box,
                         alpha=3,
                         periodic=False,
                         num_samples=0)

#Q, R, Q_ub, Q_lb, Q_samples, map_curve
Q = results.Q_star
R = results.R
map_curve = results.map_curve

# Make Q 2d
Q_2d = Q.reshape(num_bins)
R_2d = R.reshape(num_bins)

# Calibrate both R and Q to the same scale
cl = [0.0, max(Q_true.ravel())]
cmap = plt.get_cmap('bone')
Exemple #5
0
orange = [1., 0.5, 0.]
gray = [0.6, 0.6, 0.6]

# Generate data
N = 100
G = 100
alpha = 3

data_type = 'gaussian'

# Compute data set
data, defaults = simulate_data_1d.run(data_type, N)

# Do density estimation
results = deft_1d.run(data, G=G, alpha=alpha, \
  bbox=defaults.bbox, \
  periodic=defaults.periodic)

# Plot the MAP curve
map_curve = results.map_curve
points = map_curve.get_points()

ts = sp.array([p.t for p in points])

num_points = len(points)
Qs = sp.zeros([G, num_points])
for n, p in enumerate(points):
    Qs[:, n] = p.Q

log_Es, ts = map_curve.get_log_evidence_ratios(finite=True)
# Simulate data and get default deft settings
data, defaults = simulate_data_1d.run(data_type, N)

# Get bounding box
bbox = [defaults['box_min'], defaults['box_max']]

#
# Do DEFT density estimation
#

results = deft_1d.run(data, G=G, alpha=alpha, \
    bbox=bbox, \
    periodic=False,
    num_samples=100,
    errorbars=False,
    print_t=False,
    ell_guess=ell_guess,
    resolution=resolution,
    tollerance=tollerance)

# Provide feedback
print '\n%s, alpha=%d' % (data_type, alpha)
print 'laplacian_compute_time  == %.2f sec' % results.laplacian_compute_time
print 'map_curve_compute_time  == %.2f sec' % results.map_curve_compute_time
#print 'errorbar_compute_time  == %.2f sec'%results.errorbar_compute_time
print 'posterior_sample_compute_time  == %.2f sec'%results.\
    posterior_sample_compute_time
print 'copy_compute_time  == %.2f sec' % results.copy_compute_time
print 'entropy_compute_time  == %.2f sec' % results.entropy_compute_time
print 'deft_1d_compute_time == %.2f sec' % results.deft_1d_compute_time