Giter Site home page Giter Site logo

pi_monte_carlo's Introduction

pi_monte_carlo

Simple python program to estimate the value of pi using a classic monte-carlo simulation.

Monte-Carlo Methods
A monte-carlo method relies on random sampling to derive a numerical result.

Monte-Carlo Method for finding π
In this well-known approach, the value of π is estimated using a random number function which generates a number between 0 and 1. Consider a circle of radius r which is placed inside a square with length 2r; both the circle and square are centred at the origin.

If we were to pick a set of co-ordinates at random in the picture above, then the probability of our co-ordinates falling inside the circle is given by the area of the circle divided by the area of the square, i.e. P(inside circle) = πr^2/(2r)^2. The random number function affords a method for generating random co-ordiantes. Therefore, for a sample of n random co-ordinates, we can expect the ratio of co-ordinates falling inside the circle (m) to the total number of co-ordinates (n) to tend towards the probability P(inside circle).
n.b. A large value of n > 1000 is necessary to acquire a reasonably accurate estimation of π.


In fact, it is only necessary to consider a single quadrant of the picture above as πr^2/(2r)^2=(1/4)(πr^2)/r^2.
Calling the random number function twice generates an (x,y) co-ordinate for the upper-right quadrant of the square. If we do this n times, counting how many times the co-ordinates fall inside the circle m, we can compute the probability P(inside circle) = m/n. Given P(inside circle) = πr^2/(2r)^2, we can re-arrange the expression to find π like so π = 4(m/n).

pi_monte_carlo's People

Contributors

daly-gr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.