-
Notifications
You must be signed in to change notification settings - Fork 2
/
AICScore.py
29 lines (20 loc) · 1.07 KB
/
AICScore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from math import log
from pgmpy.estimators import StructureScore
class AICScore(StructureScore):
def __init__(self, data, **kwargs):
super(AICScore, self).__init__(data, **kwargs)
def local_score(self, variable, parents):
var_states = self.state_names[variable]
var_cardinality = len(var_states)
state_counts = self.state_counts(variable, parents)
sample_size = len(self.data)
num_parents_states = float(len(state_counts.columns))
score = 0
for parents_state in state_counts: # iterate over df columns (only 1 if no parents)
conditional_sample_size = sum(state_counts[parents_state])
for state in var_states:
if state_counts[parents_state][state] > 0:
score += state_counts[parents_state][state] * (log(state_counts[parents_state][state]) -
log(conditional_sample_size))
score -= num_parents_states * (var_cardinality - 1)
return score