text:
module SimilaritySpec where

import Test.Hspec
import Test.QuickCheck
import qualified Numeric.LinearAlgebra as NL

import Data.List

import Eris.Compute.Similarity
import ErisTestUtility

spec :: Spec
spec = do
    describe "L1-norm based. Property tests:" $ do
      it "SAD is L1 norm of the difference of two vectors:" $ property $
        \v1 v2 -> (length v1 /= length v2) || sumAbsoluteDifference v1 v2 == sum ( abs <$> zipWith (-) v1 v2)
      it "SAD is symmetric"$ property $ -- update this property
        \l1 l2 -> (length l1 /= length l2)  || (sumAbsoluteDifference l2 l1 == sum (fmap abs (zipWith (-) l1 l2)))
      it "manhattanDistance is SAD" $ property $
        \v1 v2 -> (length v1 /= length v2) || manhattanDistance v1 v2 == sumAbsoluteDifference v1 v2
      it "taxicab distance is SAD" $ property $
        \v1 v2 -> (length v1 /= length v2) || taxicab v1 v2 == sumAbsoluteDifference v1 v2
      it "mean absolute distance is SAD average on number of dimension" $ property $
        \v1 v2 -> (length v1 /= length v2) || meanAbsoluteDifference v1 v2 == average (abs <$> zipWith (-) v1 v2 :: [Double])
    describe "mean absolute Difference " $do
      it "naive test: based on Sum Absolute Difference" $
        meanAbsoluteDifference tl1 tl2 `shouldBe` sumAbsoluteDifference tl1 tl2 / (fromIntegral . length $ tl1)
      it "Pro test: symmetric" $ property $
        \l1 l2 -> (length l1 /= length l2)  || (meanAbsoluteDifference l2 l1 == sum (fmap abs (zipWith (-) l1 l2)) / fromIntegral (length l2))

    describe "based on l2-norm" $ do
      it "euclidean distance is l2-norm" $
        euclideanDistance tl1 tl2 `shouldBe` NL.norm_2 (NL.vector tl1 - NL.vector tl2)

    describe "mse distance: " $ do
      it "It is 0 with it self. " $
        (fromInteger. round) (meanSquaredDistance [1.0,1.0,1.0] [1.0,1.0,1.0]) `shouldBe` (0:: Double)
      it "It is squared L2-norm normal by number of dimension." $ do
        meanSquaredDistance [4.0, 0.0] [0.0, 5.0] `shouldBe` (4^2+5^2)/2
        meanSquaredDistance [1.0,1.0] [1.0,-1.0] `shouldBe` (0+4)/2

    describe "Cosine Similarity: " $ do
      it "The vector and itself is one." $
        (fromInteger. round) (cosineSimilarity [1.0,1.0,1.0] [1.0,1.0,1.0]) `shouldBe` (1.0:: Double)
      it "The similarity of two linear independent vetors is not 1 " $
        diff5Dec (cosineSimilarity [1,2] [1,3]) 1.0 > 0 `shouldBe` True
      it "The similarity of two linear dependent vetors is 1 " $
        diff5Dec (cosineSimilarity [1,2] [2,4]) 1.0 `shouldBe` 0
      it " It is 1 for linear dependencies: " $ do
        diff5Dec (cosineSimilarity [-1,2,-3,4,-5] [-4,5,-10,11,-16]) 1.0 `shouldBe` 0

      it "The vector and any perpendicular vector is 0." $ do
        cosineSimilarity [1.0, 0.0] [0.0, 1.0] `shouldBe` 0
        cosineSimilarity [1.0,1.0] [1.0,-1.0] `shouldBe` 0

    describe "pearson correlation coefficient: " $ do
      it " It is 1 for linear dependencies: " $ do
          (pearsonCC' [-1,2,-3,400,-30] [-4,5,-10,1199,-91]) `shouldBe` (1.0:: Double)

    describe "Pearson CC is a special Case of consine similarity: " $ do
        let l1 = [-3..1]
            l2 = fmap (\x -> x*3+120) l1
        it " The value of PCC and CosineSi: " $
          (pearsonCC l1 l2) `shouldBe` (cosineSimilarity l1 l2)
        it "pearson CC base on z score: " $
          (pearsonCC l1 l2) `shouldBe` (pearsonCC' l1 l2)
    where
        tl1 = [1,-1,2,-2]
        tl2 = [-1,1,-2,2]
        diff5Dec = relativeEq 5

ext: hs

max_stars_repo_name: emmettng/eris

max_issues_repo_name: emmettng/eris

max_forks_repo_name: emmettng/eris

lm_label:
1. YES
2. YES


text:
module Fuml.Base.Logistic where

import Numeric.LinearAlgebra
import qualified Data.Vector.Storable as VS
import Fuml.Optimisation.BFGS
import Data.List (foldl1')

-- maybe follow this: https://idontgetoutmuch.wordpress.com/2013/04/30/logistic-regression-and-automated-differentiation-3/

logit :: Floating a => a -> a
logit x = 1 / (1 + exp (negate x))

logLikelihood1 ::  Vector Double -> Vector Double -> Bool -> Double
logLikelihood1 theta x y = ind y * log (logit z) +
                           (1 - ind y) * log (1 - logit z)
  where
    z = VS.sum $ VS.zipWith (*) theta x

-- negative log likelihood
logLikelihood :: Double -> [(Vector Double, Bool)] -> Vector Double -> Double
logLikelihood delta theData theta = negate $ (a - delta*b)/l where
  l = fromIntegral $ length theData
  a = sum $ map (uncurry $ logLikelihood1 theta) theData
  b = (/2) $ VS.sum $ VS.map (^2) theta

--https://www.cs.ox.ac.uk/people/nando.defreitas/machinelearning/lecture6.pdf
gradLogLikelihood1 :: Vector Double -> Vector Double -> Bool -> Vector Double
gradLogLikelihood1 theta x y = VS.map (*(ind y - lz)) x
--                           (1 - ind y) * log (1 - logit z)
  where
    z = VS.sum $ VS.zipWith (*) theta x
    lz = logit z

gradLogLikelihood :: Double -> [(Vector Double, Bool)] -> Vector Double -> Vector Double
gradLogLikelihood delta theData theta =
  let vs = map (uncurry $ gradLogLikelihood1 theta) theData
      vsum = foldl1' vadd vs
      l = fromIntegral $ length theData
  in VS.map (negate . (/l)) vsum  `vadd` VS.map ((/l) . (*delta)) theta

logisticRegression :: [(Vector Double, Bool)] -> Vector Double
logisticRegression theData =
  let start = VS.map (const 0) $ fst $ head theData
      inisbox = VS.map (const (0.1:: Double)) $ fst $ head theData
      hessInit = ident $ size start
  in --fst $ minimizeV NMSimplex 1e-4 200 inisbox (logLikelihood theData) start

     --fst $ minimizeVD ConjugateFR 1e-10 500 0.01 0.01 (logLikelihood theData) (gradLogLikelihood theData) start -- inisbox (logLikelihood theData) start

     case bfgsWith myBOpts (logLikelihood 0 theData) (gradLogLikelihood 0 theData) start hessInit of
       Left s -> error s
       Right (p, h) -> p
      --fst $ minimizeV NMSimplex 1e-3 100 inisbox (logLikelihood theData) start

myBOpts = BFGSOpts 1e-7 2e-5 200

ind :: Bool -> Double
ind True = 1
ind False = 0

vadd :: Vector Double -> Vector Double -> Vector Double
vadd = VS.zipWith (+)

max_stars_repo_name: ekalosak/open

max_issues_repo_name: ekalosak/open

max_forks_repo_name: ekalosak/open


text:
module Numeric.Morpheus.StatisticsTest
(
  tests
)

where

import Test.Framework (testGroup)
import Test.Framework.Providers.HUnit
import Test.HUnit
import Test.HUnit.Approx
import Test.HUnit.Plus

import Numeric.LinearAlgebra
import Numeric.Morpheus.Statistics

x = fromList [35,32,213,54,81,92,63,70]
meanValue = 80
stddevValue = 57.60456

a = matrix 4 [35,32,213,54,
              81,92,63,70,
              77,14,91,20]
aT = tr a

columnMeansRM = fromList [64.33333, 46, 122.33333, 48]
rowMeansRM = fromList [83.5, 76.5, 50.5]
columnMeansCM = rowMeansRM
rowMeansCM = columnMeansRM

columnStddevsRM = fromList [25.48202, 40.84116, 79.75797, 25.53429]
rowStddevsRM = fromList [86.88114, 12.71482, 39.17908]
columnStddevsCM = rowStddevsRM
rowStddevsCM = columnStddevsRM

tests = [ testGroup "Statistics" [
            testCase "mean" $ assertApproxEqual "" 1e-5 meanValue (mean x)
          , testCase "stddev_m" $ assertApproxEqual "" 1e-5 stddevValue (stddev_m meanValue x)
          , testCase "stddev" $ assertApproxEqual "" 1e-5 stddevValue (stddev x)
          ]
        , testGroup "Matrix Statistics" [
            testCase "columnMean - row major" $ assertVector "" 1e-5 columnMeansRM (columnMean a)
          , testCase "columnMean - column major" $ assertVector "" 1e-5 columnMeansCM (columnMean aT)
          , testCase "rowMean - row major" $ assertVector "" 1e-5 rowMeansRM (rowMean a)
          , testCase "rowMean - column major" $ assertVector "" 1e-5 rowMeansCM (rowMean aT)
          , testCase "columnStddev_m - row major" $ assertVector "" 1e-5 columnStddevsRM (columnStddev_m columnMeansRM a)
          , testCase "columnStddev_m - column major" $ assertVector "" 1e-5 columnStddevsCM (columnStddev_m columnMeansCM aT)
          , testCase "rowStddev_m - row major" $ assertVector "" 1e-5 rowStddevsRM (rowStddev_m rowMeansRM a)
          , testCase "rowStddev_m - column major" $ assertVector "" 1e-5 rowStddevsCM (rowStddev_m rowMeansCM aT)
          ]
        ]

max_stars_repo_name: Alexander-Ignatyev/morpheus

max_issues_repo_name: aligusnet/morpheus

max_forks_repo_name: aligusnet/morpheus