Safe Haskell | None |
---|

Discrete Bayesian Network Library.

It is a very preliminary version. It has only been tested on very simple examples where it worked. On bigger networks, imported from Hugin files, it was very very very slow. So, you can use this software as a toy. Much more work is needed to validate and optimize it.

Look at the Bayes.Examples and Bayes.Examples.Tutorial in this package to see how to use the library.

- class Graph g where
- addVertex :: Vertex -> b -> g a b -> g a b
- removeVertex :: Vertex -> g a b -> g a b
- vertexValue :: g a b -> Vertex -> Maybe b
- changeVertexValue :: Vertex -> b -> g a b -> Maybe (g a b)
- someVertex :: g a b -> Maybe Vertex
- hasNoVertices :: g a b -> Bool
- allVertices :: g a b -> [Vertex]
- allVertexValues :: g a b -> [b]
- allNodes :: g a b -> [(Vertex, b)]
- isLinkedWithAnEdge :: g a b -> Vertex -> Vertex -> Bool
- addEdge :: Edge -> a -> g a b -> g a b
- removeEdge :: Edge -> g a b -> g a b
- edgeVertices :: g a b -> Edge -> Maybe (Vertex, Vertex)
- edgeValue :: g a b -> Edge -> Maybe a
- someEdge :: g a b -> Maybe Edge
- hasNoEdges :: g a b -> Bool
- endVertex :: g a b -> Edge -> Maybe Vertex
- startVertex :: g a b -> Edge -> Maybe Vertex
- allEdges :: g a b -> [Edge]
- allEdgeValues :: g a b -> [a]
- emptyGraph :: g a b
- isEmpty :: g a b -> Bool
- oriented :: g a b -> Bool
- neighbors :: g a b -> Vertex -> Maybe [Vertex]

- class Graph g => UndirectedGraph g where
- class Graph g => DirectedGraph g where
- class FoldableWithVertex g where
- foldrWithVertex :: (Vertex -> a -> b -> b) -> b -> g c a -> b
- foldlWithVertex' :: (b -> Vertex -> a -> b) -> b -> g c a -> b

- class Graph g => NamedGraph g where
- addLabeledVertex :: String -> Vertex -> b -> g a b -> g a b
- vertexLabel :: g a b -> Vertex -> Maybe String

- class Distribution d where
- createFactor :: Factor f => [DV] -> d -> Maybe f

- data GraphMonad g e f a
- type GMState g e f = (AuxiliaryState, g e f)
- graphNode :: NamedGraph g => String -> f -> GraphMonad g e f Vertex
- runGraph :: Graph g => GraphMonad g e f a -> (a, g e f)
- execGraph :: Graph g => GraphMonad g e f a -> g e f
- evalGraph :: Graph g => GraphMonad g e f a -> a
- data Vertex
- data Edge
- edge :: Vertex -> Vertex -> Edge
- newEdge :: Graph g => Vertex -> Vertex -> e -> GraphMonad g e f ()
- edgeEndPoints :: Edge -> (Vertex, Vertex)
- connectedGraph :: Graph g => g a b -> Bool
- dag :: DirectedGraph g => g a b -> Bool
- printGraphValues :: (Graph (SimpleGraph n), Show b) => SimpleGraph n e b -> IO ()
- type DirectedSG = SimpleGraph DE
- type UndirectedSG = SimpleGraph UE
- type SBN f = DirectedSG () f
- type BayesianNetwork g f = g () f
- type BNMonad g f a = GraphMonad g () (MaybeBNode f) a
- runBN :: BNMonad DirectedSG f a -> (a, DirectedSG () f)
- evalBN :: BNMonad DirectedSG f a -> a
- execBN :: BNMonad DirectedSG f a -> DirectedSG () f
- variable :: (Enum a, Bounded a, NamedGraph g) => String -> a -> BNMonad g f (TDV a)
- unamedVariable :: (Enum a, Bounded a, NamedGraph g) => a -> BNMonad g f (TDV a)
- variableWithSize :: NamedGraph g => String -> Int -> BNMonad g f DV
- tdv :: DV -> TDV s
- t :: a
- cpt :: (DirectedGraph g, BayesianDiscreteVariable v, BayesianDiscreteVariable vb) => v -> [vb] -> BNMonad g f v
- proba :: (DirectedGraph g, BayesianDiscreteVariable v) => v -> BNMonad g f v
- (~~) :: (DirectedGraph g, Factor f, Distribution d, BayesianDiscreteVariable v) => BNMonad g f v -> d -> BNMonad g f ()
- softEvidence :: (NamedGraph g, DirectedGraph g, Factor f) => TDV Bool -> BNMonad g f (TDV Bool)
- se :: Factor f => TDV s -> TDV s -> Double -> Maybe f
- logical :: (Factor f, DirectedGraph g) => TDV Bool -> LE -> BNMonad g f ()
- (.==.) :: Testable d v => d -> v -> LE
- (.!.) :: LE -> LE
- (.|.) :: LE -> LE -> LE
- (.&.) :: LE -> LE -> LE
- noisyOR :: (DirectedGraph g, Factor f, NamedGraph g) => [(TDV Bool, Double)] -> BNMonad g f (TDV Bool)
- testEdgeRemoval_prop :: DirectedSG String String -> Property
- testVertexRemoval_prop :: DirectedSG String String -> Property

