Exemplo n.º 1
0
def plot_res(res, params, dense_grid_size, f, borders, alg_name):
    dense_grid = np.linspace(borders[0], borders[1], dense_grid_size)

    cov_func = SquaredExponential.from_parameters_vector(
        np.exp(res.eta_mean_history[-1]))
    mean, cov = cov_func.predict(np.expand_dims(dense_grid, axis=1), params.x,
                                 np.exp(res.sigma_mean_history[-1]),
                                 res.g_mean_history[-1])

    lower = mean - 2 * np.sqrt(np.diag(cov))
    upper = mean + 2 * np.sqrt(np.diag(cov))

    df = pd.DataFrame({'iter': range(1, params.T + 1)})
    fig, ax = plt.subplots(1)
    ax.fill_between(dense_grid,
                    lower,
                    upper,
                    color='aquamarine',
                    edgecolor='blue')
    ax.plot(dense_grid, mean, 'b', label='mean')
    ax.plot(dense_grid, f(dense_grid), 'r', label='true f')
    ax.set_xlabel('x')
    ax.legend()
    plt.savefig('graphics_new_single/{}_predictions.eps'.format(alg_name),
                format='eps')

    if not np.any(np.isnan(res.eta_last_ensemble)):
        fig, ax = plt.subplots(1)
        sns.distplot(res.eta_last_ensemble[:, 0], label='log variance', ax=ax)
        sns.distplot(res.eta_last_ensemble[:, 1],
                     label='log lengthscale',
                     ax=ax)
        ax.legend()
        plt.savefig('graphics_new_single/{}_eta_distplot.eps'.format(alg_name),
                    format='eps')
Exemplo n.º 2
0
def plot_res(res, params, dense_grid_size, f, borders, alg_name, f_std, f_mean):
    x2 = np.linspace(borders[0, 0], borders[0, 1], dense_grid_size)
    x1 = np.linspace(borders[1, 0], borders[1, 1], dense_grid_size)
    dense_grid = np.stack(np.meshgrid(x1, x2), -1).reshape(-1, 2)

    cov_func = SquaredExponential.from_parameters_vector(np.exp(res.eta_mean_history[-1]))
    mean, cov = cov_func.predict(dense_grid, params.x,
                                         np.exp(res.sigma_mean_history[-1]), res.g_mean_history[-1])

    mean = np.exp(mean * f_std + f_mean)

    fig, ax = plt.subplots(figsize=(10, 20))
    m = Basemap(resolution='h',  # c, l, i, h, f or None
                projection='merc', llcrnrlon=borders[1, 0], llcrnrlat=borders[0, 0],
                urcrnrlon=borders[1, 1], urcrnrlat=borders[0, 1], ax=ax)

    m.drawmapboundary(fill_color='#46bcec')
    m.fillcontinents(color='#f2f2f2', lake_color='#46bcec')
    m.drawcoastlines()

    xc1, xc2 = m(dense_grid[:, 0], dense_grid[:, 1])
    xc1 = xc1.reshape((dense_grid_size, dense_grid_size))
    xc2 = xc2.reshape((dense_grid_size, dense_grid_size))
    mean = mean.reshape((dense_grid_size, dense_grid_size))
    cs = m.contour(xc1, xc2, mean, linewidths=1.5, ax=ax)

    cbar = m.colorbar(cs)
    cbar.set_label('price')

    plt.savefig('graphics/{}_mean.pdf'.format(alg_name), format='pdf')

    plt.close('all')

    fig, ax = plt.subplots(figsize=(10, 20))
    m = Basemap(resolution='c',
                projection='merc', llcrnrlon=borders[1, 0], llcrnrlat=borders[0, 0],
                urcrnrlon=borders[1, 1], urcrnrlat=borders[0, 1], ax=ax)

    m.drawmapboundary(fill_color='#46bcec')
    m.fillcontinents(color='#f2f2f2', lake_color='#46bcec')
    m.drawcoastlines()

    cov = np.diag(cov)
    cov = cov.reshape((dense_grid_size, dense_grid_size))
    cs = m.contour(xc1, xc2, cov, linewidths=1.5, ax=ax)

    cbar = m.colorbar(cs)
    cbar.set_label('cov')

    plt.savefig('graphics/{}_var.pdf'.format(alg_name), format='pdf')

    plt.close('all')
    fig, ax = plt.subplots(figsize=(10, 20))
    ax.plot(res.nmse_history)
    plt.savefig('graphics/{}_nmse.pdf'.format(alg_name), format='pdf')

    plt.close('all')
    fig, ax = plt.subplots(figsize=(10, 20))
    ax.plot(res.likelihood_history)
    plt.savefig('graphics/{}_likelihood.pdf'.format(alg_name), format='pdf')
