Cogito is a Clojure implementation of System-Z+, a probabilistic reasoner described in "Qualitative probabilities for default reasoning, belief revision, and causal modeling" by Moises Goldszmidt and Judea Pearl.
(this space intentionally left almost blank)
Cogito is a Clojure implementation of System-Z+, a qualitative-probabilistic reasoner described in "Qualitative probabilities for default reasoning, belief revision, and causal modeling" by Moises Goldszmidt and Judea Pearl.
The basic idea is that you create a rule map, where keys are pairs of antecedents and consequents, each associated with an integer value, delta, that determines the strength of the connection between the pair
The system then automatically scores and orders the rules from most general (i.e. not inconsistent with any other rules) to most specific (i.e. inconsistent with one or more earlier rules). More specific rules will have higher scores than more general rules, where the score represents the surprise associated with a violation of the rule.
Below is an example of partitioning the above rules based on a consistency test (figure 2 Goldszmidt and Pearl). The rules in the first group are tolerated by all the rules in the rule set, whereas the rules in the second group are not tolerated by those in the first group.
Once partitioned scores are assigned to each rule. First the scores of the rules in the first group are set equal to the delta values associated with each of the rules. Next each rule in later groups are evaluated to determine which rules they violate in the first group. The z-plus-order function returns map showing which rules are violated.
In the above case, both rules in the second group only violate one rule in the first group, [:b :f], which states that birds fly. The scores for these two rules will then be set equal to their respective delta values plus the score for the [:b :f] rule plus one, meaning violating more specific (i.e. later) rules will be associated with more surprise than violating more general ones.
The difference between the delta value and the score associated with each rule is that delta only represents the strength between the given antecedent and consequent, whereas the score takes all the other rules into consideration.
Queries are made by submitting competing hypotheses, the one that is the least surprising (i.e. has the lowest score associated with it) is selected.
The following example takes the above rules-map, compiles it, and runs several queries (each compare two competing hypotheses), which returns a map that associates a "surprise" score with each hypothesis, the lowest score wins.
A hypothesis is a model (map of truth values) formed from a logical statement. For instance, a statement
can be read as "penguin birds can fly" and can be represented in a truth-value map as:
The following rules,
can be translated to the following rules-map.
All of the rules have a delta-value of one, these values can be adjusted if not all the rules have the same "strength".
Next compile the rules-map,
and then run the following queries against it.
Do penguin birds fly?
This query is submitted by creating two hypotheses, one where penguin birds fly and one where they don't.
The results are that flying penguins that more surprising than non-flying ones.
Are all birds penguins?
The results are that not all birds are penguins.
Do red birds fly?
The results are that red birds do fly.
Are birds airborn?
The results are that birds are airborn.
Do penguins have wings?
This is a known area of weakness for System-Z+, it cannot decide whether penguins have wings.
Returns the antecedent of the given rule.
Returns the consequent of the given rule.
Determines if the variable has been negated.
Returns the state of the logical variable in the model, where a model consists of a map of logical variables and their associated truth values.
Returns the logical variable's name.
Associates a truth-value(s) with a logical variable, or a pair of truth-values with a rule in the given model.
Updates model with new bindings based on the given rule. One or more bindings in the model will have a value of :inconsistent, if the new rule is inconsistent with the current model.
New values are only added to the model if the antecedent, 'a', is already bound to true, then the value of 'b' is set to true if isn't bound yet, set to :inconsistent if it is already bound to false, otherwise it is left as is.
Returns the model created by adding rule to rule-set. A logical variable in the model will have a truth-value of :inconsistent if the new rule is not tolerated in the rule-set.
Determines if a rule is tolerated by an existing rule-set, an optional model can be provided as well
Partitions a set of rules into a set of groups orderd from general to specific, where the rules in each group are tolerated by all the rules in its group as well as all later groups.
If the rule-set cannot be partitioned such, then it is inconsistent and nil will be returned.
Each group forms a sub-theory, where earlier groups are more general and later groups are more specific.
See Figure 2 Procedure for testing consistency in Goldszmidt and Pearl.
Extracts logical variable names from a rule set.
Generates all models possible for a given set of logical variables, even inconsistent models.
Generates all models possible for a given rule-set, even inconsistent models.
Determines if a set of truth-conditions are entailed by a given model.
Filters a set of models so that only those consistent with the given truth-condition are returned.
Updates the rules-map with scores from the output of the z-plus-order function.
Returns a query result, that should be evaluated by score-query, given a z-ordered rule map and a query-map.
Returns a score associated with a query-result returned from the output of the query function.
Queries are performed by submitting queries for multiple hypotheses, and then selecting the hypothesis with the lowest (i.e. least surprising) score.
Given a map associating rules to delta-values, returns a new map containing two keys, :delta-values (which is associated with the original map) and :z+-scores (which is associated with a map containing a "surprise" score for each rule).
This compiled-rules map can be passed to the query function as an alternative to an uncompiled rules-map.
Given either an uncompiled or compiled rules map and a series of hypothetical models, returns a map associating each hypothesis with a "surprise" score (i.e. the lowest score is the most likely).