Example #1
0
    def k_fold_cv(self, X, Y, k=10):
        indx = np.array([i for i in range(self.dataXshape[0])])
        indx = np.random.permutation(indx)
        indx_subset = np.split(indx, k)

        ret_val = 0.
        for ki in range(k):
            temp_ind = [
                i for i in range(X.shape[0]) if i not in indx_subset[ki]
            ]
            temp_X = X[temp_ind]
            temp_Y = Y[temp_ind]

            w = self.post_mean
            lam = self.lam_memo
            alpha = self.prec_weight

            w, lam, alpha = laplace_approximation.maximized_approximate_log_marginal_likelihood(
                temp_Y, temp_X, mo_model, w, lam, alpha, warmup_num=0)
            train_ml = laplace_approximation.approximate_log_marginal_likelihood(
                temp_Y, temp_X, w, mo_model, lam, alpha)
            total_ml = laplace_approximation.approximate_log_marginal_likelihood(
                Y, X, w, mo_model, lam, alpha)
            ret_val += total_ml - train_ml
            print(ki, "ave_cv", ret_val / (ki + 1), "  temp_cv",
                  total_ml - train_ml)
        logger.log("k =", k)
        logger.log("PM_CV =", ret_val / (1. * k))
        return ret_val / (1. * k)
Example #2
0
    def __init__(self,
                 X,
                 Y,
                 init_state=None,
                 ):
        self.datasize=X.shape
        if init_state is None:
            self.init_state_mean=np.array([np.pi, 0.])
        else:
            self.init_state_mean=init_state

        y1=Y[:,0] - X[:,1]
        self.noise_var1=np.dot(y1,y1)/y1.shape[0]
        self.log_evidence_y1 = - 0.5 * y1.shape[0] *( 1. + np.log( 2.*np.pi*(self.noise_var1)))

        # remove
        '''  
        blr_x = -np.sin(X[:,0]).reshape((Y[:,0].shape[0]),1) # x' = - sin(theta)
        blr_y = Y[:,1]- alpha3*X[:,2]                        # y' = y2 - alpha3*u
        self.blr_m, self.log_evidence_y2 = blr(blr_x, blr_y)

        self.post_mean   = self.blr_m.coef_
        self.post_var    = self.blr_m.sigma_[0]
        self.prec_weight = self.blr_m.lambda_
        self.noise_var2   = 1./self.blr_m.alpha_
        ''' 
        # remove

        beta_init=np.array([0.1])  # add
        alpha_init=np.array([0.1]) # add
        w_init=np.array([-2.])      # add

        import laplace_approximation # add
        #w, beta, alpha = laplace_approximation.maximized_approximate_log_marginal_likelihood(Y[:,1].reshape(X.shape[0],1),X,so_model_11,w_init,beta_init,alpha_init) # add
        #self.log_evidence_y2 = laplace_approximation.approximate_log_marginal_likelihood(Y[:,1].reshape(X.shape[0],1),X,w,so_model_11,beta,alpha)                    # add
        minus_w_init=-w_init      # add
        print("minus_w_init=",minus_w_init)
        minus_w, beta, alpha = laplace_approximation.maximized_approximate_log_marginal_likelihood(Y[:,1].reshape(X.shape[0],1),X,so_model_11_minus,minus_w_init,beta_init,alpha_init) # add
        w = -minus_w # add
        self.log_evidence_y2 = laplace_approximation.approximate_log_marginal_likelihood(Y[:,1].reshape(X.shape[0],1),X,minus_w,so_model_11_minus,beta,alpha)                    # add

        self.post_mean    = w # add
        #self.post_var     = 1./laplace_approximation.hesse_of_w(Y[:,1].reshape(X.shape[0],1),X,w,so_model_11,beta,alpha) # add
        self.post_var     = 1./laplace_approximation.hesse_w(Y[:,1].reshape(X.shape[0],1),X,minus_w,so_model_11_minus,beta,alpha) # add
        self.prec_weight  = alpha   # add
        self.noise_var2   = 1./beta # add

        self.post_var_square   = np.sqrt(self.post_var)
        self.noise_var1_square = np.sqrt(self.noise_var1)
        self.noise_var2_square = np.sqrt(self.noise_var2)

        self.thdot_clip_value=15. # To accelerate planning process, we use this clipping value. 
                                  # More large value or no clipping is also OK, while it requires more simulation samples for planning.
                                  # For main results, this value has no effect to the finally planned policy. The effect is only to limit the search space.

        if 2==parameter_sampling_flag:
            self.parameter_sample = self.post_mean + np.random.normal(0., self.post_var_square)
