Commit 8a85d6e3 authored by Roman Sarrazin-Gendron's avatar Roman Sarrazin-Gendron
Browse files

nothing changed

parent 623a49a4
......@@ -32,10 +32,9 @@ class BayesianModelSampling(Inference):
def __init__(self, model):
if not isinstance(model, BayesianModel):
raise TypeError("Model expected type: BayesianModel, got type: ", type(model))
self.topological_order = list(nx.topological_sort(model))
super(BayesianModelSampling, self).__init__(model)
def forward_sample(self, size=1, return_type='dataframe'):
"""
Generates sample(s) from joint distribution of the bayesian network.
......@@ -201,12 +200,11 @@ class BayesianModelSampling(Inference):
rec.array([(0, 0, 1, 0.6), (0, 0, 2, 0.6)],
dtype=[('diff', '<i8'), ('intel', '<i8'), ('grade', '<i8'), ('_weight', '<f8')])
"""
types = [(var_name, 'int') for var_name in self.topological_order]
types = [(var_name, np.int8) for var_name in self.topological_order]
types.append(('_weight', 'float'))
sampled = np.zeros(size, dtype=types).view(np.recarray)
sampled['_weight'] = np.ones(size)
evidence_dict = {var: st for var, st in evidence}
for node in self.topological_order:
cpd = self.model.get_cpds(node)
states = range(self.cardinality[node])
......@@ -228,6 +226,7 @@ class BayesianModelSampling(Inference):
for i in range(size):
sampled['_weight'][i] *= cpd.values[evidence_dict[node]]
else:
print('CALLING SAMPLE_DISCRETE',states,cpd.values,size)
sampled[node] = sample_discrete(states, cpd.values, size)
return _return_samples(return_type, sampled)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment