Repository navigation
Expand file tree
/
Copy pathPercolationStats.java
More file actions
102 lines (87 loc) · 2.79 KB
/
Copy pathPercolationStats.java
File metadata and controls
102 lines (87 loc) · 2.79 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package percolation;
import percolation.Percolation;
import edu.princeton.cs.algs4.StdRandom;
public class PercolationStats {
private int size = 0;
private int trials = 0;
private Percolation perc = null;
private double[] percThreshold = null;
private double mean = 0;
private double stddev = 0;
private double confidenceLo = 0;
private double confidenceHi = 0;
public PercolationStats(int n,int trials){
if(n <= 0 || trials <= 0 )
{
throw new IllegalArgumentException();
}
this.size = n;
this.trials = trials;
}
public double mean(){
percThreshold = new double[trials];
for(int i = 0 ;i < trials ; i++){
perc = new Percolation(size);
double openTimes = 0;
while(!perc.percolates()){
int x = StdRandom.uniform(1,size + 1);
int y = StdRandom.uniform(1,size + 1);
if(perc.isOpen(x, y)){
continue;
}else{
perc.open(x, y);
openTimes++;
}
}
percThreshold[i] = openTimes / (size * size );
perc = null;
}
double sum = 0;
for (double d : percThreshold) {
sum += d;
}
mean = sum/percThreshold.length;
return mean;
}
public double stddev(){
if(mean == 0 || null == percThreshold){
this.mean();
}
double sqrSum = 0;
for(int i = 0;i < percThreshold.length;i++){
sqrSum += (percThreshold[i] - mean) * (percThreshold[i] - mean);
}
stddev = Math.sqrt( sqrSum / (percThreshold.length - 1));
return stddev;
}
public double confidenceLo(){
if(confidenceLo == 0){
confidence();
}
return confidenceLo;
}
private void confidence() {
if(stddev == 0 ){
this.stddev();
}
confidenceLo = mean - 1.96 * stddev /Math.sqrt(percThreshold.length);
confidenceHi = mean + 1.96 * stddev /Math.sqrt(percThreshold.length);
}
public double confidenceHi(){
if(confidenceHi == 0){
confidence();
}
return confidenceHi;
}
public static void main(String[] args){
PercolationStats percS = new PercolationStats(200, 100);
double mean = percS.mean();
System.out.println("mean:"+mean);
double stddev = percS.stddev();
System.out.println("stddev:"+stddev);
double confidenceLo = percS.confidenceLo();
double confidenceHi = percS.confidenceHi();
System.out.println("confidenceLo:"+confidenceLo);
System.out.println("confidenceHi:"+confidenceHi);
}
}