Example #1
0
    def __init__(self, X=None, y=None, theta=None, model_order=3):
        if X is None:
            raise (ValueError, "Data matrix must be specified")

        if len(X.shape) == 1:
            self.D = 1
        else:
            self.D = X.shape[1]

        # Force a default value and check datatype
        if model_order is None:
            model_order = 3
        elif type(model_order) is not int:
            model_order = int(model_order)

        self._n_params = 1 + self.D * model_order
        self._model_order = model_order

        print("initialising BLR ( order", model_order, ")")
        if (theta is None) or (len(theta) != self._n_params):
            print("Using default hyperparameters")
            self.theta0 = np.zeros(self._n_params)
        else:
            self.theta0 = theta
        self.theta = self.theta0

        if (theta is not None) and (y is not None):
            self.Phi = create_poly_basis(X, self._model_order)
            self.gpr = BLR(theta, self.Phi, y)
        else:
            self.gpr = BLR()
Example #2
0
    def predict(self, X, y, Xs, theta=None):
        if theta is None:
            theta = self.theta

        Phis = create_poly_basis(Xs, self._model_order)
        yhat, s2 = self.blr.predict(theta, self.Phi, y, Phis)

        return yhat, s2
Example #3
0
    def estimate(self, X, y, theta=None):
        if not hasattr(self, 'Phi'):
            self.Phi = create_poly_basis(X, self._model_order)
        if len(y.shape) > 1:
            y = y.ravel()

        if theta is None:
            theta = self.theta0
            self.blr = BLR(theta, self.Phi, y)

        self.theta = self.blr.estimate(theta, self.Phi, y)

        return self.theta
Example #4
0
    def estimate(self, X, y, **kwargs):
        theta = kwargs.pop('theta', None)

        if not hasattr(self, 'Phi'):
            self.Phi = create_poly_basis(X, self._model_order)
        if len(y.shape) > 1:
            y = y.ravel()

        if theta is None:
            theta = self.theta0
            self.blr = BLR(theta, self.Phi, y, var_groups=self.var_groups)

        self.theta = self.blr.estimate(theta,
                                       self.Phi,
                                       y,
                                       optimizer=self.optim_alg)

        return self
Example #5
0
    def predict(self, Xs, X=None, y=None, **kwargs):
        theta = kwargs.pop('theta', None)
        
        if theta is None:
            theta = self.theta

        Phis = create_poly_basis(Xs, self._model_order)
        
        if 'var_groups_test' in kwargs:
            var_groups_test_file = kwargs.pop('var_groups_test')
            if var_groups_test_file.endswith('.pkl'):
                var_groups_te = pd.read_pickle(var_groups_test_file)
            else:
                var_groups_te = np.loadtxt(var_groups_test_file)
        else:
            var_groups_te = None
            
        yhat, s2 = self.blr.predict(theta, self.Phi, y, Phis, 
                                    var_groups_test=var_groups_te)
        
        return yhat, s2
Example #6
0
    def __init__(self, **kwargs):  #X=None, y=None, theta=None,
        X = kwargs.pop('X', None)
        y = kwargs.pop('y', None)
        theta = kwargs.pop('theta', None)
        self.optim_alg = kwargs.pop('optimizer', 'cg')

        if X is None:
            raise (ValueError, "Data matrix must be specified")

        if len(X.shape) == 1:
            self.D = 1
        else:
            self.D = X.shape[1]

        # Parse model order
        if kwargs is None:
            model_order = 1
        elif 'configparam' in kwargs:
            model_order = kwargs.pop('configparam')
        elif 'model_order' in kwargs:
            model_order = kwargs.pop('model_order')
        else:
            model_order = 1

        # Force a default value and check datatype
        if model_order is None:
            model_order = 1
        if type(model_order) is not int:
            model_order = int(model_order)

        if 'var_groups' in kwargs:
            var_groups_file = kwargs.pop('var_groups')
            if var_groups_file.endswith('.pkl'):
                self.var_groups = pd.read_pickle(var_groups_file)
            else:
                self.var_groups = np.loadtxt(var_groups_file)
            var_ids = set(self.var_groups)
            var_ids = sorted(list(var_ids))
            n_beta = len(var_ids)
        else:
            self.var_groups = None
            n_beta = 1

        # are we using ARD?
        if 'use_ard' in kwargs:
            self.use_ard = kwargs.pop('use_ard')
        else:
            self.use_ard = False
        if self.use_ard:
            n_alpha = self.D * model_order
        else:
            n_alpha = 1

        self._n_params = n_alpha + n_beta
        self._model_order = model_order

        print("initialising BLR ( order", model_order, ")")
        if (theta is None) or (len(theta) != self._n_params):
            print("Using default hyperparameters")
            self.theta0 = np.zeros(self._n_params)
        else:
            self.theta0 = theta
        self.theta = self.theta0

        if (theta is not None) and (y is not None):
            self.Phi = create_poly_basis(X, self._model_order)
            self.blr = BLR(theta, self.Phi, y)
        else:
            self.blr = BLR()