Skip to content

Commit

Permalink
Merge pull request #74 from jethronap/book_examples
Browse files Browse the repository at this point in the history
Add Markove chain Monte Carlo example
  • Loading branch information
pockerman authored May 1, 2020
2 parents c2181ad + 9504e65 commit 7fae597
Show file tree
Hide file tree
Showing 12 changed files with 10,277 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ You can start using Jstat by adding the following dependency to your project:
- <a href="src/main/java/examples/stats/example5/example.md">Example 5</a>: Calculate descriptive statistics metrics
- <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
- <a href="src/main/java/examples/stats/example7/example.md">Example 7</a>: Type II error and the sample size
- <a href="src/main/java/examples/stats/example8/example.md">Example 8</a>: Simulate the Central Limit Theorem

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

### Plotting

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





12 changes: 12 additions & 0 deletions src/main/java/base/CommonConstants.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package base;

import java.nio.file.Paths;

public final class CommonConstants {

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

public static String examplesPath(){
String path = "src/main/java/examples";
return path;
}

public static String mcExamplesPath(){
String path = CommonConstants.examplesPath();
return path + "/mc";
}

protected static double tol = 1.0e-8;
private CommonConstants(){}

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/examples/mc/example3/Example3.java
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);

}
}
64 changes: 64 additions & 0 deletions src/main/java/examples/mc/example3/example.md
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>
55 changes: 55 additions & 0 deletions src/main/java/examples/mc/example4/Example4.java
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));
}
}
}
40 changes: 40 additions & 0 deletions src/main/java/examples/mc/example4/example.md
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>
Loading

0 comments on commit 7fae597

Please sign in to comment.