From 550358f0cd1b1c1efd07890377767924a025e84d Mon Sep 17 00:00:00 2001 From: Filip Gralinski Date: Tue, 28 Jan 2020 20:32:09 +0100 Subject: [PATCH] Automatically set precision when bootstrap resampling is used --- app/Main.hs | 17 +---------------- geval.cabal | 1 + src/GEval/Formatting.hs | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 src/GEval/Formatting.hs diff --git a/app/Main.hs b/app/Main.hs index 87805f1..337da7f 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -5,12 +5,11 @@ import GEval.EvaluationScheme import GEval.Common import GEval.OptionsParser import GEval.ParseParams +import GEval.Formatting import System.Environment import Options.Applicative -import Text.Printf - import System.IO import System.Exit @@ -112,17 +111,3 @@ formatSourceSpec spec = show spec formatTheMetricAndResult :: Maybe Int -> (EvaluationScheme, MetricResult) -> String 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 diff --git a/geval.cabal b/geval.cabal index 950d4cc..e3426d4 100644 --- a/geval.cabal +++ b/geval.cabal @@ -52,6 +52,7 @@ library , Data.CartesianStrings , Data.SplitIntoCrossTabs , Data.Conduit.Bootstrap + , GEval.Formatting , Paths_geval build-depends: base >= 4.7 && < 5 , cond diff --git a/src/GEval/Formatting.hs b/src/GEval/Formatting.hs new file mode 100644 index 0000000..62667ca --- /dev/null +++ b/src/GEval/Formatting.hs @@ -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'