-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathData.java
More file actions
246 lines (182 loc) · 5.93 KB
/
Data.java
File metadata and controls
246 lines (182 loc) · 5.93 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import java.util.*;
public class Data {
double arr[]; // INSTANCE VARIABLE
int size; // INSTANCE VARIABLE
/* CONSTRUCTOR */
public Data(int size, double input[]) throws DataSetEmptyException{
arr = new double[size];
this.size = size;
//checking if the dataset recieved as parameter is empty, if yes, we throw an exception
if (size== 0) {
throw new DataSetEmptyException("Please add some values in your data");
}
if (input.length==0){
throw new DataSetEmptyException("Please add some values in your data");
}
for(int i = 0; i < size; i++) {
arr[i] = input[i];
}
}
/* METHODS */
public double medianCalc()
{
Arrays.sort(arr); // we sort the array to calculate the median
if (size% 2 != 0) // check for even size case
return(double)arr[size/ 2];
return(double)(arr[(size- 1) / 2] + arr[size/ 2]) / 2.0;
}
public double meanCalc() {
double sum = 0;
for(int i = 0; i < size; i++) {
sum+=arr[i];
}
return(sum/(float)size);
}
//calculating frequency of a particular element
public int freqCalc() {
int count=0;
for (double i : arr)
{
if(i==size)
count++;
}
return(count);
}
//Utility method for returning sum of values a different array
public double sumCalc(double array[]) {
double sum = 0;
for(double i : array) {
sum+=i;
}
return(sum);
}
//Utility method for returning sum of values of the array of the particular object
public double sumCalc() {
double sum = 0;
for(double i : this.arr) {
sum+=i;
}
return(sum);
}
//finding the maximum element in the Dataset
public double maxCalc() {
double max = arr[0];
for (double i : arr ) {
if ( max < i ) {
max = i;
}
}
return(max);
}
//Finding the minimum element in the Dataset
public double minCalc() {
double min= arr[0];
for (double i : arr ) {
if(min>i) {
min = i;
}
}
return(min);
}
//calculating the trimmed mean taking percentage trimmed as input
public double trimmedMean(double perc) {
int g = (int)Math.floor((perc/100)*size);
int len = size - 2*g;
double temp[] = arr.clone();
Arrays.sort(temp);
for(int i = 0; i<size && g!=0;i++,g--) {
temp[i] = 0;
temp[arr.length-(i+1)] = 0 ;//defining the trimmed values as 0
}
return(this.sumCalc(temp)/len);
}
//method to calculate the firstQuartile
public double firstQuartile() {
double b[] = arr.clone();
Arrays.sort(b);
return(b[(int)(Math.floor(((size+1)/4)))]);
}
//method to calculate the second quartile
public double thirdQuartile() {
double b[] = arr.clone();
Arrays.sort(b);
return(b[(int)(Math.floor((3*(size+1)/4)))]);
}
//method to calculate inter-quartile
public double interQuartile()
{
return(thirdQuartile()-firstQuartile());
}
//method to calculate variance
public double variance() {
double squareDifference = 0;
int size = arr.length;
for (int i = 0; i < size; i++)
squareDifference += (arr[i] - meanCalc()) *
(arr[i] - meanCalc());
return(squareDifference / size);
}
//method to calculate standard deviation
public double stndDeviation() {
return(Math.sqrt(this.variance()));
}
// Taking a Data object as an parameter
public double coVariance(Data obj) throws UnequalArrayException {
if(this.size!=obj.size) {
throw new UnequalArrayException("Co-variance methods require arrays of equal length");
}
else {
double sum = 0;
double mean_a = this.meanCalc();
double mean_b = obj.meanCalc();
for(int i = 0; i < this.size; i++)
sum = sum + (this.arr[i] - mean_a) * (obj.arr[i] - mean_b);
return sum / (this.size - 1);
}
}
//method to calculate correlation
public double correlation(Data obj) throws UnequalArrayException {
if (this.size != obj.size) {
throw new UnequalArrayException("Co-relation methods require arrays of equal length");
}
else {
double sum_a = this.sumCalc(), sum_b = obj.sumCalc(), sum_ab = 0;
double squaresum_a = 0, squaresum_b = 0;
for (int i = 0; i < this.size; i++)
{
// sum of a[i] * b[i].
sum_ab = sum_ab + this.arr[i] * obj.arr[i];
// sum of square of array elements.
squaresum_a = squaresum_a + this.arr[i] * this.arr[i];
squaresum_b = squaresum_b + obj.arr[i] * obj.arr[i];
}
// use formula for calculating correlation
// coefficient.
double corr = (float)(this.size * sum_ab - sum_a * sum_b)/
(float)(Math.sqrt((this.size * squaresum_a -
sum_a * sum_a) * (this.size * squaresum_b -
sum_b * sum_b)));
return corr;
}
}
//method to return a map where dataset elements are the keys and their frequency is the value
public Map<Double, Integer> frequencyNumber()
{
// Creating a HashMap containing integer
// as a key and occurrences as a value
HashMap<Double, Integer> freqMap = new HashMap<Double, Integer>();
for (int i=0;i<size;i++) {
if (freqMap.containsKey(arr[i])) {
// If number is present in freqMap,
// incrementing it's count by 1
freqMap.put(arr[i], freqMap.get(arr[i]) + 1);
}
else {
// If integer is not present in freqMap,
// putting this integer to freqMap with 1 as it's value
freqMap.put(arr[i], 1);
}
}
return freqMap;
}
}