def test_unscented_transform_linear_combination(): """Test unscented transform with a linear combination.""" mvn = MVN(mean=np.zeros(2), covariance=np.eye(2), random_state=42) points = mvn.sigma_points() new_points = np.empty_like(points) new_points[:, 0] = points[:, 1] new_points[:, 1] = points[:, 0] - 0.5 * points[:, 1] new_points += np.array([-0.5, 3.0]) transformed_mvn = mvn.estimate_from_sigma_points(new_points) assert_array_almost_equal(transformed_mvn.mean, np.array([-0.5, 3.0])) assert_array_almost_equal(transformed_mvn.covariance, np.array([[1.0, -0.5], [-0.5, 1.25]]))
def test_unscented_transform_linear_transformation(): """Test unscented transform with a linear transformation.""" mvn = MVN(mean=np.zeros(2), covariance=np.eye(2), random_state=42) points = mvn.sigma_points() new_points = np.copy(points) new_points[:, 1] *= 10 new_points += np.array([0.5, -3.0]) transformed_mvn = mvn.estimate_from_sigma_points(new_points) assert_array_almost_equal(transformed_mvn.mean, np.array([0.5, -3.0])) assert_array_almost_equal(transformed_mvn.covariance, np.array([[1.0, 0.0], [0.0, 100.0]])) sample1 = transformed_mvn.sample(1) sample2 = mvn.estimate_from_sigma_points(new_points, random_state=42).sample(1) assert_array_almost_equal(sample1, sample2)
def test_unscented_transform_quadratic(): """Test unscented transform with a quadratic transformation.""" mvn = MVN(mean=np.zeros(2), covariance=np.eye(2), random_state=42) points = mvn.sigma_points(alpha=0.67, kappa=5.0) def f(points): new_points = np.empty_like(points) new_points[:, 0] = points[:, 0] ** 2 * np.sign(points[:, 0]) new_points[:, 1] = points[:, 1] ** 2 * np.sign(points[:, 1]) new_points += np.array([5.0, -3.0]) return new_points transformed_mvn = mvn.estimate_from_sigma_points(f(points), alpha=0.67, kappa=5.0) assert_array_almost_equal(transformed_mvn.mean, np.array([5.0, -3.0])) assert_array_almost_equal( transformed_mvn.covariance, np.array([[3.1, 0.0], [0.0, 3.1]]), decimal=1 )
def test_unscented_transform_projection_to_more_dimensions(): """Test unscented transform with a projection to 3D.""" mvn = MVN(mean=np.zeros(2), covariance=np.eye(2), random_state=42) points = mvn.sigma_points() def f(points): new_points = np.empty((len(points), 3)) new_points[:, 0] = points[:, 0] new_points[:, 1] = points[:, 1] new_points[:, 2] = -0.5 * points[:, 0] + 0.5 * points[:, 1] new_points += np.array([-0.5, 3.0, 10.0]) return new_points transformed_mvn = mvn.estimate_from_sigma_points(f(points)) assert_array_almost_equal(transformed_mvn.mean, np.array([-0.5, 3.0, 10.0])) assert_array_almost_equal( transformed_mvn.covariance, np.array([[1.0, 0.0, -0.5], [0.0, 1.0, 0.5], [-0.5, 0.5, 0.5]]))
ax.set_ylim((-8, 8)) mvn_cartesian = MVN( mean=np.array([2.5, 1.3]), covariance=np.array([[1.0, -1.5], [-1.5, 4.0]]), random_state=0) plot_error_ellipse(ax, mvn_cartesian) samples_cartesian = mvn_cartesian.sample(1000) ax.scatter(samples_cartesian[:, 0], samples_cartesian[:, 1], s=1) ax = plt.subplot(132) ax.set_title("(2) Polar coordinates") ax.set_xlabel("$r$") ax.set_ylabel("$\phi$") ax.set_xlim((-8, 8)) ax.set_ylim((-8, 8)) sigma_points_cartesian = mvn_cartesian.sigma_points(alpha=alpha, kappa=kappa) sigma_points_polar = cartesian_to_polar(sigma_points_cartesian) mvn_polar = mvn_cartesian.estimate_from_sigma_points(sigma_points_polar, alpha=alpha, beta=beta, kappa=kappa) plot_error_ellipse(ax, mvn_polar) samples_polar = cartesian_to_polar(samples_cartesian) ax.scatter(samples_polar[:, 0], samples_polar[:, 1], s=1) ax = plt.subplot(133) ax.set_title("(3) Cartesian coordinates") ax.set_xlabel("$x_1$") ax.set_ylabel("$x_2$") ax.set_xlim((-8, 8)) ax.set_ylim((-8, 8)) sigma_points_polar2 = mvn_polar.sigma_points(alpha=alpha, kappa=kappa) sigma_points_cartesian2 = polar_to_cartesian(sigma_points_polar2) mvn_cartesian2 = mvn_polar.estimate_from_sigma_points(sigma_points_cartesian2, alpha=alpha, beta=beta, kappa=kappa)