-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from jethronap/book_examples
Add Markove chain Monte Carlo example
- Loading branch information
Showing
12 changed files
with
10,277 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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,35 @@ | ||
package examples.mc.example3; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
public class Example3 { | ||
|
||
static public void main(String[] args){ | ||
|
||
|
||
|
||
final int N_ITERATIONS = 10000; | ||
final double DELTA = 0.1; | ||
double x = 1.0; | ||
double y = 1.0; | ||
double area_under_curve = 0.0; | ||
|
||
for(int itr=0; itr < N_ITERATIONS; ++itr){ | ||
|
||
double del_x = ThreadLocalRandom.current().nextDouble(-DELTA, DELTA); | ||
double del_y = ThreadLocalRandom.current().nextDouble(-DELTA, DELTA); | ||
|
||
if(Math.abs(x + del_x) < 1.0 && Math.abs(y + del_y) < 1.0){ | ||
x += del_x; | ||
y += del_y; | ||
} | ||
|
||
if(x*x + y*y < 1.0){ | ||
area_under_curve += 1; | ||
} | ||
} | ||
|
||
System.out.println("Pi is: " + 4.0* area_under_curve/(double)N_ITERATIONS); | ||
|
||
} | ||
} |
This file contains 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,64 @@ | ||
# Markov Chain Monte Carlo For Pi Calculation | ||
|
||
## Contents | ||
* [Overview](#overview) | ||
* [Import files](#include_files) | ||
* [The main function](#m_func) | ||
* [Results](#results) | ||
* [Source Code](#source_code) | ||
|
||
## <a name="overview"></a> Overview | ||
|
||
## <a name="include_files"></a> Import files | ||
|
||
``` | ||
package examples.mc.example3; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
``` | ||
|
||
## <a name="m_func"></a> The main function | ||
|
||
``` | ||
public class Example3 { | ||
static public void main(String[] args){ | ||
final int N_ITERATIONS = 10000; | ||
final double DELTA = 0.1; | ||
double x = 1.0; | ||
double y = 1.0; | ||
double area_under_curve = 0.0; | ||
for(int itr=0; itr < N_ITERATIONS; ++itr){ | ||
double del_x = ThreadLocalRandom.current().nextDouble(-DELTA, DELTA); | ||
double del_y = ThreadLocalRandom.current().nextDouble(-DELTA, DELTA); | ||
if(Math.abs(x + del_x) < 1.0 && Math.abs(y + del_y) < 1.0){ | ||
x += del_x; | ||
y += del_y; | ||
} | ||
if(x*x + y*y < 1.0){ | ||
area_under_curve += 1; | ||
} | ||
} | ||
System.out.println("Pi is: " + 4.0* area_under_curve/(double)N_ITERATIONS); | ||
} | ||
} | ||
``` | ||
|
||
## <a name="results"></a> Results | ||
|
||
``` | ||
Pi is: 3.0244 | ||
``` | ||
|
||
## <a name="source_code"></a> Source Code | ||
|
||
<a href="Example3.java">Example3.java</a> |
This file contains 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,55 @@ | ||
package examples.mc.example4; | ||
|
||
import base.CommonConstants; | ||
import io.CSVFileWriter; | ||
import org.apache.commons.math3.distribution.AbstractRealDistribution; | ||
import org.apache.commons.math3.distribution.NormalDistribution; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Example4 { | ||
|
||
|
||
public static double target(double x){ | ||
|
||
if(x < 0.){ | ||
return 0.0; | ||
} | ||
|
||
return Math.exp(-x); | ||
} | ||
|
||
public static void main(String[] args){ | ||
|
||
final int N_ITERATIONS = 10000; | ||
List<Double> pos = new ArrayList<>(N_ITERATIONS); | ||
for(int i=0; i<N_ITERATIONS; ++i){ | ||
pos.add(0.0); | ||
} | ||
|
||
CSVFileWriter writer = new CSVFileWriter(CommonConstants.mcExamplesPath()+"/example4/positions.csv"); | ||
writer.writeColumnNames("ITERATION", "POSITION"); | ||
pos.set(0, 2.0); | ||
|
||
writer.writeRow(0, pos.get(0)); | ||
|
||
AbstractRealDistribution normal = new NormalDistribution(0.0, 1.0); | ||
|
||
for(int i=1; i<N_ITERATIONS; ++i){ | ||
double xk = pos.get(i-1); | ||
double xkPlusOne = xk + normal.sample(); | ||
|
||
double A = Example4.target(xkPlusOne/xk); | ||
|
||
if(A < 1.0 ){ | ||
pos.set(i, xkPlusOne); | ||
} | ||
else{ | ||
pos.set(i, xk); | ||
} | ||
|
||
writer.writeRow(i, pos.get(i)); | ||
} | ||
} | ||
} |
This file contains 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,40 @@ | ||
# Example 4: The Metropolis Algorithm | ||
|
||
## Contents | ||
* [Acknowledgements](#acknowledgement) | ||
* [Overview](#overview) | ||
* [Metropolis algorithm](#goodness_of_fit) | ||
* [Import files](#include_files) | ||
* [The main function](#m_func) | ||
* [Results](#results) | ||
* [Source Code](#source_code) | ||
|
||
## <a name="acknowledgement"></a> Acknowledgements | ||
This example was taken from <a href="https://stephens999.github.io/fiveMinuteStats/index.html">fiveMinuteStats</a>. | ||
## <a name="overview"></a> Overview | ||
|
||
### <a name="goodness_of_fit"></a> Metropolis algorithm | ||
|
||
|
||
## <a name="include_files"></a> Import files | ||
|
||
``` | ||
``` | ||
|
||
## <a name="m_func"></a> The main function | ||
|
||
``` | ||
``` | ||
|
||
## <a name="results"></a> Results | ||
|
||
``` | ||
``` | ||
|
||
## <a name="source_code"></a> Source Code | ||
|
||
<a href="Example4.java">Example4.java</a> |
Oops, something went wrong.