Exemple #1
0
def test_probability_density():
    """Test PDF of GMM."""
    global X
    global random_state

    gmm = GMM(n_components=2, random_state=random_state)
    gmm.from_samples(X)

    x = np.linspace(-100, 100, 201)
    X_grid = np.vstack(map(np.ravel, np.meshgrid(x, x))).T
    p = gmm.to_probability_density(X_grid)
    approx_int = np.sum(p) * ((x[-1] - x[0]) / 201) ** 2
    assert_less(np.abs(1.0 - approx_int), 0.01)
Exemple #2
0
def test_probability_density():
    """Test PDF of GMM."""
    global X
    global random_state

    gmm = GMM(n_components=2, random_state=random_state)
    gmm.from_samples(X)

    x = np.linspace(-100, 100, 201)
    X_grid = np.vstack(list(map(np.ravel, np.meshgrid(x, x)))).T
    p = gmm.to_probability_density(X_grid)
    approx_int = np.sum(p) * ((x[-1] - x[0]) / 201)**2
    assert_less(np.abs(1.0 - approx_int), 0.01)
Exemple #3
0
    gmm.from_samples(X)
    cond = gmm.condition(np.array([0]), np.array([1.0]))

    plt.figure(figsize=(15, 5))

    plt.subplot(1, 3, 1)
    plt.title("Gaussian Mixture Model")
    plt.xlim((-10, 10))
    plt.ylim((-10, 10))
    plot_error_ellipses(plt.gca(), gmm, colors=["r", "g", "b"])
    plt.scatter(X[:, 0], X[:, 1])

    plt.subplot(1, 3, 2)
    plt.title("Probability Density and Samples")
    plt.xlim((-10, 10))
    plt.ylim((-10, 10))
    x, y = np.meshgrid(np.linspace(-10, 10, 100), np.linspace(-10, 10, 100))
    X_test = np.vstack((x.ravel(), y.ravel())).T
    p = gmm.to_probability_density(X_test)
    p = p.reshape(*x.shape)
    plt.contourf(x, y, p)
    X_sampled = gmm.sample(100)
    plt.scatter(X_sampled[:, 0], X_sampled[:, 1], c="r")

    plt.subplot(1, 3, 3)
    plt.title("Conditional PDF $p(y | x = 1)$")
    X_test = np.linspace(-10, 10, 100)
    plt.plot(X_test, cond.to_probability_density(X_test[:, np.newaxis]))

    plt.show()
ax.scatter(samples[:, 0], samples[:, 1], alpha=0.9, s=1, label="Samples")
plot_error_ellipses(ax, gmm, factors=(1.0, 2.0), colors=["orange", "orange"])
ax.set_xlim((-10, 10))
ax.set_ylim((-10, 10))

ax = plt.subplot(142)
ax.set_title(r"95.45 % Confidence Region ($2\sigma$)")
samples = gmm.sample_confidence_region(n_samples, 0.9545)
ax.scatter(samples[:, 0], samples[:, 1], alpha=0.9, s=1, label="Samples")
plot_error_ellipses(ax, gmm, factors=(1.0, 2.0), colors=["orange", "orange"])
ax.set_xlim((-10, 10))
ax.set_ylim((-10, 10))

ax = plt.subplot(143)
ax.set_title(r"68.27 % Confidence Region ($\sigma$)")
samples = gmm.sample_confidence_region(n_samples, 0.6827)
ax.scatter(samples[:, 0], samples[:, 1], alpha=0.9, s=1, label="Samples")
plot_error_ellipses(ax, gmm, factors=(1.0, 2.0), colors=["orange", "orange"])
ax.set_xlim((-10, 10))
ax.set_ylim((-10, 10))
ax.legend()

ax = plt.subplot(144)
ax.set_title(r"Probability density")
x, y = np.meshgrid(np.linspace(-10, 10, 100), np.linspace(-10, 10, 100))
X_test = np.vstack((x.ravel(), y.ravel())).T
p = gmm.to_probability_density(X_test)
p = p.reshape(*x.shape)
plt.contourf(x, y, p)

plt.show()
Exemple #5
0
    label = "Normalized" if i == 0 else None
    ax.plot(X_train[:, 0], X_train[:, 1], color="k", alpha=0.2, label=label)

sampled_paths = []
for sample_idx in range(10):
    x = mean_start.copy()
    sampled_path = [x]
    sampling_dt = 0.01
    for t in range(500):
        cgmm = gmm.condition([0, 1], x)
        # default alpha defines the confidence region (e.g., 0.7 -> 70 %)
        x_dot = safe_sample(cgmm, alpha=0.9)

        # TODO correct stopping criterion: marginalize over velocities
        # https://github.com/AlexanderFabisch/gmr/issues/18
        p_joint_xxd = gmm.to_probability_density(
            np.hstack((x, x_dot))[np.newaxis])[0]
        p_cond_xdx = cgmm.to_probability_density(x_dot[np.newaxis])
        p_x = p_joint_xxd / p_cond_xdx
        if p_x < 1e-5 or np.linalg.norm(mean_goal - x) < 4.0:
            break
        x = x + sampling_dt * x_dot
        sampled_path.append(x)
    sampled_path = np.array(sampled_path)
    sampled_paths.append(sampled_path)

    label = "Sampled" if sample_idx == 0 else None
    ax.plot(sampled_path[:, 0], sampled_path[:, 1], color="r", label=label)
xlim = ax.get_xlim()
ylim = ax.get_ylim()
ax.legend(loc="best")