Skip to content

Instantly share code, notes, and snippets.

@vasanthaganeshk
Created July 4, 2020 15:55
Show Gist options
  • Save vasanthaganeshk/d2d11e3b52a736209bfb848a2e01216c to your computer and use it in GitHub Desktop.
Save vasanthaganeshk/d2d11e3b52a736209bfb848a2e01216c to your computer and use it in GitHub Desktop.
Banking simulation
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Banking simulation
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
import Control.Monad
import System.Random
--------------------------------------------------------------------------------
-- Control Paramenters
--------------------------------------------------------------------------------
yellow = (2, 5, "yellow")
red = (2, 2, "red")
blue = (5, 1, "blue")
p = 200
alpha = 100
pickP = 0.6
randRange = (0, 1000)
iters = 1000
invIters = 0.001
--------------------------------------------------------------------------------
betaDist (a,b,_) x = p * (x^(a-1)) * ((1-x)^(b-1))
pArrival t = 1 - ((exp 1)**((-1.0)*(fromIntegral t) / fromIntegral alpha))
-- intervals = map (uncurry (-)) $ zip a (tail a)
-- where a = map pArrival [1..1000]
waitY :: Integer -> Double
waitY x
| y > pickP = z + betaDist yellow (z*invIters)
| otherwise = z
where y = pArrival x
z = fromIntegral x
--------------------------------------------------------------------------------
statsCol col@(a,b,c) = (maximum aVal - ((sum $ aVal) / (fromIntegral $ length simL)), c)
where simL = [invIters, invIters*2 .. 1.0]
aVal = map (\x -> betaDist col x) simL
q3 = snd $ minimum $ map statsCol [yellow, red, blue]
--------------------------------------------------------------------------------
main = do
-- q1
xs <- replicateM iters (randomRIO randRange) :: IO [Integer]
ys <- return $ waitY <$> xs
putStrLn $ "q1: Average wait: " ++ show ((sum ys)/(fromIntegral $ length xs)) ++ " Maximum wait: "
-- q2
-- q3
putStrLn $ "q3: closest time: " ++ q3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment