From 8dc7ae89677fca16ee974a30cff8c4df53c955ce Mon Sep 17 00:00:00 2001 From: Cathy Yeh Date: Sun, 3 Dec 2017 19:16:32 -0800 Subject: PR comments --- beliefs/factors/BernoulliOrCPD.py | 42 ---------------------------------- beliefs/factors/CPD.py | 45 ------------------------------------- beliefs/factors/bernoulli_or_cpd.py | 42 ++++++++++++++++++++++++++++++++++ beliefs/factors/cpd.py | 45 +++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 87 deletions(-) delete mode 100644 beliefs/factors/BernoulliOrCPD.py delete mode 100644 beliefs/factors/CPD.py create mode 100644 beliefs/factors/bernoulli_or_cpd.py create mode 100644 beliefs/factors/cpd.py (limited to 'beliefs/factors') diff --git a/beliefs/factors/BernoulliOrCPD.py b/beliefs/factors/BernoulliOrCPD.py deleted file mode 100644 index 2c6a31e..0000000 --- a/beliefs/factors/BernoulliOrCPD.py +++ /dev/null @@ -1,42 +0,0 @@ -import numpy as np - -from beliefs.factors.CPD import TabularCPD - - -class BernoulliOrCPD(TabularCPD): - """CPD class for a Bernoulli random variable whose relationship to its - parents (also Bernoulli random variables) is described by OR logic. - - If at least one of the variable's parents is True, then the variable - is True, and False otherwise. - """ - def __init__(self, variable, parents=[]): - """ - Args: - variable: int or string - parents: optional, list of int and/or strings - """ - super().__init__(variable=variable, - variable_card=2, - parents=parents, - parents_card=[2]*len(parents), - values=[]) - self._values = [] - - @property - def values(self): - if not any(self._values): - self._values = self._build_kwise_values_array(len(self.variables)) - self._values = self._values.reshape(self.cardinality) - return self._values - - @staticmethod - def _build_kwise_values_array(k): - # special case a completely independent factor, and - # return the uniform prior - if k == 1: - return np.array([0.5, 0.5]) - - return np.array( - [1.,] + [0.]*(2**(k-1)-1) + [0.,] + [1.]*(2**(k-1)-1) - ) diff --git a/beliefs/factors/CPD.py b/beliefs/factors/CPD.py deleted file mode 100644 index a286aaa..0000000 --- a/beliefs/factors/CPD.py +++ /dev/null @@ -1,45 +0,0 @@ -import numpy as np - - -class TabularCPD: - """ - Defines the conditional probability table for a discrete variable - whose parents are also discrete. - - TODO: have this inherit from DiscreteFactor implementing explicit factor methods - """ - def __init__(self, variable, variable_card, - parents=[], parents_card=[], values=[]): - """ - Args: - variable: int or string - variable_card: int - parents: optional, list of int and/or strings - parents_card: optional, list of int - values: optional, 2d list or array - """ - self.variable = variable - self.parents = parents - self.variables = [variable] + parents - self.cardinality = [variable_card] + parents_card - self._values = np.array(values) - - @property - def values(self): - return self._values - - def get_values(self): - """ - Returns the tabular cpd form of the values. - """ - if len(self.cardinality) == 1: - return self.values.reshape(1, np.prod(self.cardinality)) - else: - return self.values.reshape(self.cardinality[0], np.prod(self.cardinality[1:])) - - def copy(self): - return self.__class__(self.variable, - self.cardinality[0], - self.parents, - self.cardinality[1:], - self._values) diff --git a/beliefs/factors/bernoulli_or_cpd.py b/beliefs/factors/bernoulli_or_cpd.py new file mode 100644 index 0000000..bfb3a95 --- /dev/null +++ b/beliefs/factors/bernoulli_or_cpd.py @@ -0,0 +1,42 @@ +import numpy as np + +from beliefs.factors.cpd import TabularCPD + + +class BernoulliOrCPD(TabularCPD): + """CPD class for a Bernoulli random variable whose relationship to its + parents (also Bernoulli random variables) is described by OR logic. + + If at least one of the variable's parents is True, then the variable + is True, and False otherwise. + """ + def __init__(self, variable, parents=[]): + """ + Args: + variable: int or string + parents: optional, list of int and/or strings + """ + super().__init__(variable=variable, + variable_card=2, + parents=parents, + parents_card=[2]*len(parents), + values=[]) + self._values = [] + + @property + def values(self): + if not any(self._values): + self._values = self._build_kwise_values_array(len(self.variables)) + self._values = self._values.reshape(self.cardinality) + return self._values + + @staticmethod + def _build_kwise_values_array(k): + # special case a completely independent factor, and + # return the uniform prior + if k == 1: + return np.array([0.5, 0.5]) + + return np.array( + [1.,] + [0.]*(2**(k-1)-1) + [0.,] + [1.]*(2**(k-1)-1) + ) diff --git a/beliefs/factors/cpd.py b/beliefs/factors/cpd.py new file mode 100644 index 0000000..a286aaa --- /dev/null +++ b/beliefs/factors/cpd.py @@ -0,0 +1,45 @@ +import numpy as np + + +class TabularCPD: + """ + Defines the conditional probability table for a discrete variable + whose parents are also discrete. + + TODO: have this inherit from DiscreteFactor implementing explicit factor methods + """ + def __init__(self, variable, variable_card, + parents=[], parents_card=[], values=[]): + """ + Args: + variable: int or string + variable_card: int + parents: optional, list of int and/or strings + parents_card: optional, list of int + values: optional, 2d list or array + """ + self.variable = variable + self.parents = parents + self.variables = [variable] + parents + self.cardinality = [variable_card] + parents_card + self._values = np.array(values) + + @property + def values(self): + return self._values + + def get_values(self): + """ + Returns the tabular cpd form of the values. + """ + if len(self.cardinality) == 1: + return self.values.reshape(1, np.prod(self.cardinality)) + else: + return self.values.reshape(self.cardinality[0], np.prod(self.cardinality[1:])) + + def copy(self): + return self.__class__(self.variable, + self.cardinality[0], + self.parents, + self.cardinality[1:], + self._values) -- cgit v1.2.3