Exemplo n.º 1
0
def _generate_gaussian_prior(
        grid: UE4Grid,
        means: "list of 2 means",
        covariance_matrix: "2x2 covariance matrix",
        initial_belief_sum=0.5
) -> "Tuple(np.array normed prior probs, prior_dict":
    '''A method that returns the normed z vector as well as the prior dict. Given a 2d vector of means, 2X2 covariance matrix, returns a 2D gaussian prior'''
    #maybe should check all dimensions of data here
    prior = {}
    x, y = np.mgrid[0:grid.get_no_points_x() *
                    grid.get_lng_spacing():grid.get_lng_spacing(),
                    0:grid.get_no_points_y() *
                    grid.get_lat_spacing():grid.get_lat_spacing()]
    pos = np.empty(x.shape + (2, ))
    pos[:, :, 0] = x
    pos[:, :, 1] = y
    rv = multivariate_normal(means, covariance_matrix)
    z = rv.pdf(pos)
    normed_z = z * (initial_belief_sum / z.sum())

    #normed_z = normed_z.astype(float)
    #return likelihoods in dict with grid points
    for x_value in [_[0] for _ in x]:
        for y_value in y[0]:
            prior[Vector3r(x_value,
                           y_value)] = float(normed_z[x_value][y_value])

    #place prior into ordered numpy array
    list_of_grid_points = []
    for grid_loc in grid.get_grid_points():
        list_of_grid_points.append(prior[grid_loc])

    list_of_grid_points = np.array(list_of_grid_points)
    numpy_prior = np.append(list_of_grid_points, 1 - list_of_grid_points.sum())
    return numpy_prior
Exemplo n.º 2
0
def plot_prior(grid: UE4Grid, prior):
    fig = plt.figure()
    x, y = np.mgrid[0:grid.get_no_points_x() *
                    grid.get_lng_spacing():grid.get_lng_spacing(),
                    0:grid.get_no_points_y() *
                    grid.get_lat_spacing():grid.get_lat_spacing()]
    ax = fig.gca(projection='3d')
    prior = prior[:-1]
    z_lim = prior.max() * 1.02
    ax.set_zlim3d(0, z_lim)
    if prior.shape != (len(x), len(y)):
        prior = prior.reshape(len(x), len(y))
    ax.plot_wireframe(x, y, prior)
    ax.set_xlabel("physical x-axis of grid")
    ax.set_ylabel("physical y-axis of grid")
    ax.set_zlabel("Initial prob. evidence present at grid location")
    return fig