nonlinear-optimization-ad 0.2.0 → 0.2.1
raw patch · 4 files changed
+44/−15 lines, 4 filesdep +csvdep +nonlinear-optimization-addep ~addep ~basedep ~vectornew-component:exe:LinearRegression
Dependencies added: csv, nonlinear-optimization-ad
Dependency ranges changed: ad, base, vector
Files
- .travis.yml +3/−3
- nonlinear-optimization-ad.cabal +20/−1
- samples/LinearRegression.hs +19/−8
- src/Numeric/Optimization/Algorithms/HagerZhang05/AD.hs +2/−3
.travis.yml view
@@ -17,7 +17,7 @@ # - CABALVER=1.18 GHCVER=7.8.1 # see note about Alex/Happy for GHC >= 7.8 # - CABALVER=1.18 GHCVER=7.8.2 - CABALVER=1.18 GHCVER=7.8.3- - CABALVER=1.22 GHCVER=7.10.1+ - CABALVER=1.22 GHCVER=7.10.2 # - CABALVER=head GHCVER=head # see section about GHC HEAD snapshots # Note: the distinction between `before_install` and `install` is not important.@@ -31,12 +31,12 @@ - cabal --version - echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]" - travis_retry cabal update- - cabal install --only-dependencies --enable-tests --enable-benchmarks+ - cabal install --only-dependencies --enable-tests --enable-benchmarks -fBuildSamplePrograms # Here starts the actual work to be performed for the package under test; any command which exits with a non-zero exit code causes the build to fail. script: - if [ -f configure.ac ]; then autoreconf -i; fi- - cabal configure --enable-tests --enable-benchmarks -v2 # -v2 provides useful information for debugging+ - cabal configure --enable-tests --enable-benchmarks -fBuildSamplePrograms -v2 # -v2 provides useful information for debugging - cabal build # this builds all libraries and executables (including tests/benchmarks) - cabal test - cabal check
nonlinear-optimization-ad.cabal view
@@ -2,7 +2,7 @@ -- further documentation, see http://haskell.org/cabal/users-guide/ name: nonlinear-optimization-ad-version: 0.2.0+version: 0.2.1 synopsis: Wrapper of nonlinear-optimization package for using with AD package description: Wrapper of nonlinear-optimization package for using with AD package homepage: https://github.com/msakai/nonlinear-optimization-ad@@ -25,6 +25,11 @@ type: git location: git://github.com/msakai/nonlinear-optimization-ad.git +flag BuildSamplePrograms+ Description: build sample programs+ Default: False+ Manual: True+ library exposed-modules: Numeric.Optimization.Algorithms.HagerZhang05.AD build-depends:@@ -42,3 +47,17 @@ TypeFamilies CPP +-- Sample Programs++Executable LinearRegression+ If !flag(BuildSamplePrograms)+ Buildable: False+ Main-is: LinearRegression.hs+ HS-Source-Dirs: samples+ Build-Depends:+ base,+ csv,+ nonlinear-optimization-ad+ Default-Language: Haskell2010+ Other-extensions:+ TypeFamilies
samples/LinearRegression.hs view
@@ -1,26 +1,37 @@+{-# LANGUAGE GADTs #-}+{-# OPTIONS_GHC -Wall #-} module Main where -import Control.Monad-import qualified Data.Vector as V-import Numeric.AD import qualified Numeric.Optimization.Algorithms.HagerZhang05.AD as CG import Text.Printf import qualified Text.CSV as CSV main :: IO () main = do- Right csv <- CSV.parseCSVFromFile "galton.csv"+ Right csv <- CSV.parseCSVFromFile "samples/galton.csv" let samples :: [(Double, Double)] samples = [(read parent, read child) | [child,parent] <- tail csv] -- hypothesis h [theta0,theta1] x = theta0 + theta1 * x -- cost function- cost theta = mse [(auto x, auto y) | (x,y) <- samples] (h theta)+ -- cost :: (Fractional m, CG.Mode m, CG.Scalar m ~ Double) => [m] -> m]+ cost theta = mse [(CG.auto x, CG.auto y) | (x,y) <- samples] (h theta) params = CG.defaultParameters{ CG.printFinal = True, CG.printParams = True, CG.verbose = CG.Verbose } grad_tol = 0.0000001- (theta, result, stat) <- CG.optimize params grad_tol [0,0] cost- print theta+ (theta@[theta0,theta1], _result, _stat) <- CG.optimize params grad_tol [0,0] cost+ printf "y = %f x + %f\n" theta1 theta0+ printf "MSE = %f\n" (mse samples (h theta))+ printf "R^2 = %f\n" (r2 samples (h theta)) -- mean squared error mse :: Fractional y => [(x,y)] -> (x -> y) -> y-mse samples h = sum [(h x - y)^(2::Int) | (x,y) <- samples] / fromIntegral (length samples)+mse samples h = mean [(h x - y)^(2::Int) | (x,y) <- samples]++r2 :: Fractional y => [(x,y)] -> (x -> y) -> y+r2 samples h = 1 - mse samples h / sum [(y - ym)^(2::Int) | y <- ys]+ where+ ys = map snd samples+ ym = mean ys++mean :: Fractional x => [x] -> x+mean xs = sum [x | x <- xs] / fromIntegral (length xs)
src/Numeric/Optimization/Algorithms/HagerZhang05/AD.hs view
@@ -3,9 +3,6 @@ module Numeric.Optimization.Algorithms.HagerZhang05.AD ( -- * Main function optimize- -- ** Kinds of function types- , Simple- , Mutable -- * Result and statistics , Result(..) , Statistics(..)@@ -18,6 +15,8 @@ , EstimateError(..) -- * Technical parameters , TechParameters(..)+ -- * Re-export+ , Mode (..) ) where import Prelude hiding (mapM)