# Graph

## Graph classes

Graph class used for graph processing algorithms. A graph processing algorithm does not have to know how the graph is implemented nor if it is directed or undirected

addVertex :: Vertex -> b -> g a b -> g a bSource

Add a new vertex

removeVertex :: Vertex -> g a b -> g a bSource

Remove a vertex

vertexValue :: g a b -> Vertex -> Maybe bSource

Get the vertex value if the vertex is found in the graph

changeVertexValue :: Vertex -> b -> g a b -> Maybe (g a b)Source

Change the vertex value if the vertex is found in the graph

someVertex :: g a b -> Maybe VertexSource

Generate a "random" vertex

hasNoVertices :: g a b -> BoolSource

Check is the graph has no vertrex

allVertices :: g a b -> [Vertex]Source

Generate all vertices

allVertexValues :: g a b -> [b]Source

Get all the values

allNodes :: g a b -> [(Vertex, b)]Source

Get all nodes

isLinkedWithAnEdge :: g a b -> Vertex -> Vertex -> BoolSource

Check if two vertices are linked by a vertex

addEdge :: Edge -> a -> g a b -> g a bSource

Add an edge

removeEdge :: Edge -> g a b -> g a bSource

Remove an dedge

edgeVertices :: g a b -> Edge -> Maybe (Vertex, Vertex)Source

Vertices for an edge

edgeValue :: g a b -> Edge -> Maybe aSource

Edge value if the edge is found in the graph

someEdge :: g a b -> Maybe EdgeSource

Return a "random" edge

hasNoEdges :: g a b -> BoolSource

Check if the graph has no edges

endVertex :: g a b -> Edge -> Maybe VertexSource

One extremity of the edge (which is the end only for directed edge)

startVertex :: g a b -> Edge -> Maybe VertexSource

One extremity of the edge (which is the start only for directed edge)

allEdges :: g a b -> [Edge]Source

All edges of the graph

allEdgeValues :: g a b -> [a]Source

All values of the graph

emptyGraph :: g a bSource

Returns an empty graph

isEmpty :: g a b -> BoolSource

Check if the graph is empty

oriented :: g a b -> BoolSource

Check if the graph is oriented

neighbors :: g a b -> Vertex -> Maybe [Vertex]Source

All the neighbors of a vertex

Graph UndirectedSG | SimpleGraph is an instance of Graph. |

Graph DirectedSG | SimpleGraph is an instance of Graph. |

class Graph g => UndirectedGraph g whereSource

Undirected graph

class Graph g => DirectedGraph g whereSource

Directed graph

class FoldableWithVertex g whereSource

The foldable class is limited. For a graph g we may need the vertex in addition to the value

foldrWithVertex :: (Vertex -> a -> b -> b) -> b -> g c a -> bSource

Fold with vertex

foldlWithVertex' :: (b -> Vertex -> a -> b) -> b -> g c a -> bSource

FoldableWithVertex (SimpleGraph local) |

class Graph g => NamedGraph g whereSource

A named graph is a graph where the vertices have a name. This name is not a vertex value. Putting this name in the vertex value would make algorithm less readable. A vertex name is only useful to display the graph. Labeled graph has a different meaning in graph theory.

addLabeledVertex :: String -> Vertex -> b -> g a b -> g a bSource

Add a vertex with a vertex name in addition to the value

vertexLabel :: g a b -> Vertex -> Maybe StringSource

Returns the vertex label

class Distribution d whereSource

A distribution which can be used to create a factor

createFactor :: Factor f => [DV] -> d -> Maybe fSource

Create a factor from variables and a distributions for those variables

Real a => Distribution [a] |

## Graph Monad

data GraphMonad g e f a Source

Graph monad.
The monad used to simplify the description of a new graph
g is the graph type. e the edge type. f the node type (generally a `Factor`

)

Monad (GraphMonad g e f) | |

MonadState (GMState g e f) (GraphMonad g e f) |

