Exemplo n.º 1
0
    def conditional_sample(self, data):
        assert isinstance(data, XSeqDataSet)
        sample = np.zeros(data.arrays['X'].shape)

        input_indices = list(range(self.output_index)) + \
                        list(range(self.output_index + 1, data.arrays['X'].shape[1], 1))

        inputs = data.variable_subsets(input_indices)
        outputs = data.variable_subsets([self.output_index])

        X_data = inputs

        XY_data = XYDataSet()
        XY_data.labels['X'] = inputs.labels['X']
        XY_data.labels['Y'] = outputs.labels['X']
        XY_data.arrays['X'] = inputs.arrays['X']
        XY_data.arrays['Y'] = outputs.arrays['X']

        X_sample = self.input_distribution.conditional_sample(X_data)
        XY_data.arrays['X'] = X_sample
        Y_sample = self.output_distribution.conditional_samples(XY_data)

        sample[:, input_indices] = X_sample
        sample[:, self.output_index] = Y_sample
        return sample
Exemplo n.º 2
0
    def llh(self, data):
        assert isinstance(data, XSeqDataSet)
        sample = np.zeros(data.arrays['X'].shape)

        input_indices = list(range(self.output_index)) + \
                        list(range(self.output_index + 1, data.arrays['X'].shape[1], 1))

        inputs = data.variable_subsets(input_indices)
        outputs = data.variable_subsets([self.output_index])

        X_data = inputs

        XY_data = XYDataSet()
        XY_data.labels['X'] = inputs.labels['X']
        XY_data.labels['Y'] = outputs.labels['X']
        XY_data.arrays['X'] = inputs.arrays['X']
        XY_data.arrays['Y'] = outputs.arrays['X']

        llh = self.input_distribution.llh(X_data)
        llh += self.output_distribution.llh(XY_data)
        return llh
Exemplo n.º 3
0
    def conditional_sample(self, data):
        assert isinstance(data, XSeqDataSet)
        sample = np.zeros(data.arrays['X'].shape)

        input_indices = list(range(self.output_index)) + \
                        list(range(self.output_index + 1, data.arrays['X'].shape[1], 1))

        inputs = data.variable_subsets(input_indices)
        outputs = data.variable_subsets([self.output_index])

        X_data = inputs

        XY_data = XYDataSet()
        XY_data.labels['X'] = inputs.labels['X']
        XY_data.labels['Y'] = outputs.labels['X']
        XY_data.arrays['X'] = inputs.arrays['X']
        XY_data.arrays['Y'] = outputs.arrays['X']

        X_sample = self.input_distribution.conditional_sample(X_data)
        XY_data.arrays['X'] = X_sample
        Y_sample = self.output_distribution.conditional_samples(XY_data)

        sample[:, input_indices] = X_sample
        sample[:, self.output_index] = Y_sample
        return sample
Exemplo n.º 4
0
    def fit(self):
        best_llh = -np.Inf
        best_distribution = None
        # best_index = None

        for input_var in range(self.data.arrays['X'].shape[1]):
            inputs = self.data.variable_subsets(list(range(input_var)) +
                                                list(range(input_var + 1, self.data.arrays['X'].shape[1], 1)))
            outputs = self.data.variable_subsets([input_var])
            input_agent = self.input_learner()

            input_agent.load_data(inputs)
            input_agent.fit()

            XY_data = XYDataSet()
            XY_data.labels['X'] = inputs.labels['X']
            XY_data.labels['Y'] = outputs.labels['X']
            XY_data.arrays['X'] = inputs.arrays['X']
            XY_data.arrays['Y'] = outputs.arrays['X']

            output_agent = self.output_learner()
            output_agent.load_data(XY_data)
            output_agent.fit()

            conditional_distribution = RegressionDAG(input_var, input_agent.conditional_distributions[0],
                                                                output_agent.conditional_distributions[0])

            score = LLHScorer.score(self.data, conditional_distribution)
            if score > best_llh:
                best_llh = score
                best_distribution = conditional_distribution
                # best_index = input_var

        # print best_index

        self.conditional_distributions = [best_distribution]
Exemplo n.º 5
0
    def llh(self, data):
        assert isinstance(data, XSeqDataSet)
        sample = np.zeros(data.arrays['X'].shape)

        input_indices = list(range(self.output_index)) + \
                        list(range(self.output_index + 1, data.arrays['X'].shape[1], 1))

        inputs = data.variable_subsets(input_indices)
        outputs = data.variable_subsets([self.output_index])

        X_data = inputs

        XY_data = XYDataSet()
        XY_data.labels['X'] = inputs.labels['X']
        XY_data.labels['Y'] = outputs.labels['X']
        XY_data.arrays['X'] = inputs.arrays['X']
        XY_data.arrays['Y'] = outputs.arrays['X']

        llh = self.input_distribution.llh(X_data)
        llh += self.output_distribution.llh(XY_data)
        return llh
Exemplo n.º 6
0
    def fit(self):
        best_llh = -np.Inf
        best_distribution = None
        # best_index = None

        for input_var in range(self.data.arrays['X'].shape[1]):
            inputs = self.data.variable_subsets(
                list(range(input_var)) +
                list(range(input_var + 1, self.data.arrays['X'].shape[1], 1)))
            outputs = self.data.variable_subsets([input_var])
            input_agent = self.input_learner()

            input_agent.load_data(inputs)
            input_agent.fit()

            XY_data = XYDataSet()
            XY_data.labels['X'] = inputs.labels['X']
            XY_data.labels['Y'] = outputs.labels['X']
            XY_data.arrays['X'] = inputs.arrays['X']
            XY_data.arrays['Y'] = outputs.arrays['X']

            output_agent = self.output_learner()
            output_agent.load_data(XY_data)
            output_agent.fit()

            conditional_distribution = RegressionDAG(
                input_var, input_agent.conditional_distributions[0],
                output_agent.conditional_distributions[0])

            score = LLHScorer.score(self.data, conditional_distribution)
            if score > best_llh:
                best_llh = score
                best_distribution = conditional_distribution
                # best_index = input_var

        # print best_index

        self.conditional_distributions = [best_distribution]