Skip to content

Added Monte Carlo in Haskell #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions chapters/monte_carlo/code/haskell/monteCarlo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import System.Random

monteCarloPi :: RandomGen g => g -> Int -> Float
monteCarloPi g n = count $ filter inCircle $ makePairs
where makePairs = take n $ toPair (randomRs (0, 1) g :: [Float])
toPair (x:y:rest) = (x, y) : toPair rest
inCircle (x, y) = x^2 + y^2 < 1
count l = 4 * fromIntegral (length l) / fromIntegral n

main = do
g <- newStdGen
let p = monteCarloPi g 100000
putStrLn $ "Estimated pi: " ++ show p
putStrLn $ "Percent error: " ++ show (100 * abs (pi - p) / pi)
11 changes: 8 additions & 3 deletions chapters/monte_carlo/monte_carlo.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ each point is tested to see whether it's in the circle or not:
{% method %}
{% sample lang="jl" %}
[import:2-8, lang:"julia"](code/julia/monte_carlo.jl)
{% sample lang="hs" %}
[import:7-7, lang:"haskell"](code/haskell/monteCarlo.hs)
{% endmethod %}

If it's in the circle, we increase an internal count by one, and in the end,
Expand All @@ -56,14 +58,14 @@ If we use a small number of points, this will only give us a rough approximation
The true power of monte carlo comes from the fact that it can be used to integrate literally any object that can be embedded into the square.
As long as you can write some function to tell whether the provided point is inside the shape you want (like `in_circle()` in this case), you can use monte carlo integration!
This is obviously an incredibly powerful tool and has been used time and time again for many different areas of physics and engineering.
I can gaurantee that we will see similar methods crop up all over the place in the future!
I can guarantee that we will see similar methods crop up all over the place in the future!

# Example Code
Monte carlo methods are famous for their simplicity.
It doesn't take too many lines to get something simple going.
Here, we are just integrating a circle, like we described above; however, there is a small twist and trick.
Instead of calculating the area of the circle, we are instead trying to find the value of $$\pi$$, and
rather than integrating the entire circle, we are only integrating the upper right quadrant of the circle from $$0 < x,y < 1$$.
rather than integrating the entire circle, we are only integrating the upper right quadrant of the circle from $$0 < x, y < 1$$.
This saves a bit of computation time, but also requires us to multiply our output by $$4$$.

That's all there is to it!
Expand All @@ -73,13 +75,16 @@ Feel free to submit your version via pull request, and thanks for reading!
{% sample lang="jl" %}
### Julia
[import, lang:"julia"](code/julia/monte_carlo.jl)
{% sample lang="hs" %}
### Haskell
[import, lang:"haskell"](code/haskell/monteCarlo.hs)
{% endmethod %}


<script>
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
</script>
$$
$$
\newcommand{\d}{\mathrm{d}}
\newcommand{\bff}{\boldsymbol{f}}
\newcommand{\bfg}{\boldsymbol{g}}
Expand Down