Documentation ¶
Overview ¶
Package bbn provides a Bayesian Belief Network implementation.
Example (Sprinkler) ¶
package main import ( "fmt" "github.com/mlange-42/bbn" ) func main() { variables := []bbn.Variable{ {Name: "Rain", Outcomes: []string{"yes", "no"}}, {Name: "Sprinkler", Outcomes: []string{"yes", "no"}}, {Name: "GrassWet", Outcomes: []string{"yes", "no"}}, } factors := []bbn.Factor{ { For: "Rain", Table: []float64{ // rain+, rain- 0.2, 0.8, }, }, { For: "Sprinkler", Given: []string{"Rain"}, Table: []float64{ // yes no 0.01, 0.99, // rain+ 0.2, 0.8, // rain- }, }, { For: "GrassWet", Given: []string{"Rain", "Sprinkler"}, Table: []float64{ // yes no 0.99, 0.01, // rain+, sprikler+s 0.8, 0.2, // rain+, sprikler- 0.9, 0.1, // rain-, sprikler+ 0.0, 1.0, // rain-, sprikler- }, }, } net, err := bbn.New("Sprinkler", "", variables, factors) if err != nil { panic(err) } evidence := map[string]string{ "GrassWet": "yes", "Rain": "no", } query := []string{ "Sprinkler", } result, _, err := net.SolveQuery(evidence, query, false) if err != nil { panic(err) } fmt.Println(result) }
Output: map[Sprinkler:[1 0]]
Index ¶
- Constants
- func ToYAML(network *Network) ([]byte, error)
- type Factor
- type Network
- func (n *Network) Info() string
- func (n *Network) Marginal(f *ve.Factor, variable string) ve.Factor
- func (n *Network) Name() string
- func (n *Network) Normalize(f *ve.Factor) ve.Factor
- func (n *Network) NormalizeUtility(utility *ve.Factor, probs *ve.Factor) ve.Factor
- func (n *Network) Rearrange(f *ve.Factor, variables []string) ve.Factor
- func (n *Network) SolvePolicies(stepwise bool) (map[string]Factor, error)
- func (n *Network) SolveQuery(evidence map[string]string, query []string, ignorePolicies bool) (map[string][]float64, *ve.Factor, error)
- func (n *Network) SolveUtility(evidence map[string]string, query []string, utilityVar string, ...) (*ve.Factor, error)
- func (n *Network) ToEvidence(variable string, value string) ([]float64, error)
- func (n *Network) TotalUtilityIndex() int
- func (n *Network) Variables() []Variable
- type Trainer
- type Variable
Examples ¶
Constants ¶
const ( ChanceNodeType = "nature" DecisionNodeType = "decision" UtilityNodeType = "utility" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Factor ¶ added in v0.5.0
type Factor struct { For string // Primary variable of the factor. Given []string `yaml:",omitempty"` // Names of dependency variables, i.e. parents. Table []float64 `yaml:",omitempty"` // Flat representation of the factor's table. // contains filtered or unexported fields }
Factor definition, encoding a conditional probability or utility table.
type Network ¶
type Network struct {
// contains filtered or unexported fields
}
Network is the primary type for solving bbn networks.
func FromBIFXML ¶ added in v0.2.0
func (*Network) Marginal ¶ added in v0.5.0
Marginal calculates marginal probabilities from a factor for a variable.
func (*Network) NormalizeUtility ¶ added in v0.5.0
NormalizeUtility normalizes utility factor by dividing it by a probability factor.
func (*Network) SolvePolicies ¶ added in v0.5.0
SolvePolicies solves and inserts policies for decisions, using variable elimination.
Returns a map of policies for each decision variable, by variable name.
func (*Network) SolveQuery ¶ added in v0.5.0
func (n *Network) SolveQuery(evidence map[string]string, query []string, ignorePolicies bool) (map[string][]float64, *ve.Factor, error)
SolveQuery solves a query, using variable elimination.
Returns a map of normalized marginal probabilities for each query variable, by variable name. Further, it returns the resulting factor containing the query variables.
func (*Network) SolveUtility ¶ added in v0.5.0
func (n *Network) SolveUtility(evidence map[string]string, query []string, utilityVar string, ignorePolicies bool) (*ve.Factor, error)
SolveUtility solves utility, using variable elimination.
Argument utilityVar can be used to solve only the utility for a certain variable. With utilityVar set to an empty sting (""), the total utility is solved.
If there is a variable for total utility, defined by having utility variables as parents, utility nodes are weighted according to the total utility variable's factor.
Returns a factor for utility, containing the query variables.
func (*Network) ToEvidence ¶ added in v0.5.0
ToEvidence converts a string variable/value pair to marginal probabilities for the evidence variable.
As an example, say we have a variable with outcomes [yes, no]. Given evidence "yes" (index 0): we get the following probabilities: [1, 0].
func (*Network) TotalUtilityIndex ¶ added in v0.5.0
TotalUtilityIndex return the index of the total utility node. -1 if none.
type Trainer ¶
type Trainer struct {
// contains filtered or unexported fields
}
Trainer is a utility type to train a Network.
func NewTrainer ¶
func (*Trainer) AddSample ¶
AddSample adds a training sample. Order of values in the sample is the same as the order in which nodes were passed into the Network constructor.
func (*Trainer) UpdateNetwork ¶
UpdateNetwork applies the training to the network, and returns a pointer to the original network.
type Variable ¶ added in v0.5.0
type Variable struct { Name string // Name of the variable. NodeType ve.NodeType // Node type of the variable. Outcomes []string // Possible outcomes. Position [2]int // Position in bbni visualization, in terminal cells. Color string // Name of the node color in bbni visualization. Factor *Factor // Don't set this, it is initialized when constructing the network. }
Variable definition for creating a Network.
Directories ¶
Path | Synopsis |
---|---|
benchmark
module
|
|
cmd
|
|
bbn
BBN's command line interface.
|
BBN's command line interface. |
bbni
BBN's interactive terminal app.
|
BBN's interactive terminal app. |
internal
|
|
Package logic provides shortcuts to create factors/CPTs for logic operations.
|
Package logic provides shortcuts to create factors/CPTs for logic operations. |
Package ve provides low-level access to variable elimination and factor operations in general.
|
Package ve provides low-level access to variable elimination and factor operations in general. |