Skip to content

Commit 7fae597

Browse files
authored
Merge pull request #74 from jethronap/book_examples
Add Markove chain Monte Carlo example
2 parents c2181ad + 9504e65 commit 7fae597

File tree

12 files changed

+10277
-3
lines changed

12 files changed

+10277
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@ You can start using Jstat by adding the following dependency to your project:
4949
- <a href="src/main/java/examples/stats/example5/example.md">Example 5</a>: Calculate descriptive statistics metrics
5050
- <a href="src/main/java/examples/stats/example6/example.md">Example 6</a>: Hypothesis tests on the mean of a normal distribution with known variance
5151
- <a href="src/main/java/examples/stats/example7/example.md">Example 7</a>: Type II error and the sample size
52+
- <a href="src/main/java/examples/stats/example8/example.md">Example 8</a>: Simulate the Central Limit Theorem
5253

5354
### Monte Carlo
5455
- <a href="src/main/java/examples/mc/example1/example.md">Example 1</a>: Monte Carlo Simulation for the Monty Hall Problem
5556
- <a href="src/main/java/examples/mc/example2/example.md">Example 2</a>: Calculate the area of a circle using Monte Carlo integration
57+
- <a href="src/main/java/examples/mc/example3/example.md">Example 3</a>: Markov chain Monte Carlo for pi calculation
58+
- <a href="src/main/java/examples/mc/example4/example.md">Example 4</a>: The Metropolis algorithm
5659

5760
### Plotting
5861

@@ -66,5 +69,6 @@ You can start using Jstat by adding the following dependency to your project:
6669
- <a href="src/main/java/examples/es/example1/example.md">Example 1</a>: Load data from an Elasticsearch index
6770

6871

72+
6973

7074

src/main/java/base/CommonConstants.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
package base;
22

