File size: 4,028 Bytes
5697766
1
2
{"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}}