HLearn-distributions 0.1.0.1 → 0.2.1
raw patch · 4 files changed
+103/−51 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- HLearn.Gnuplot.Distributions: genPlotPoints :: PlottableDistribution dist => dist -> [Double]
- HLearn.Gnuplot.Distributions: plotPoints :: PlotParams -> [Double]
- HLearn.Models.Distributions.Categorical: instance (Ord label, Ord prob, Floating prob, Random prob) => Distribution (Categorical label prob) label prob
- HLearn.Models.Distributions.Categorical: instance DefaultModel CategoricalParams (Categorical Int Double)
+ HLearn.Gnuplot.Distributions: gnuplot :: PlottableDistribution dist => PlotParams -> dist -> String
+ HLearn.Gnuplot.Distributions: instance (Show label, Show prob, Num prob, Ord prob) => PlottableDistribution (Categorical label prob)
+ HLearn.Gnuplot.Distributions: plotFile :: String -> PlotParams
+ HLearn.Gnuplot.Distributions: plotdata :: PlottableDistribution dist => dist -> String
+ HLearn.Models.Distributions.Categorical: instance (Ord label, Ord prob, Fractional prob, Random prob) => Distribution (Categorical label prob) label prob
+ HLearn.Models.Distributions.Categorical: instance DefaultModel CategoricalParams (Categorical label probtype)
- HLearn.Gnuplot.Distributions: PlotParams :: FilePath -> FilePath -> FilePath -> [Double] -> PlotParams
+ HLearn.Gnuplot.Distributions: PlotParams :: FilePath -> FilePath -> FilePath -> PlotParams
- HLearn.Gnuplot.Distributions: class PlottableDistribution dist
+ HLearn.Gnuplot.Distributions: class PlottableDistribution dist where plotDistribution params dist = do { putStrLn "Creating data file..."; datah <- openFile (dataFile params) WriteMode; hPutStrLn datah $ plotdata dist; hClose datah; putStrLn "Plotting data"; gnuh <- openFile (gnuFile params) WriteMode; hPutStrLn gnuh $ gnuplot params dist; hClose gnuh; system $ "gnuplot " ++ (gnuFile params); putStrLn "done."; return () }
- HLearn.Gnuplot.Distributions: genPlotParams :: PlottableDistribution dist => String -> dist -> PlotParams
+ HLearn.Gnuplot.Distributions: genPlotParams :: String -> a -> PlotParams
- HLearn.Gnuplot.Distributions: plotDistribution :: Distribution dist Double Double => PlotParams -> dist -> IO ()
+ HLearn.Gnuplot.Distributions: plotDistribution :: PlottableDistribution dist => PlotParams -> dist -> IO ()
- HLearn.Models.Distributions.Common: class Distribution dist dp prob | dist -> dp
+ HLearn.Models.Distributions.Common: class Distribution dist dp prob | dist -> dp, dist -> prob
Files
- HLearn-distributions.cabal +1/−1
- src/HLearn/Gnuplot/Distributions.hs +97/−45
- src/HLearn/Models/Distributions/Categorical.hs +4/−4
- src/HLearn/Models/Distributions/Common.hs +1/−1
HLearn-distributions.cabal view
@@ -1,5 +1,5 @@ Name: HLearn-distributions-Version: 0.1.0.1+Version: 0.2.1 Synopsis: Distributions for use with the HLearn library Description: This module is used to estimate statistical distributions from data. The focus is a clean interface inspired by algebra. Category: Data Mining, Machine Learning, Statistics
src/HLearn/Gnuplot/Distributions.hs view
@@ -10,6 +10,7 @@ import HLearn.Algebra import HLearn.Models.Distributions +import qualified Data.Map as Map import qualified Data.Vector.Unboxed as VU import Control.Monad@@ -20,62 +21,113 @@ { dataFile :: FilePath , gnuFile :: FilePath , picFile :: FilePath- , plotPoints :: [Double] } -class PlottableDistribution dist where- genPlotPoints :: dist -> [Double]--instance PlottableDistribution (KDE Double) where- genPlotPoints (SGJust kde) = VU.toList $ samplePoints $ params kde--genPlotParams :: (PlottableDistribution dist) => String -> dist -> PlotParams-genPlotParams str dist = PlotParams+plotFile :: String -> PlotParams+plotFile str = PlotParams { dataFile = str++".dat" , gnuFile = str++".gnu" , picFile = str++".ps"- , plotPoints = genPlotPoints dist } +-- | provided due to backwards compatibility with the nuclear weapons blog post.+genPlotParams :: String -> a -> PlotParams+genPlotParams str a = plotFile str+ defPlotParams = PlotParams { dataFile = "hlearn-distributions.dat" , gnuFile = "hlearn-distributions.gnu" , picFile = "hlearn-distributions.ps"- , plotPoints = [-5..5] }- -plotDistribution :: (Distribution dist Double Double) => PlotParams -> dist -> IO ()-plotDistribution params dist = do- - -- Create data file- putStrLn "Creating data file..."- datah <- openFile (dataFile params) WriteMode- forM_ (plotPoints params) $ \x -> do--- forM_ (map (/10) [-50..50]) $ \x -> do- hPutStrLn datah $ show (x::Double) ++ " " ++ show (pdf dist x::Double)- hClose datah- - -- Create gnuplot file- putStrLn "Plotting data"- gnuh <- openFile (gnuFile params) WriteMode- hPutStrLn gnuh $ "set terminal postscript \"Times-Roman\" 25"- hPutStrLn gnuh $ "set output \"" ++ (picFile params) ++ "\""- hPutStrLn gnuh $ "unset xtics; unset ytics; unset key"- hPutStrLn gnuh $ "set border 0; set xzeroaxis lt 1; set yzeroaxis lt 1"- hPutStrLn gnuh $ "plot '"++(dataFile params)++"' using 1:2 lt 1 lw 4 lc rgb '#ccccff' with filledcurves, \\"- hPutStrLn gnuh $ " '"++(dataFile params)++"' using 1:2 lt 1 lw 4 lc rgb '#0000ff' with lines"- hClose gnuh++class PlottableDistribution dist where+ plotDistribution :: PlotParams -> dist -> IO ()+ plotDistribution params dist = do+ -- Create data file+ putStrLn "Creating data file..."+ datah <- openFile (dataFile params) WriteMode+ hPutStrLn datah $ plotdata dist+ hClose datah+ + -- Create gnuplot file+ putStrLn "Plotting data"+ gnuh <- openFile (gnuFile params) WriteMode+ hPutStrLn gnuh $ gnuplot params dist+ hClose gnuh+ + -- Run gnuplot, creating picture+ system $ "gnuplot "++(gnuFile params)+ putStrLn "done."+ return ()++ plotdata :: dist -> String+ gnuplot :: PlotParams -> dist -> String - -- Run gnuplot, creating picture- system $ "gnuplot "++(gnuFile params)- putStrLn "done."- return ()+instance PlottableDistribution (KDE Double) where+ plotdata dist@(SGJust kde) = mconcat [show (x::Double) ++ " " ++ show (pdf dist x::Double) | x <- plotPoints]+ where+ plotPoints = VU.toList $ samplePoints $ params kde++ gnuplot params dist+ = "set terminal postscript \"Times-Roman\" 25"+ ++ "set output \"" ++ (picFile params) ++ "\""+ ++ "unset xtics; unset ytics; unset key"+ ++ "set border 0; set xzeroaxis lt 1; set yzeroaxis lt 1"+ ++ "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"+ +instance (Show label, Show prob, Num prob, Ord prob) => PlottableDistribution (Categorical label prob) where+ plotdata dist@(Categorical pdf) = concat $ do+ (label,prob) <- Map.toList pdf+ return $ show label++" "++show prob++"\n"+ + gnuplot params (Categorical pdf)+ = "set terminal postscript \"Times-Roman\" 25; set output \""++ (picFile params) ++ "\"\n"+ ++"set tics scale 0; set xtics nomirror; unset ytics; unset key\n"+ ++"set border 2; set xzeroaxis lt 1\n"+ ++"set ylabel \"Probability\"\n"+ ++yrange+ ++"set style fill border -1\n"+ ++"set style data histogram; set style histogram cluster gap 1\n"+ ++"plot '"++(dataFile params)++"' using 2:xticlabels(1)\n"+ where+ positiveSamples = or $ map (\(k,v) -> v>0) $ Map.toList pdf+ negativeSamples = or $ map (\(k,v) -> v<0) $ Map.toList pdf+ yrange = if positiveSamples && negativeSamples+ then ""+ else if positiveSamples+ then "set yrange [0:]\n"+ else "set yrange [:0]\n"+{- = "set terminal postscript \"Times-Roman\" 25 \n"+ ++ "set output \"" ++ (picFile params) ++ "\" \n"+ ++ "unset xtics; unset ytics; unset key\n"+ ++ "set border 0; set xzeroaxis lt 1; set yzeroaxis lt 1\n"+ ++ "set style data histogram \n"+ ++ "plot '"++(dataFile params)++"' using 1:2 \n"-} --- testL = [1,1.1,1.2,0,5,-3,2,1,2.5,2.9::Double]--- kde = train' (KDEParams 1 (VU.fromList $ map (/10) [-50..50::Double]) (KernelBox Gaussian)) testL :: KDE Double--- --- pdfL = sequence_ $ do--- x <- map (/10) [-50..50]--- let y = pdf kde x :: Double--- seq y (return ())--- return $ putStrLn $ (show (x::Double)) ++ " " ++ show y+-- plotDistribution :: (Distribution dist Double Double) => PlotParams -> dist -> IO ()+-- plotDistribution params dist = do+-- +-- -- Create data file+-- putStrLn "Creating data file..."+-- datah <- openFile (dataFile params) WriteMode+-- forM_ (plotPoints params) $ \x -> do+-- -- forM_ (map (/10) [-50..50]) $ \x -> do+-- hPutStrLn datah $ show (x::Double) ++ " " ++ show (pdf dist x::Double)+-- hClose datah+-- +-- -- Create gnuplot file+-- putStrLn "Plotting data"+-- gnuh <- openFile (gnuFile params) WriteMode+-- hPutStrLn gnuh $ "set terminal postscript \"Times-Roman\" 25"+-- hPutStrLn gnuh $ "set output \"" ++ (picFile params) ++ "\""+-- hPutStrLn gnuh $ "unset xtics; unset ytics; unset key"+-- hPutStrLn gnuh $ "set border 0; set xzeroaxis lt 1; set yzeroaxis lt 1"+-- hPutStrLn gnuh $ "plot '"++(dataFile params)++"' using 1:2 lt 1 lw 4 lc rgb '#ccccff' with filledcurves, \\"+-- hPutStrLn gnuh $ " '"++(dataFile params)++"' using 1:2 lt 1 lw 4 lc rgb '#0000ff' with lines"+-- hClose gnuh+-- +-- -- Run gnuplot, creating picture+-- system $ "gnuplot "++(gnuFile params)+-- putStrLn "done."+-- return ()
src/HLearn/Models/Distributions/Categorical.hs view
@@ -44,8 +44,8 @@ instance Model CategoricalParams (Categorical label probtype) where getparams model = CategoricalParams --- instance DefaultModel CategoricalParams (Categorical label probtype) where-instance DefaultModel CategoricalParams (Categorical Int Double) where+instance DefaultModel CategoricalParams (Categorical label probtype) where+-- instance DefaultModel CategoricalParams (Categorical Int Double) where defparams = CategoricalParams -------------------------------------------------------------------------------@@ -69,10 +69,10 @@ ------------------------------------------------------------------------------- -- Distribution -instance (Ord label, Ord prob, Floating prob, Random prob) => Distribution (Categorical label prob) label prob where+instance (Ord label, Ord prob, Fractional prob, Random prob) => Distribution (Categorical label prob) label prob where {-# INLINE pdf #-}- pdf dist label = 0.0001+(val/tot)+ pdf dist label = {-0.0001+-}(val/tot) where val = case Map.lookup label (pdfmap dist) of Nothing -> 0
src/HLearn/Models/Distributions/Common.hs view
@@ -12,7 +12,7 @@ -- Distribution -- | We use the same class for both discrete and continuous distributions. Unfortunately, we cannot use the type classes from the 'statistics' package because we require more generalilty.-class Distribution dist dp prob | dist -> dp where+class Distribution dist dp prob | dist -> dp, dist -> prob where pdf :: dist -> dp -> prob -- cdf :: dist -> dp -> prob -- cdfInverse :: dist -> prob -> dp