3+
import java.nio.file.Paths;
4+
35
public final class CommonConstants {
46

57
public static double getTol(){return CommonConstants.tol;}
68
public static void setTol(double tol){CommonConstants.tol = tol;}
79

10+
public static String examplesPath(){
11+
String path = "src/main/java/examples";
12+
return path;
13+
}
14+
15+
public static String mcExamplesPath(){
16+
String path = CommonConstants.examplesPath();
17+
return path + "/mc";
18+
}
19+
820
protected static double tol = 1.0e-8;
921
private CommonConstants(){}
1022

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package examples.mc.example3;
2+
3+
import java.util.concurrent.ThreadLocalRandom;
4+
5+
public class Example3 {
6+
7+
static public void main(String[] args){
8+
9+
10+
11+
final int N_ITERATIONS = 10000;
12+
final double DELTA = 0.1;
13+
double x = 1.0;
14+
double y = 1.0;
15+
double area_under_curve = 0.0;
16+
17+
for(int itr=0; itr < N_ITERATIONS; ++itr){
18+
19+
double del_x = ThreadLocalRandom.current().nextDouble(-DELTA, DELTA);
20+
double del_y = ThreadLocalRandom.current().nextDouble(-DELTA, DELTA);
21+
22+
if(Math.abs(x + del_x) < 1.0 && Math.abs(y + del_y) < 1.0){
23+
x += del_x;
24+
y += del_y;
25+
}
26+
27+
if(x*x + y*y < 1.0){
28+
area_under_curve += 1;
29+
}
30+
}
31+
32+
System.out.println("Pi is: " + 4.0* area_under_curve/(double)N_ITERATIONS);
33+
34+
}
35+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Markov Chain Monte Carlo For Pi Calculation
2+
3+
## Contents
4+
* [Overview](#overview)
5+
* [Import files](#include_files)
6+
* [The main function](#m_func)
7+
* [Results](#results)
8+
* [Source Code](#source_code)
9+
10+
## <a name="overview"></a> Overview
11+
12+
## <a name="include_files"></a> Import files
13+
14+
```
15+
package examples.mc.example3;
16+
17+
import java.util.concurrent.ThreadLocalRandom;
18+
```
19+
20+
## <a name="m_func"></a> The main function
21+
22+
```
23+
public class Example3 {
24+
25+
static public void main(String[] args){
26+
27+
28+
29+
final int N_ITERATIONS = 10000;
30+
final double DELTA = 0.1;
31+
double x = 1.0;
32+
double y = 1.0;
33+
double area_under_curve = 0.0;
34+
35+
for(int itr=0; itr < N_ITERATIONS; ++itr){
36+
37+
double del_x = ThreadLocalRandom.current().nextDouble(-DELTA, DELTA);
38+
double del_y = ThreadLocalRandom.current().nextDouble(-DELTA, DELTA);
39+
40+
if(Math.abs(x + del_x) < 1.0 && Math.abs(y + del_y) < 1.0){
41+
x += del_x;
42+
y += del_y;
43+
}
44+
45+
if(x*x + y*y < 1.0){
46+
area_under_curve += 1;
47+
}
48+
}
49+
50+
System.out.println("Pi is: " + 4.0* area_under_curve/(double)N_ITERATIONS);
51+
52+
}
53+
}
54+
```
55+
56+
## <a name="results"></a> Results
57+
58+
```
59+
Pi is: 3.0244
60+
```
61+
62+
## <a name="source_code"></a> Source Code
63+
64+
<a href="Example3.java">Example3.java</a>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package examples.mc.example4;
2+
3+
import base.CommonConstants;
4+
import io.CSVFileWriter;
5+
import org.apache.commons.math3.distribution.AbstractRealDistribution;
6+
import org.apache.commons.math3.distribution.NormalDistribution;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class Example4 {
12+
13+
14+
public static double target(double x){
15+
16+
if(x < 0.){
17+
return 0.0;
18+
}
19+
20+
return Math.exp(-x);
21+
}
22+
23+
public static void main(String[] args){
24+
25+
final int N_ITERATIONS = 10000;
26+
List<Double> pos = new ArrayList<>(N_ITERATIONS);
27+
for(int i=0; i<N_ITERATIONS; ++i){
28+
pos.add(0.0);
29+
}
30+
31+
CSVFileWriter writer = new CSVFileWriter(CommonConstants.mcExamplesPath()+"/example4/positions.csv");
32+
writer.writeColumnNames("ITERATION", "POSITION");
33+
pos.set(0, 2.0);
34+
35+
writer.writeRow(0, pos.get(0));
36+
37+
AbstractRealDistribution normal = new NormalDistribution(0.0, 1.0);
38+
39+
for(int i=1; i<N_ITERATIONS; ++i){
40+
double xk = pos.get(i-1);
41+
double xkPlusOne = xk + normal.sample();
42+
43+
double A = Example4.target(xkPlusOne/xk);
44+
45+
if(A < 1.0 ){
46+
pos.set(i, xkPlusOne);
47+
}
48+
else{
49+
pos.set(i, xk);
50+
}
51+
52+
writer.writeRow(i, pos.get(i));
53+
}
54+
}
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Example 4: The Metropolis Algorithm
2+
3+
## Contents
4+
* [Acknowledgements](#acknowledgement)
5+
* [Overview](#overview)
6+
* [Metropolis algorithm](#goodness_of_fit)
7+
* [Import files](#include_files)
8+
* [The main function](#m_func)
9+
* [Results](#results)
10+
* [Source Code](#source_code)
11+
12+
## <a name="acknowledgement"></a> Acknowledgements
13+
This example was taken from <a href="https://stephens999.github.io/fiveMinuteStats/index.html">fiveMinuteStats</a>.
14+
## <a name="overview"></a> Overview
15+
16+
### <a name="goodness_of_fit"></a> Metropolis algorithm
17+
18+
19+
## <a name="include_files"></a> Import files
20+
21+
```
22+
23+
```
24+
25+
## <a name="m_func"></a> The main function
26+
27+
```
28+
29+
30+
```
31+
32+
## <a name="results"></a> Results
33+
34+
```
35+
36+
```
37+
38+
## <a name="source_code"></a> Source Code
39+
40+
<a href="Example4.java">Example4.java</a>

0 commit comments

Comments
 (0)