Beispiel #1
0
    def stratify(self, variable):
        """ Create a new variable, stratified by the levels of a Factor.

        Parameters
        ----------
        variable : str or a simple sympy expression whose string representation
            are all lower or upper case letters, i.e. it can be interpreted
            as a name

        Returns
        -------
        formula : Formula
            Formula whose mean has one parameter named variable%d, for each
            level in self.levels
        
        Examples
        --------
        >>> f = Factor('a', ['x','y'])
        >>> sf = f.stratify('theta')
        >>> sf.mean
        _theta0*a_x + _theta1*a_y
        """
        if not set(str(variable)).issubset(lowercase +
                                           uppercase + '0123456789'):
            raise ValueError('variable should be interpretable as a '
                             'name and not have anything but digits '
                             'and numbers')
        variable = sympy.sympify(variable)
        f = Formula(self.formula.terms, char=variable)
        f.name = self.name
        return f
Beispiel #2
0
def stratify(factor, variable):
    """ Create a new variable, stratified by the levels of a Factor.

    Parameters
    ----------
    variable : str or a simple sympy expression whose string representation
        are all lower or upper case letters, i.e. it can be interpreted
        as a name

    Returns
    -------
    formula : Formula
        Formula whose mean has one parameter named _variable%d, for each
        level in factor.levels

    Examples
    --------
    >>> f = Factor('a', ['x','y'])
    >>> sf = f.stratify('theta')
    >>> sf.mean
    _theta0*a_x + _theta1*a_y
    """
    if not set(str(variable)).issubset(lowercase + uppercase + '0123456789'):
        raise ValueError('variable should be interpretable as a '
                         'name and not have anything but digits '
                         'and letters')
    variable = sympy.sympify(variable)
    f = Formula(factor.formula.terms, char=variable)
    f.name = factor.name
    return f
Beispiel #3
0
    def formulaVars(self, f: Formula) -> Set[str]:
        """ List variables in formula f.

        This depends on correct implementation of subf() and Variable.name.
        """
        if isinstance(f, Variable):
            return set([f.name()])
        return set.union(set(), *(self.formulaVars(sf) for sf in f.subf()))
Beispiel #4
0
 def formulaIsSatisfied(self, f: Formula, v: Valuation):
     if isinstance(f, Variable):
         return v[f.name()]
     elif isinstance(f, Negation):
         return not self.formulaIsSatisfied(f.subf()[0], v)
     elif isinstance(f, Conjunction):
         return all(self.formulaIsSatisfied(sf, v) for sf in f.subf())
     elif isinstance(f, Disjunction):
         return any(self.formulaIsSatisfied(sf, v) for sf in f.subf())
     elif isinstance(f, Implication):
         return (not self.formulaIsSatisfied(
             f.subf()[0], v)) or self.formulaIsSatisfied(f.subf()[1], v)
     elif isinstance(f, Equivalence):
         return self.formulaIsSatisfied(f.subf()[0],
                                        v) == self.formulaIsSatisfied(
                                            f.subf()[1], v)
     else:
         raise TypeError("Formula expected, got %s" % repr(f))