org.apache.hadoop.examples
Class QuasiMonteCarlo
java.lang.Object
org.apache.hadoop.conf.Configured
org.apache.hadoop.examples.QuasiMonteCarlo
- All Implemented Interfaces:
- org.apache.hadoop.conf.Configurable, org.apache.hadoop.util.Tool
public class QuasiMonteCarlo
- extends org.apache.hadoop.conf.Configured
- implements org.apache.hadoop.util.Tool
A map/reduce program that estimates the value of Pi
using a quasi-Monte Carlo (qMC) method.
Arbitrary integrals can be approximated numerically by qMC methods.
In this example,
we use a qMC method to approximate the integral $I = \int_S f(x) dx$,
where $S=[0,1)^2$ is a unit square,
$x=(x_1,x_2)$ is a 2-dimensional point,
and $f$ is a function describing the inscribed circle of the square $S$,
$f(x)=1$ if $(2x_1-1)^2+(2x_2-1)^2 <= 1$ and $f(x)=0$, otherwise.
It is easy to see that Pi is equal to $4I$.
So an approximation of Pi is obtained once $I$ is evaluated numerically.
There are better methods for computing Pi.
We emphasize numerical approximation of arbitrary integrals in this example.
For computing many digits of Pi, consider using bbp.
The implementation is discussed below.
Mapper:
Generate points in a unit square
and then count points inside/outside of the inscribed circle of the square.
Reducer:
Accumulate points inside/outside results from the mappers.
Let numTotal = numInside + numOutside.
The fraction numInside/numTotal is a rational approximation of
the value (Area of the circle)/(Area of the square) = $I$,
where the area of the inscribed circle is Pi/4
and the area of unit square is 1.
Finally, the estimated value of Pi is 4(numInside/numTotal).
Method Summary |
static BigDecimal |
estimatePi(int numMaps,
long numPoints,
org.apache.hadoop.conf.Configuration conf)
Run a map/reduce job for estimating Pi. |
static void |
main(String[] argv)
main method for running it as a stand alone command. |
int |
run(String[] args)
Parse arguments and then runs a map/reduce job. |
Methods inherited from class org.apache.hadoop.conf.Configured |
getConf, setConf |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface org.apache.hadoop.conf.Configurable |
getConf, setConf |
QuasiMonteCarlo
public QuasiMonteCarlo()
estimatePi
public static BigDecimal estimatePi(int numMaps,
long numPoints,
org.apache.hadoop.conf.Configuration conf)
throws IOException,
ClassNotFoundException,
InterruptedException
- Run a map/reduce job for estimating Pi.
- Returns:
- the estimated value of Pi
- Throws:
IOException
ClassNotFoundException
InterruptedException
run
public int run(String[] args)
throws Exception
- Parse arguments and then runs a map/reduce job.
Print output in standard out.
- Specified by:
run
in interface org.apache.hadoop.util.Tool
- Returns:
- a non-zero if there is an error. Otherwise, return 0.
- Throws:
Exception
main
public static void main(String[] argv)
throws Exception
- main method for running it as a stand alone command.
- Throws:
Exception
Copyright © 2009 The Apache Software Foundation