-
Notifications
You must be signed in to change notification settings - Fork 0
/
diag.trajectory.drifters.colloc.discrete.triple.paired.jl
542 lines (496 loc) · 31.8 KB
/
diag.trajectory.drifters.colloc.discrete.triple.paired.jl
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#=
= Perform a pair of analysis calibration and performance estimates, followed by
= recalibration using the OLS slope and intercept from the opposing collocations,
= followed by another calibration and performance estimate. The important issue
= of data outlier detection and removal is treated using the DetMCD algorithm in
= R. For morphing experiments, the linearity of the bijective mapping into and
= out of Gaussian-anamorphosis space is also checked (given that the in situ slope
= and intercept remain the reference) - RD June, July, October, November 2016.
=#
using My, Morph, RCall ; R"library(DetMCD)"
const ODAT = 1 # identify indecies of the input data:
const OLAT = 2 # date/lat/lon/obs on the collocation grid
const OLON = 3
const OCUR = 4
const MISS = -9999.0 # generic missing value
const MORPH = false # perform Gaussian anamorphosis
const EXTRA = true # recalibrate the extrapolated data using extra collocations
const MCDTRIM = 0.95 # Minimum Covariance Determinant trimming (nonoutlier percent)
const SDTRIM = 6.0 # standard deviation trimming limit
if (argc = length(ARGS)) != 2
print("\nUsage: jjj $(basename(@__FILE__)) buoydata_1993_2014_drogON.asc.nonmdt.locate_2.0_calib.ucur.got2000_obs.comb v2.0_global_025_deg_total_15m\n\n")
exit(1)
end
shift = -1
ARGS[2] == "v2.0_global_025_deg_ekman_15m" && (shift = 0)
ARGS[2] == "v2.0_global_025_deg_ekman_hs" && (shift = 3)
ARGS[2] == "v2.0_global_025_deg_geostrophic" && (shift = 6)
ARGS[2] == "v2.0_global_025_deg_total_15m" && (shift = 9)
ARGS[2] == "v2.0_global_025_deg_total_hs" && (shift = 12)
if shift > -1
const TOTB = 5 + shift # identify the three analysis indecies
const TOTN = 6 + shift
const TOTA = 7 + shift
else
print("\nERROR: $(ARGS[2]) is not recognized\n\n")
exit(-1)
end
#=
= Function returning triple collocation cal/val measures for a group of analyses, following McColl
= et al. (2014). Inputs are an array of collocated values and stats are returned for a collocation
= set, where it is assumed that extrapolation from before and after is done using the same analysis,
= so no consideration of relative effective resolution is necessary (cf. Vogelzang et al. 2011)
=#
function triple(curr::Array{Float64,3})
#=mask = masquextreme(curr[1,:,2], SDTRIM) & # get the parametric center of mass
masquextreme(curr[1,:,1], SDTRIM) & # after trimming extreme values first
masquextreme(curr[2,:,1], SDTRIM)
mass = mean(curr[2,mask,2])
sampsitu = curr[1,mask,2]
samprefa = curr[1,mask,1]
samprefb = curr[2,mask,1]
# @show length(mask) length(mask[mask])
# mass = mean(curr[2,:,2])
# sampsitu = curr[1,:,2] - mass
# samprefa = curr[1,:,1] - mass
# samprefb = curr[2,:,1] - mass
avg1 = mean(sampsitu) # and use a robust calculation of covariance
avg2 = mean(samprefa) # (two-pass here, but more algorithms are at
avg3 = mean(samprefb) # en.wikipedia.org/wiki/Algorithms_for_calculating_variance)
cv11 = mean((sampsitu - avg1) .* (sampsitu - avg1))
cv12 = mean((sampsitu - avg1) .* (samprefa - avg2))
cv13 = mean((sampsitu - avg1) .* (samprefb - avg3))
cv22 = mean((samprefa - avg2) .* (samprefa - avg2))
cv23 = mean((samprefa - avg2) .* (samprefb - avg3))
cv33 = mean((samprefb - avg3) .* (samprefb - avg3))
=#
mass = mean(curr[2,:,2])
curr[1,:,2] -= mass
curr[1,:,1] -= mass
curr[2,:,1] -= mass
temp = [curr[1,:,2]' curr[1,:,1]' curr[2,:,1]']
remp = rcopy(R"DetMCD($temp, alpha = $MCDTRIM)")
mask = falses(length(temp[:,1])) ; for a in remp[:Hsubsets] mask[a] = true end
# mass = mean(curr[2,mask,2])
avg1 = remp[:center][1]
avg2 = remp[:center][2]
avg3 = remp[:center][3]
cv11 = remp[:cov][1,1]
cv12 = remp[:cov][1,2]
cv13 = remp[:cov][1,3]
cv22 = remp[:cov][2,2]
cv23 = remp[:cov][2,3]
cv33 = remp[:cov][3,3]
bet2 = cv23 / cv13
bet3 = cv23 / cv12
alp2 = avg2 - bet2 * avg1 + mass * (1.0 - bet2)
alp3 = avg3 - bet3 * avg1 + mass * (1.0 - bet3)
tmpval = cv11 - cv12 * cv13 / cv23 ; sig1 = tmpval > 0 ? sqrt(tmpval) : 0.0
tmpval = cv22 - cv12 * cv23 / cv13 ; sig2 = tmpval > 0 ? sqrt(tmpval) : 0.0
tmpval = cv33 - cv13 * cv23 / cv12 ; sig3 = tmpval > 0 ? sqrt(tmpval) : 0.0
tmpval = cv12 * cv13 / cv11 / cv23 ; cor1 = tmpval > 0 ? sqrt(tmpval) : 0.0
tmpval = cv12 * cv23 / cv22 / cv13 ; cor2 = tmpval > 0 ? sqrt(tmpval) : 0.0
tmpval = cv13 * cv23 / cv33 / cv12 ; cor3 = tmpval > 0 ? sqrt(tmpval) : 0.0
return(mass, sig1, cor1, alp2, bet2, sig2, cor2, alp3, bet3, sig3, cor3) # then return all statistics
end
#=
= main program
=#
const MEMO = 1 # center-of-mass parameter
const MEMB = 2 # error model x = ALPH + BETA * truth + error
const MEMA = 3 # error model x = ALPH + BETA * truth + error
const MEMS = 3 # number of triple collocation members
const MASS = 1 # center-of-mass parameter (again...)
const ALPH = 2 # error model x = ALPH + BETA * truth + error
const BETA = 3 # error model x = ALPH + BETA * truth + error
const SIGM = 4 # triple coll RMSE
const CORR = 5 # triple coll correlation coefficient
const PARS = 5 # number of triple collocation parameters
const CUTOFF = 170.0 # gain cutoff
ARGS222 = replace(ARGS[1], "calib", "valid") # for both cal and val, if necessary,
if contains(ARGS[1], "wcur") # substitute speed for current component
fila = replace(ARGS[1], "wcur", "ucur")
filc = replace(ARGS[1], "wcur", "vcur")
filb = replace(ARGS222, "wcur", "ucur")
fild = replace(ARGS222, "wcur", "vcur")
fpa = My.ouvre(fila, "r") ; linea = readlines(fpa) ; close(fpa) ; linuma = length(linea)
fpc = My.ouvre(filc, "r") ; linec = readlines(fpc) ; close(fpc) ; linumc = length(linec)
fpb = My.ouvre(filb, "r") ; lineb = readlines(fpb) ; close(fpb) ; linumb = length(lineb)
fpd = My.ouvre(fild, "r") ; lined = readlines(fpd) ; close(fpd) ; linumd = length(lined)
(linuma == linumc) && (linumb == linumd) || (print("\nERROR: $linuma != $linumc || $linumb != $linumd\n\n") ; exit(-1))
for a = 1:linuma
vala = float(split(linea[a]))
valc = float(split(linec[a]))
out = linea[a][1:30]
for b = 4:19
valz = (vala[b]^2 + valc[b]^2)^0.5
out *= @sprintf(" %9.3f", valz)
end
linea[a] = out
end
for a = 1:linumb
valb = float(split(lineb[a]))
vald = float(split(lined[a]))
out = lineb[a][1:30]
for b = 4:19
valz = (valb[b]^2 + vald[b]^2)^0.5
out *= @sprintf(" %9.3f", valz)
end
lineb[a] = out
end
else
fpa = My.ouvre(ARGS[1], "r") ; linea = readlines(fpa) ; close(fpa) # read both sets of collocations and allocate
fpb = My.ouvre(ARGS222, "r") ; lineb = readlines(fpb) ; close(fpb) # the triple collocation input/output arrays
linuma = length(linea)
linumb = length(lineb)
end
if EXTRA
fname = replace(ARGS[1], "calib", "extra") * "." * ARGS[2] * ".extra.reg" # also read the regression coefficient pairs
fpa = My.ouvre(fname, "r") ; line = readline(fpa) ; close(fpa) # for calibrating the extrapolations relative
(intb, slob, inta, sloa) = float(split(line)) # to the extra collocation target (TOTN)
# intz = 0.5 * (intb + inta) ; intb = inta = intz
# sloz = 0.5 * (slob + sloa) ; slob = sloa = sloz
end
refa = Array(Float64, linuma) # and calculate a pair of reference variables
refb = Array(Float64, linumb) # (either from observations or from analyses)
for a = 1:linuma
vala = float(split(linea[a]))
EXTRA && (vala[TOTB] = (vala[TOTB] - intb) / slob ;
vala[TOTA] = (vala[TOTA] - inta) / sloa)
# refa[a] = vala[OCUR]
# refa[a] = vala[TOTN]
# refa[a] = 0.9 * vala[OCUR] + 0.1 * vala[TOTN]
refa[a] = 0.5 * (vala[OCUR] + vala[TOTN])
end
for a = 1:linumb
vala = float(split(lineb[a]))
EXTRA && (vala[TOTB] = (vala[TOTB] - intb) / slob ;
vala[TOTA] = (vala[TOTA] - inta) / sloa)
# refb[a] = vala[OCUR]
# refb[a] = vala[TOTN]
# refb[a] = 0.9 * vala[OCUR] + 0.1 * vala[TOTN]
refb[a] = 0.5 * (vala[OCUR] + vala[TOTN])
end
statis = [MISS for a = 1:4, b = 1:MEMS, c = 1:PARS] # allocate a set of global cal/val arrays
allmas = [MISS for a = 1:4]
allalp = [MISS for a = 1:4]
allbet = [MISS for a = 1:4]
allsig = [MISS for a = 1:4]
allcor = [MISS for a = 1:4]
cura = zeros(2, linuma, 2)
curb = zeros(2, linumb, 2)
if MORPH # define for both collocation sets a mapping
tmpa = Array(Float64, linuma) # (Gaussian anamorphosis) based on the in situ
tmpb = Array(Float64, linumb) # observations (which are taken to be unbiased)
for a = 1:linuma vals = float(split(linea[a])) ; tmpa[a] = vals[OCUR] end # and apply each map to the opposite set
for a = 1:linumb vals = float(split(lineb[a])) ; tmpb[a] = vals[OCUR] end
shfa = minimum(tmpa) < 0 ? -2.0 * minimum(tmpa) : 0.0
shfb = minimum(tmpb) < 0 ? -2.0 * minimum(tmpb) : 0.0
rawa = sort(tmpa) + shfa
rawb = sort(tmpb) + shfb
R"library(RGeostats)"
R"dba = db.create(aa = c($rawa))"
R"fia = anam.fit(dba, name='aa', type='emp')"
R"ana <- anam.z2y(fia, dba, 'aa')"
cdfa = unique(rcopy(R"db.extract(ana, names = c('Gaussian.aa'))"))
R"rm(dba, fia, ana)"
rawa = unique(rawa - shfa)
if length(rawa) != length(cdfa)
print("\nERROR: length(rawa) $(length(rawa)) != $(length(cdfa)) length(cdfa)\n\n")
exit(1)
end
R"dbb = db.create(bb = c($rawb))"
R"fib = anam.fit(dbb, name='bb', type='emp')"
R"anb <- anam.z2y(fib, dbb, 'bb')"
cdfb = unique(rcopy(R"db.extract(anb, names = c('Gaussian.bb'))"))
R"rm(dbb, fib, anb)"
rawb = unique(rawb - shfb)
if length(rawb) != length(cdfb)
print("\nERROR: length(rawb) $(length(rawb)) != $(length(cdfb)) length(cdfb)\n\n")
exit(1)
end # finally extend and equate the tails
rawa[ 1] > rawb[ 1] && (rawa[ 1] = rawb[ 1]) ; rawb[ 1] > rawa[ 1] && (rawb[ 1] = rawa[ 1])
rawa[end] < rawb[end] && (rawa[end] = rawb[end]) ; rawb[end] < rawa[end] && (rawb[end] = rawa[end])
cdfa[ 1] > cdfb[ 1] && (cdfa[ 1] = cdfb[ 1]) ; cdfb[ 1] > cdfa[ 1] && (cdfb[ 1] = cdfa[ 1])
cdfa[end] < cdfb[end] && (cdfa[end] = cdfb[end]) ; cdfb[end] < cdfa[end] && (cdfb[end] = cdfa[end])
cdfa[ 1] > -9 && (cdfa[ 1] = -9) ; cdfb[ 1] > -9 && (cdfb[ 1] = -9)
cdfa[end] < 9 && (cdfa[end] = 9) ; cdfb[end] < 9 && (cdfb[end] = 9)
end
for a = 1:linuma # report cal/val parameters for the first set
vals = float(split(linea[a])) # (in original units regardless of morphing)
EXTRA && (vals[TOTB] = (vals[TOTB] - intb) / slob ;
vals[TOTA] = (vals[TOTA] - inta) / sloa)
cura[1,a,:] = [vals[TOTB] vals[OCUR]]
cura[2,a,:] = [vals[TOTA] refa[a] ]
end
a = 1 ; (mass, sig1, cor1, alp2, bet2, sig2, cor2, alp3, bet3, sig3, cor3) = triple(cura)
statis[a,MEMO,MASS] = statis[a,MEMB,MASS] = statis[a,MEMA,MASS] = allmas[a] = mass
statis[a,MEMO,ALPH] = 0.0 ; statis[a,MEMB,ALPH] = alp2 ; statis[a,MEMA,ALPH] = alp3 ; allalp[a] = 0.5 * (alp2 + alp3)
statis[a,MEMO,BETA] = 1.0 ; statis[a,MEMB,BETA] = bet2 ; statis[a,MEMA,BETA] = bet3 ; allbet[a] = 0.5 * (bet2 + bet3)
statis[a,MEMO,SIGM] = sig1 ; statis[a,MEMB,SIGM] = sig2 ; statis[a,MEMA,SIGM] = sig3 ; allsig[a] = 0.5 * (sig2 + sig3)
statis[a,MEMO,CORR] = cor1 ; statis[a,MEMB,CORR] = cor2 ; statis[a,MEMA,CORR] = cor3 ; allcor[a] = 0.5 * (cor2 + cor3)
#a = 1 ; (allmas[a], allalp[a], allbet[a], allsig[a], allcor[a]) = triple(cura)
@printf("\nnumb = %15d for %s\n", linuma, ARGS[1])
@printf("cala = %15.8f mean(vals[TOTB]) = %15.8f\n", allalp[a], mean(cura[1,:,1]))
@printf("calb = %15.8f mean(vals[TOTA]) = %15.8f\n", allbet[a], mean(cura[2,:,1]))
@printf("mean = %15.8f mean(vals[OCUR]) = %15.8f\n", mean(allmas[a]), mean(cura[1,:,2]))
@printf("%33s %8s %8s %8s %8s\n", " ", "allalp", "allbet", "allsig", "allcor")
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", allalp[a], allbet[a], allsig[a], allcor[a])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMO,ALPH], statis[a,MEMO,BETA], statis[a,MEMO,SIGM], statis[a,MEMO,CORR])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMB,ALPH], statis[a,MEMB,BETA], statis[a,MEMB,SIGM], statis[a,MEMB,CORR])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMA,ALPH], statis[a,MEMA,BETA], statis[a,MEMA,SIGM], statis[a,MEMA,CORR])
for a = 1:linumb # report cal/val parameters for the second set
vals = float(split(lineb[a])) # (in original units regardless of morphing)
EXTRA && (vals[TOTB] = (vals[TOTB] - intb) / slob ;
vals[TOTA] = (vals[TOTA] - inta) / sloa)
curb[1,a,:] = [vals[TOTB] vals[OCUR]]
curb[2,a,:] = [vals[TOTA] refb[a] ]
end
a = 2 ; (mass, sig1, cor1, alp2, bet2, sig2, cor2, alp3, bet3, sig3, cor3) = triple(curb)
statis[a,MEMO,MASS] = statis[a,MEMB,MASS] = statis[a,MEMA,MASS] = allmas[a] = mass
statis[a,MEMO,ALPH] = 0.0 ; statis[a,MEMB,ALPH] = alp2 ; statis[a,MEMA,ALPH] = alp3 ; allalp[a] = 0.5 * (alp2 + alp3)
statis[a,MEMO,BETA] = 1.0 ; statis[a,MEMB,BETA] = bet2 ; statis[a,MEMA,BETA] = bet3 ; allbet[a] = 0.5 * (bet2 + bet3)
statis[a,MEMO,SIGM] = sig1 ; statis[a,MEMB,SIGM] = sig2 ; statis[a,MEMA,SIGM] = sig3 ; allsig[a] = 0.5 * (sig2 + sig3)
statis[a,MEMO,CORR] = cor1 ; statis[a,MEMB,CORR] = cor2 ; statis[a,MEMA,CORR] = cor3 ; allcor[a] = 0.5 * (cor2 + cor3)
#a = 2 ; (allmas[a], allalp[a], allbet[a], allsig[a], allcor[a]) = triple(curb)
@printf("\nnumb = %15d for %s\n", linumb, ARGS222)
@printf("cala = %15.8f mean(vals[TOTB]) = %15.8f\n", allalp[a], mean(curb[1,:,1]))
@printf("calb = %15.8f mean(vals[TOTA]) = %15.8f\n", allbet[a], mean(curb[2,:,1]))
@printf("mean = %15.8f mean(vals[OCUR]) = %15.8f\n", mean(allmas[a]), mean(curb[1,:,2]))
@printf("%33s %8s %8s %8s %8s\n", " ", "allalp", "allbet", "allsig", "allcor")
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", allalp[a], allbet[a], allsig[a], allcor[a])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMO,ALPH], statis[a,MEMO,BETA], statis[a,MEMO,SIGM], statis[a,MEMO,CORR])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMB,ALPH], statis[a,MEMB,BETA], statis[a,MEMB,SIGM], statis[a,MEMB,CORR])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMA,ALPH], statis[a,MEMA,BETA], statis[a,MEMA,SIGM], statis[a,MEMA,CORR])
if MORPH # save linear mapping metrics
rawavga11 = mean(cura[1,:,1]) ; rawavga21 = mean(cura[2,:,1]) ; rawavga22 = mean(cura[2,:,2])
rawvara11 = var(cura[1,:,1]) ; rawvara21 = var(cura[2,:,1]) ; rawvara22 = var(cura[2,:,2])
rawavgb11 = mean(curb[1,:,1]) ; rawavgb21 = mean(curb[2,:,1]) ; rawavgb22 = mean(curb[2,:,2])
rawvarb11 = var(curb[1,:,1]) ; rawvarb21 = var(curb[2,:,1]) ; rawvarb22 = var(curb[2,:,2])
end
MORPH && (fpb = My.ouvre(ARGS[1] * "." * ARGS[2] * ".cali.pair.morph", "w"))
MORPH || (fpb = My.ouvre(ARGS[1] * "." * ARGS[2] * ".cali.pair", "w"))
form = @sprintf(" mean param MASS is %6.2f\n", mean(allmas[1]))
write(fpb, form)
form = @sprintf(" mean param MASS is %6.2f\n", mean(allmas[2]))
write(fpb, form)
form = @sprintf("%77s %8s %8s %8s %8s\n", " ", "allalp", "allbet", "allsig", "allcor")
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", ARGS[1] * "." * ARGS[2], allalp[1], allbet[1], allsig[1], allcor[1])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", ARGS222 * "." * ARGS[2], allalp[2], allbet[2], allsig[2], allcor[2])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "obs", statis[1,MEMO,ALPH], statis[1,MEMO,BETA], statis[1,MEMO,SIGM], statis[1,MEMO,CORR])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "obs", statis[2,MEMO,ALPH], statis[2,MEMO,BETA], statis[2,MEMO,SIGM], statis[2,MEMO,CORR])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "bef", statis[1,MEMB,ALPH], statis[1,MEMB,BETA], statis[1,MEMB,SIGM], statis[1,MEMB,CORR])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "bef", statis[2,MEMB,ALPH], statis[2,MEMB,BETA], statis[2,MEMB,SIGM], statis[2,MEMB,CORR])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "aft", statis[1,MEMA,ALPH], statis[1,MEMA,BETA], statis[1,MEMA,SIGM], statis[1,MEMA,CORR])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "aft", statis[2,MEMA,ALPH], statis[2,MEMA,BETA], statis[2,MEMA,SIGM], statis[2,MEMA,CORR])
write(fpb, form)
close(fpb)
fpb = My.ouvre(ARGS[1] * "." * ARGS[2] * ".recalibrate", "w") # and save these bias corrections
@printf(fpb, "%33.11f %33.11f %33.11f %33.11f\n",
allalp[1], allbet[1], allalp[2], allbet[2])
close(fpb)
if MORPH
for a = 1:linuma # compute cal/val parameters for the first set
vals = float(split(linea[a])) # (in Gaussian units if morphing)
EXTRA && (vals[TOTB] = (vals[TOTB] - intb) / slob ;
vals[TOTA] = (vals[TOTA] - inta) / sloa)
MORPH && (vals[TOTB] = rawtonorm(cdfb, rawb, vals[TOTB]) ;
vals[TOTA] = rawtonorm(cdfb, rawb, vals[TOTA]) ;
vals[OCUR] = rawtonorm(cdfb, rawb, vals[OCUR]) ;
refa[a] = rawtonorm(cdfb, rawb, refa[a]))
cura[1,a,:] = [vals[TOTB] vals[OCUR]]
cura[2,a,:] = [vals[TOTA] refa[a] ]
end
a = 1 ; (mass, sig1, cor1, alp2, bet2, sig2, cor2, alp3, bet3, sig3, cor3) = triple(cura)
statis[a,MEMO,MASS] = statis[a,MEMB,MASS] = statis[a,MEMA,MASS] = allmas[a] = mass
statis[a,MEMO,ALPH] = 0.0 ; statis[a,MEMB,ALPH] = alp2 ; statis[a,MEMA,ALPH] = alp3 ; allalp[a] = 0.5 * (alp2 + alp3)
statis[a,MEMO,BETA] = 1.0 ; statis[a,MEMB,BETA] = bet2 ; statis[a,MEMA,BETA] = bet3 ; allbet[a] = 0.5 * (bet2 + bet3)
statis[a,MEMO,SIGM] = sig1 ; statis[a,MEMB,SIGM] = sig2 ; statis[a,MEMA,SIGM] = sig3 ; allsig[a] = 0.5 * (sig2 + sig3)
statis[a,MEMO,CORR] = cor1 ; statis[a,MEMB,CORR] = cor2 ; statis[a,MEMA,CORR] = cor3 ; allcor[a] = 0.5 * (cor2 + cor3)
# a = 1 ; (allmas[a], allalp[a], allbet[a], allsig[a], allcor[a]) = triple(cura)
for a = 1:linumb # compute cal/val parameters for the second set
vals = float(split(lineb[a])) # (in Gaussian units if morphing)
EXTRA && (vals[TOTB] = (vals[TOTB] - intb) / slob ;
vals[TOTA] = (vals[TOTA] - inta) / sloa)
MORPH && (vals[TOTB] = rawtonorm(cdfa, rawa, vals[TOTB]) ;
vals[TOTA] = rawtonorm(cdfa, rawa, vals[TOTA]) ;
vals[OCUR] = rawtonorm(cdfa, rawa, vals[OCUR]) ;
refb[a] = rawtonorm(cdfb, rawb, refb[a]))
curb[1,a,:] = [vals[TOTB] vals[OCUR]]
curb[2,a,:] = [vals[TOTA] refb[a] ]
end
a = 2 ; (mass, sig1, cor1, alp2, bet2, sig2, cor2, alp3, bet3, sig3, cor3) = triple(curb)
statis[a,MEMO,MASS] = statis[a,MEMB,MASS] = statis[a,MEMA,MASS] = allmas[a] = mass
statis[a,MEMO,ALPH] = 0.0 ; statis[a,MEMB,ALPH] = alp2 ; statis[a,MEMA,ALPH] = alp3 ; allalp[a] = 0.5 * (alp2 + alp3)
statis[a,MEMO,BETA] = 1.0 ; statis[a,MEMB,BETA] = bet2 ; statis[a,MEMA,BETA] = bet3 ; allbet[a] = 0.5 * (bet2 + bet3)
statis[a,MEMO,SIGM] = sig1 ; statis[a,MEMB,SIGM] = sig2 ; statis[a,MEMA,SIGM] = sig3 ; allsig[a] = 0.5 * (sig2 + sig3)
statis[a,MEMO,CORR] = cor1 ; statis[a,MEMB,CORR] = cor2 ; statis[a,MEMA,CORR] = cor3 ; allcor[a] = 0.5 * (cor2 + cor3)
# a = 2 ; (allmas[a], allalp[a], allbet[a], allsig[a], allcor[a]) = triple(curb)
# save linear mapping metrics
gauavga11 = mean(cura[1,:,1]) ; gauavga21 = mean(cura[2,:,1]) ; gauavga22 = mean(cura[2,:,2])
gauvara11 = var(cura[1,:,1]) ; gauvara21 = var(cura[2,:,1]) ; gauvara22 = var(cura[2,:,2])
gauavgb11 = mean(curb[1,:,1]) ; gauavgb21 = mean(curb[2,:,1]) ; gauavgb22 = mean(curb[2,:,2])
gauvarb11 = var(curb[1,:,1]) ; gauvarb21 = var(curb[2,:,1]) ; gauvarb22 = var(curb[2,:,2])
end
for a = 1:linuma # apply recalibration either in Gaussian or original units
vals = float(split(linea[a])) # using calibration parameters (and anamorphosis) from the
EXTRA && (vals[TOTB] = (vals[TOTB] - intb) / slob ; # other set and report new cal/val parameters in original
vals[TOTA] = (vals[TOTA] - inta) / sloa) # units
MORPH && (vals[TOTB] = rawtonorm(cdfb, rawb, vals[TOTB]) ;
vals[TOTA] = rawtonorm(cdfb, rawb, vals[TOTA]))
vals[TOTB] = (vals[TOTB] - allalp[2]) / allbet[2]
vals[TOTA] = (vals[TOTA] - allalp[2]) / allbet[2]
# vals[TOTB] = (vals[TOTB] - statis[2,MEMB,ALPH]) / statis[2,MEMB,ALPH]
# vals[TOTA] = (vals[TOTA] - statis[2,MEMA,ALPH]) / statis[2,MEMA,BETA]
MORPH && (vals[TOTB] = normtoraw(cdfb, rawb, vals[TOTB]) ;
vals[TOTA] = normtoraw(cdfb, rawb, vals[TOTA]))
cura[1,a,:] = [vals[TOTB] vals[OCUR]]
cura[2,a,:] = [vals[TOTA] refa[a] ]
end
a = 3 ; (mass, sig1, cor1, alp2, bet2, sig2, cor2, alp3, bet3, sig3, cor3) = triple(cura)
statis[a,MEMO,MASS] = statis[a,MEMB,MASS] = statis[a,MEMA,MASS] = allmas[a] = mass
statis[a,MEMO,ALPH] = 0.0 ; statis[a,MEMB,ALPH] = alp2 ; statis[a,MEMA,ALPH] = alp3 ; allalp[a] = 0.5 * (alp2 + alp3)
statis[a,MEMO,BETA] = 1.0 ; statis[a,MEMB,BETA] = bet2 ; statis[a,MEMA,BETA] = bet3 ; allbet[a] = 0.5 * (bet2 + bet3)
statis[a,MEMO,SIGM] = sig1 ; statis[a,MEMB,SIGM] = sig2 ; statis[a,MEMA,SIGM] = sig3 ; allsig[a] = 0.5 * (sig2 + sig3)
statis[a,MEMO,CORR] = cor1 ; statis[a,MEMB,CORR] = cor2 ; statis[a,MEMA,CORR] = cor3 ; allcor[a] = 0.5 * (cor2 + cor3)
#a = 3 ; (allmas[a], allalp[a], allbet[a], allsig[a], allcor[a]) = triple(cura)
tmpstr = "after recalibration only (using alpha and beta from the opposite set of collocations)"
MORPH && (tmpstr = "after applying Gaussian anamorphosis, recalibration, and inverse Gaussian anamorphosis")
@printf("\nnumb = %15.0f for %s\n", linuma, ARGS[1])
@printf("cala = %15.8f %s\n", allalp[a], tmpstr)
@printf("calb = %15.8f %s\n", allbet[a], tmpstr)
@printf("mean = %15.8f %s\n", mean(allmas[a]), tmpstr)
@printf("%33s %8s %8s %8s %8s\n", " ", "allalp", "allbet", "allsig", "allcor")
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", allalp[a], allbet[a], allsig[a], allcor[a])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMO,ALPH], statis[a,MEMO,BETA], statis[a,MEMO,SIGM], statis[a,MEMO,CORR])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMB,ALPH], statis[a,MEMB,BETA], statis[a,MEMB,SIGM], statis[a,MEMB,CORR])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMA,ALPH], statis[a,MEMA,BETA], statis[a,MEMA,SIGM], statis[a,MEMA,CORR])
for a = 1:linumb # apply recalibration either in Gaussian or original units
vals = float(split(lineb[a])) # using calibration parameters (and anamorphosis) from the
EXTRA && (vals[TOTB] = (vals[TOTB] - intb) / slob ; # other set and report new cal/val parameters in original
vals[TOTA] = (vals[TOTA] - inta) / sloa) # units
MORPH && (vals[TOTB] = rawtonorm(cdfa, rawa, vals[TOTB]) ;
vals[TOTA] = rawtonorm(cdfa, rawa, vals[TOTA]))
vals[TOTB] = (vals[TOTB] - allalp[1]) / allbet[1]
vals[TOTA] = (vals[TOTA] - allalp[1]) / allbet[1]
# vals[TOTB] = (vals[TOTB] - statis[1,MEMB,ALPH]) / statis[1,MEMB,ALPH]
# vals[TOTA] = (vals[TOTA] - statis[1,MEMA,ALPH]) / statis[1,MEMA,BETA]
MORPH && (vals[TOTB] = normtoraw(cdfa, rawa, vals[TOTB]) ;
vals[TOTA] = normtoraw(cdfa, rawa, vals[TOTA]))
curb[1,a,:] = [vals[TOTB] vals[OCUR]]
curb[2,a,:] = [vals[TOTA] refb[a] ]
end
a = 4 ; (mass, sig1, cor1, alp2, bet2, sig2, cor2, alp3, bet3, sig3, cor3) = triple(curb)
statis[a,MEMO,MASS] = statis[a,MEMB,MASS] = statis[a,MEMA,MASS] = allmas[a] = mass
statis[a,MEMO,ALPH] = 0.0 ; statis[a,MEMB,ALPH] = alp2 ; statis[a,MEMA,ALPH] = alp3 ; allalp[a] = 0.5 * (alp2 + alp3)
statis[a,MEMO,BETA] = 1.0 ; statis[a,MEMB,BETA] = bet2 ; statis[a,MEMA,BETA] = bet3 ; allbet[a] = 0.5 * (bet2 + bet3)
statis[a,MEMO,SIGM] = sig1 ; statis[a,MEMB,SIGM] = sig2 ; statis[a,MEMA,SIGM] = sig3 ; allsig[a] = 0.5 * (sig2 + sig3)
statis[a,MEMO,CORR] = cor1 ; statis[a,MEMB,CORR] = cor2 ; statis[a,MEMA,CORR] = cor3 ; allcor[a] = 0.5 * (cor2 + cor3)
#a = 4 ; (allmas[a], allalp[a], allbet[a], allsig[a], allcor[a]) = triple(curb)
@printf("\nnumb = %15.0f for %s\n", linumb, ARGS222)
@printf("cala = %15.8f %s\n", allalp[a], tmpstr)
@printf("calb = %15.8f %s\n", allbet[a], tmpstr)
@printf("mean = %15.8f %s\n", mean(allmas[a]), tmpstr)
@printf("%33s %8s %8s %8s %8s\n", " ", "allalp", "allbet", "allsig", "allcor")
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", allalp[a], allbet[a], allsig[a], allcor[a])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMO,ALPH], statis[a,MEMO,BETA], statis[a,MEMO,SIGM], statis[a,MEMO,CORR])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMB,ALPH], statis[a,MEMB,BETA], statis[a,MEMB,SIGM], statis[a,MEMB,CORR])
@printf("%33s %8.4f %8.4f %8.4f %8.4f\n", " ", statis[a,MEMA,ALPH], statis[a,MEMA,BETA], statis[a,MEMA,SIGM], statis[a,MEMA,CORR])
if MORPH # get average linear mapping metrics (as
linrawa = (abs(rawavga11 - normtoraw(cdfb, rawb, gauavga11)) / rawvara11 + # mean difference normalized by variance)
abs(rawavga21 - normtoraw(cdfb, rawb, gauavga21)) / rawvara21 +
abs(rawavga22 - normtoraw(cdfb, rawb, gauavga22)) / rawvara22) * 100 / 3
linrawb = (abs(rawavgb11 - normtoraw(cdfb, rawb, gauavgb11)) / rawvarb11 +
abs(rawavgb21 - normtoraw(cdfb, rawb, gauavgb21)) / rawvarb21 +
abs(rawavgb22 - normtoraw(cdfb, rawb, gauavgb22)) / rawvarb22) * 100 / 3
lingaua = (abs(gauavga11 - rawtonorm(cdfb, rawb, rawavga11)) / gauvara11 +
abs(gauavga21 - rawtonorm(cdfb, rawb, rawavga21)) / gauvara21 +
abs(gauavga22 - rawtonorm(cdfb, rawb, rawavga22)) / gauvara22) * 100 / 3
lingaub = (abs(gauavgb11 - rawtonorm(cdfb, rawb, rawavgb11)) / gauvarb11 +
abs(gauavgb21 - rawtonorm(cdfb, rawb, rawavgb21)) / gauvarb21 +
abs(gauavgb22 - rawtonorm(cdfb, rawb, rawavgb22)) / gauvarb22) * 100 / 3
linstr = @sprintf("linrawa = %5.1f linrawb = %5.1f lingaua = %5.1f lingaub = %5.1f", linrawa, linrawb, lingaua, lingaub)
println("\n$linstr\n")
tmpstr *= " $linstr"
end
MORPH && (fpb = My.ouvre(ARGS[1] * "." * ARGS[2] * ".cali.pair.morph", "a"))
MORPH || (fpb = My.ouvre(ARGS[1] * "." * ARGS[2] * ".cali.pair", "a"))
form = @sprintf(" mean param MASS is %6.2f %s\n", mean(allmas[3]), tmpstr)
write(fpb, form)
form = @sprintf(" mean param MASS is %6.2f %s\n", mean(allmas[4]), tmpstr)
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", ARGS[1] * "." * ARGS[2], allalp[3], allbet[3], allsig[3], allcor[3])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", ARGS222 * "." * ARGS[2], allalp[4], allbet[4], allsig[4], allcor[4])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "obs", statis[3,MEMO,ALPH], statis[3,MEMO,BETA], statis[3,MEMO,SIGM], statis[3,MEMO,CORR])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "obs", statis[4,MEMO,ALPH], statis[4,MEMO,BETA], statis[4,MEMO,SIGM], statis[4,MEMO,CORR])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "bef", statis[3,MEMB,ALPH], statis[3,MEMB,BETA], statis[3,MEMB,SIGM], statis[3,MEMB,CORR])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "bef", statis[4,MEMB,ALPH], statis[4,MEMB,BETA], statis[4,MEMB,SIGM], statis[4,MEMB,CORR])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "aft", statis[3,MEMA,ALPH], statis[3,MEMA,BETA], statis[3,MEMA,SIGM], statis[3,MEMA,CORR])
write(fpb, form)
form = @sprintf("%77s %8.4f %8.4f %8.4f %8.4f\n", "aft", statis[4,MEMA,ALPH], statis[4,MEMA,BETA], statis[4,MEMA,SIGM], statis[4,MEMA,CORR])
write(fpb, form)
close(fpb)
exit(0)
#=
tmp = Winston.FramedPlot(title="Empirical anamorphosis", xlabel="Gaussian Current", ylabel="Actual Current")
ppp = Winston.add(tmp)
tmp = Winston.Curve(cdfa, rawa, "color", parse(Winston.Colorant, "black"))
Winston.add(ppp, tmp)
tmp = Winston.Curve(cdfb, rawb, "color", parse(Winston.Colorant, "red"))
Winston.add(ppp, tmp)
xyzzy = "gaussian_anamorphosis_test.png"
print("writing $xyzzy\n")
Winston.savefig(ppp, xyzzy, "width", 1700, "height", 1000)
function triple(curr::Array{Float64,3})
allalp = MISS
allbet = MISS
allsig = MISS
allcor = MISS
allmas = MISS
mask = masquextreme(curr[1, :,2], SDTRIM) & # get the parametric center of mass
masquextreme(curr[1, :,1], SDTRIM) & # after trimming extreme values first
masquextreme(curr[2, :,1], SDTRIM)
sampsitu = curr[1,mask,2]
samprefa = curr[1,mask,1]
samprefb = curr[2,mask,1]
allmas = mean(curr[2,mask,2])
avg1 = mean(sampsitu) # and use a robust calculation of covariance
avg2 = mean(samprefa) # (two-pass here, but more algorithms are at
avg3 = mean(samprefb) # en.wikipedia.org/wiki/Algorithms_for_calculating_variance)
cv11 = mean((sampsitu - avg1) .* (sampsitu - avg1))
cv12 = mean((sampsitu - avg1) .* (samprefa - avg2))
cv13 = mean((sampsitu - avg1) .* (samprefb - avg3))
cv22 = mean((samprefa - avg2) .* (samprefa - avg2))
cv23 = mean((samprefa - avg2) .* (samprefb - avg3))
cv33 = mean((samprefb - avg3) .* (samprefb - avg3))
bet2 = cv23 / cv13
bet3 = cv23 / cv12
alp2 = avg2 - bet2 * avg1
alp3 = avg3 - bet3 * avg1
tmpval = cv11 - cv12 * cv13 / cv23 ; sig1 = tmpval > 0 ? sqrt(tmpval) : 0.0
tmpval = cv22 - cv12 * cv23 / cv13 ; sig2 = tmpval > 0 ? sqrt(tmpval) : 0.0
tmpval = cv33 - cv13 * cv23 / cv12 ; sig3 = tmpval > 0 ? sqrt(tmpval) : 0.0
tmpval = cv12 * cv13 / cv11 / cv23 ; cor1 = tmpval > 0 ? sqrt(tmpval) : 0.0
tmpval = cv12 * cv23 / cv22 / cv13 ; cor2 = tmpval > 0 ? sqrt(tmpval) : 0.0
tmpval = cv13 * cv23 / cv33 / cv12 ; cor3 = tmpval > 0 ? sqrt(tmpval) : 0.0
allalp = 0.5 * (alp2 + alp3)
allbet = 0.5 * (bet2 + bet3)
allsig = 0.5 * (sig2 + sig3)
allcor = 0.5 * (cor2 + cor3)
return(allmas, allalp, allbet, allsig, allcor) # then return the average stats
end
=#