-
- Notifications
You must be signed in to change notification settings - Fork 359
adding draft of monte carlo chapter #115
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
Changes from 5 commits
Commits
Show all changes
6 commits Select commit Hold shift + click to select a range
e9fbef6 adding draft of monte carlo chapter.
leios cf51503 removing unnecessary tag language tag
leios 3754146 changing images to gif.
leios 45d44e8 fixing typo
leios d7a11bc fixing typo.
leios 438a6e0 fixing a few mroe smallscale typos
leios File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # function to determine whether an x, y point is in the unit circle | ||
| function in_circle(x_pos::Float64, y_pos::Float64, radius::Float64) | ||
| if (x_pos^2 + y_pos^2 < radius^2) | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
| end | ||
| | ||
| # function to integrate a unit circle to find pi via monte_carlo | ||
| function monte_carlo(n::Int64, radius::Float64) | ||
| | ||
| pi_count = 0 | ||
| for i = 1:n | ||
| point_x = rand() | ||
| point_y = rand() | ||
| | ||
| if (in_circle(point_x, point_y, radius)) | ||
| pi_count += 1 | ||
| end | ||
| end | ||
| | ||
| pi_estimate = 4*pi_count/(n*radius^2) | ||
| println("Percent error is: ", signif(100*(pi - pi_estimate), 3), " %") | ||
| end | ||
| | ||
| monte_carlo(10000000, 0.5) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # Monte Carlo Integration | ||
| | ||
| Monte Carlo methods were some of the first methods I ever used for research, and when I learned about them, they seemed like some sort of magic. | ||
| Their premise is simple: random numbers can be used to integrate arbitrary shapes embedded into other objects. | ||
| Nowadays, "monte carlo" has become a bit of a catch-all term for methods that use random numbers to produce real results, but it all started as a straightforward method to integrate objects. | ||
| No matter how you slice it, the idea seems a bit crazy at first. | ||
| After all, random numbers are random. | ||
| How could they possibly be used to find non-random values? | ||
| | ||
| Well, imagine you have a square. | ||
| The area of the square is simple, $$\text{Area}_{\text{square}} = \text{length} \times \text{width}$$. | ||
| Since it's a square, the $$\text{length}$$ and $$\text{width}$$ are the same, so the formula is technically just $$\text{Area}_{\text{square}} = \text{length}^2$$. | ||
| If we embed a circle into the square with a radius $$r = \text{length}$$ (shown below), then it's area is $$\text{Area}_{\text{circle}}=\pi r^2$$. | ||
| For simplicity, we can also say that $$\text{Area}_{\text{square}}=4r^2$$. | ||
| | ||
| <p align="center"> | ||
| <img src="res/square_circle.png" width="300"/> | ||
| </p> | ||
| | ||
| Now, let's say we want to find the area of the circle without an equation. | ||
| As we said before, it's embedded in the square, so we should be able to find some ratio of the area of the square to the area of the circle: | ||
| | ||
| $$ | ||
| \text{Ratio} = \frac{\text{Area}_{\text{circle}}}{\text{Area}_{\text{square}}} | ||
| $$ | ||
| | ||
| This means, | ||
| | ||
| $$ | ||
| \text{Area}_{\text{circle}} = \text{Area}_{\text{square}}\times\text{Ratio} = 4r^2 \times \text{ratio} | ||
| $$ | ||
| | ||
| So, if we can find the $$\text{Ratio}$$ and we know $$r$$, we should be able to easily find the $$\text{Area}_{\text{circle}}$$. | ||
| The question is, "How do we easily find the $$\text{Ratio}$$?" | ||
| Well, one way is with *random sampling*. | ||
| We basically just pick a bunch of points randomly in the square, and | ||
| 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) | ||
| {% endmethod %} | ||
| | ||
| If it's in the circle, we increase an internal count by one, and in the end, | ||
| | ||
| $$ | ||
| \text{Ratio} = \frac{\text{count in circle}}{\text{total number of points used}} | ||
| $$ | ||
| | ||
| If we use a small number of points, this will only give us a rough approximation, but as we start adding more and more points, the approximation becomes much, much better (as shown below)! | ||
| | ||
| <p align="center"> | ||
| <img src="res/monte_carlo.gif" width="400"/> | ||
| </p> | ||
| | ||
| 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! | ||
| | ||
| # 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 left 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! | ||
| Feel free to submit your version via pull request, and thanks for reading! | ||
| | ||
| {% method %} | ||
| {% sample lang="jl" %} | ||
| ### Julia | ||
| [import, lang:"julia"](code/julia/monte_carlo.jl) | ||
| {% endmethod %} | ||
| | ||
| | ||
| <script> | ||
| MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | ||
| </script> | ||
| $$ | ||
| \newcommand{\d}{\mathrm{d}} | ||
| \newcommand{\bff}{\boldsymbol{f}} | ||
| \newcommand{\bfg}{\boldsymbol{g}} | ||
| \newcommand{\bfp}{\boldsymbol{p}} | ||
| \newcommand{\bfq}{\boldsymbol{q}} | ||
| \newcommand{\bfx}{\boldsymbol{x}} | ||
| \newcommand{\bfu}{\boldsymbol{u}} | ||
| \newcommand{\bfv}{\boldsymbol{v}} | ||
| \newcommand{\bfA}{\boldsymbol{A}} | ||
| \newcommand{\bfB}{\boldsymbol{B}} | ||
| \newcommand{\bfC}{\boldsymbol{C}} | ||
| \newcommand{\bfM}{\boldsymbol{M}} | ||
| \newcommand{\bfJ}{\boldsymbol{J}} | ||
| \newcommand{\bfR}{\boldsymbol{R}} | ||
| \newcommand{\bfT}{\boldsymbol{T}} | ||
| \newcommand{\bfomega}{\boldsymbol{\omega}} | ||
| \newcommand{\bftau}{\boldsymbol{\tau}} | ||
| $$ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you need to divide (pi - pi_estimate) by pi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, you are right. Fixing soon. Thanks!