Beispiel #1
0
def plot_contour(x_grid, y_grid, p_grid, title):
    # Get min and max from grids
    x_min: float = np.min(x_grid[0, :])
    x_max: float = np.max(x_grid[0, :])
    y_min: float = np.min(y_grid[:, 0])
    y_max: float = np.max(y_grid[:, 0])

    # Plot the contours of f(x,y)
    fig, ax = plt.subplots(figsize=[13, 10])
    ax.set_title(title)
    ax.set_xlabel('$x$')
    ax.set_ylabel('$y$')
    ax.set_xlim(x_min, x_max)
    ax.set_ylim(y_min, y_max)
    ax.set_xticks(arange_inc(x_min, x_max, 2.0))
    ax.set_yticks(arange_inc(y_min, y_max, 2.0))
    ax.set_aspect('equal', 'datalim')
    # compute desired contour levels
    p_max = np.max(p_grid)
    num_levels = 8
    step_size = p_max / (num_levels - 1)
    half_step = 0.5 * step_size
    levels = arange_inc(half_step, p_max - half_step, step_size)
    # Generate contour plot
    cs = ax.contour(x_grid, y_grid, p_grid, levels=levels, linewidths=2)
    fig.colorbar(cs, ax=ax)
    ax.grid()
    return fig, ax
def cdf_and_inverse(f, a, b, dx):
    """Generate a numerical inverse CDF to the PDF given by f(x)
    f:  The probability density function whose CDF is to be numerically inverted
    a:  The start of the support for f(x)
    b:  The end of the support for f(x)
    dx: The step size to use in sampling on [a, b]    
    """
    # Sample f_X(x) on the interval [a, b] with step size dx
    dx = 0.01
    sample_x = arange_inc(a, b, dx)
    sample_f = np.array([f(x) for x in sample_x])

    # Numerical integral of F using cumtrapz library function
    sample_F = np.zeros_like(sample_f)
    sample_F[1:] = cumtrapz(sample_f, sample_x)
    # Normalize this to guarantee it ranges from [0, 1] notwithstanding any round-off
    sample_F = sample_F / sample_F[-1]

    # Use the Pchip interpolator b/c this guarantees that monotonic input is sent to monotonic output
    # Numerical CDF using interpolation
    F = PchipInterpolator(sample_x, sample_F)
    # Numerical inverse CDF using interpolation
    # Silence these warnings; it's OK, the splined inverse interpolant is horizontal in places but it works
    with np.errstate(divide='ignore', invalid='ignore'):
        F_inv = PchipInterpolator(sample_F, sample_x)
    # Return the splined CDF and inverse CDF function
    return F, F_inv
def main_1():
    # Values for plots
    plot_x = arange_inc(1, 9, 0.05)
    plot_f = np.array([f_X(x) for x in plot_x])
    plot_F = np.array([F_X(x) for x in plot_x])

    # Plot the PDF f_X(x)
    fig, ax = plt.subplots()
    fig.set_size_inches([16, 8])
    ax.set_title('PDF $f_X(x)$')
    ax.set_xlabel('x')
    ax.set_ylabel('$f_X(x)$')
    ax.set_xlim([1, 9])
    ax.plot(plot_x, plot_f, label='PDF')
    ax.grid()
    plt.show()

    # Plot the CDF F_X(x)
    fig, ax = plt.subplots()
    fig.set_size_inches([16, 8])
    ax.set_title('CDF $F_X(x)$')
    ax.set_xlabel('x')
    ax.set_ylabel('$F_X(x)$')
    ax.set_xlim([1, 9])
    ax.set_ylim([0, 1])
    ax.plot(plot_x, plot_F, label='CDF')
    ax.legend()
    ax.grid()
    plt.show()
Beispiel #4
0
# *************************************************************************************************
# A1. Visualize  𝑝(𝑥,𝑦)  with a contour or surface plot.
# Make sure to title your plot and label all axes.
# What do you notice about  𝑝(𝑥,𝑦) ? Do you think it will be an easy function to sample?

# Range of x and y
x_min: float = -8.0
x_max: float = 18.0
y_min: float = -6.0
y_max: float = 14.0

# Step size for x and y
dx: float = 0.125
dy: float = 0.125
# Discrete sample points for x and y
xx = arange_inc(x_min, x_max, dx)
yy = arange_inc(y_min, y_max, dy)
# Grid of x and y
grid_size_x: int = len(xx)
grid_size_y: int = len(yy)
# Grid of sample points for contour plots
x_grid, y_grid = np.meshgrid(xx, yy)

# Grid of p(x, y)
try:
    # raise ValueError
    p_grid = vartbl['p_grid']
    print(f'Loaded grid of p(x, y))')
except:
    p_grid = np.zeros((grid_size_y, grid_size_x))
    for j, x in enumerate(xx):
        return (1 / 12) * (x2 / 2 - x + 1 / 2)
    elif x <= 5:
        return F_X(3) + 1 / 12 * (5 * x - x2 / 2 - 21 / 2)
    elif x <= 7:
        return F_X(5) + 1 / 6 * (x2 / 2 - 5 * x + 25 / 2)
    elif x <= 9:
        return F_X(7) + 1 / 6 * (9 * x - x2 / 2 - 77 / 2)
    else:
        return 1


# *************************************************************************************************
# 1. Visualize PDF and CDF

# Values for plots
plot_x = arange_inc(1, 9, 0.05)
plot_f = np.array([f_X(x) for x in plot_x])
plot_F = np.array([F_X(x) for x in plot_x])

# Plot the PDF f_X(x)
fig, ax = plt.subplots()
fig.set_size_inches([16, 8])
ax.set_title('PDF $f_X(x)$')
ax.set_xlabel('x')
ax.set_ylabel('$f_X(x)$')
ax.set_xlim([1, 9])
ax.plot(plot_x, plot_f, label='PDF')
ax.grid()
plt.show()

# Plot the CDF F_X(x)
        sgds_by_start[i] = sgd_curr
    # Save these calculations
    vartbl['gds_by_start'] = gds_by_start
    vartbl['sgds_by_start'] = sgds_by_start
    save_vartbl(vartbl, fname)

# Loss function vs. iteration iteration for each starting value
loss_gd_by_start = num_starts * [np.array([])]
loss_sgd_by_start = num_starts * [np.array([])]

for i, lambda_init in enumerate(lambdas_init):
    loss_gd_by_start[i] = gds_by_start[i]['history']
    loss_sgd_by_start[i] = sgds_by_start[i]['history']

# Plot loss vs. iteration for each start
nn = arange_inc(0, max_iterations)
for i, lambda_init in enumerate(lambdas_init):
    lambda1, lambda2 = lambda_init
    fig, ax = plt.subplots()
    fig.set_size_inches([16, 8])
    ax.set_title(
        f'Loss vs. Iteration Number Starting at $\lambda_1={lambda1:0.3f}$, $\lambda_2={lambda2:0.1f}$'
    )
    ax.set_xlabel('Iteration Number')
    ax.set_ylabel('Loss Function')
    # Data to plot on this iteration - loss by iteration for GD & SGD
    plot_loss_gd = loss_gd_by_start[i]
    plot_loss_sgd = loss_sgd_by_start[i]
    n_gd = plot_loss_gd.shape[0]
    n_sgd = plot_loss_sgd.shape[0]
    # Plot the loss by iteration