# Module Mn.Factor

`module Factor: `sig` .. `end``
The Factor module is used for representing a factor in a factor graph.

###### Data structures

`type variable = `int` `
Variables are represented as integer indices.
`type varvalue = `int` `
A variable state is an integer index over a variable's range of values.
`type condition = `bool * variable * varvalue` `
`condition` represents an equality or inequality constraint on a variable's value. For example, (true, 10, 1) says that variable 10 has a value of 1, and (false, 5, 0) says that variable 5 does not have a value of 0.
`type feature = `Factor.feature` = {`
 `  ` `cond : condition array;` `  ` `weight_id : int;` `  ` `mutable weight : float;`
}
A feature is a set of conditions and a weight parameter.
`type tree = `Factor.tree` = `
 `|` `Leaf of float` `|` `Vertex of variable * varvalue * tree * tree`
Recursive structure for tree representation of a factor.
`type factor = `Factor.factor` = `
 `|` `Feature of feature` `|` `FeatureSet of feature list` `|` `Table of variable array * int array * float array` `(*` Table representation of a factor consists of an array of variables, an array of variable ranges, and array of parameters. `*)` `|` `Tree of tree` `|` `Const of float`
Different factor representations for factor graphs. See Section 1.1 of the user manual for more information about representations.

###### Factor operations

`val simple_cond_cmp : `condition -> condition -> int``
Comparision function used for sorting conditions.
`val remove_redundant_conds : `condition list -> condition list``
Removes redundant conditions from a condition list. Assumes that conditions are sorted by simple_cond_cmp and the condition is satisfiable (not contradictory).
`val condl_to_valsetl : `int array -> condition list -> (int * bool array) list``
`condl_to_valsetl schema condl` converts list of conditions `condl` to a list of variable value sets, so that each variable appears only once in the list.
`val fmatch : `varvalue array -> condition array -> bool``
`fmatch x cond_ar` checks whether example `x` satisfies set of conditions `cond_ar`.
`val fweight : `varvalue array -> feature -> float``
`fweight x f` returns the weight of feature `f` if example `x` satisfies the feature. Otherwise returns 0.
`val log_value : `varvalue array -> factor -> float``
`log_value x f` returns the value of the factor for the configuration given by example `x`.
`val expected_log_value : `factor -> float array array -> float array array -> float``
`expected_log_value f logmarg negmarg` returns the expected value of factor `f` given a fully factorized distribution. `logmarg.(var).(value)` is the log-probability that variable `var` has value `value`, and `negmarg` is log-probability that variable `var` does not have value `value`.
`val numparams : `factor -> int``
Returns the number of parameters in a factor.
`val simplify_feature : `varvalue array -> feature -> feature``
`simplify_feature ev f` removes all conditions of feature `f` that are satisfied by evidence `ev` and returns a new, simpler feature. If `f` is inconsistent with `ev`, returns a feature with no conditions instead.
`val simplify : `varvalue array -> factor -> factor``
`simplify ev f` simplifies factor `f` by conditioning it on evidence `ev`.
`val tree_to_features : `condition list -> tree -> feature list``
Generates an equivalent set of features for a given tree factor.
`val table_to_features : `variable array * int array * float array -> feature list``
Converts table CPD to features
`val to_features : `factor -> feature list``
Converts factor to a set of conjunctive features.
`val set_weights : `float array -> int -> factor -> factor * int``
`set_weights w wi f` updates the weight in factor `f` using a global weight vector `w`. The weights of features in `f` starts from index `wi`. `set_weights` returns a tuple `(nf, nw)`, in which `nf` is the new modified feature and `nw` is the number of features in `f`.
`val vars : `factor -> variable list``
Returns a list of variables used by a factor.
`val feature_set_vars : `feature list -> variable list``
`val tree_vars : `tree -> variable list``
`val to_table : `int array -> factor -> factor``
`to_table schema f` converts factor `f` to another factor with a table representation.
`val copy : `factor -> factor``
`copy f` creates a duplicate of factor `f`.
`val rescale : `float -> factor -> factor``
`rescale alpha f` rescales factor `f` by a factor of `alpha`. Rescaling is done in log space, so this is equivalent to raising a potential function to the power of `alpha`.

###### Writing factors

`val output_factor : `Pervasives.out_channel -> factor -> unit``
`output_factor out f` writes factor `f` to channel `out`.
`val output_feature : `Pervasives.out_channel -> feature -> unit``
`val output_featurelist : `Pervasives.out_channel -> feature list -> unit``
`val output_tree : `Pervasives.out_channel -> string -> tree -> unit``

###### Parsing factors

`module MP: `sig` .. `end``
Auxilary module for parsing factors.
`val pfactor_to_factor : `int array -> MP.pfactor -> factor``
Build a factor from parser output.