Exemplo n.º 1
0
def generate_uniform_prior(grid: UE4Grid,
                           initial_belief_sum=0.5,
                           fixed_value=None):
    '''Generates a uniform prior which equates to delta/|grid|. The sum of each grid location prior is equal to initial_belief_sum. If fixed_value provided, 
    each location in the prior will have fixed_value as prior'''
    if fixed_value and 0 <= fixed_value <= 1:
        prior_val = fixed_value
    else:
        prior_val = initial_belief_sum / len(grid.get_grid_points())
    return np.array([prior_val for grid_loc in grid.get_grid_points()] +
                    [1 - prior_val * grid.get_no_grid_points()])
Exemplo n.º 2
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