![]() |
Quantum Fog
0.9.3
|
Public Member Functions | |
def | __init__ (self, states_df, vtx_to_parents, vtx_to_states, score_type, ess=1) |
def | get_vtx_num_params (self, vtx, new_parents=None) |
def | get_tot_num_params (self, new_vtx_to_parents=None) |
def | refresh_scores (self) |
def | get_vtx_score_ch (self, vtx, new_parents, for_end_vtx) |
def | score_move (self, move) |
def | do_move (self, move, score_change) |
def | LL_vtx_score (self, vtx, new_parents=None) |
def | BIC_vtx_score (self, vtx, new_parents=None) |
def | BIC_size_penalty (self, num_params) |
def | AIC_vtx_score (self, vtx, new_parents=None) |
def | AIC_size_penalty (self, num_params) |
def | get_pot (self, vtx, new_parents=None) |
def | BDEU_vtx_score (self, vtx, new_parents=None) |
def | K2_vtx_score (self, vtx, new_parents=None) |
Static Public Member Functions | |
def | BD_family_vtx_score (n_ijk, a_ijk) |
Public Attributes | |
states_df | |
vertices | |
vtx_to_parents | |
vtx_to_states | |
vtx_to_size | |
score_type | |
ess | |
vtx_score_fun | |
size_penalty_fun | |
vtx_to_score | |
tot_score | |
NetStrucScorer (Net Structure Scorer) is a class that can be used to give a score (i.e., measuring agreement with the data in states_df), to a graph structure (the plain graph, not the pots aka parameters) using various scoring functions, both frequentist and Bayesian. But NetStrucScorer does more than score individual graphs. It also keeps a running record of the scores of the moves performed by objects of class HillClimbingLner and its subclasses. frequentist scoring functions (from info theory): "LL" log likelihood "BIC" Bayesian Information Criterion "AIC" Akaike Information Criterion Bayesian scoring functions: "BDEU" "K2" IMPORTANT: We will use the word 'vtx' = vertex to denote a node name and the word 'node' to denote a Node object. References ---------- 1. "Scoring functions for learning Bayesian networks", by Alexandra M. Carvalho (powerpoint presentation on web) Attributes ---------- ess : float Equivalent Sample Size, a parameter in BDEU scorer. Fudge factor that is supposed to grow as the amount of prior knowledge grows. score_type : str score type, either 'LL', 'BIC, 'AIC', 'BDEU' or 'K2' size_penalty_fun : function This is the name of a size penalty function (either BIC_size_penalty_fun or AIC_size_penalty_fun without quotes). Some of the scoring methods specified by the score_type parameter deduct some points to the sum of scores of all vertices to penalize the use of too many parameters for the given sample size (overfitting). states_df : pandas.DataFrame a Pandas DataFrame with training data. column = node and row = sample. Each row/sample gives the state of the col/node. tot_score : float The total score for the whole graph. It equals the sum over all vertices of each vertex's score, minus, for some scoring methods, a size penalty. vertices : list[str] a list of the vertices of the graph. Equal to self.states_df.columns. vtx_score_fun : function the name of a function that takes as input a vertex and returns a score for it. vtx_to_parents : dict[str, list[str]] a dictionary that maps each vertex to a list of its parents. vtx_to_states : dict[str, list[str]] A dictionary mapping each node name to a list of its state names. This information will be stored in self.bnet. If vtx_to_states=None, constructor will learn vtx_to_states from states_df vtx_to_score : dict[str, float] a dictionary that maps each vertex to its score vtx_to_size : dict[str, int] a dictionary that maps each vertex to its number of states (aka its size)
def learning.NetStrucScorer.NetStrucScorer.__init__ | ( | self, | |
states_df, | |||
vtx_to_parents, | |||
vtx_to_states, | |||
score_type, | |||
ess = 1 |
|||
) |
Constructor Parameters ---------- states_df : pandas.core.frame.DataFrame vtx_to_parents : dict[str, list[str]] vtx_to_states : dict[str, list[str]] score_type : str ess : float Returns ------- None
def learning.NetStrucScorer.NetStrucScorer.AIC_size_penalty | ( | self, | |
num_params | |||
) |
This function returns the size penalty for AIC. Parameters ---------- num_params : int Returns ------- int
def learning.NetStrucScorer.NetStrucScorer.AIC_vtx_score | ( | self, | |
vtx, | |||
new_parents = None |
|||
) |
AIC = Akaike Information Criterion. This function returns the AIC score for vtx. If new_parents=None, it assumes the parents of vtx are those in vtx_to_parents[vtx]. Otherwise, it assumes the parents of vtx are given by new_parents. Parameters ---------- vtx : str new_parents : list[str] Returns ------- float
|
static |
This function returns the value of a special log Gamma function of n_ijk, a_ijk that occurs in both (BDEU and K2) bayesian scoring functions. Parameters ---------- n_ijk : numpy.array a_ijk : numpy.array Returns ------- float
def learning.NetStrucScorer.NetStrucScorer.BDEU_vtx_score | ( | self, | |
vtx, | |||
new_parents = None |
|||
) |
This function returns the contribution or 'score' of a single vertex vtx to log(P(G|D)) according to the BDEU method. If new_parents=None, it assumes the parents of vtx are those in vtx_to_parents[vtx]. Otherwise, it assumes the parents of vtx are given by new_parents. Parameters ---------- vtx : str new_parents : int Returns ------- float
def learning.NetStrucScorer.NetStrucScorer.BIC_size_penalty | ( | self, | |
num_params | |||
) |
This function returns the size penalty for BIC. Parameters ---------- num_params : int Returns ------- float
def learning.NetStrucScorer.NetStrucScorer.BIC_vtx_score | ( | self, | |
vtx, | |||
new_parents = None |
|||
) |
BIC = Bayesian Information Criterion. This function returns the BIC score for vtx. If new_parents=None, it assumes the parents of vtx are those in vtx_to_parents[vtx]. Otherwise, it assumes the parents of vtx are given by new_parents. Parameters ---------- vtx : str new_parents : list[str] Returns ------- float
def learning.NetStrucScorer.NetStrucScorer.do_move | ( | self, | |
move, | |||
score_change | |||
) |
This function takes as input a move and its score change and changes all attributes of the class object to realize that move. Only one move of a try makes it to this function. Parameters ---------- move : tuple[str, str, str] score_change : tuple[float, float, float] Returns ------- None
def learning.NetStrucScorer.NetStrucScorer.get_pot | ( | self, | |
vtx, | |||
new_parents = None |
|||
) |
This function estimates and returns a Potential based on the data in the dataframe states_df. If new_parents=None, it assumes the parents of vtx are those in vtx_to_parents[vtx]. Otherwise, it assumes the parents of vtx are given by new_parents. Parameters ---------- vtx : str new_parents : list[str] Returns ------- Potential
def learning.NetStrucScorer.NetStrucScorer.get_tot_num_params | ( | self, | |
new_vtx_to_parents = None |
|||
) |
This function returns the sum of the number of parameters for all the vertices. If new_vtx_to_parents[vtx] is nonempty, it uses that for the parents of vtx. Otherwise, it uses self.vtx_to_parents[vtx] for its parents. Parameters ---------- new_vtx_to_parents : dict[str, list[str]] Returns ------- int
def learning.NetStrucScorer.NetStrucScorer.get_vtx_num_params | ( | self, | |
vtx, | |||
new_parents = None |
|||
) |
This function returns the number of degrees of freedom of a vertex ( so it subtracts one from vtx size, and multiplies that by the product of the sizes of the parents of vtx). If new_parents=None, it assumes the parents of vtx are those in vtx_to_parents[vtx]. Otherwise, it assumes the parents of vtx are given by new_parents. Parameters ---------- vtx : str new_parents : list[str] Returns ------- int
def learning.NetStrucScorer.NetStrucScorer.get_vtx_score_ch | ( | self, | |
vtx, | |||
new_parents, | |||
for_end_vtx | |||
) |
Returns score change for either beg_vtx or end_vtx Parameters ---------- new_parents : list[str] vtx : str either beg_vtx or edn_vtx for_end_vtx : bool True if end vtx score is changing. False if beg vtx score is changing. Returns ------- list[str, str, str]
def learning.NetStrucScorer.NetStrucScorer.K2_vtx_score | ( | self, | |
vtx, | |||
new_parents = None |
|||
) |
This function returns the contribution or 'score' of a single vertex vtx to log(P(G|D)) according to the K2 method. If new_parents=None, it assumes the parents of vtx are those in vtx_to_parents[vtx]. Otherwise, it assumes the parents of vtx are given by new_parents. Parameters ---------- vtx : str new_parents : list[str] Returns ------- float
def learning.NetStrucScorer.NetStrucScorer.LL_vtx_score | ( | self, | |
vtx, | |||
new_parents = None |
|||
) |
LL = Log Likelihood. This function returns the LL score for vtx. If new_parents=None, it assumes the parents of vtx are those in vtx_to_parents[vtx]. Otherwise, it assumes the parents of vtx are given by new_parents. Parameters ---------- vtx : str new_parents : list[str] Returns ------- float
def learning.NetStrucScorer.NetStrucScorer.refresh_scores | ( | self | ) |
This function recalculates from scratch vtx_to_score[vtx] for all vtx. It also calculates self.tot_score for the graph. Returns ------- None
def learning.NetStrucScorer.NetStrucScorer.score_move | ( | self, | |
move | |||
) |
This function takes as input a move = (beg_vtx, end_vtx, action) and returns the move's score_change = (beg_vtx_score_ch, end_vtx_score_ch, tot_score_ch ) where beg=beginning, vtx=vertex, tot=total, ch=change Parameters ---------- move : tuple[str, str, str] Returns ------- tuple[float, float, float]