type GMState g e f = (AuxiliaryState, g e f)Source

The state of the graph monad : the graph and auxiliary data useful during the construction

graphNode :: NamedGraph g => String -> f -> GraphMonad g e f VertexSource

Add a node in the graph using the graph monad

runGraph :: Graph g => GraphMonad g e f a -> (a, g e f)Source

execGraph :: Graph g => GraphMonad g e f a -> g e fSource

evalGraph :: Graph g => GraphMonad g e f a -> aSource

## Support functions for Graph constructions

newEdge :: Graph g => Vertex -> Vertex -> e -> GraphMonad g e f ()Source

Add a new labeled edge to the graph

edgeEndPoints :: Edge -> (Vertex, Vertex)Source

Endpoints of an edge

connectedGraph :: Graph g => g a b -> BoolSource

Check if the graph is connected

dag :: DirectedGraph g => g a b -> BoolSource

Check if the graph is a directed Acyclic graph

printGraphValues :: (Graph (SimpleGraph n), Show b) => SimpleGraph n e b -> IO ()Source

Print the values of the graph vertices

# SimpleGraph implementation

## The SimpleGraph type

type DirectedSG = SimpleGraph DESource

Directed simple graph

type UndirectedSG = SimpleGraph UESource

Undirected simple graph

## Bayesian network

type SBN f = DirectedSG () fSource

An implementation of the BayesianNetwork using the simple graph and no value of edges

type BayesianNetwork g f = g () fSource

Bayesian network. g must be a directed graph and f a factor

# Bayesian Monad used to ease creation of Bayesian Networks

type BNMonad g f a = GraphMonad g () (MaybeBNode f) aSource

The Bayesian monad

runBN :: BNMonad DirectedSG f a -> (a, DirectedSG () f)Source

Create a bayesian network using the simple graph implementation The initialized nodes are replaced by the factor. Returns the monad values and the built graph.

evalBN :: BNMonad DirectedSG f a -> aSource

Create a bayesian network but only returns the monad value. Mainly used for testing.

execBN :: BNMonad DirectedSG f a -> DirectedSG () fSource

Create a bayesian network but only returns the monad value. Mainly used for testing.

## Variable creation

:: (Enum a, Bounded a, NamedGraph g) | |

=> String | Variable name |

-> a | Variable bounds |

-> BNMonad g f (TDV a) |

Define a Bayesian variable (name and bounds)

:: (Enum a, Bounded a, NamedGraph g) | |

=> a | Variable bounds |

-> BNMonad g f (TDV a) |

Create a new unamed variable

:: NamedGraph g | |

=> String | Variable name |

-> Int | Variable size |

-> BNMonad g f DV |

Define a Bayesian variable (name and bounds)

Synonym for undefined because it is clearer to use t to set the Enum bounds of a variable

## Creation of conditional probability tables

cpt :: (DirectedGraph g, BayesianDiscreteVariable v, BayesianDiscreteVariable vb) => v -> [vb] -> BNMonad g f vSource

Define a conditional probability between different variables Variables are ordered like FFF FFT FTF FTT TFF TFT TTF TTT and same for other enumeration keeping enumeration order

proba :: (DirectedGraph g, BayesianDiscreteVariable v) => v -> BNMonad g f vSource

Define proba for a variable Values are ordered like FFF FFT FTF FTT TFF TFT TTF TTT and same for other enumeration keeping enumeration order

:: (DirectedGraph g, Factor f, Distribution d, BayesianDiscreteVariable v) | |

=> BNMonad g f v | Discrete variable in the graph |

-> d | List of values |

-> BNMonad g f () |

Initialize the values of a factor

:: (NamedGraph g, DirectedGraph g, Factor f) | |

=> TDV Bool | Variable on which we want to define Soft evidence |

-> BNMonad g f (TDV Bool) | Return a soft evidence node (for the factor encoding the soft evidence values) and an hard evidence node to activate the soft evidence observation |

Create an auxiliairy node to force soft evidence

:: Factor f | |

=> TDV s | Soft evidence node |

-> TDV s | Node on which the soft evidence is imposed |

-> Double | Soft evidence (probability of right detection) |

-> Maybe f |

Soft evidence factor

## Creation of truth tables

(.==.) :: Testable d v => d -> v -> LESource

Create a variable instantiation using values from an enumeration

## Noisy OR

:: (DirectedGraph g, Factor f, NamedGraph g) | |

=> [(TDV Bool, Double)] | Variables and probability of no influence |

-> BNMonad g f (TDV Bool) |

Noisy OR. The Noisy-OR with leak can be implemented by using the standard Noisy-OR and a leak variable.