LoginSignup
1
1

More than 5 years have passed since last update.

Statistics.Distribution

Posted at

http://hackage.haskell.org/package/statistics を試してみる。正規分布から1,000個サンプルをとって、母集団平均の95%信頼区間をt値を使って計算してみる。

% cabal install --user statistics
% runghc
module Main where
import Data.Vector.Unboxed as V
import Statistics.Distribution
import Statistics.Distribution.Normal
import Statistics.Distribution.StudentT
import Statistics.Sample as S;
import System.Random.MWC
import Text.Printf

pickSample :: IO Sample
pickSample = withSystemRandom $
              \gen -> replicateM 1000 (randomDouble gen) :: IO Sample
    where randomDouble = genContVar standard

meanRange95 :: Sample -> (Double, Double)
meanRange95 sample = (S.mean sample - base, S.mean sample + base)
    where n = fromIntegral (V.length sample) - 1 :: Double
          tQuantile95 = quantile (studentT n) 0.975
          base = tQuantile95 * S.stdDev sample / sqrt n

main :: IO ()
main = do
  sample <- pickSample
  printf "mean of sample: %.4f\n" (S.mean sample)
  printf "stddev of sample: %.4f\n" (S.stdDev sample)

  let (min, max) = meanRange95 sample
  printf "%.4f <= mean of population <= %.4f\n" min max

【実行結果】
mean of sample: 0.0050
stddev of sample: 1.0415
-0.0597 <= mean of population <= 0.0697

Statistics.Test にも色々入ってるけど、そっちは試してない。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1