Monte-Carlo Method With Haskell

Going to use a package by Patrick O Perry.

The obvious problem that I face is that there is no documentation. Hope this blog save someone else’s time1. Installation (On linux for windows use cygwin ) [Tested on Linux ┬áMint and Windows XP

a. Install gsl-random.. Prerequisites is to install gsl for linux.. Follow the steps here

i) Go here –.

ii) Get the latest gsl-version.tar.gz from there. My case it was gsl-1.16.tar.gz

iii) Open the terminal and do the following

cd ~/Downloads

tar -xzf gsl-1.16.tar.gz

cd gsl-1.16/
sudo ./configure

sudo make

sudo make install

b.) Install gsl-random from hackage

1. sudo cabal install gsl-random

2. sudo cabal install monte-carlo

——————————————————Using the package———————————————————

1. Generate random number for various distributions (check)

———- Please note that all of the example is not 100 % my effort —–

—– in fact I have written only bmt and display random functions here —

<span style="font-family: ‘Helvetica Neue’, Helvetica, Arial, ‘Nimbus Sans L’, sans-serif; font-size: 15px; font-style: inherit; line-height: 1.625;">import System.Random</span></pre>
import Data.List
— Number of samples to take
count = 10000

— Function to process our random sequence
process :: [(Double, Double)] -> (Int, Int)
process = foldl’ sumInCircle (0, 0)

— Function to process a running value and a random value, producing a new running value.
sumInCircle :: (Int, Int) -> (Double, Double) -> (Int, Int)
sumInCircle (ins, total) (x, y) = (ins + if x*x + y*y [Double]
bmt (u1:u2:xs) = [sqrt(-2 * log u1) * cos (2 * pi * u1),sqrt(-2 * log u1) * cos ( 2* pi * u2) ] ++ (bmt xs)

— Function to display the running value.
display:: (Int, Int) -> String
display (heads, coins) = "pi = " ++ (show $ 4.0 * fromIntegral heads / fromIntegral coins)

— to display random numbers
displayRand:: [Double] -> String
displayRand [] = ""
displayRand (x:xs) = "list = " ++ show x ++ displayRand xs

— function to prepare the random sequence for processing
prep :: [Double] -> [(Double, Double)]
prep (a:b:r) = (a,b):prep r

main = do
g <- newStdGen
putStrLn . display .process . take count . prep $ randoms g
putStrLn . displayRand . take 100 $ (randomRs (0,1) g)
putStrLn . displayRand . take 100 $ (bmt (randomRs (0,1) g))


API –> (should be similar to this one )

simulate 1000 (with randnormal 1000) (fun “callprice”)

—- plot random numbers generated by last and second last line —-

so the good news is that plot looks like they the random numbers are good enough to be used

— To Do —

— use MC simulations to get put and call price and compare with the Black scholes prices
—– To be continued ———