{- | A comparison of variable elimination and factor elimination on a simple graph.

It is a non regression test.

module Bayes.Test.CompareEliminations(
 ) where

import Test.HUnit.Lang(assertFailure)

import Bayes.Examples(example)
import Bayes.Factor
import Bayes
import Bayes.VariableElimination
import Bayes.FactorElimination

compareFactors :: String -> Maybe CPT -> CPT -> IO ()
compareFactors s Nothing _ = assertFailure s
compareFactors s (Just a) b = 
    if a `isomorphicFactor` b 
            return () 
            assertFailure s

-- | Compare that variable elemination and factor elimination are giving
-- similar results on a simple example
compareVariableFactor :: IO ()
compareVariableFactor = do 
    let ([winter,sprinkler,rain,wet,road],exampleG) = example
        jt = createJunctionTree nodeComparisonForTriangulation exampleG
    compareFactors "PRIOR FOR RAIN" (posterior jt rain) (priorMarginal exampleG [winter,sprinkler,wet,road] [rain])

    let jt1 = updateEvidence [wet =: True] jt 
        jt2 = updateEvidence [wet =: True, sprinkler =: True] jt1 

    compareFactors "POSTERIOR RAIN FOR WET" (posterior jt1 rain) 
         (posteriorMarginal exampleG [winter,sprinkler,wet,road] [rain]  [wet =: True])
    compareFactors "POSTERIOR RAIN FOR WET" (posterior jt2 rain) 
         (posteriorMarginal exampleG [winter,sprinkler,wet,road] [rain]  [wet =: True, sprinkler =: True])

    compareFactors "PRIOR FOR WINTER" (posterior jt winter) (priorMarginal exampleG [sprinkler,wet,road,rain] [winter])
    compareFactors "PRIOR FOR SPRINKLER" (posterior jt sprinkler) (priorMarginal exampleG [winter,wet,road,rain] [sprinkler])
    compareFactors "PRIOR FOR WET" (posterior jt wet) (priorMarginal exampleG [winter,sprinkler,road,rain] [wet])
    compareFactors "PRIOR FOR ROAD" (posterior jt road) (priorMarginal exampleG [winter,sprinkler,wet,rain] [road])