Automatically set precision when bootstrap resampling is used

This commit is contained in:
Filip Gralinski 2020-01-28 20:32:09 +01:00
parent 102b167ffc
commit 550358f0cd
3 changed files with 29 additions and 16 deletions

View File

@ -5,12 +5,11 @@ import GEval.EvaluationScheme
import GEval.Common import GEval.Common
import GEval.OptionsParser import GEval.OptionsParser
import GEval.ParseParams import GEval.ParseParams
import GEval.Formatting
import System.Environment import System.Environment
import Options.Applicative import Options.Applicative
import Text.Printf
import System.IO import System.IO
import System.Exit import System.Exit
@ -112,17 +111,3 @@ formatSourceSpec spec = show spec
formatTheMetricAndResult :: Maybe Int -> (EvaluationScheme, MetricResult) -> String formatTheMetricAndResult :: Maybe Int -> (EvaluationScheme, MetricResult) -> String
formatTheMetricAndResult mPrecision (scheme, val) = (evaluationSchemeName scheme) ++ "\t" ++ (formatTheResult mPrecision val) formatTheMetricAndResult mPrecision (scheme, val) = (evaluationSchemeName scheme) ++ "\t" ++ (formatTheResult mPrecision val)
formatTheResult :: Maybe Int -> MetricResult -> String
formatTheResult mPrecision (SimpleRun val) = formatSimpleResult mPrecision val
formatTheResult mPrecision (BootstrapResampling vals) = (formatSimpleResult mPrecision pointEstimate)
++ "±"
++ (formatSimpleResult mPrecision errorBound)
where pointEstimate = (upperBound + lowerBound) / 2.0
errorBound = (upperBound - lowerBound) / 2.0
(lowerBound, upperBound) = getConfidenceBounds defaultConfidenceLevel vals
formatSimpleResult :: Maybe Int -> MetricValue -> String
formatSimpleResult Nothing = show
formatSimpleResult (Just prec) = printf "%0.*f" prec

View File

@ -52,6 +52,7 @@ library
, Data.CartesianStrings , Data.CartesianStrings
, Data.SplitIntoCrossTabs , Data.SplitIntoCrossTabs
, Data.Conduit.Bootstrap , Data.Conduit.Bootstrap
, GEval.Formatting
, Paths_geval , Paths_geval
build-depends: base >= 4.7 && < 5 build-depends: base >= 4.7 && < 5
, cond , cond

27
src/GEval/Formatting.hs Normal file
View File

@ -0,0 +1,27 @@
module GEval.Formatting
(formatTheResult, formatSimpleResult)
where
import GEval.Common
import Data.Conduit.Bootstrap
import Text.Printf
formatTheResult :: Maybe Int -> MetricResult -> String
formatTheResult mPrecision (SimpleRun val) = formatSimpleResult mPrecision val
formatTheResult mPrecision (BootstrapResampling vals) = (formatSimpleResult correctedPrecision pointEstimate)
++ "±"
++ (formatSimpleResult correctedPrecision errorBound)
where pointEstimate = (upperBound + lowerBound) / 2.0
errorBound = (upperBound - lowerBound) / 2.0
(lowerBound, upperBound) = getConfidenceBounds defaultConfidenceLevel vals
errorBoundMagnitude = (floor (logBase 10.0 errorBound)) - 1
correctedPrecision = Just $ selectLowerPrecision (max (-errorBoundMagnitude) 0) mPrecision
formatSimpleResult :: Maybe Int -> MetricValue -> String
formatSimpleResult Nothing = show
formatSimpleResult (Just prec) = printf "%0.*f" prec
selectLowerPrecision :: Int -> Maybe Int -> Int
selectLowerPrecision p Nothing = p
selectLowerPrecision p (Just p') = min p p'