Skip to content

Instantly share code, notes, and snippets.

@lquenti
Created October 12, 2023 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lquenti/132e24f2d177368e06ec2955b578f011 to your computer and use it in GitHub Desktop.
Save lquenti/132e24f2d177368e06ec2955b578f011 to your computer and use it in GitHub Desktop.
Using statistics.types
module Main where
import Statistics.Types
import Data.Maybe (fromJust)
-----------------------
-- Confidence levels --
-----------------------
cl95' :: CL Double
cl95' = mkCL 0.95
cl95'' :: Maybe (CL Double)
cl95'' = mkCLE 0.95
cl95''' :: (CL Double)
cl95''' = mkCLFromSignificance 0.05
cl95'''' :: Maybe (CL Double)
cl95'''' = mkCLFromSignificanceE 0.05
printCL :: (Show a, Num a) => CL a -> IO ()
printCL cl = putStrLn str
where str = (show $ confidenceLevel cl) ++ " " ++ (show $ significanceLevel cl)
-- pid == partial id
pid' :: (Ord a, Num a) => a -> a
pid' = confidenceLevel . mkCL
pid'' :: (Ord a, Num a) => a -> a
pid'' = significanceLevel . mkCLFromSignificance
--------------
-- P-Values --
--------------
--
pVal :: PValue Double
pVal = mkPValue 0.03
-- from nSgima
p68 :: PValue Double
p68 = nSigma 1.0
p95 :: PValue Double
p95 = nSigma 2.0
p99_7 :: PValue Double
p99_7 = nSigma 3.0
-- Some more ids
pid''' :: Double -> Double
pid''' = getNSigma . nSigma
-- print pvalue
printPValue :: PValue Double -> IO ()
printPValue p = putStrLn str
where str = (show $ pValue p) ++ "p, " ++ (show $ getNSigma p) ++ "σ"
---------------------
-- Point Estimates --
---------------------
-- TODO add types everywhere
-- 144 ± 5
symEstimate :: Estimate NormalErr Double
symEstimate = Estimate {
estPoint = 144.0
,estError = NormalErr 5.0
}
symEstimate' :: Estimate NormalErr Double
symEstimate' = estimateNormErr 144.0 5.0
symEstimate'' :: Estimate NormalErr Double
symEstimate'' = 144.0 ± 5.0
-- 144 + 6 - 4, alpha = 0.05
asymEstimate :: Estimate ConfInt Double
asymEstimate = Estimate {
estPoint = 144.0
,estError = ConfInt{
confIntLDX = 4.0
,confIntUDX = 6.0
,confIntCL = cl95
}
}
asymEstimate' :: Estimate ConfInt Double
asymEstimate' = estimateFromErr 144.0 (4.0, 6.0) cl95
printEstimateNormErr :: Estimate NormalErr Double -> IO ()
printEstimateNormErr e = do
putStrLn $ "Point Estimate: " ++ show (estPoint e)
putStrLn $ "Error Estimate: " ++ show (normalError $ estError e)
printEstimateConfInt :: Estimate ConfInt Double -> IO ()
printEstimateConfInt e = do
putStrLn $ "Point Estimate: " ++ show (estPoint e)
putStrLn $ "Lower Error: " ++ show (confIntLDX $ estError e)
putStrLn $ "Upper Error: " ++ show (confIntUDX $ estError e)
putStrLn $ "Confidence Level: " ++ show (confIntCL $ estError e)
main :: IO ()
main = do
putStrLn "Confidence Levels:"
putStrLn "cl95':"
printCL cl95'
putStrLn "cl95'':"
printCL (fromJust cl95'')
putStrLn "cl95''':"
printCL cl95'''
putStrLn "cl95'''':"
printCL (fromJust cl95'''')
putStrLn "\nTesting pid functions:"
putStrLn "pid' 0.5:"
print $ pid' 0.5
putStrLn "pid'' 0.5:"
print $ pid'' 0.5
putStrLn "\nP-Values:"
putStrLn "pVal:"
printPValue pVal
putStrLn "p68:"
printPValue p68
putStrLn "p95:"
printPValue p95
putStrLn "p99_7:"
printPValue p99_7
putStrLn "pid''' 3.0:"
print $ pid''' 3.0
putStrLn "\nPoint Estimates:"
putStrLn "symEstimate:"
printEstimateNormErr symEstimate
putStrLn "symEstimate':"
printEstimateNormErr symEstimate'
putStrLn "symEstimate'':"
printEstimateNormErr symEstimate''
putStrLn "asymEstimate:"
printEstimateConfInt asymEstimate
putStrLn "asymEstimate':"
printEstimateConfInt asymEstimate'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment