module Numeric.MaxEnt.Moment (
ExpectationConstraint,
(.=.),
average,
variance,
maxent
) where
import qualified Data.Vector.Storable as S
import Numeric.Optimization.Algorithms.HagerZhang05 (Result, Statistics)
import Numeric.AD.Lagrangian
import Numeric.MaxEnt.General
newtype ExpectationConstraint = ExpCon
{ unExpCon :: forall a. (Floating a) => [a] -> ([a] -> a, a) }
infixr 1 .=.
(.=.) :: (forall a. (Floating a) => a -> a)
-> (forall b. (Floating b) => b)
-> ExpectationConstraint
f .=. c = ExpCon $ \vals -> (sum .zipWith (*) vals . map f , c)
expCon2Con :: (forall a. (Floating a) => [a])
-> ExpectationConstraint
-> Constraint
expCon2Con vals expCon = f <=> c where
(f, c) = unExpCon expCon vals
average :: (forall a. (Floating a) => a) -> ExpectationConstraint
average m = id .=. m
variance :: (forall a. (Floating a) => a) -> ExpectationConstraint
variance sigma = (^(2 :: Int)) .=. sigma
maxent :: Double
-> (forall a. (Floating a) => [a])
-> [ExpectationConstraint]
-> Either (Result, Statistics) (S.Vector Double)
maxent tolerance values expConstraints = general tolerance n constraints where
constraints = map (expCon2Con values) expConstraints
n = length values