module StatisticalApps
( anyBirthdayMatch
, specificBirthdayMatch
) where

{- Classic birthday problem. What is the probability that in a room with n people, at least two people will have the same birthday.
-- Input n -- number of people in room
-- Return  -- probability that at least two people will share a birthday
-}
anyBirthdayMatch :: Integer -> Float
anyBirthdayMatch n
    | n > 365 = 1.0
    | n < 2 = 0.0
    | otherwise = anyBirthdayMatchLoop n 364.0 1.0

anyBirthdayMatchLoop :: Integer -> Float -> Float -> Float
anyBirthdayMatchLoop 1 _ rv = 1.0 - rv
anyBirthdayMatchLoop n num rv = anyBirthdayMatchLoop new_n new_num new_rv
    where new_n = n - 1
          new_num = num - 1
          new_rv = rv * num / 365.0
          
{- Probability that in a room of n OTHER people at least one person will share your birthday.
-- Input n -- number of people in room
-- Return  -- probability that at least one other person will share your birthday
-}
specificBirthdayMatch :: Integer -> Float
specificBirthdayMatch n
    | n < 1 = 0.0
    | otherwise = 1.0 - (364.0 / 365.0) ** (fromInteger n)

{-
End of Code:

ghci> anyBirthdayMatch 23
0.50729716
ghci> specificBirthdayMatch 253
0.5004773
-}