-
Notifications
You must be signed in to change notification settings - Fork 0
/
PercolationStats.java
50 lines (48 loc) · 1.54 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
public class PercolationStats {
private double mean,stddev,confidenceHi,confidenceLo;
// perform T independent computational experiments on an N-by-N grid
public PercolationStats(int N, int T) {
if(N<=0 || T<=0) throw new java.lang.IllegalArgumentException("Index out of bound");
double[] percolationBound=new double[T];
for(int t=0;t<T;t++) {
Percolation p = new Percolation(N);
int count = 0;
while(!p.percolates()) {
int x = StdRandom.uniform(N)+1;
int y = StdRandom.uniform(N)+1;
if(!p.isOpen(x, y)) {
count++;
p.open(x, y);
}
}
percolationBound[t] = ((double)count)/(N*N);
}
mean=StdStats.mean(percolationBound);
stddev=StdStats.stddev(percolationBound);
confidenceLo = mean - ((1.96*stddev)/Math.sqrt(T));
confidenceHi = mean + ((1.96*stddev)/Math.sqrt(T));
}
// sample mean of percolation threshold
public double mean() {
return this.mean;
}
// sample standard deviation of percolation threshold
public double stddev() {
return this.stddev;
}
// returns lower bound of the 95% confidence interva
public double confidenceLo() {
return this.confidenceLo;
}
// returns upper bound of the 95% confidence intervall
public double confidenceHi() {
return this.confidenceHi;
}
// test client, described below
public static void main(String[] args) {
PercolationStats stats = new PercolationStats(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
StdOut.println(stats.mean());
StdOut.println(stats.stddev());
StdOut.println(stats.confidenceLo()+","+stats.confidenceHi());
}
}