Safe Haskell | Safe-Infered |
---|
Examples of networks
Creating a simple network
The example
function is the typical example.
It is using the monad BNMonad
. The goal of this monad is to offer
a way of describing the network which is natural.
There are only three functions to understand inside the monad:
-
variable
to create a discrete variable of typeDV
. Creating a discrete variable is using aBounded
andEnum
type like for instanceBool
. -
proba
to define the probability P(A) of a variable A -
cpt
to define the conditional probability table P(A | BC)
It is important to understand how the values are organized. If you define P( wet | sprinkler road) then you have to give the values in the order:
wet=False, sprinkler=False, road=False wet=False, sprinkler=False, road=True wet=False, sprinkler=True, road=False wet=False, sprinkler=True, road=True
Finally, don't forget to return the discrete variables at the end of your network construction because those variables are used for making inferences.
example :: (DVSet
,SBN
CPT
) example =runBN
$ do winter <-variable
"winter" (t :: Bool) sprinkler <-variable
"sprinkler" (t :: Bool) wet <-variable
"wet grass" (t :: Bool) rain <-variable
"rain" (t :: Bool) road <-variable
"slippery road" (t :: Bool) --proba
winter ~~ [0.4,0.6]cpt
sprinkler [winter] ~~ [0.25,0.8,0.75,0.2]cpt
rain [winter] ~~ [0.9,0.2,0.1,0.8]cpt
wet [sprinkler,rain] ~~ [1,0.2,0.1,0.05,0,0.8,0.9,0.95]cpt
road [rain] ~~ [1,0.3,0,0.7] return [winter,sprinkler,rain,wet,road]
Importing a network from a Hugin file
The exampleImport
function can be used to import a file in Hugin format.
Only a subset of the format is supported.
The function will return a mapping from node names to Discrete Variables DV
.
The node name is used and not the node's label.
The function is also returning a simple bayesian network SBN
using CPT
as factors.
The implementation is using getDataFileName
to find the path of the
test pattern installed by cabal.
exampleImport :: IO (Map.Map StringDV
,SBN
CPT
) exampleImport = do path <-getDataFileName
"cancer.net" r <-importBayesianGraph
path return (runBN
$ fromJust r)
- example :: ([DV], SBN CPT)
- exampleJunction :: UndirectedSG () Vertex
- exampleImport :: IO (Map String DV, SBN CPT)
- exampleDiabete :: IO (Map String DV, SBN CPT)
- exampleAsia :: IO (Map String DV, SBN CPT)
- examplePoker :: IO (Map String DV, SBN CPT)
- exampleFarm :: IO (Map String DV, SBN CPT)
- examplePerso :: IO (Map String DV, SBN CPT)
- testJunction :: DirectedSG () Vertex
- anyExample :: FilePath -> IO (Map String DV, SBN CPT)