-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercolationStats.java
90 lines (68 loc) · 2.92 KB
/
PercolationStats.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* *****************************************************************************
* Name: Sebastian Zapata Mardini
* Date: 01/06/2018
* Description: PercolationsStats performs several percolation grids to find
* the percolation treshold from each of them. It opens an API for computing
* some stats variables like the mean, standard deviation and the 95% confidence
* percentil.
**************************************************************************** */
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
public class PercolationStats {
private static final double CONFIDENCE_95 = 1.96;
private final int gridSize; // Grid size
private final int trials; // Number of experiments
private double sttdev;
private double mean;
private final double[] experimentResults; // Number of open sites when grid percolated
public PercolationStats(int n, int numberOfTrials) {
validInput(n, numberOfTrials);
gridSize = n;
trials = numberOfTrials;
experimentResults = new double[trials];
int numberOfSites = gridSize * gridSize;
for (int i = 0; i < trials; i++) {
Percolation perc = new Percolation(n);
while (!perc.percolates()) {
int row = randomNumber();
int col = randomNumber();
if (!perc.isOpen(row, col)) {
perc.open(row, col);
}
}
double treshold = (double) perc.numberOfOpenSites() / numberOfSites;
experimentResults[i] = treshold;
}
}
public double mean() {
if (mean == 0.0) mean = StdStats.mean(experimentResults);
return mean;
}
public double stddev() {
if (trials == 1) return Double.NaN;
if (sttdev == 0.0) sttdev = StdStats.stddev(experimentResults);
return sttdev;
}
public double confidenceLo() {
return mean() - ((CONFIDENCE_95 * stddev()) / Math.sqrt(trials));
}
public double confidenceHi() {
return mean() + ((CONFIDENCE_95 * stddev()) / Math.sqrt(trials));
}
private void validInput(int n, int numberOfTrials) {
if (n < 1 || numberOfTrials < 1) {
throw new IllegalArgumentException("n or trials are less than one. n: " + n + ", trials: " + numberOfTrials);
}
}
private int randomNumber() {
return StdRandom.uniform(gridSize) + 1;
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int trials = Integer.parseInt(args[1]);
PercolationStats stats = new PercolationStats(n, trials);
System.out.println("mean = " + stats.mean());
System.out.println("stddev: = " + stats.stddev());
System.out.println("95% confidence interval = [" + stats.confidenceLo() + ", " + stats.confidenceHi() + "]");
}
}