File tree Expand file tree Collapse file tree 12 files changed +10277
-3
lines changed Expand file tree Collapse file tree 12 files changed +10277
-3
lines changed Original file line number Diff line number Diff line change @@ -49,10 +49,13 @@ You can start using Jstat by adding the following dependency to your project:
49
49
- <a href =" src/main/java/examples/stats/example5/example.md " >Example 5</a >: Calculate descriptive statistics metrics
50
50
- <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
51
51
- <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
52
53
53
54
### Monte Carlo
54
55
- <a href =" src/main/java/examples/mc/example1/example.md " >Example 1</a >: Monte Carlo Simulation for the Monty Hall Problem
55
56
- <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
56
59
57
60
### Plotting
58
61
@@ -66,5 +69,6 @@ You can start using Jstat by adding the following dependency to your project:
66
69
- <a href =" src/main/java/examples/es/example1/example.md " >Example 1</a >: Load data from an Elasticsearch index
67
70
68
71
72
+
69
73
70
74
Original file line number Diff line number Diff line change 1
1
package base ;
2
2
3
+ import java .nio .file .Paths ;
4
+
3
5
public final class CommonConstants {
4
6
5
7
public static double getTol (){return CommonConstants .tol ;}
6
8
public static void setTol (double tol ){CommonConstants .tol = tol ;}
7
9
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
+
8
20
protected static double tol = 1.0e-8 ;
9
21
private CommonConstants (){}
10
22
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 >
You can’t perform that action at this time.
0 commit comments