-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path151.java
More file actions
418 lines (378 loc) · 13.4 KB
/
Copy path151.java
File metadata and controls
418 lines (378 loc) · 13.4 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package oms3.ngmf.util.cosu;
import oms3.util.Stats;
/**
*
* @author od
*/
public class Efficiencies {
public static final int MAXIMIZATION = 1;
public static final int MINIMIZATION = 2;
public static final int ABSMAXIMIZATION = 3;
public static final int ABSMINIMIZATION = 4;
private Efficiencies() {
}
private static void sameArrayLen(double[]... arr) {
int len = arr[0].length;
for (double[] a : arr) {
if (a.length != len) {
throw new IllegalArgumentException("obs and sim data have not same size (" + a.length + "/" + len + ")");
}
}
}
/** Calculates the efficiency between a test data set and a verification data set
* after Nash & Sutcliffe (1970). The efficiency is described as the proportion of
* the cumulated cubic deviation between both data sets and the cumulated cubic
* deviation between the verification data set and its mean value.
* @param sim the simulation data set
* @param obs the validation (observed) data set
* @param pow the power for the deviation terms
* @return the calculated efficiency or -9999 if an error occurs
*/
public static double nashSutcliffe(double[] obs, double[] sim, double pow) {
sameArrayLen(obs, sim);
int pre_size = sim.length;
int steps = pre_size;
double sum_td = 0;
double sum_vd = 0;
/**summing up both data sets */
for (int i = 0; i < steps; i++) {
sum_td = sum_td + sim[i];
sum_vd = sum_vd + obs[i];
}
/** calculating mean values for both data sets */
double mean_vd = sum_vd / steps;
/** calculating mean pow deviations */
double td_vd = 0;
double vd_mean = 0;
for (int i = 0; i < steps; i++) {
td_vd = td_vd + (Math.pow((Math.abs(obs[i] - sim[i])), pow));
vd_mean = vd_mean + (Math.pow((Math.abs(obs[i] - mean_vd)), pow));
}
return 1 - (td_vd / vd_mean);
}
/** Calculates the efficiency between the log values of a test data set and a verification data set
* after Nash & Sutcliffe (1970). The efficiency is described as the proportion of
* the cumulated cubic deviation between both data sets and the cumulated cubic
* deviation between the verification data set and its mean value. If either prediction or validation has a
* value of <= 0 then the pair is ommited from the calculation and a message is put to system out.
* @param sim the simulation data set
* @param obs the validation (observed) data set
* @param pow the power for the deviation terms
* @return the calculated log_efficiency or -9999 if an error occurs
*/
public static double nashSutcliffeLog(double[] obs, double[] sim, double pow) {
sameArrayLen(obs, sim);
int size = sim.length;
double sum_log_pd = 0;
double sum_log_vd = 0;
/** calculating logarithmic values of both data sets. Sets 0 if data is 0 */
double[] log_preData = new double[size];
double[] log_valData = new double[size];
int validPairs = 0;
for (int i = 0; i < size; i++) {
//either prediction or validation shows a value of zero
//in this case the pair is excluded from the further calculation,
//simply by setting the values to -1 and not increasing valid pairs
if (sim[i] <= 0 || obs[i] <= 0) {
log_preData[i] = -1;
log_valData[i] = -1;
}
//both prediction and validation shows a value of exact zero
//in this case the pair is taken as a perfect fit and included
//into the further calculation
if (sim[i] == 0 && obs[i] == 0) {
log_preData[i] = 0;
log_valData[i] = 0;
validPairs++;
}
//both prediction and validation are greater than zero
//no problem for the calculation
if (sim[i] > 0 && obs[i] > 0) {
log_preData[i] = Math.log(sim[i]);
log_valData[i] = Math.log(obs[i]);
validPairs++;
}
}
/**summing up both data sets */
for (int i = 0; i < size; i++) {
if (log_preData[i] >= 0) {
sum_log_pd = sum_log_pd + log_preData[i];
sum_log_vd = sum_log_vd + log_valData[i];
}
}
/** calculating mean values for both data sets */
double mean_log_vd = sum_log_vd / validPairs;
/** calculating mean pow deviations */
double pd_log_vd = 0;
double vd_log_mean = 0;
for (int i = 0; i < size; i++) {
if (log_preData[i] >= 0) {
pd_log_vd = pd_log_vd + (Math.pow(Math.abs(log_valData[i] - log_preData[i]), pow));
vd_log_mean = vd_log_mean + (Math.pow(Math.abs(log_valData[i] - mean_log_vd), pow));
}
}
return 1 - (pd_log_vd / vd_log_mean);
}
/** Calculates the index of agreement (ioa) between a test data set and a verification data set
* after Willmot & Wicks (1980). The ioa is described as the proportion of
* the cumulated cubic deviation between both data sets and the squared sum of the absolute
* deviations between the verification data set and the test mean value and the test data set and
* its mean value.
* @param sim the test Data set
* @param obs the verification data set
* @param pow the power
* @return the calculated ioa or -9999 if an error occurs
*/
public static double ioa(double[] obs, double[] sim, double pow) {
sameArrayLen(obs, sim);
int steps = sim.length;
double sum_obs = 0;
/**summing up both data sets */
for (int i = 0; i < steps; i++) {
sum_obs += obs[i];
}
/** calculating mean values for both data sets */
double mean_obs = sum_obs / steps;
/** calculating mean cubic deviations */
/** calculating absolute squared sum of deviations from verification mean */
double td_vd = 0;
double abs_sqDevi = 0;
for (int i = 0; i < steps; i++) {
td_vd += (Math.pow((Math.abs(obs[i] - sim[i])), pow));
abs_sqDevi += Math.pow(Math.abs(sim[i] - mean_obs) + Math.abs(obs[i] - mean_obs), pow);
}
return 1.0 - (td_vd / abs_sqDevi);
}
/**
* Calcs coefficients of linear regression between x, y data
* @param xData the independent data array (x)
* @param yData the dependent data array (y)
* @return (intercept, gradient, r?)
*/
public static double[] linearReg(double[] xData, double[] yData) {
sameArrayLen(xData, yData);
double sumYValue = 0;
double meanYValue = 0;
double sumXValue = 0;
double meanXValue = 0;
double sumX = 0;
double sumY = 0;
double prod = 0;
double NODATA = -9999;
int nstat = xData.length;
double[] regCoef = new double[3]; //(intercept, gradient, r?)
int counter = 0;
//calculating sums
for (int i = 0; i < nstat; i++) {
if ((yData[i] != NODATA) && (xData[i] != NODATA)) {
sumYValue += yData[i];
sumXValue += xData[i];
counter++;
}
}
//calculating means
meanYValue = sumYValue / counter;
meanXValue = sumXValue / counter;
//calculating regression coefficients
for (int i = 0; i < nstat; i++) {
if ((yData[i] != NODATA) && (xData[i] != NODATA)) {
sumX += Math.pow((xData[i] - meanXValue), 2);
sumY += Math.pow((yData[i] - meanYValue), 2);
prod += ((xData[i] - meanXValue) * (yData[i] - meanYValue));
}
}
if (sumX > 0 && sumY > 0) {
regCoef[1] = prod / sumX; //gradient
regCoef[0] = meanYValue - regCoef[1] * meanXValue; //intercept
regCoef[2] = Math.pow((prod / Math.sqrt(sumX * sumY)), 2); //r?
} else {
regCoef[1] = 0;
regCoef[0] = 0;
regCoef[2] = 0;
}
return regCoef;
}
/**
*
* @param prediction
* @param validation
* @return
*/
public static double dsGrad(double[] obs, double[] sim) {
sameArrayLen(obs, sim);
int dsLength = sim.length;
double[] cumPred = new double[dsLength];
double[] cumVali = new double[dsLength];
double cp = 0;
double cv = 0;
for (int i = 0; i < dsLength; i++) {
cp += sim[i];
cv += obs[i];
cumPred[i] = cp;
cumVali[i] = cv;
}
//interc., grad., r?
double[] regCoef = linearReg(cumVali, cumPred);
return regCoef[1];
}
/**
*
* @param prediction
* @param validation
* @return
*/
public static double absVolumeError(double[] obs, double[] sim) {
sameArrayLen(obs, sim);
double volError = 0;
for (int i = 0; i < sim.length; i++) {
volError += (sim[i] - obs[i]);
}
return Math.abs(volError);
}
/**
*
* @param prediction
* @param validation
* @return
*/
public static double pbias(double[] obs, double[] sim) {
sameArrayLen(obs, sim);
double sumObs = 0;
double sumDif = 0;
for (int i = 0; i < sim.length; i++) {
sumDif += (sim[i] - obs[i]);
sumObs += obs[i];
}
return (sumDif / sumObs) * 100;
}
/**
*
* @param prediction
* @param validation
* @return
*/
public static double rmse(double[] obs, double[] sim) {
sameArrayLen(obs, sim);
double error = 0;
for (int i = 0; i < sim.length; i++) {
error += Math.pow((sim[i] - obs[i]), 2);
}
return Math.sqrt(error / sim.length);
}
/**
*
* @param validation
* @param prediction
* @param missVal
* @return
*/
public static double absDiffLog(double[] obs, double[] sim) {
sameArrayLen(obs, sim);
int N = obs.length;
double abs = 0;
for (int i = 0; i < N; i++) {
double measured = obs[i];
double simulated = sim[i];
if (measured == 0) {
measured = 0.0000001;
} else if (measured < 0) {
throw new RuntimeException("Error on Absolute Difference (log): Observed value is negative.");
}
if (simulated == 0) {
simulated = 0.0000001;
} else if (simulated < 0) {
throw new RuntimeException("Error on Absolute Difference (log): Simulated value is negative.");
}
abs += Math.abs(Math.log(measured) - Math.log(simulated));
}
return abs;
}
/**
*
* @param validation
* @param prediction
* @param missVal
* @return
*/
public static double absDiff(double[] obs, double[] sim) {
sameArrayLen(obs, sim);
int N = obs.length;
double abs = 0;
for (int i = 0; i < N; i++) {
double measured = obs[i];
if (measured == 0) {
measured = 0.0000001;
}
abs += Math.abs((measured - sim[i]) / measured);
}
return abs;
}
/**
*
* @param validation
* @param prediction
* @param missVal
* @return
*/
public static double pearsonsCorrelatrion(double[] obs, double[] sim) {
sameArrayLen(obs, sim);
double syy = 0.0, sxy = 0.0, sxx = 0.0, ay = 0.0, ax = 0.0;
int n = 0;
for (int j = 0; j < obs.length; j++) {
ax += obs[j];
ay += sim[j];
n++;
}
if (n == 0) {
throw new RuntimeException("Pearson's Correlation cannot be calculated due to no observed values");
}
ax = ax / ((double) n);
ay = ay / ((double) n);
for (int j = 0; j < obs.length; j++) {
double xt = obs[j] - ax;
double yt = sim[j] - ay;
sxx += xt * xt;
syy += yt * yt;
sxy += xt * yt;
}
return sxy / Math.sqrt(sxx * syy);
}
/**
* transformedRootMeanSquareError TRMSE
*
* @param obs
* @param sim
* @return
*/
public static double transformedRmse(double[] obs, double[] sim) {
sameArrayLen(sim, obs);
double error = 0;
double z_pred = 0.;
double z_val = 0.;
for (int i = 0; i < sim.length; i++) {
z_pred = (Math.pow((1.0 + sim[i]), 0.3) - 1.0) / 0.3;
z_val = (Math.pow((1.0 + obs[i]), 0.3) - 1.0) / 0.3;
error += (z_pred - z_val) * (z_pred - z_val);
}
return Math.sqrt(error / sim.length);
}
/** Runoff coefficient error ROCE
*
* @param obs
* @param sim
* @param precip
* @return
*/
public static double runoffCoefficientError(double[] obs, double[] sim, double[] precip) {
sameArrayLen(sim, obs, precip);
double mean_pred = Stats.mean(sim);
double mean_val = Stats.mean(obs);
double mean_ppt = Stats.mean(precip);
double error = Math.abs((mean_pred / mean_ppt) - (mean_val / mean_ppt));
return Math.sqrt(error);
}
}