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

It is a non regression test.

-}
module Bayes.Test.CompareEliminations(
compareVariableFactor
, compareFactorChange
) where

import Test.HUnit.Lang(assertFailure)

import Bayes.Examples(example,exampleWithFactorChange)
import Bayes.Factor
import Bayes
import Bayes.VariableElimination
import Bayes.FactorElimination
import Bayes.Factor.CPT
import Data.Maybe(fromJust)

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

compareFactorChange :: IO ()
compareFactorChange = do
(_,exampleGF) = exampleWithFactorChange
theNewFactor = fromJust \$ factorWithVariables [wet,sprinkler,rain] [1,0.2,0.1,0.05,0,0.8,0.9,0.95]
exampleGF' = changeFactor theNewFactor exampleGF
compareG s a b = compareFactors s (Just \$ priorMarginal exampleGF' a b) (priorMarginal exampleG a b)

-- Now we test that the junction tree factors can be changed and the result will be right.
-- We first create a wrong junction tree using the wrong graph (where factor values are wrong)
let jt' = createJunctionTree nodeComparisonForTriangulation exampleGF
-- We then update the values to the right ones by using the right factor
jt = changeFactor theNewFactor jt'

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

compareFactors "POSTERIOR RAIN FOR WET" (posterior jt1 rain)
compareFactors "POSTERIOR RAIN FOR WET" (posterior jt2 rain)

-- | Compare that variable elemination and factor elimination are giving
-- similar results on a simple example
compareVariableFactor :: IO ()
compareVariableFactor = do
jt = createJunctionTree nodeComparisonForTriangulation exampleG

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

compareFactors "POSTERIOR RAIN FOR WET" (posterior jt1 rain)