Exemplo n.º 3
0
 def compute_log_likelihood(self, x_sample, f_true_sample):
     """
     :param x_sample: location of the points to predict
     :param f_true_sample: true value at sample points
     :return: log likelihood of true values at sample points given the estimated model
     """
     log_gp_params, log_sigma = self.get_log_mean_params()
     cov_func = SquaredExponential.from_parameters_vector(
         np.exp(log_gp_params))
     return cov_func.log_likelihood(f_true_sample, x_sample,
                                    np.exp(log_sigma))
Exemplo n.º 4
0
    def __predict_observations(self, x_new):
        for ens_idx in range(self.ensemble_size):

            if self.learn_gp_parameters and self.learn_sigma:
                state, log_hyperparameters, log_sigma = self.__decompose_augmented_state(
                    self.augmented_state_ensemble[ens_idx])
                cov_func = SquaredExponential.from_parameters_vector(
                    np.exp(log_hyperparameters))
                mean, _ = cov_func.predict(x_new,
                                           self.inducing_points_locations,
                                           np.exp(log_sigma), state)
            elif self.learn_gp_parameters:
                state, log_hyperparameters = self.__decompose_augmented_state(
                    self.augmented_state_ensemble[ens_idx])
                cov_func = SquaredExponential.from_parameters_vector(
                    np.exp(log_hyperparameters))
                mean, _ = cov_func.predict(x_new,
                                           self.inducing_points_locations,
                                           np.exp(self.initial_log_sigma),
                                           state)
            elif self.learn_sigma:
                state, log_sigma = self.__decompose_augmented_state(
                    self.augmented_state_ensemble[ens_idx])
                cov_func = SquaredExponential.from_parameters_vector(
                    np.exp(self.initial_log_gp_params))
                mean, _ = cov_func.predict(x_new,
                                           self.inducing_points_locations,
                                           np.exp(log_sigma), state)
            else:
                state = self.__decompose_augmented_state(
                    self.augmented_state_ensemble[ens_idx])
                cov_func = SquaredExponential.from_parameters_vector(
                    np.exp(self.initial_log_gp_params))
                mean, _ = cov_func.predict(x_new,
                                           self.inducing_points_locations,
                                           np.exp(self.initial_log_sigma),
                                           state)

            self.predictions[ens_idx] = mean
Exemplo n.º 5
0
    def compute_nmse(self, x_sample, f_true_sample):
        """
        :param x_sample: location of the points to predict
        :param f_true_sample: true value at sample points
        :return: NMSE between predicted and true values at sample points
        """
        log_hyperparameters, log_sigma = self.get_log_mean_params()
        g_mean = self.get_g_mean()

        cov_func = SquaredExponential.from_parameters_vector(
            np.exp(log_hyperparameters))
        mean, _ = cov_func.predict(x_sample, self.inducing_points_locations,
                                   np.exp(log_sigma), g_mean)

        return np.mean(
            np.sqrt((mean - f_true_sample)**2) / np.sqrt(f_true_sample**2))
Exemplo n.º 6
0
    def __predict_at_obs(self, x_sample, log_params, g_mean):
        if self.learn_gp_parameters and self.learn_sigma:
            log_gp_params = log_params[:-1]
            log_sigma = log_params[-1]

        elif self.learn_gp_parameters:
            log_gp_params = log_params
            log_sigma = self.initial_log_sigma

        elif self.learn_sigma:
            log_gp_params = self.initial_log_gp_params
            log_sigma = log_params

        else:
            log_gp_params = self.initial_log_gp_params
            log_sigma = self.initial_log_sigma

        cov_func = SquaredExponential.from_parameters_vector(
            np.exp(log_gp_params))
        mean, _ = cov_func.predict(x_sample, self.inducing_points_locations,
                                   np.exp(log_sigma), g_mean)

        return mean