-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercolationStats.java
More file actions
73 lines (64 loc) · 2.94 KB
/
Copy pathPercolationStats.java
File metadata and controls
73 lines (64 loc) · 2.94 KB
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
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
import edu.princeton.cs.algs4.Stopwatch;
public class PercolationStats {
private final double elapsedTime;
private final double[] percolationThresholds;
// perform independent trials on an n-by-n grid
public PercolationStats(int n, int trials) {
final Stopwatch stopwatch = new Stopwatch();
if (n <= 0 || trials <= 0) {
throw new IllegalArgumentException("PercolationStats initialization value must be greater than 0.");
}
percolationThresholds = new double[trials];
for (int t = 0; t < trials; t++) {
// System.out.println("trial: " + t);
final Percolation trial = new Percolation(n);
do {
// randomly open cell
final int row = StdRandom.uniform(1,n+1);
final int col = StdRandom.uniform(1,n+1);
trial.open(row, col);
} while (trial.percolates() == false);
// System.out.println("elapsedtime: " + stopwatch.elapsedTime());
percolationThresholds[t] = trial.numberOfOpenSites() /(Math.pow(n, 2));
}
elapsedTime = stopwatch.elapsedTime();
}
// sample mean of percolation threshold
public double mean() {
return StdStats.mean(percolationThresholds);
}
// sample standard deviation of percolation threshold
public double stddev() {
return StdStats.stddev(percolationThresholds);
}
// low endpoint of 95% confidence interval
public double confidenceLo() {
double x = mean();
double s = stddev();
double T = Math.sqrt(percolationThresholds.length);
double result = x - (1.96d * (s / T));
return result;
}
// high endpoint of 95% confidence interval
public double confidenceHi() {
double x = mean();
double s = stddev();
double T = Math.sqrt(percolationThresholds.length);
double result = x + (1.96d * (s/T));
return result;
}
// test client (see below)
public static void main(String[] args) {
final Integer n = Integer.valueOf(args[0]); // board size
final Integer T = Integer.valueOf(args[1]); // number of simulations
PercolationStats stats = new PercolationStats(n, T);
System.out.println("mean = " + stats.mean());
System.out.println("stddev = " + stats.stddev());
System.out.println(String.format("95%% confidence interval = [%f, %f]", stats.confidenceHi(), stats.confidenceLo()));
// main() method that takes two command-line arguments n and T, performs T independent computational experiments (discussed above) on an n-by-n grid,
// and prints the sample mean, sample standard deviation, and the 95% confidence interval for the percolation threshold.
// Use StdRandom to generate random numbers; use StdStats to compute the sample mean and sample standard deviation.
}
}