HLearn-distributions 1.0.0.2 → 1.1.0
raw patch · 24 files changed
+457/−481 lines, 24 filesdep +HLearn-datastructuresdep +erfdep +gamma
Dependencies added: HLearn-datastructures, erf, gamma
Files
- HLearn-distributions.cabal +26/−4
- src/HLearn/Models/Distributions.hs +6/−6
- src/HLearn/Models/Distributions/Common.hs +0/−2
- src/HLearn/Models/Distributions/Kernels.hs +85/−0
- src/HLearn/Models/Distributions/Multivariate/Interface.hs +8/−6
- src/HLearn/Models/Distributions/Multivariate/Internal/CatContainer.hs +1/−15
- src/HLearn/Models/Distributions/Multivariate/Internal/Container.hs +40/−44
- src/HLearn/Models/Distributions/Multivariate/Internal/Ignore.hs +0/−14
- src/HLearn/Models/Distributions/Multivariate/Internal/Marginalization.hs +3/−0
- src/HLearn/Models/Distributions/Multivariate/Internal/TypeLens.hs +30/−13
- src/HLearn/Models/Distributions/Multivariate/Internal/Unital.hs +0/−10
- src/HLearn/Models/Distributions/Multivariate/MultiNormal.hs +18/−15
- src/HLearn/Models/Distributions/Univariate/Binomial.hs +14/−18
- src/HLearn/Models/Distributions/Univariate/Categorical.hs +46/−29
- src/HLearn/Models/Distributions/Univariate/Exponential.hs +9/−24
- src/HLearn/Models/Distributions/Univariate/Geometric.hs +14/−29
- src/HLearn/Models/Distributions/Univariate/Internal/MissingData.hs +0/−14
- src/HLearn/Models/Distributions/Univariate/Internal/Moments.hs +1/−14
- src/HLearn/Models/Distributions/Univariate/KernelDensityEstimator.hs +67/−0
- src/HLearn/Models/Distributions/Univariate/LogNormal.hs +22/−24
- src/HLearn/Models/Distributions/Univariate/Normal.hs +47/−29
- src/HLearn/Models/Distributions/Univariate/Poisson.hs +15/−29
- src/HLearn/Models/Distributions/Visualization/Gnuplot.hs +5/−11
- src/HLearn/Models/Distributions/Visualization/Graphviz.hs +0/−131
HLearn-distributions.cabal view
@@ -1,5 +1,5 @@ Name: HLearn-distributions-Version: 1.0.0.2+Version: 1.1.0 Synopsis: Distributions for use with the HLearn library Description: This module is used to estimate statistical distributions from data. It is based on the algebraic properties of the "HomTrainer" type class from the HLearn-algebra package. Category: Data Mining, Machine Learning, Statistics@@ -15,6 +15,7 @@ Library Build-Depends: HLearn-algebra >= 1.0.0.1,+ HLearn-datastructures >= 1.1, ConstraintKinds >= 0.0.1, base >= 3 && < 5, @@ -29,7 +30,9 @@ vector-th-unbox >= 0.2, graphviz >= 2999.16, hmatrix >= 0.14,- + gamma >= 0.9.0.2, + erf >= 2.0.0.0,+ -- are these really necessary? array >= 0.4.0, process >= 1.1.0.2,@@ -49,12 +52,14 @@ Exposed-modules: HLearn.Models.Distributions HLearn.Models.Distributions.Common+ HLearn.Models.Distributions.Kernels HLearn.Models.Distributions.Visualization.Gnuplot- HLearn.Models.Distributions.Visualization.Graphviz+ --HLearn.Models.Distributions.Visualization.Graphviz HLearn.Models.Distributions.Univariate.Binomial HLearn.Models.Distributions.Univariate.Categorical HLearn.Models.Distributions.Univariate.Exponential HLearn.Models.Distributions.Univariate.Geometric+ HLearn.Models.Distributions.Univariate.KernelDensityEstimator HLearn.Models.Distributions.Univariate.LogNormal HLearn.Models.Distributions.Univariate.Normal HLearn.Models.Distributions.Univariate.Poisson@@ -69,4 +74,21 @@ HLearn.Models.Distributions.Multivariate.Internal.Marginalization HLearn.Models.Distributions.Multivariate.Internal.TypeLens HLearn.Models.Distributions.Multivariate.Internal.Unital- ++ Extensions:+ FlexibleInstances+ FlexibleContexts+ MultiParamTypeClasses+ FunctionalDependencies+ UndecidableInstances+ ScopedTypeVariables+ BangPatterns+ TypeOperators+ GeneralizedNewtypeDeriving+ --DataKinds+ TypeFamilies+ --PolyKinds+ StandaloneDeriving+ GADTs+ KindSignatures+
src/HLearn/Models/Distributions.hs view
@@ -1,12 +1,8 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}- -- | This file exports the most commonly used modules within HLearn-distributions. Most likely this is the only file you will have to import. module HLearn.Models.Distributions ( module HLearn.Models.Distributions.Common+ , module HLearn.Models.Distributions.Kernels , module HLearn.Models.Distributions.Visualization.Gnuplot , module HLearn.Models.Distributions.Visualization.Graphviz , module HLearn.Models.Distributions.Univariate.Binomial@@ -16,9 +12,10 @@ , module HLearn.Models.Distributions.Univariate.LogNormal , module HLearn.Models.Distributions.Univariate.Normal -- , module HLearn.Models.Distributions.Univariate.Uniform+-- , module HLearn.Models.Distributions.Univariate.Student , module HLearn.Models.Distributions.Univariate.Poisson , module HLearn.Models.Distributions.Univariate.Internal.MissingData--- , module HLearn.Models.Distributions.KernelDensityEstimator+ , module HLearn.Models.Distributions.Univariate.KernelDensityEstimator , module HLearn.Models.Distributions.Multivariate.Interface , module HLearn.Models.Distributions.Multivariate.MultiNormal , module HLearn.Models.Distributions.Multivariate.Internal.TypeLens@@ -26,15 +23,18 @@ where import HLearn.Models.Distributions.Common+import HLearn.Models.Distributions.Kernels import HLearn.Models.Distributions.Visualization.Gnuplot import HLearn.Models.Distributions.Visualization.Graphviz import HLearn.Models.Distributions.Univariate.Binomial import HLearn.Models.Distributions.Univariate.Categorical import HLearn.Models.Distributions.Univariate.Exponential import HLearn.Models.Distributions.Univariate.Geometric+import HLearn.Models.Distributions.Univariate.KernelDensityEstimator import HLearn.Models.Distributions.Univariate.LogNormal import HLearn.Models.Distributions.Univariate.Normal -- import HLearn.Models.Distributions.Univariate.Uniform+-- import HLearn.Models.Distributions.Univariate.Student import HLearn.Models.Distributions.Univariate.Poisson import HLearn.Models.Distributions.Univariate.Internal.MissingData import HLearn.Models.Distributions.Multivariate.Interface
src/HLearn/Models/Distributions/Common.hs view
@@ -1,5 +1,3 @@-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE EmptyDataDecls #-} -- | This module contains the type classes for manipulating distributions.
+ src/HLearn/Models/Distributions/Kernels.hs view
@@ -0,0 +1,85 @@+-- | This list of kernels is take from wikipedia's: <https://en.wikipedia.org/wiki/Uniform_kernel#Kernel_functions_in_common_use>+module HLearn.Models.Distributions.Kernels+ (+ -- * Data types+ Kernel (..)+ , KernelBox (..)+ + -- * Kernels+ , Uniform (..)+ , Triangular (..)+ , Epanechnikov (..)+ , Quartic (..)+ , Triweight (..)+ , Tricube (..)+ , Gaussian (..)+ , Cosine (..)+ + )+ where+ +import Control.DeepSeq++-- | A kernel is function in one parameter that takes a value on the x axis and spits out a "probability." We create a data object for each kernel, and a corresponding class to make things play nice with the type system.+class Kernel kernel num where+ evalKernel :: kernel -> num -> num++-- | A KernelBox is a universal object for storing kernels. Whatever kernel it stores, it becomes a kernel with the same properties.+data KernelBox num where KernelBox :: (Kernel kernel num, Show kernel) => kernel -> KernelBox num++instance Kernel (KernelBox num) num where+ evalKernel (KernelBox k) p = evalKernel k p+instance Show (KernelBox num) where+ show (KernelBox k) = "KB "++show k+instance Eq (KernelBox num) where+ KernelBox k1 == KernelBox k2 = (show k1) == (show k2)+instance Ord (KernelBox num) where+ _ `compare` _ = EQ+instance NFData (KernelBox num) where+ rnf (KernelBox num)= seq num ()+ +data Uniform = Uniform deriving (Read,Show)+instance (Fractional num, Ord num) => Kernel Uniform num where+ evalKernel Uniform u = if abs u < 1+ then 1/2+ else 0++data Triangular = Triangular deriving (Read,Show)+instance (Fractional num, Ord num) => Kernel Triangular num where+ evalKernel Triangular u = if abs u<1+ then 1-abs u+ else 0+ +data Epanechnikov = Epanechnikov deriving (Read,Show)+instance (Fractional num, Ord num) => Kernel Epanechnikov num where+ evalKernel Epanechnikov u = if abs u<1+ then (3/4)*(1-u^^2)+ else 0++data Quartic = Quartic deriving (Read,Show)+instance (Fractional num, Ord num) => Kernel Quartic num where+ evalKernel Quartic u = if abs u<1+ then (15/16)*(1-u^^2)^^2+ else 0+ +data Triweight = Triweight deriving (Read,Show)+instance (Fractional num, Ord num) => Kernel Triweight num where+ evalKernel Triweight u = if abs u<1+ then (35/32)*(1-u^^2)^^3+ else 0++data Tricube = Tricube deriving (Read,Show)+instance (Fractional num, Ord num) => Kernel Tricube num where+ evalKernel Tricube u = if abs u<1+ then (70/81)*(1-u^^3)^^3+ else 0+ +data Cosine = Cosine deriving (Read,Show)+instance (Floating num, Ord num) => Kernel Cosine num where+ evalKernel Cosine u = if abs u<1+ then (pi/4)*(cos $ (pi/2)*u)+ else 0+ +data Gaussian = Gaussian deriving (Read,Show)+instance (Floating num, Ord num) => Kernel Gaussian num where+ evalKernel Gaussian u = (1/(2*pi))*(exp $ (-1/2)*u^^2)
src/HLearn/Models/Distributions/Multivariate/Interface.hs view
@@ -1,3 +1,5 @@++ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE MultiParamTypeClasses #-}@@ -35,7 +37,7 @@ import Control.DeepSeq import GHC.TypeLits -import HLearn.Algebra hiding (Index (..))+import HLearn.Algebra hiding (Index) import HLearn.Models.Distributions.Common import HLearn.Models.Distributions.Multivariate.Internal.CatContainer hiding (ds,baseparams) import HLearn.Models.Distributions.Multivariate.Internal.Container@@ -135,11 +137,11 @@ type instance MultiCategorical (x ': xs) = (CatContainer x) ': (MultiCategorical xs) -- type Dependent dist (xs :: [*]) = '[ MultiContainer (dist xs) xs ]-type family Dependent (dist::a) (xs :: [*]) :: [* -> * -> *]-type instance Dependent dist xs = '[ MultiContainer (dist xs) xs ]+type family Dependent (dist:: * -> [*] -> *) (xs :: [*]) :: [* -> * -> *]+type instance Dependent dist xs = '[ MultiContainer dist xs ] -type family Independent (dist :: a) (sampleL :: [*]) :: [* -> * -> *]+type family Independent (dist :: * -> * -> *) (sampleL :: [*]) :: [* -> * -> *] type instance Independent dist '[] = '[]-type instance Independent (dist :: * -> *) (x ': xs) = (Container dist x) ': (Independent dist xs)-type instance Independent (dist :: * -> * -> *) (x ': xs) = (Container (dist x) x) ': (Independent dist xs)+-- type instance Independent (dist :: * -> *) (x ': xs) = (Container dist x) ': (Independent dist xs)+type instance Independent (dist :: * -> * -> *) (x ': xs) = (Container dist x) ': (Independent dist xs)
src/HLearn/Models/Distributions/Multivariate/Internal/CatContainer.hs view
@@ -1,18 +1,4 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}-{-# LANGUAGE GADTs #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE TypeOperators #-} {-# LANGUAGE DataKinds #-}-{-# LANGUAGE FunctionalDependencies #-}-{-# LANGUAGE PolyKinds #-}-{-# LANGUAGE StandaloneDeriving #-}- -- | The categorical distribution is used for discrete data. It is also sometimes called the discrete distribution or the multinomial distribution. For more, see the wikipedia entry: <https://en.wikipedia.org/wiki/CatContainer_distribution> module HLearn.Models.Distributions.Multivariate.Internal.CatContainer {- ( @@ -166,7 +152,7 @@ ) => Marginalize' (Nat1Box Zero) (CatContainer label basedist prob) where - type Margin' (Nat1Box Zero) (CatContainer label basedist prob) = (Categorical label prob) + type Margin' (Nat1Box Zero) (CatContainer label basedist prob) = (Categorical prob label) getMargin' _ dist = Categorical $ probmap dist --Map.map numdp (pdfmap dist) type MarginalizeOut' (Nat1Box Zero) (CatContainer label basedist prob) = Ignore' label basedist prob
src/HLearn/Models/Distributions/Multivariate/Internal/Container.hs view
@@ -1,19 +1,5 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}-{-# LANGUAGE GADTs #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE TypeOperators #-} {-# LANGUAGE DataKinds #-}-{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE PolyKinds #-}-{-# LANGUAGE StandaloneDeriving #-}---- | module HLearn.Models.Distributions.Multivariate.Internal.Container ( Container , MultiContainer@@ -31,13 +17,13 @@ ------------------------------------------------------------------------------- -- data types -data Container dist sample basedist (prob:: * ) = Container - { dist :: dist prob+data Container (dist :: * -> a -> *) (sample:: a) basedist (prob:: * ) = Container + { dist :: dist prob sample , basedist :: basedist } deriving (Read,Show,Eq,Ord) -instance (NFData (dist prob), NFData basedist) => NFData (Container dist sample basedist prob) where+instance (NFData (dist prob sample), NFData basedist) => NFData (Container dist sample basedist prob) where rnf c = deepseq (dist c) $ rnf (basedist c) newtype MultiContainer dist sample basedist prob = MultiContainer (Container dist sample basedist prob)@@ -46,9 +32,9 @@ ------------------------------------------------------------------------------- -- Algebra -instance (Abelian (dist prob), Abelian basedist) => Abelian (Container dist sample basedist prob) +instance (Abelian (dist prob sample), Abelian basedist) => Abelian (Container dist sample basedist prob) instance - ( Monoid (dist prob)+ ( Monoid (dist prob sample) , Monoid basedist ) => Monoid (Container dist sample basedist prob) where@@ -59,7 +45,7 @@ } instance - ( Group (dist prob)+ ( Group (dist prob sample) , Group basedist ) => Group (Container dist sample basedist prob) where@@ -69,26 +55,26 @@ } instance - ( HasRing (dist prob)+ ( HasRing (dist prob sample) , HasRing basedist- , Ring (dist prob) ~ Ring basedist+ , Ring (dist prob sample) ~ Ring basedist ) => HasRing (Container dist sample basedist prob) where- type Ring (Container dist sample basedist prob) = Ring (dist prob)+ type Ring (Container dist sample basedist prob) = Ring (dist prob sample) instance - ( HasRing (dist prob)+ ( HasRing (dist prob sample) , HasRing basedist- , Ring (dist prob) ~ Ring basedist+ , Ring (dist prob sample) ~ Ring basedist ) => HasRing (MultiContainer dist sample basedist prob) where- type Ring (MultiContainer dist sample basedist prob) = Ring (dist prob)+ type Ring (MultiContainer dist sample basedist prob) = Ring (dist prob sample) instance - ( Module (dist prob)+ ( Module (dist prob sample) , Module basedist- , Ring (dist prob) ~ Ring basedist+ , Ring (dist prob sample) ~ Ring basedist ) => Module (Container dist sample basedist prob) where r .* c = Container@@ -97,9 +83,9 @@ } deriving instance - ( Module (dist prob)+ ( Module (dist prob sample) , Module basedist- , Ring (dist prob) ~ Ring basedist+ , Ring (dist prob sample) ~ Ring basedist ) => Module (MultiContainer dist sample basedist prob) @@ -107,42 +93,52 @@ -- Training instance - ( HomTrainer (dist prob)+ ( HomTrainer (dist prob sample) , HomTrainer basedist , Datapoint basedist ~ HList ys ) => HomTrainer (Container dist sample basedist prob) where type Datapoint (Container dist sample basedist prob) = - (Datapoint (dist prob)) `HCons` (Datapoint basedist)+ (Datapoint (dist prob sample)) `HCons` (Datapoint basedist) train1dp (dp:::basedp) = Container { dist = train1dp dp , basedist = train1dp basedp } -instance (NumDP (dist prob), HasRing basedist, Ring basedist ~ Ring (dist prob)) => NumDP (Container dist sample basedist prob) where+instance + ( NumDP (dist prob sample)+ , HasRing basedist+ , Ring basedist ~ Ring (dist prob sample)+ ) => NumDP (Container dist sample basedist prob) + where numdp container = numdp $ dist container --------------------------------------- instance - ( HomTrainer (dist prob)+ ( HomTrainer (dist prob sample) , HomTrainer basedist- , Datapoint (dist prob) ~ HList zs+ , Datapoint (dist prob sample) ~ HList zs , Datapoint basedist ~ HList ys , HTake1 (Nat1Box (Length1 zs)) (HList (zs++ys)) (HList zs) , HDrop1 (Nat1Box (Length1 zs)) (HList (zs++ys)) (HList ys) ) => HomTrainer (MultiContainer dist sample basedist prob) where type Datapoint (MultiContainer dist sample basedist prob) = - (Datapoint (dist prob)) `HAppend` (Datapoint basedist)+ (Datapoint (dist prob sample)) `HAppend` (Datapoint basedist) train1dp dpL = MultiContainer $ Container { dist = train1dp $ htake1 (Nat1Box :: Nat1Box (Length1 zs)) dpL , basedist = train1dp $ hdrop1 (Nat1Box :: Nat1Box (Length1 zs)) dpL } -instance (NumDP (dist prob), HasRing basedist, Ring basedist ~ Ring (dist prob)) => NumDP (MultiContainer dist sample basedist prob) where+instance + ( NumDP (dist prob sample)+ , HasRing basedist+ , Ring basedist ~ Ring (dist prob sample)+ ) => NumDP (MultiContainer dist sample basedist prob) + where numdp (MultiContainer container) = numdp $ dist container -------------------------------------------------------------------------------@@ -152,13 +148,13 @@ type Probability (Container dist sample basedist prob) = prob instance - ( PDF (dist prob)+ ( PDF (dist prob sample) , PDF basedist- , Probability (dist prob) ~ prob+ , Probability (dist prob sample) ~ prob , Probability basedist ~ prob , Probabilistic (Container dist sample basedist prob) , Datapoint basedist ~ HList ys- , Datapoint (dist prob) ~ y+ , Datapoint (dist prob sample) ~ y , Datapoint (Container dist sample basedist prob) ~ HList (y ': ys) , Num prob ) => PDF (Container dist sample basedist prob) @@ -169,7 +165,7 @@ pdf2 = pdf (basedist container) basedp instance Marginalize' (Nat1Box Zero) (Container dist (sample :: *) basedist prob) where- type Margin' (Nat1Box Zero) (Container dist sample basedist prob) = dist prob+ type Margin' (Nat1Box Zero) (Container dist sample basedist prob) = dist prob sample getMargin' _ container = dist container type MarginalizeOut' (Nat1Box Zero) (Container dist sample basedist prob) = Ignore' sample basedist prob@@ -211,12 +207,12 @@ type Probability (MultiContainer dist sample basedist prob) = prob instance - ( PDF (dist prob)+ ( PDF (dist prob sample) , PDF basedist- , prob ~ Probability (dist prob)+ , prob ~ Probability (dist prob sample) , prob ~ Probability basedist , Num prob- , Datapoint (dist prob) ~ HList dpL+ , Datapoint (dist prob sample) ~ HList dpL , Datapoint basedist ~ HList basedpL , HTake1 (Nat1Box (Length1 dpL)) (HList (dpL ++ basedpL)) (HList dpL) , HDrop1 (Nat1Box (Length1 dpL)) (HList (dpL ++ basedpL)) (HList basedpL)
src/HLearn/Models/Distributions/Multivariate/Internal/Ignore.hs view
@@ -1,18 +1,4 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}-{-# LANGUAGE GADTs #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE TypeOperators #-} {-# LANGUAGE DataKinds #-}-{-# LANGUAGE FunctionalDependencies #-}-{-# LANGUAGE PolyKinds #-}-{-# LANGUAGE StandaloneDeriving #-}- -- | Used for ignoring data module HLearn.Models.Distributions.Multivariate.Internal.Ignore ( Ignore
src/HLearn/Models/Distributions/Multivariate/Internal/Marginalization.hs view
@@ -1,3 +1,5 @@++ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE MultiParamTypeClasses #-}@@ -64,3 +66,4 @@ condition' :: index -> dist -> Datapoint (Margin' index dist) -> MarginalizeOut' index dist -- conditionAllButOne :: index -> dist -> Datapoint dist -> MarginalizeOut index dist+
src/HLearn/Models/Distributions/Multivariate/Internal/TypeLens.hs view
@@ -1,9 +1,4 @@-{-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE TypeOperators #-} {-# LANGUAGE DataKinds #-}-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-} -- | This module provides convenient TemplateHaskell functions for making type lens suitable for use with multivariate distributions. -- @@ -68,6 +63,7 @@ -- * Lens Trainable (..) , TypeLens (..)+ , TypeFunction (..) -- * TemplateHaskell , makeTypeLenses , nameTransform@@ -75,8 +71,8 @@ where import HLearn.Algebra-import Language.Haskell.TH-import Language.Haskell.TH.Syntax+import Language.Haskell.TH hiding (Range)+import Language.Haskell.TH.Syntax hiding (Range) -------------------------------------------------------------------------------@@ -99,10 +95,19 @@ class TypeLens i where type TypeLensIndex i +class TypeFunction f where+ type Domain f+ type Range f+ + typefunc :: f -> Domain f -> Range f+ -- | given the name of one of our records, transform it into the name of our type lens nameTransform :: String -> String nameTransform str = "TH"++str +nameTransform' :: Name -> Name+nameTransform' name = mkName $ "TH"++(nameBase name)+ -- | constructs the type lens makeTypeLenses :: Name -> Q [Dec] makeTypeLenses name = do@@ -110,7 +115,8 @@ indexNames <- makeIndexNames name trainableInstance <- makeTrainable name multivariateLabels <- makeMultivariateLabels name- return $ datatypes ++ indexNames ++ trainableInstance ++ multivariateLabels+ typeFunctions <- makeTypeFunctions name+ return $ datatypes ++ indexNames ++ trainableInstance ++ multivariateLabels ++ typeFunctions makeDatatypes :: Name -> Q [Dec] makeDatatypes name = fmap (map makeEmptyData) $ extractContructorNames name@@ -126,7 +132,16 @@ where typeNat 0 = ConT $ mkName "Zero" typeNat n = AppT (ConT $ mkName "Succ") $ typeNat (n-1)- ++makeTypeFunctions :: Name -> Q [Dec]+makeTypeFunctions constructorName = fmap (map makeTypeFunction) $ extractConstructorFields constructorName+ where+ makeTypeFunction (recordName,_,recordType) = InstanceD [] (AppT (ConT $ mkName "TypeFunction") (ConT $ nameTransform' recordName)) + [ TySynInstD (mkName "Domain") [ConT $ nameTransform' recordName] (ConT constructorName)+ , TySynInstD (mkName "Range") [ConT $ nameTransform' recordName] (SigT recordType StarT)+ , FunD (mkName "typefunc") [Clause [VarP $ mkName "_"{-, VarP $ mkName "domain"-}] (NormalB $ VarE recordName) []]+ ]+ makeTrainable :: Name -> Q [Dec] makeTrainable name = do hlistType <- extractHListType name@@ -146,10 +161,7 @@ go [] = ConE $ mkName "[]" go (x:xs) = AppE (AppE (ConE $ mkName ":") (LitE $ StringL (nameTransform x))) $ go xs ----------------------------------------------------------------------------------- below taken from Data.Lens --type ConstructorFieldInfo = (Name, Strict, Type)+--------------------------------------- extractHListType :: Name -> Q Type extractHListType name = do@@ -170,6 +182,11 @@ go (x:xs) = AppE (AppE (ConE $ mkName ":::") (AppE (VarE x) (VarE var))) $ go xs getName (n,s,t) = n++-------------------------------------------------------------------------------+-- below taken from Data.Lens ++type ConstructorFieldInfo = (Name, Strict, Type) extractContructorNames :: Name -> Q [String] extractContructorNames datatype = fmap (map name) $ extractConstructorFields datatype
src/HLearn/Models/Distributions/Multivariate/Internal/Unital.hs view
@@ -1,14 +1,4 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE DataKinds #-}-{-# LANGUAGE GADTs #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}- module HLearn.Models.Distributions.Multivariate.Internal.Unital where
src/HLearn/Models/Distributions/Multivariate/MultiNormal.hs view
@@ -1,3 +1,5 @@++ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE MultiParamTypeClasses #-}@@ -51,13 +53,13 @@ instance NFData (MultiNormalVec n prob) where rnf mn = seq mn () -newtype MultiNormal (xs::[*]) prob = MultiNormal (MultiNormalVec (Length xs) prob)+newtype MultiNormal prob (xs::[*]) = MultiNormal (MultiNormalVec (Length xs) prob) deriving (Read,Show,Eq,Ord,NFData) -deriving instance (Monoid (MultiNormalVec (Length xs) prob)) => Monoid (MultiNormal xs prob)-deriving instance (Abelian (MultiNormalVec (Length xs) prob)) => Abelian (MultiNormal xs prob)-deriving instance (Group (MultiNormalVec (Length xs) prob)) => Group (MultiNormal xs prob)-deriving instance (Module (MultiNormalVec (Length xs) prob)) => Module (MultiNormal xs prob)+deriving instance (Monoid (MultiNormalVec (Length xs) prob)) => Monoid (MultiNormal prob xs)+deriving instance (Abelian (MultiNormalVec (Length xs) prob)) => Abelian (MultiNormal prob xs)+deriving instance (Group (MultiNormalVec (Length xs) prob)) => Group (MultiNormal prob xs)+deriving instance (Module (MultiNormalVec (Length xs) prob)) => Module (MultiNormal prob xs) ------------------------------------------------------------------------------- -- algebra@@ -96,8 +98,8 @@ --------------------------------------- -instance (Num prob) => HasRing (MultiNormal xs prob) where- type Ring (MultiNormal xs prob) = prob+instance (Num prob) => HasRing (MultiNormal prob xs) where+ type Ring (MultiNormal prob xs) = prob ------------------------------------------------------------------------------- -- training@@ -116,13 +118,13 @@ ( SingI (Length xs) , Num prob , VU.Unbox prob- , HList2List (Datapoint (MultiNormal xs prob)) prob- ) => HomTrainer (MultiNormal xs prob) + , HList2List (Datapoint (MultiNormal prob xs)) prob+ ) => HomTrainer (MultiNormal prob xs) where- type Datapoint (MultiNormal xs prob) = HList xs+ type Datapoint (MultiNormal prob xs) = HList xs train1dp dp = MultiNormal $ train1dp $ VU.fromList $ hlist2list dp -instance (Num prob) => NumDP (MultiNormal xs prob) where+instance (Num prob) => NumDP (MultiNormal prob xs) where numdp (MultiNormal mn) = q0 mn -------------------------------------------------------------------------------@@ -162,9 +164,9 @@ , VU.Unbox prob , Num prob , SingI (FromNat1 (Length1 dpL))- ) => Probabilistic (MultiNormal dpL prob) + ) => Probabilistic (MultiNormal prob dpL) where- type Probability (MultiNormal dpL prob) = prob+ type Probability (MultiNormal prob dpL) = prob instance ( HList2List (HList dpL) prob@@ -175,7 +177,7 @@ , SingI (FromNat1 (Length1 dpL)) -- , Covariance (MultiNormal dpL prob) , Storable prob- ) => PDF (MultiNormal dpL prob) + ) => PDF (MultiNormal prob dpL) where pdf (MultiNormal dist) dpL = 1/(sqrt $ (2*pi)^(k)*(det sigma))*(exp $ (-1/2)*(top) ) where@@ -213,5 +215,6 @@ , 3:::1:::1:::HNil , 3:::2:::1:::HNil ]-test = train ds :: MultiNormal '[Double,Double,Double] Double+test = train ds :: MultiNormal Double '[Double,Double,Double] +
src/HLearn/Models/Distributions/Univariate/Binomial.hs view
@@ -4,10 +4,6 @@ {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE BangPatterns #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}-- module HLearn.Models.Distributions.Univariate.Binomial where @@ -22,44 +18,44 @@ ------------------------------------------------------------------------------- -- data types -newtype Binomial sample prob = Binomial { bmoments :: (Moments3 sample) }+newtype Binomial prob dp = Binomial { bmoments :: (Moments3 dp) } deriving (Read,Show,Eq,Ord,Monoid,Group) ------------------------------------------------------------------------------- -- Training -instance (Num sample) => HomTrainer (Binomial sample prob) where- type Datapoint (Binomial sample prob) = sample+instance (Num dp) => HomTrainer (Binomial prob dp) where+ type Datapoint (Binomial prob dp) = dp train1dp dp = Binomial $ train1dp dp ------------------------------------------------------------------------------- -- distribution -instance (Num sample) => Probabilistic (Binomial sample prob) where- type Probability (Binomial sample prob) = prob+instance (Num dp) => Probabilistic (Binomial prob dp) where+ type Probability (Binomial prob dp) = prob -instance (Floating prob) => PDF (Binomial Int Double) where+instance (Floating prob) => PDF (Binomial Double Int) where pdf (Binomial dist) dp = S.probability (binomial n p) dp where n = bin_n $ Binomial dist p = bin_p $ Binomial dist -bin_n :: Binomial Int Double -> Int+bin_n :: Binomial Double Int -> Int bin_n (Binomial dist) = round $ ((fromIntegral $ m1 dist :: Double) / (fromIntegral $ m0 dist)) / (bin_p $ Binomial dist) -bin_p :: Binomial Int Double -> Double+bin_p :: Binomial Double Int -> Double bin_p (Binomial dist) = ((fromIntegral $ m1 dist) / (fromIntegral $ m0 dist)) + 1 - (fromIntegral $ m2 dist)/(fromIntegral $ m1 dist) instance - ( PDF (Binomial sample prob)--- , PlottableDataPoint sample+ ( PDF (Binomial prob dp)+-- , PlottableDataPoint dp , Show prob- , Show sample- , Ord sample+ , Show dp+ , Ord dp , Ord prob , Num prob- , Integral sample- ) => PlottableDistribution (Binomial sample prob) + , Integral dp+ ) => PlottableDistribution (Binomial prob dp) -- instance PlottableDistribution (Poisson Int Double) where
src/HLearn/Models/Distributions/Univariate/Categorical.hs view
@@ -1,12 +1,4 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE TypeFamilies #-} - -- | The categorical distribution is used for discrete data. It is also sometimes called the discrete distribution or the multinomial distribution. For more, see the wikipedia entry: <https://en.wikipedia.org/wiki/Categorical_distribution> module HLearn.Models.Distributions.Univariate.Categorical ( @@ -28,6 +20,7 @@ import qualified Data.Map.Strict as Map import qualified Data.Foldable as F +import qualified Control.ConstraintKinds as CK import HLearn.Algebra import HLearn.Models.Distributions.Common import HLearn.Models.Distributions.Visualization.Gnuplot@@ -35,49 +28,73 @@ ------------------------------------------------------------------------------- -- Categorical -data Categorical sampletype prob = Categorical - { pdfmap :: !(Map.Map sampletype prob)+newtype Categorical prob label = Categorical + { pdfmap :: Map.Map label prob } deriving (Show,Read,Eq,Ord) -instance (NFData sampletype, NFData prob) => NFData (Categorical sampletype prob) where+instance (NFData label, NFData prob) => NFData (Categorical prob label) where rnf d = rnf $ pdfmap d +uniformNoise :: (Fractional prob, Ord label) => prob -> [label] -> label -> Categorical prob label+uniformNoise n xs dp = trainW xs'+ where+ xs' = (1-n,dp):(map (\x -> (weight,x)) xs)+ weight = n/(fromIntegral $ length xs)+ ------------------------------------------------------------------------------- -- Algebra -instance (Ord label, Num prob) => Abelian (Categorical label prob)-instance (Ord label, Num prob) => Monoid (Categorical label prob) where+instance (Ord label, Num prob) => Abelian (Categorical prob label)+instance (Ord label, Num prob) => Monoid (Categorical prob label) where mempty = Categorical Map.empty mappend !d1 !d2 = Categorical $ res where res = Map.unionWith (+) (pdfmap d1) (pdfmap d2) -instance (Ord label, Num prob) => Group (Categorical label prob) where+instance (Ord label, Num prob) => Group (Categorical prob label) where inverse d1 = d1 {pdfmap=Map.map (0-) (pdfmap d1)} -instance (Num prob) => HasRing (Categorical label prob) where- type Ring (Categorical label prob) = prob-instance (Ord label, Num prob) => Module (Categorical label prob) where+instance (Num prob) => HasRing (Categorical prob label) where+ type Ring (Categorical prob label) = prob+instance (Ord label, Num prob) => Module (Categorical prob label) where p .* (Categorical pdf) = Categorical $ Map.map (*p) pdf +---------------------------------------++instance CK.Functor (Categorical prob) where+ type FunctorConstraint (Categorical prob) label = (Ord label, Num prob)+ fmap f cat = Categorical $ Map.mapKeysWith (+) f $ pdfmap cat++-- instance (Num prob) => CK.Pointed (Categorical prob) where+-- point dp = Categorical $ Map.singleton dp 1+ +instance (Num prob, Ord prob) => CK.Monad (Categorical prob) where+ return dp = Categorical $ Map.singleton dp 1+ x >>= f = join $ CK.fmap f x++join :: (Num prob, Ord label) => Categorical prob (Categorical prob label) -> Categorical prob label+join cat = reduce . map f $ Map.assocs $ pdfmap cat+ where+ f (cat,v) = v .* cat+ ------------------------------------------------------------------------------- -- Training -instance (Ord label, Num prob) => HomTrainer (Categorical label prob) where- type Datapoint (Categorical label prob) = label+instance (Ord label, Num prob) => HomTrainer (Categorical prob label) where+ type Datapoint (Categorical prob label) = label train1dp dp = Categorical $ Map.singleton dp 1 -instance (Num prob) => NumDP (Categorical label prob) where+instance (Num prob) => NumDP (Categorical prob label) where numdp dist = F.foldl' (+) 0 $ pdfmap dist ------------------------------------------------------------------------------- -- Distribution -instance Probabilistic (Categorical label prob) where- type Probability (Categorical label prob) = prob+instance Probabilistic (Categorical prob label) where+ type Probability (Categorical prob label) = prob -instance (Ord label, Ord prob, Fractional prob) => PDF (Categorical label prob) where+instance (Ord label, Ord prob, Fractional prob) => PDF (Categorical prob label) where {-# INLINE pdf #-} pdf dist label = {-0.0001+-}(val/tot)@@ -87,7 +104,7 @@ Just x -> x tot = F.foldl' (+) 0 $ pdfmap dist -instance (Ord label, Ord prob, Fractional prob) => CDF (Categorical label prob) where+instance (Ord label, Ord prob, Fractional prob) => CDF (Categorical prob label) where {-# INLINE cdf #-} cdf dist label = (Map.foldl' (+) 0 $ Map.filterWithKey (\k a -> k<=label) $ pdfmap dist) @@ -112,22 +129,22 @@ -- return $ cdfInverse dist (x::prob) -instance (Num prob, Ord prob, Ord label) => Mean (Categorical label prob) where+instance (Num prob, Ord prob, Ord label) => Mean (Categorical prob label) where mean dist = fst $ argmax snd $ Map.toList $ pdfmap dist -- | Extracts the element in the distribution with the highest probability-mostLikely :: Ord prob => Categorical label prob -> label+mostLikely :: Ord prob => Categorical prob label -> label mostLikely dist = fst $ argmax snd $ Map.toList $ pdfmap dist -- | Converts a distribution into a list of (sample,probability) pai-dist2list :: Categorical sampletype prob -> [(sampletype,prob)]+dist2list :: Categorical prob label -> [(label,prob)] dist2list (Categorical pdfmap) = Map.toList pdfmap instance ( Ord label, Show label , Ord prob, Show prob, Fractional prob- ) => PlottableDistribution (Categorical label prob) + ) => PlottableDistribution (Categorical prob label) where samplePoints (Categorical dist) = Map.keys dist plotType dist = Bar@@ -138,7 +155,7 @@ -- instance -- ( Ord label -- , Num prob--- ) => Morphism (Categorical label prob) FreeModParams (FreeMod prob label) +-- ) => Morphism (Categorical prob label) FreeModParams (FreeMod prob label) -- where -- Categorical pdf $> FreeModParams = FreeMod pdf --
src/HLearn/Models/Distributions/Univariate/Exponential.hs view
@@ -1,18 +1,3 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE TypeOperators #-}-{-# LANGUAGE FunctionalDependencies #-}--{-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE DataKinds #-}-{-# LANGUAGE KindSignatures #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}- -- | The method of moments can be used to estimate a number of commonly used distributions. This module is still under construction as I work out the best way to handle morphisms from the Moments3 type to types of other distributions. For more information, see the wikipedia entry: <https://en.wikipedia.org/wiki/Method_of_moments_(statistics)> module HLearn.Models.Distributions.Univariate.Exponential@@ -33,27 +18,27 @@ ------------------------------------------------------------------------------- -- Exponential -newtype Exponential prob = Exponential (Moments3 prob)+newtype Exponential prob dp = Exponential (Moments3 prob) deriving (Read,Show,Eq,Ord,Monoid,Group) -instance (Num prob) => HomTrainer (Exponential prob) where- type Datapoint (Exponential prob) = prob+instance (Num prob) => HomTrainer (Exponential prob prob) where+ type Datapoint (Exponential prob prob) = prob train1dp dp = Exponential $ train1dp dp -instance (Num prob) => Probabilistic (Exponential prob) where- type Probability (Exponential prob) = prob+instance (Num prob) => Probabilistic (Exponential prob dp) where+ type Probability (Exponential prob dp) = prob -instance (Floating prob) => PDF (Exponential prob) where+instance (Floating prob) => PDF (Exponential prob prob) where pdf dist dp = lambda*(exp $ (-1)*lambda*dp) where lambda = e_lambda dist e_lambda (Exponential dist) = (m0 dist)/(m1 dist) -instance (Fractional prob) => Mean (Exponential prob) where+instance (Fractional prob) => Mean (Exponential prob prob) where mean dist = 1/(e_lambda dist) -instance (Fractional prob) => Variance (Exponential prob) where+instance (Fractional prob) => Variance (Exponential prob prob) where variance dist = 1/(e_lambda dist)^^2 instance @@ -61,7 +46,7 @@ , Enum prob , Show prob , Ord prob- ) => PlottableDistribution (Exponential prob) where+ ) => PlottableDistribution (Exponential prob prob) where plotType _ = Continuous
src/HLearn/Models/Distributions/Univariate/Geometric.hs view
@@ -1,18 +1,3 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE TypeOperators #-}-{-# LANGUAGE FunctionalDependencies #-}--{-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE DataKinds #-}-{-# LANGUAGE KindSignatures #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}- -- | The method of moments can be used to estimate a number of commonly used distributions. This module is still under construction as I work out the best way to handle morphisms from the Moments3 type to types of other distributions. For more information, see the wikipedia entry: <https://en.wikipedia.org/wiki/Method_of_moments_(statistics)> module HLearn.Models.Distributions.Univariate.Geometric@@ -36,31 +21,31 @@ ------------------------------------------------------------------------------- -- Geometric -newtype Geometric sample prob = Geometric { moments :: (Moments3 sample) }+newtype Geometric prob dp = Geometric { moments :: (Moments3 dp) } deriving (Read,Show,Eq,Ord,Monoid,Group) -instance (Num sample) => HomTrainer (Geometric sample prob) where- type Datapoint (Geometric sample prob) = sample+instance (Num dp) => HomTrainer (Geometric prob dp) where+ type Datapoint (Geometric prob dp) = dp train1dp dp = Geometric $ train1dp dp -instance (Num sample) => Probabilistic (Geometric sample prob) where- type Probability (Geometric sample prob) = prob+instance (Num dp) => Probabilistic (Geometric prob dp) where+ type Probability (Geometric prob dp) = prob -instance (Integral sample, Floating prob) => PDF (Geometric sample prob) where+instance (Integral dp, Floating prob) => PDF (Geometric prob dp) where pdf dist dp = p*(1-p)^^dp where p = geo_p dist instance - ( PDF (Geometric sample prob)+ ( PDF (Geometric prob dp) , Show prob- , Show sample- , Ord sample+ , Show dp+ , Ord dp , Ord prob , Fractional prob , RealFrac prob- , Integral sample- ) => PlottableDistribution (Geometric sample prob) + , Integral dp+ ) => PlottableDistribution (Geometric prob dp) where plotType _ = Points@@ -70,13 +55,13 @@ min = 0 max = maximum [20,round $ 3*(fromIntegral $ mean dist)] -geo_p :: (Fractional prob, Integral sample) => Geometric sample prob -> prob+geo_p :: (Fractional prob, Integral dp) => Geometric prob dp -> prob geo_p (Geometric dist) = 1/((fromIntegral $ m1 dist)/(fromIntegral $ m0 dist) +1) -instance (Integral sample, RealFrac prob) => Mean (Geometric sample prob) where+instance (Integral dp, RealFrac prob) => Mean (Geometric prob dp) where mean dist = round $ 1/(geo_p dist) -instance (Integral sample, Fractional prob) => Variance (Geometric sample prob) where+instance (Integral dp, Fractional prob) => Variance (Geometric prob dp) where variance dist = (1-p)/(p*p) where p = geo_p dist
src/HLearn/Models/Distributions/Univariate/Internal/MissingData.hs view
@@ -1,18 +1,4 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}-{-# LANGUAGE GADTs #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE TypeOperators #-} {-# LANGUAGE DataKinds #-}-{-# LANGUAGE FunctionalDependencies #-}-{-# LANGUAGE PolyKinds #-}-{-# LANGUAGE StandaloneDeriving #-}- -- | Adapts any distribution into one that can handle missing data module HLearn.Models.Distributions.Univariate.Internal.MissingData ( MissingData
src/HLearn/Models/Distributions/Univariate/Internal/Moments.hs view
@@ -1,18 +1,5 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE TypeOperators #-}-{-# LANGUAGE FunctionalDependencies #-}- {-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE DataKinds #-}-{-# LANGUAGE KindSignatures #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}-+ -- | The method of moments can be used to estimate a number of commonly used distributions. This module is still under construction as I work out the best way to handle morphisms from the Moments3 type to types of other distributions. For more information, see the wikipedia entry: <https://en.wikipedia.org/wiki/Method_of_moments_(statistics)> module HLearn.Models.Distributions.Univariate.Internal.Moments
+ src/HLearn/Models/Distributions/Univariate/KernelDensityEstimator.hs view
@@ -0,0 +1,67 @@+{-# LANGUAGE DataKinds #-}+-- | Kernel Density Estimation (KDE) is a generic and powerful method for estimating a probability distribution. See wikipedia for more information: <http://en.wikipedia.org/wiki/Kernel_density_estimation>+module HLearn.Models.Distributions.Univariate.KernelDensityEstimator+ where++import Control.DeepSeq+import qualified Data.Map as Map+import GHC.TypeLits+import qualified Data.Foldable as F+import qualified Control.ConstraintKinds as CK++import HLearn.Algebra+import HLearn.Models.Distributions.Common+import HLearn.Models.Distributions.Kernels+import HLearn.DataStructures.SortedVector++-------------------------------------------------------------------------------+-- data types++-- | The KDE type is implemented as an isomorphism with the FreeModule+newtype KDE kernel (h::Nat) prob dp = KDE+-- { freemod :: FreeModule prob dp + { freemod :: SortedVector dp + }+ deriving (Read,Show,Eq,Ord,NFData,Monoid,Group,Abelian{-,Module-})++-------------------------------------------------------------------------------+-- Training+ +instance (Num (Ring (SortedVector dp))) => HasRing (KDE kernel h prob dp) where+-- type Ring (KDE kernel h prob dp) = prob+ type Ring (KDE kernel h prob dp) = Ring (SortedVector dp) + +instance (Num prob, NumDP (SortedVector dp)) => NumDP (KDE kernel h prob dp) where+ numdp (KDE v) = numdp v + +instance (Num prob, Ord prob) => HomTrainer (KDE kernel h prob prob) where+ type Datapoint (KDE kernel h prob prob) = prob+ train1dp dp = KDE $ train1dp dp ++---------------------------------------++instance CK.Functor (KDE kernel h prob) where+ type FunctorConstraint (KDE kernel h prob) dp = Ord dp + fmap f = KDE . CK.fmap f . freemod++-------------------------------------------------------------------------------+-- Distribution+ +instance Probabilistic (KDE kernel h prob dp) where+ type Probability (KDE kernel h prob dp) = prob+ +instance + ( Kernel kernel prob+ , SingI h+ , Fractional prob+ , prob ~ Ring (SortedVector prob)+ , NumDP (SortedVector prob)+ ) => PDF (KDE kernel h prob prob) + where+ pdf kde dp = (1/(n*h))*(foldr (+) 0 $ map (\x -> f $ (dp-x)/h) dpList)+ where + f = evalKernel (undefined::kernel)+ n = numdp kde+ h = fromIntegral $ fromSing (sing :: Sing h)+-- dpList = Map.keys (getMap $ freemod kde)+ dpList = F.toList (freemod kde)
src/HLearn/Models/Distributions/Univariate/LogNormal.hs view
@@ -1,18 +1,3 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE TypeOperators #-}-{-# LANGUAGE FunctionalDependencies #-}--{-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE DataKinds #-}-{-# LANGUAGE KindSignatures #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}- -- | LogNormal module HLearn.Models.Distributions.Univariate.LogNormal@@ -21,6 +6,7 @@ where import Debug.Trace+import Data.Number.Erf import HLearn.Algebra import HLearn.Models.Distributions.Common@@ -30,23 +16,23 @@ ------------------------------------------------------------------------------- -- data types -newtype LogNormal prob = LogNormal (Moments3 prob)+newtype LogNormal prob dp = LogNormal (Moments3 prob) deriving (Read,Show,Eq,Ord,Monoid,Group) ------------------------------------------------------------------------------- -- training -instance (Floating prob) => HomTrainer (LogNormal prob) where- type Datapoint (LogNormal prob) = prob+instance (Floating prob) => HomTrainer (LogNormal prob prob) where+ type Datapoint (LogNormal prob prob) = prob train1dp dp = LogNormal $ train1dp $ dp ------------------------------------------------------------------------------- -- distribution -instance Probabilistic (LogNormal prob) where- type Probability (LogNormal prob) = prob+instance Probabilistic (LogNormal prob dp) where+ type Probability (LogNormal prob dp) = prob -instance (Floating prob) => PDF (LogNormal prob) where+instance (Floating prob) => PDF (LogNormal prob prob) where pdf (LogNormal dist) dp = (1 / (dp * (sqrt $ s2 * 2 * pi)))*(exp $ (-1)*((log dp)-m)^2/(2*s2)) where -- sigma2 = variance dist@@ -61,7 +47,19 @@ raw1 = (m1 dist)/(m0 dist) raw2 = (m2 dist)/(m0 dist) -instance (Floating prob) => Mean (LogNormal prob) where+instance (Floating prob, Erf prob) => CDF (LogNormal prob prob) where+ cdf (LogNormal dist) dp = ( 0.5 + 0.5 * ( 1 + erf ( (log (dp - m) ) / (sqrt $ s2 *2) )))+ where+ m = 2*log1 - (1/2) *log1+ s2 = log2 - 2*log1++ log1 = log raw1+ log2 = log raw2+ + raw1 = (m1 dist)/(m0 dist)+ raw2 = (m2 dist)/(m0 dist)++instance (Floating prob) => Mean (LogNormal prob prob) where mean (LogNormal dist) = exp $ m+s2/2 where m = 2*log1 - (1/2)*log1@@ -73,7 +71,7 @@ raw1 = (m1 dist)/(m0 dist) raw2 = (m2 dist)/(m0 dist) -instance (Show prob, Floating prob) => Variance (LogNormal prob) where+instance (Show prob, Floating prob) => Variance (LogNormal prob prob) where variance (LogNormal dist) = trace ("m="++show m++"; s2="++show s2) $ ((exp s2) -1)*(exp $ 2*m+s2) where m = 2*log1 - (1/2)*log1@@ -90,7 +88,7 @@ , Enum prob , Show prob , Ord prob- ) => PlottableDistribution (LogNormal prob) where+ ) => PlottableDistribution (LogNormal prob prob) where plotType _ = Continuous
src/HLearn/Models/Distributions/Univariate/Normal.hs view
@@ -1,18 +1,3 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE TypeOperators #-}-{-# LANGUAGE FunctionalDependencies #-}--{-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE DataKinds #-}-{-# LANGUAGE KindSignatures #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}- -- | The method of moments can be used to estimate a number of commonly used distributions. This module is still under construction as I work out the best way to handle morphisms from the Moments3 type to types of other distributions. For more information, see the wikipedia entry: <https://en.wikipedia.org/wiki/Method_of_moments_(statistics)> module HLearn.Models.Distributions.Univariate.Normal@@ -24,6 +9,8 @@ import GHC.TypeLits import qualified Data.Vector.Unboxed as U import Data.Vector.Unboxed.Deriving+import Math.Gamma+import Data.Number.Erf import HLearn.Algebra import HLearn.Models.Distributions.Common@@ -33,35 +20,60 @@ ------------------------------------------------------------------------------- -- data types -newtype Normal prob = Normal (Moments3 prob)+newtype Normal prob dp = Normal (Moments3 prob) deriving (Read,Show,Eq,Ord,Monoid,Group,Abelian,Module,NumDP,NFData) +mkNormal :: (Num prob) => prob -> prob -> Normal prob dp+mkNormal mu sigma = Normal $ Moments3+ { m0 = 1+ , m1 = mu+ , m2 = sigma*sigma + mu*mu+ }++addNoise :: (Num prob) => (prob -> prob) -> prob -> Normal prob prob+addNoise f dp = mkNormal dp (f dp)+ ------------------------------------------------------------------------------- -- training -instance (Num prob) => HomTrainer (Normal prob) where- type Datapoint (Normal prob) = prob+instance (Num prob) => HomTrainer (Normal prob (Normal prob dp)) where+ type Datapoint (Normal prob (Normal prob dp)) = Normal prob dp+ train1dp (Normal dp) = Normal dp++instance (Num prob) => HomTrainer (Normal prob prob) where+ type Datapoint (Normal prob prob) = prob train1dp dp = Normal $ train1dp dp -instance (Num prob) => HasRing (Normal prob) where- type Ring (Normal prob) = prob+instance (Num prob) => HasRing (Normal prob dp) where+ type Ring (Normal prob dp) = prob +---------------------------------------++join :: Normal prob (Normal prob dp) -> Normal prob dp+join (Normal moments) = Normal moments+ ---------------------------------------------------------------------------------- algebra+-- distribution -instance (Num prob) => Probabilistic (Normal prob) where- type Probability (Normal prob) = prob+instance (Num prob) => Probabilistic (Normal prob dp) where+ type Probability (Normal prob dp) = prob -instance (Floating prob) => PDF (Normal prob) where+instance (Floating prob) => PDF (Normal prob prob) where pdf dist dp = (1 / (sqrt $ sigma2 * 2 * pi))*(exp $ (-1)*(dp-mu)*(dp-mu)/(2*sigma2)) where sigma2 = variance dist mu = mean dist -instance (Fractional prob) => Mean (Normal prob) where+instance (Floating prob, Erf prob) => CDF (Normal prob prob) where+ cdf dist dp = ( 0.5 * ( 1 + erf ( (dp - mu) / (sqrt $ sigma2 *2) )))+ where+ sigma2 = variance dist+ mu = mean dist++instance (Fractional prob) => Mean (Normal prob prob) where mean (Normal dist) = m1 dist / m0 dist -instance (Fractional prob) => Variance (Normal prob) where+instance (Fractional prob) => Variance (Normal prob prob) where variance normal@(Normal dist) = m2 dist / m0 dist - (mean normal)*(mean normal) instance @@ -69,13 +81,19 @@ , Enum prob , Show prob , Ord prob- ) => PlottableDistribution (Normal prob) where+ ) => PlottableDistribution (Normal prob prob) where plotType _ = Continuous samplePoints dist = samplesFromMinMax min max--- fmap (\x -> min+x/(numsamples*(max-min))) [0..numsamples] where- numsamples = 1000 min = (mean dist)-5*(sqrt $ variance dist) max = (mean dist)+5*(sqrt $ variance dist)++-------------------------------------------------------------------------------+-- test++dp1 = mkNormal 1 1+dp2 = mkNormal 10 1++model = train [dp1,dp2] :: Normal Double (Normal Double Double)
src/HLearn/Models/Distributions/Univariate/Poisson.hs view
@@ -1,18 +1,4 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE TypeOperators #-}-{-# LANGUAGE FunctionalDependencies #-} -{-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE DataKinds #-}-{-# LANGUAGE KindSignatures #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}- -- | The method of moments can be used to estimate a number of commonly used distributions. This module is still under construction as I work out the best way to handle morphisms from the Moments3 type to types of other distributions. For more information, see the wikipedia entry: <https://en.wikipedia.org/wiki/Method_of_moments_(statistics)> module HLearn.Models.Distributions.Univariate.Poisson@@ -37,17 +23,17 @@ ------------------------------------------------------------------------------- -- Poisson -newtype Poisson sample prob = Poisson { pmoments :: (Moments3 sample) }+newtype Poisson prob dp = Poisson { pmoments :: (Moments3 dp) } deriving (Read,Show,Eq,Ord,Monoid,Group) -instance (Num sample) => HomTrainer (Poisson sample prob) where- type Datapoint (Poisson sample prob) = sample+instance (Num dp) => HomTrainer (Poisson prob dp) where+ type Datapoint (Poisson prob dp) = dp train1dp dp = Poisson $ train1dp dp -instance (Num sample) => Probabilistic (Poisson sample prob) where- type Probability (Poisson sample prob) = prob+instance (Num dp) => Probabilistic (Poisson prob dp) where+ type Probability (Poisson prob dp) = prob --- instance (Integral sample, Floating prob) => PDF (Poisson sample prob) where+-- instance (Integral dp, Floating prob) => PDF (Poisson prob dp) where -- pdf (Poisson dist) dp -- | dp < 0 = 0 -- | dp > 100 = pdf (Normal $ Moments3 (fromIntegral $ m0 dist) (fromIntegral $ m1 dist) (fromIntegral $ m2 dist)) $ fromIntegral dp@@ -58,21 +44,21 @@ -- factorial 0 = 1 -- factorial n = n*(factorial $ n-1) -instance (Integral sample, Floating prob) => PDF (Poisson sample Double) where+instance (Integral dp, Floating prob) => PDF (Poisson Double dp) where pdf (Poisson dist) dp = S.probability (poisson lambda) $ fromIntegral dp where lambda = (fromIntegral $ m1 dist) / (fromIntegral $ m0 dist) instance - ( PDF (Poisson sample prob)--- , PlottableDataPoint sample+ ( PDF (Poisson prob dp)+-- , PlottableDataPoint dp , Show prob- , Show sample- , Ord sample+ , Show dp+ , Ord dp , Ord prob , Fractional prob- , Integral sample- ) => PlottableDistribution (Poisson sample prob) + , Integral dp+ ) => PlottableDistribution (Poisson prob dp) -- instance PlottableDistribution (Poisson Int Double) where @@ -84,8 +70,8 @@ max = maximum [20,floor $ 3*lambda] lambda = (fromIntegral $ m1 $ pmoments dist) / (fromIntegral $ m0 $ pmoments dist) --- instance (Fractional prob) => Mean (Poisson sample prob) where+-- instance (Fractional prob) => Mean (Poisson prob dp) where -- mean (Poisson dist) = (fromIntegral $ m1 $ pmoments dist) / (fromIntegral $ m0 $ pmoments dist) -- --- instance (Fractional prob) => Variance (Poisson sample prob) where+-- instance (Fractional prob) => Variance (Poisson prob dp) where -- variance dist = mean dist
src/HLearn/Models/Distributions/Visualization/Gnuplot.hs view
@@ -1,12 +1,3 @@-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE TypeSynonymInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FunctionalDependencies #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE UndecidableInstances #-}- {-# LANGUAGE OverlappingInstances #-} -- {-# LANGUAGE IncoherentInstances #-} @@ -142,12 +133,15 @@ -- ++ "set border 0; set xzeroaxis lt 1; set yzeroaxis lt 1 \n" ++ "zero(x)=0\n" ++ "set border 2; set style fill solid 1\n"+ ++ "set xlabel tc rgb \"#555555\"\n"+ ++ "set ylabel \"Probability\" tc rgb \"#555555\"\n"+ ++ "set tics textcolor rgb \"#444444\"\n" where terminal = case picType params of EPS -> "set terminal postscript \"Times-Roman\" 25 \n" ++ "set size 0.81, 1\n" -- PNG _ _ -> "png"- PNG w h -> "set terminal pngcairo size "++show w++","++show h++" enhanced font 'Times-Roman,10' \n"+ PNG w h -> "set terminal pngcairo size "++show w++","++show h++" enhanced font 'Times-Roman,8' \n" -- class PlotHList t where -- plotargs :: t -> [(String,String)] -> [String]@@ -168,7 +162,7 @@ ++ "set xzeroaxis lt 1 lc rgb '#000000'\n" -- ++ "set ylabel \"Probability\"\n" ++ "set style data histogram; set style histogram cluster gap 1\n"- ++ "plot '"++(dataFile params)++"' using 2:xticlabels(1) lw 4 linecolor rgb '#0000ff' fs solid 1\n"+ ++ "plot '"++(dataFile params)++"' using 2:xticlabels(1) linecolor rgb '#0000ff' #fs solid 1\n" Continuous -> "plot '"++(dataFile params)++"' using 1:2:(zero($2)) lt 1 lw 4 lc rgb '#ccccff' with filledcurves, " -- Continuous -> "plot '"++(dataFile params)++"' using 1:2 lt 1 lw 4 lc rgb '#ccccff' with filledcurves, " ++ " '"++(dataFile params)++"' using 1:2 lt 1 lw 4 lc rgb '#0000ff' with lines"
− src/HLearn/Models/Distributions/Visualization/Graphviz.hs
@@ -1,131 +0,0 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}-{-# LANGUAGE GADTs #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE TypeOperators #-}-{-# LANGUAGE DataKinds #-}-{-# LANGUAGE FunctionalDependencies #-}-{-# LANGUAGE PolyKinds #-}-{-# LANGUAGE StandaloneDeriving #-}-{-# LANGUAGE OverloadedStrings #-}---- | Displays Multivariate dependencies--module HLearn.Models.Distributions.Visualization.Graphviz- ( MultivariateLabels (..)- , MarkovNetwork (..)- )- where--import HLearn.Algebra-import HLearn.Models.Distributions.Multivariate.Interface-import HLearn.Models.Distributions.Multivariate.Internal.CatContainer-import HLearn.Models.Distributions.Multivariate.Internal.Container-import HLearn.Models.Distributions.Multivariate.Internal.TypeLens--import Data.GraphViz.Exception-import Data.GraphViz hiding (graphToDot)-import Data.GraphViz.Attributes.Complete{-( Attribute(RankDir, Splines, FontName)- , RankDir(FromLeft), EdgeType(SplineEdges))-}-import Control.Arrow(second)-import GHC.TypeLits------------------------------------------------------------------------------------ clases--class (Trainable datatype) => MultivariateLabels datatype where- getLabels :: datatype -> [String]- -class (MultivariateLabels (Datapoint dist)) => MarkovNetwork dist where- graphL :: dist -> [String] -> [(String,[String])]- - plotNetwork :: FilePath -> dist -> IO Bool- plotNetwork file dist = graphToDotPng file $ graphL dist $ getLabels (undefined :: Datapoint dist)- ----------------------------------------------------------------------------------- instances--instance - ( MultivariateLabels datapoint- ) => MarkovNetwork (Multivariate datapoint '[] prob) - where- graphL _ labels = []--instance - ( MultivariateLabels datapoint- , MarkovNetwork (Multivariate datapoint xs prob)- ) => MarkovNetwork (Multivariate datapoint ( ('[]) ': xs) prob) - where- graphL _ labels = graphL (undefined :: Multivariate datapoint xs prob) labels--instance - ( MultivariateLabels datapoint- , MarkovNetwork (Multivariate datapoint ( ys ': xs) prob)- ) => MarkovNetwork (Multivariate datapoint ( (Ignore' label ': ys) ': xs) prob) - where- graphL _ labels = (graphL (undefined :: Multivariate datapoint ( ys ': xs) prob) (tail labels))--instance - ( MultivariateLabels datapoint- , MarkovNetwork (Multivariate datapoint ( ys ': xs) prob)- ) => MarkovNetwork (Multivariate datapoint ( (CatContainer label ': ys) ': xs) prob) - where- graphL _ labels = (head labels, tail labels)- : (graphL (undefined :: Multivariate datapoint ( ys ': xs) prob) (tail labels))--instance - ( MultivariateLabels datapoint- , MarkovNetwork (Multivariate datapoint (ys ': xs) prob) - ) => MarkovNetwork (Multivariate datapoint ( (Container dist label ': ys) ': xs) prob) - where- graphL _ l = (head l,[]):(graphL (undefined::Multivariate datapoint (ys ': xs) prob) (tail l))--instance - ( MultivariateLabels datapoint- , SingI (Length labelL)- , MarkovNetwork (Multivariate datapoint ( ys ': xs) prob) - ) => MarkovNetwork (Multivariate datapoint ( (MultiContainer dist (labelL:: [*]) ': ys) ': xs) prob) - where- graphL _ l = go (take n l) ++ (graphL (undefined :: Multivariate datapoint ( ys ': xs ) prob) $ drop n l)- where- go [] = []- go (x:xs) = (x,xs):(go xs)- - n = fromIntegral $ fromSing $ (sing :: Sing (Length labelL))------------------------------------------------------------------------------------ Graphviz helpers-------------------------------------------- These functions are taken from the graphviz tutorial at:--- http://ivanmiljenovic.wordpress.com/2011/10/16/graphviz-in-vacuum/--graphToDot :: (Ord a) => [(a, [a])] -> DotGraph a-graphToDot = graphToDotParams vacuumParams- -graphToDotParams :: (Ord a, Ord cl) => GraphvizParams a () () cl l -> [(a, [a])] -> DotGraph a-graphToDotParams params nes = graphElemsToDot params ns es- where- ns = map (second $ const ()) nes- es = concatMap mkEs nes- mkEs (f,ts) = map (\t -> (f,t,())) ts- -------------------------------------------------- -vacuumParams :: GraphvizParams a () () () ()-vacuumParams = defaultParams { globalAttributes = gStyle }- -gStyle :: [GlobalAttributes]-gStyle = [ GraphAttrs [RankDir FromLeft, {-Splines SplineEdges, -}FontName "courier", Layout Circo]- , NodeAttrs [textLabel "\\N", shape PlainText, fontColor Black, Shape Ellipse, style filled, fillColor AliceBlue, penWidth 2, color Navy]- , EdgeAttrs [color Black, Dir NoDir]- ]- -graphToDotPng :: FilePath -> [(String,[String])] -> IO Bool-graphToDotPng fpre g = handle (\(e::GraphvizException) -> return False)- $ addExtension (runGraphviz (graphToDot g)) Png fpre >> return True