-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMethods.java
170 lines (131 loc) · 4.4 KB
/
Methods.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package statisticstools;
import java.lang.Math;
import java.util.*;
import javax.swing.*;
public class Methods implements StatisticsTools {
public double[] generateRandom1DArray(int numberOfRows, double range1, double range2) {
double[] array = new double[numberOfRows];
for(int i = 0; i < numberOfRows; i++) {
array[i] = (Math.random() * range2 - range1) + range1;
}
return array;
}
public double[][] generateRandom2DArray(int numberOfRows, int numberOfColumns, double range1, double range2) {
double[][] array = new double[numberOfRows][numberOfColumns];
for (int i = 0; i < numberOfRows; i++) {
for (int j = 0; j < numberOfColumns; j++) {
array[i][j] = (Math.random() * range2 - range1) + range1;
}
}
return array;
}
public double average1DArray(double[] x) {
double sum = 0;
int arrayLength = x.length;
for(int i = 0; i < arrayLength; i++) {
sum += x[i];
}
return sum / arrayLength;
}
public double average2DArray(double[][] x, int columnOrder) {
double sum = 0;
int arrayLength = x.length;
for (int i = 0; i < arrayLength; i++) {
sum += x[i][columnOrder];
}
return sum / arrayLength;
}
public double standardDiv1DArray(double[] x) {
double sum = 0;
double avg = average1DArray(x);
int size = x.length;
for (int i = 0; i < size; i++) {
sum += Math.pow(avg - x[i], 2);
}
return Math.sqrt(sum / size);
}
public double standardDiv2DArray(double[][] x, int columnOrder) {
double sum = 0;
double avg = average2DArray(x, columnOrder);
int size = x.length;
for (int i = 0; i < size; i++) {
sum += Math.pow(avg - x[i][columnOrder], 2);
}
return Math.sqrt(sum / size);
}
public double[] confidenceInterval1DArray(double[] x, double zScore) {
double avg = average1DArray(x);
double std = standardDiv1DArray(x);
double meanOfError = zScore * std / Math.sqrt(x.length);
double[] result = new double[2];
result[0] = avg - meanOfError;
result[1] = avg + meanOfError;
return result;
}
public double[] confidenceInterval2DArray(double[][] x, double zScore, int columnOrder) {
double avg = average2DArray(x, columnOrder);
double std = standardDiv2DArray(x, columnOrder);
double meanOfError = zScore * std / Math.sqrt(x.length);
double[] result = new double[2];
result[0] = avg - meanOfError;
result[1] = avg + meanOfError;
return result;
}
public void print1DArray(double[] array) {
for(int i = 0; i < array.length; i++) {
System.out.printf(i + ": %.15f\n", array[i]);
}
}
public void print2DArray(double[][] array) {
int numberOfColumns = array[0].length;
int numberOfRows = array.length;
for (int i = 0; i < numberOfRows; i++) {
System.out.print(i + ": [ ");
for (int j = 0; j < numberOfColumns; j++) {
System.out.printf("%.16f\t", array[i][j]);
}
System.out.print(" ]\n");
}
}
public double combinationCal(ArrayList<Double> array) {
for (int i = 0; i < array.size(); i++){
System.out.print(array.get(i) + " ");
}
System.out.print("\n\n");
double result = 0;
List<List<Double>> resultArray = new ArrayList<List<Double>>();
resultArray = combinationCal_helper(array);
for(List<Double> i: resultArray) {
for(double j: i) {
System.out.print(j + " ");
}
System.out.print("\n");
}
return result;
}
public List<List<Double>> combinationCal_helper(ArrayList<Double> array) {
ArrayList<Double> temp = new ArrayList<Double>(array);
List<List<Double>> result = new ArrayList<List<Double>>();
if (array.size() <= 1) {
result.add(array);
return result;
} else {
double x = temp.get(0);
temp.remove(0);
List<List<Double>> nullResult = combinationCal_helper(temp);
List<List<Double>> addResult = combinationCal_helper(temp);
for(List<Double> i: addResult) {
i.add(x);
}
return addResult;
}
}
public void drawHistogram(String title, double[] values, double x_start, double x_end, double x_interval, int y_start, int y_end, int y_interval) {
JFrame frame = new JFrame("Histogram");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawHistogram tools = new DrawHistogram(title, values, x_start, x_end, x_interval, y_start, y_end, y_interval);
frame.add(tools);
frame.setSize(1000,500);
frame.setVisible(true);
}
}