-
Notifications
You must be signed in to change notification settings - Fork 17
/
PercolationStats.java
72 lines (62 loc) · 2.3 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
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdStats;
import edu.princeton.cs.algs4.StdRandom;
public class PercolationStats {
private static final double FRACTILE = 1.96; // 95% confidence interval
private int size;
private int times;
private double mean;
private double stddev;
private double[] data;
// perform trials independent experiments on an n-by-n grid
public PercolationStats(int n, int trials) {
if (n <= 0 || trials <= 0) {
throw new IllegalArgumentException("Error: n or T <= 0");
}
size = n;
times = trials;
data = new double[trials];
for (int i = 0; i < trials; i++) {
Percolation perc = new Percolation(n);
while (!perc.percolates()) {
int row = getRandom();
int col = getRandom();
perc.open(row, col);
}
data[i] = ((double) perc.numberOfOpenSites()) / (size * size);
}
mean = StdStats.mean(data);
stddev = StdStats.stddev(data);
}
private int getRandom() {
return StdRandom.uniform(0, size) + 1;
}
// sample mean of percolation threshold
public double mean() {
return mean;
}
// sample standard deviation of percolation threshold
public double stddev() {
return stddev;
}
// low endpoint of 95% confidence interval
public double confidenceLo() {
double tmp = FRACTILE * stddev / Math.sqrt(times);
return mean - tmp;
}
// high endpoint of 95% confidence interval
public double confidenceHi() {
double tmp = FRACTILE * stddev / Math.sqrt(times);
return mean + tmp;
}
// test client (described below)
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int trials = Integer.parseInt(args[1]);
PercolationStats percStats = new PercolationStats(n, trials);
StdOut.println("mean = " + percStats.mean());
StdOut.println("stddev = " + percStats.stddev());
StdOut.println("95% confidence interval = [" + percStats.confidenceLo()
+ ", " + percStats.confidenceHi() + "]");
}
}