def to_conditional_random_variable(self):
        query, evidences = self._remove_conditional()

        if ... in evidences:
            variables = self._probability_distribution.variables
            evidences = Parser.lazy_notation(variables, subset=evidences, ignore=query)

        return Conditional(SetOfRandomVariable(query), SetOfRandomVariable(evidences))
    def reduction(self, *variables: RandomVariable) -> ProbabilityDistribution:
        """
        :param list/tuple variables:
        """
        variables = SetOfRandomVariable(variables)
        P = self

        events = Utils.extract_events(self, variables)
        marginalized = P(*variables.only_random_variables())

        # Fixme - Any event not presents in joint distribution (index)
        try:
            reduction = marginalized.series.loc[events]

            # Fixme - All possibilities
            #new_index = pd.MultiIndex.from_product(events, names=variables.names)
            #reduction = reduction.reindex(new_index, fill_value=0, copy=False)

        except:
            reduction = 0

        # FIXME Extract
        if not isinstance(reduction, pd.Series):
            names = list(variable.name for variable in variables)
            if len(names) > 1:
                index = pd.MultiIndex.from_arrays(events, names=names)
            else:
                index = pd.Index(events, name=names[0])

            reduction = pd.Series([reduction], index=index)
            reduction.name = 'P({})'.format(variables)  # FIXME

        # FIXME Returns a ProbabilityDistribution, not a Joint distribuion
        return JointDistribution(reduction)
    def from_series(series: Series) -> 'JointDistribution':
        series = series.copy()
        variables = tuple(RandomVariable(variable_name) for variable_name in series.index.names)

        series.name = Builder._make_name(SetOfRandomVariable(variables))

        return JointDistribution(series).normalize()  # FIXME
Example #4
0
    def lazy_notation(
        variables: SetOfRandomVariable,
        subset: Tuple[Union[RandomVariable, type(...)], ...],
        ignore: Union[SetOfRandomVariable,
                      None] = None) -> SetOfRandomVariable:
        """
        Replace ellipsis (...) in subset for variables - subset - ignore
        """
        if Ellipsis not in subset:
            return SetOfRandomVariable(subset)

        ignore = SetOfRandomVariable(tuple()) if ignore is None else ignore
        index = subset.index(...)

        head = subset[:index]
        middle = tuple(set(variables) - set(subset) - set(ignore))
        tail = subset[index + 1:]

        return SetOfRandomVariable(head + middle + tail)
    def joint_distribution(self, *variables: RandomVariable):
        variables = Parser.lazy_notation(self.variables, subset=variables)

        series = self.series
        series = series.groupby(level=variables.names).sum()

        new_probability = JointDistribution(series)
        new_probability.series.rename('P({})'.format(SetOfRandomVariable(tuple(variables))), inplace=True)

        return new_probability
Example #6
0
    def variables(self) -> SetOfRandomVariable:
        index = self.series.index
        names = [index.name] if type(index) == pd.Index else index.names

        return SetOfRandomVariable(
            tuple(RandomVariable(column) for column in names))
 def _make_name(variables: SetOfRandomVariable) -> str:
     return 'P({})'.format(variables.__repr__())