Multiply the factor by another :doc:`TableCPDFactor `. Multiplying factors means taking the union of the scopes, and for each combination of variables in the scope, multiplying together the probabilities from each factor that that combination will be found.
(self, other)
| 96 | '''A dict of {vertex: value} pairs for each vertex in *scope*, where vertex is the name of the vertex and value is the stride of that vertex in the *vals* array.''' |
| 97 | |
| 98 | def multiplyfactor(self, other): # cf. PGM 359 |
| 99 | ''' |
| 100 | Multiply the factor by another :doc:`TableCPDFactor <tablecpdfactor>`. Multiplying factors means taking the union of the scopes, and for each combination of variables in the scope, multiplying together the probabilities from each factor that that combination will be found. |
| 101 | |
| 102 | Arguments: |
| 103 | 1. *other* -- An instance of the :doc:`TableCPDFactor <tablecpdfactor>` class representing the factor to multiply by. |
| 104 | |
| 105 | Attributes modified: |
| 106 | *vals*, *scope*, *stride*, *card* -- Modified to reflect the data of the new product factor. |
| 107 | |
| 108 | For more information cf. Koller et al. 359. |
| 109 | |
| 110 | ''' |
| 111 | if (not isinstance(other, TableCPDFactor)): |
| 112 | msg = "Error: in method 'multiplyfactor', input was not a TableCPDFactor instance" |
| 113 | sys.exit(msg) |
| 114 | j = 0 |
| 115 | k = 0 |
| 116 | result = dict() |
| 117 | |
| 118 | # merge scopes |
| 119 | result["scope"] = self.scope |
| 120 | result["card"] = self.card |
| 121 | for x in range(len(other.scope)): |
| 122 | try: |
| 123 | result["scope"].index(other.scope[x]) |
| 124 | except: |
| 125 | result["scope"].append(other.scope[x]) |
| 126 | result["card"].append(other.card[x]) |
| 127 | |
| 128 | # calculate possible combinations of scope variables |
| 129 | possiblevals = 1 |
| 130 | for val in result["card"]: |
| 131 | possiblevals *= val |
| 132 | |
| 133 | # algorithm (see book) |
| 134 | assignment = [0 for l in range(len(result["scope"]))] |
| 135 | result["vals"] = [] |
| 136 | for _ in range(possiblevals): |
| 137 | result["vals"].append(self.vals[j] * other.vals[k]) |
| 138 | for l in range(len(result["scope"])): |
| 139 | assignment[l] = assignment[l] + 1 |
| 140 | if (assignment[l] == result["card"][l]): |
| 141 | assignment[l] = 0 |
| 142 | try: |
| 143 | j = j - (result["card"][l] - 1) * self.stride[result["scope"][l]] |
| 144 | except: |
| 145 | pass |
| 146 | try: |
| 147 | k = k - (result["card"][l] - 1) * other.stride[result["scope"][l]] |
| 148 | except: |
| 149 | pass |
| 150 | else: |
| 151 | try: |
| 152 | j = j + self.stride[result["scope"][l]] |
| 153 | except: |
| 154 | pass |
| 155 | try: |
no outgoing calls