Example #3
0
    def __init__(
        self,
        X,
        Y,
        init_state=None,
        parameter_sampling_flag=0,
        noiseless_flag=False,
    ):
        self.dataXshape = X.shape
        self.dataYshape = Y.shape
        self.init_state_mean = np.array((0., 0., np.pi, 0.))
        '''
        if init_state is None:
            self.init_state_mean=np.array((0.,0.,np.pi,0.))
        else:
            self.init_state_mean=init_state
        '''

        w_init = 0.1 * np.ones(dimw)
        #lam_init = np.array([10000., 10000., 10000., 10000.])
        lam_init = np.array([100., 100., 100., 100.])
        alpha_init = np.array([5.])

        unload_flag = 1
        import os
        if os.path.isfile("./result_apply/temp_param_cartpole.csv"):
            param_load = np.loadtxt('./result_apply/temp_param_cartpole.csv',
                                    delimiter=',')
            if abs(param_load[0] - self.dataXshape[0]) < 1:
                unload_flag = 0
                w = np.array([param_load[1], param_load[2]])
                lam = np.array([
                    param_load[3], param_load[4], param_load[5], param_load[6]
                ])
                alpha = np.array([param_load[7]])
        if unload_flag:
            w, lam, alpha = laplace_approximation.maximized_approximate_log_marginal_likelihood(
                Y, X, mo_model, w_init, lam_init, alpha_init)

        param_save = np.array([
            self.dataXshape[0], w[0], w[1], lam[0], lam[1], lam[2], lam[3],
            alpha[0]
        ])
        np.savetxt('./result_apply/temp_param_cartpole.csv',
                   param_save,
                   delimiter=',')

        self.prec_weight = alpha
        self.post_mean = w
        self.post_var = inv(
            laplace_approximation.hesse_w(Y, X, w, mo_model, lam, alpha))
        self.noise_var1 = 1. / lam[0]
        self.noise_var2 = 1. / lam[1]
        self.noise_var3 = 1. / lam[2]
        self.noise_var4 = 1. / lam[3]
        self.lam_memo = lam

        self.log_evidence_memo = laplace_approximation.approximate_log_marginal_likelihood(
            Y, X, w, mo_model, lam, alpha)

        self.parameter_sampling_flag = parameter_sampling_flag
        self.noiseless_flag = noiseless_flag
        if 2 == self.parameter_sampling_flag:
            self.parameter_sample = np.random.multivariate_normal(
                self.post_mean, self.post_var)
            print("self.parameter_sample =", self.parameter_sample)
        if 3 == self.parameter_sampling_flag:
            self.parameter_sample = self.post_mean

        self.sqrt_noise_var1 = np.sqrt(self.noise_var1)
        self.sqrt_noise_var2 = np.sqrt(self.noise_var2)
        self.sqrt_noise_var3 = np.sqrt(self.noise_var3)
        self.sqrt_noise_var4 = np.sqrt(self.noise_var4)

        if self.noiseless_flag:
            self.sqrt_noise_var1 = 0.
            self.sqrt_noise_var2 = 0.
            self.sqrt_noise_var3 = 0.
            self.sqrt_noise_var4 = 0.
lam_init=0.1*lam_true
alpha_init=0.1*prior_alpha
eb_lam, eb_alpha  = test_la.empirical_bayes(y,X,w_map,mo_model,lam_init,alpha_init)
print("empirical_bayes_alpha =",eb_alpha)
print("empirical_bayes_lam =",eb_lam)
print("lam_true            =",lam_true)




# test 3 (Bayesian Regression ... repeating empirical Bayes and MAP estimation)
print("\n\n###  test 3  ###")
lam_init=0.1*lam_true
alpha_init=0.1*prior_alpha
w_init=0.1*w_true
w_map2, lam, alpha = test_la.maximized_approximate_log_marginal_likelihood(y,X,mo_model,w_init,lam_init,alpha_init)
print("w_map          =",w_map2,", obtained under empirical Bayes hyperparameters.")
print("w_map (test 1) =",w_map ,", obtained under initial hyperparameters.")
print("w_true         =",w_true)

print("lam      =",lam)
print("lam_true =",lam_true)

print("alpha =",alpha)


'''
print(X)
print(X_double)
y22a_double = np.vstack([y22a, y22a])
w2_map_analytical=marginal_likelihood_blr.map_estimator(prior_alpha,lam_true[0],X2_double,y22a_double).T[0]