{"text": "module SimilaritySpec where\n\nimport Test.Hspec\nimport Test.QuickCheck\nimport qualified Numeric.LinearAlgebra as NL\n\nimport Data.List\n\nimport Eris.Compute.Similarity\nimport ErisTestUtility\n\n\nspec :: Spec\nspec = do\n    describe \"L1-norm based. Property tests:\" $ do\n      it \"SAD is L1 norm of the difference of two vectors:\" $ property $\n        \\v1 v2 -> (length v1 /= length v2) || sumAbsoluteDifference v1 v2 == sum ( abs <$> zipWith (-) v1 v2)\n      it \"SAD is symmetric\"$ property $ -- update this property\n        \\l1 l2 -> (length l1 /= length l2)  || (sumAbsoluteDifference l2 l1 == sum (fmap abs (zipWith (-) l1 l2)))\n      it \"manhattanDistance is SAD\" $ property $\n        \\v1 v2 -> (length v1 /= length v2) || manhattanDistance v1 v2 == sumAbsoluteDifference v1 v2\n      it \"taxicab distance is SAD\" $ property $\n        \\v1 v2 -> (length v1 /= length v2) || taxicab v1 v2 == sumAbsoluteDifference v1 v2\n      it \"mean absolute distance is SAD average on number of dimension\" $ property $\n        \\v1 v2 -> (length v1 /= length v2) || meanAbsoluteDifference v1 v2 == average (abs <$> zipWith (-) v1 v2 :: [Double])\n    describe \"mean absolute Difference \" $do\n      it \"naive test: based on Sum Absolute Difference\" $\n        meanAbsoluteDifference tl1 tl2 `shouldBe` sumAbsoluteDifference tl1 tl2 / (fromIntegral . length $ tl1)\n      it \"Pro test: symmetric\" $ property $\n        \\l1 l2 -> (length l1 /= length l2)  || (meanAbsoluteDifference l2 l1 == sum (fmap abs (zipWith (-) l1 l2)) / fromIntegral (length l2)) \n   \n    describe \"based on l2-norm\" $ do\n      it \"euclidean distance is l2-norm\" $ \n        euclideanDistance tl1 tl2 `shouldBe` NL.norm_2 (NL.vector tl1 - NL.vector tl2)\n    \n    describe \"mse distance: \" $ do\n      it \"It is 0 with it self. \" $ \n        (fromInteger. round) (meanSquaredDistance [1.0,1.0,1.0] [1.0,1.0,1.0]) `shouldBe` (0:: Double)\n      it \"It is squared L2-norm normal by number of dimension.\" $ do\n        meanSquaredDistance [4.0, 0.0] [0.0, 5.0] `shouldBe` (4^2+5^2)/2\n        meanSquaredDistance [1.0,1.0] [1.0,-1.0] `shouldBe` (0+4)/2\n   \n    describe \"Cosine Similarity: \" $ do\n      it \"The vector and itself is one.\" $ \n        (fromInteger. round) (cosineSimilarity [1.0,1.0,1.0] [1.0,1.0,1.0]) `shouldBe` (1.0:: Double)\n      it \"The similarity of two linear independent vetors is not 1 \" $ \n        diff5Dec (cosineSimilarity [1,2] [1,3]) 1.0 > 0 `shouldBe` True\n      it \"The similarity of two linear dependent vetors is 1 \" $ \n        diff5Dec (cosineSimilarity [1,2] [2,4]) 1.0 `shouldBe` 0\n      it \" It is 1 for linear dependencies: \" $ do\n        diff5Dec (cosineSimilarity [-1,2,-3,4,-5] [-4,5,-10,11,-16]) 1.0 `shouldBe` 0\n\n      it \"The vector and any perpendicular vector is 0.\" $ do\n        cosineSimilarity [1.0, 0.0] [0.0, 1.0] `shouldBe` 0\n        cosineSimilarity [1.0,1.0] [1.0,-1.0] `shouldBe` 0\n\n\n\n    describe \"pearson correlation coefficient: \" $ do\n      it \" It is 1 for linear dependencies: \" $ do\n          (pearsonCC' [-1,2,-3,400,-30] [-4,5,-10,1199,-91]) `shouldBe` (1.0:: Double)\n    \n\n    describe \"Pearson CC is a special Case of consine similarity: \" $ do\n        let l1 = [-3..1]\n            l2 = fmap (\\x -> x*3+120) l1\n        it \" The value of PCC and CosineSi: \" $ \n          (pearsonCC l1 l2) `shouldBe` (cosineSimilarity l1 l2)\n        it \"pearson CC base on z score: \" $ \n          (pearsonCC l1 l2) `shouldBe` (pearsonCC' l1 l2)\n    where \n        tl1 = [1,-1,2,-2]\n        tl2 = [-1,1,-2,2]\n        diff5Dec = relativeEq 5\n", "meta": {"hexsha": "704974f8918c10f8fead65d48a674c0782e0c0b1", "size": 3526, "ext": "hs", "lang": "Haskell", "max_stars_repo_path": "test/SimilaritySpec.hs", "max_stars_repo_name": "emmettng/eris", "max_stars_repo_head_hexsha": "15f10774898f2d5d636641156b8c512f2b5a0006", "max_stars_repo_licenses": ["BSD-3-Clause"], "max_stars_count": null, "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/SimilaritySpec.hs", "max_issues_repo_name": "emmettng/eris", "max_issues_repo_head_hexsha": "15f10774898f2d5d636641156b8c512f2b5a0006", "max_issues_repo_licenses": ["BSD-3-Clause"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/SimilaritySpec.hs", "max_forks_repo_name": "emmettng/eris", "max_forks_repo_head_hexsha": "15f10774898f2d5d636641156b8c512f2b5a0006", "max_forks_repo_licenses": ["BSD-3-Clause"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 47.0133333333, "max_line_length": 143, "alphanum_fraction": 0.6142938174, "num_tokens": 1185, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9433475715065794, "lm_q2_score": 0.9111797130267453, "lm_q1q2_score": 0.8595591694898421}}
{"text": "module Fuml.Base.Logistic where\n\nimport Numeric.LinearAlgebra\nimport qualified Data.Vector.Storable as VS\nimport Fuml.Optimisation.BFGS\nimport Data.List (foldl1')\n\n-- maybe follow this: https://idontgetoutmuch.wordpress.com/2013/04/30/logistic-regression-and-automated-differentiation-3/\n\nlogit :: Floating a => a -> a\nlogit x = 1 / (1 + exp (negate x))\n\nlogLikelihood1 ::  Vector Double -> Vector Double -> Bool -> Double\nlogLikelihood1 theta x y = ind y * log (logit z) +\n                           (1 - ind y) * log (1 - logit z)\n  where\n    z = VS.sum $ VS.zipWith (*) theta x\n\n-- negative log likelihood\nlogLikelihood :: Double -> [(Vector Double, Bool)] -> Vector Double -> Double\nlogLikelihood delta theData theta = negate $ (a - delta*b)/l where\n  l = fromIntegral $ length theData\n  a = sum $ map (uncurry $ logLikelihood1 theta) theData\n  b = (/2) $ VS.sum $ VS.map (^2) theta\n\n\n--https://www.cs.ox.ac.uk/people/nando.defreitas/machinelearning/lecture6.pdf\ngradLogLikelihood1 :: Vector Double -> Vector Double -> Bool -> Vector Double\ngradLogLikelihood1 theta x y = VS.map (*(ind y - lz)) x\n--                           (1 - ind y) * log (1 - logit z)\n  where\n    z = VS.sum $ VS.zipWith (*) theta x\n    lz = logit z\n\ngradLogLikelihood :: Double -> [(Vector Double, Bool)] -> Vector Double -> Vector Double\ngradLogLikelihood delta theData theta =\n  let vs = map (uncurry $ gradLogLikelihood1 theta) theData\n      vsum = foldl1' vadd vs\n      l = fromIntegral $ length theData\n  in VS.map (negate . (/l)) vsum  `vadd` VS.map ((/l) . (*delta)) theta\n\n\nlogisticRegression :: [(Vector Double, Bool)] -> Vector Double\nlogisticRegression theData =\n  let start = VS.map (const 0) $ fst $ head theData\n      inisbox = VS.map (const (0.1:: Double)) $ fst $ head theData\n      hessInit = ident $ size start\n  in --fst $ minimizeV NMSimplex 1e-4 200 inisbox (logLikelihood theData) start\n\n     --fst $ minimizeVD ConjugateFR 1e-10 500 0.01 0.01 (logLikelihood theData) (gradLogLikelihood theData) start -- inisbox (logLikelihood theData) start\n\n     case bfgsWith myBOpts (logLikelihood 0 theData) (gradLogLikelihood 0 theData) start hessInit of\n       Left s -> error s\n       Right (p, h) -> p\n      --fst $ minimizeV NMSimplex 1e-3 100 inisbox (logLikelihood theData) start\n\nmyBOpts = BFGSOpts 1e-7 2e-5 200\n\nind :: Bool -> Double\nind True = 1\nind False = 0\n\nvadd :: Vector Double -> Vector Double -> Vector Double\nvadd = VS.zipWith (+)\n", "meta": {"hexsha": "7c1b8d661a4d3aa773f136ab4a459c95486ac69a", "size": 2439, "ext": "hs", "lang": "Haskell", "max_stars_repo_path": "fuml/lib/Fuml/Base/Logistic.hs", "max_stars_repo_name": "ekalosak/open", "max_stars_repo_head_hexsha": "9faadba7614fc9d9448f1d3000c58cc3e93a8a36", "max_stars_repo_licenses": ["MIT"], "max_stars_count": 81, "max_stars_repo_stars_event_min_datetime": "2017-05-22T22:42:01.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-15T10:41:20.000Z", "max_issues_repo_path": "fuml/lib/Fuml/Base/Logistic.hs", "max_issues_repo_name": "ekalosak/open", "max_issues_repo_head_hexsha": "9faadba7614fc9d9448f1d3000c58cc3e93a8a36", "max_issues_repo_licenses": ["MIT"], "max_issues_count": 55, "max_issues_repo_issues_event_min_datetime": "2017-05-31T09:06:45.000Z", "max_issues_repo_issues_event_max_datetime": "2020-11-18T12:00:27.000Z", "max_forks_repo_path": "fuml/lib/Fuml/Base/Logistic.hs", "max_forks_repo_name": "ekalosak/open", "max_forks_repo_head_hexsha": "9faadba7614fc9d9448f1d3000c58cc3e93a8a36", "max_forks_repo_licenses": ["MIT"], "max_forks_count": 23, "max_forks_repo_forks_event_min_datetime": "2017-05-22T15:39:31.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-13T19:26:20.000Z", "avg_line_length": 37.5230769231, "max_line_length": 154, "alphanum_fraction": 0.6715867159, "num_tokens": 749, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9546474233166328, "lm_q2_score": 0.8991213684847577, "lm_q1q2_score": 0.8583438976728986}}
{"text": "module Numeric.Morpheus.StatisticsTest\n(\n  tests\n)\n\nwhere\n\nimport Test.Framework (testGroup)\nimport Test.Framework.Providers.HUnit\nimport Test.HUnit\nimport Test.HUnit.Approx\nimport Test.HUnit.Plus\n\nimport Numeric.LinearAlgebra\nimport Numeric.Morpheus.Statistics\n\n\nx = fromList [35,32,213,54,81,92,63,70]\nmeanValue = 80\nstddevValue = 57.60456\n\na = matrix 4 [35,32,213,54,\n              81,92,63,70,\n              77,14,91,20]\naT = tr a\n\ncolumnMeansRM = fromList [64.33333, 46, 122.33333, 48]\nrowMeansRM = fromList [83.5, 76.5, 50.5]\ncolumnMeansCM = rowMeansRM\nrowMeansCM = columnMeansRM\n\ncolumnStddevsRM = fromList [25.48202, 40.84116, 79.75797, 25.53429]\nrowStddevsRM = fromList [86.88114, 12.71482, 39.17908]\ncolumnStddevsCM = rowStddevsRM\nrowStddevsCM = columnStddevsRM\n\n\ntests = [ testGroup \"Statistics\" [\n            testCase \"mean\" $ assertApproxEqual \"\" 1e-5 meanValue (mean x)\n          , testCase \"stddev_m\" $ assertApproxEqual \"\" 1e-5 stddevValue (stddev_m meanValue x)\n          , testCase \"stddev\" $ assertApproxEqual \"\" 1e-5 stddevValue (stddev x)\n          ]\n        , testGroup \"Matrix Statistics\" [\n            testCase \"columnMean - row major\" $ assertVector \"\" 1e-5 columnMeansRM (columnMean a)\n          , testCase \"columnMean - column major\" $ assertVector \"\" 1e-5 columnMeansCM (columnMean aT)\n          , testCase \"rowMean - row major\" $ assertVector \"\" 1e-5 rowMeansRM (rowMean a)\n          , testCase \"rowMean - column major\" $ assertVector \"\" 1e-5 rowMeansCM (rowMean aT)\n          , testCase \"columnStddev_m - row major\" $ assertVector \"\" 1e-5 columnStddevsRM (columnStddev_m columnMeansRM a)\n          , testCase \"columnStddev_m - column major\" $ assertVector \"\" 1e-5 columnStddevsCM (columnStddev_m columnMeansCM aT)\n          , testCase \"rowStddev_m - row major\" $ assertVector \"\" 1e-5 rowStddevsRM (rowStddev_m rowMeansRM a)\n          , testCase \"rowStddev_m - column major\" $ assertVector \"\" 1e-5 rowStddevsCM (rowStddev_m rowMeansCM aT)\n          ]\n        ]\n", "meta": {"hexsha": "2935052abcea1dce7a5b6f4a02a266489879f562", "size": 1990, "ext": "hs", "lang": "Haskell", "max_stars_repo_path": "hmatrix-morpheus/test/Numeric/Morpheus/StatisticsTest.hs", "max_stars_repo_name": "Alexander-Ignatyev/morpheus", "max_stars_repo_head_hexsha": "ee01b67441cb2e27abff4a025bd0be4a44762108", "max_stars_repo_licenses": ["BSD-3-Clause"], "max_stars_count": 2, "max_stars_repo_stars_event_min_datetime": "2018-08-04T19:44:16.000Z", "max_stars_repo_stars_event_max_datetime": "2019-01-14T11:30:47.000Z", "max_issues_repo_path": "hmatrix-morpheus/test/Numeric/Morpheus/StatisticsTest.hs", "max_issues_repo_name": "aligusnet/morpheus", "max_issues_repo_head_hexsha": "ee01b67441cb2e27abff4a025bd0be4a44762108", "max_issues_repo_licenses": ["BSD-3-Clause"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "hmatrix-morpheus/test/Numeric/Morpheus/StatisticsTest.hs", "max_forks_repo_name": "aligusnet/morpheus", "max_forks_repo_head_hexsha": "ee01b67441cb2e27abff4a025bd0be4a44762108", "max_forks_repo_licenses": ["BSD-3-Clause"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 36.8518518519, "max_line_length": 125, "alphanum_fraction": 0.6874371859, "num_tokens": 638, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9621075690244282, "lm_q2_score": 0.8887587993853654, "lm_q1q2_score": 0.8550815679257233}}
