-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LGS-173 Merge branch 'bernoulli_and_node'
- Loading branch information
Showing
5 changed files
with
209 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.0.2 | ||
0.0.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import numpy as np | ||
|
||
from beliefs.factors.cpd import TabularCPD | ||
|
||
|
||
class BernoulliAndCPD(TabularCPD): | ||
"""CPD class for a Bernoulli random variable whose relationship to its | ||
parents (also Bernoulli random variables) is described by AND logic. | ||
If all of the variable's parents are 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 = None | ||
|
||
@property | ||
def values(self): | ||
if self._values is None: | ||
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]) | ||
|
||
# values are stored as a row vector using an ordering such that | ||
# the right-most variables as defined in [variable].extend(parents) | ||
# cycle through their values the fastest. | ||
return np.array( | ||
[1.]*(2**(k-1)-1) + [0.] + [0.,]*(2**(k-1)-1) + [1.] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters