-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemafit.f
More file actions
3834 lines (3389 loc) · 132 KB
/
Copy pathemafit.f
File metadata and controls
3834 lines (3389 loc) · 132 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
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c****|subroutine emafit
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c REVISED VERSION FROM 2013
c
c this routine fits the pearson type iii distribution to
c a data set using the ema algorithm
c
c this was prepared by tim cohn, us geological survey, to support
c development of an operational version of peakfq (bulletin 17b
c implementation) which would include the expected moments
c algorithm (ema; cohn et al. 1997; 2001)
c
c *** do not modify without author''s consent ***
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c development history
c
c timothy a. cohn 09 nov 2003
c modified 31 dec 2003
c modified 27 mar 2004 (better small skew approx)
c modified 07 dec 2004 (typos in comments corrected)
c modified 07 feb 2007 (major rewrite; reparameterized
c ema in terms of sample moments
c rather than gamma parameters)
c modified 24 may 2007 changed default pqd values (TAC)
c corrected errors (John England)
c modified 05 jun 2007 added gbthrsh0 to subroutine call
c permits user to specify lower bound
c modified 21 jun 2007 corrected error in rGmse treatment wrt
c generalized skew
c modified 13 aug 2007 final low-outlier default procedure
c implemented
c modified 17 aug 2007 final, final LO default procedure
c implemented
c modified 25 sep 2007 final 'argh!' w/update to mseg
c to correct for censored data
c modified 28 feb 2008 computed moments for both B17B
c and EMA at-site MSE(skew)
c modified 18 jun 2008 added constraint to ensure skews
c within reliable range of computation
c modified 02 jul 2008 set default MSE for at-site skew to
c equal B17B MSE when only systematic
c data are present ('ADJE')
c modified 15 sep 2011 set default lskewXmax to .FALSE.
c this determines whether to
c adjust skew so that Qmax is inside
c support of fitted distribution
c (previously set to .TRUE.)
c B17B provides no test or correction
c modified 26 sep 2011 added regional estimate for M
c this is analogous to region S2, G
c modified 24 oct 2011 simplified calls to p3est; results same
c modified 08 feb 2012 added return for var_est in
c modified 10 oct 2012 added redefined computation method for
c mn2m_var using inverse modified
c Cholesky Gaussian Quadrature
c modified 14 feb 2013 added effective record length for skew
c returned in /tac005/...,as_G_ERL
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c NEW VERSION 05 jan 2011 employs
c 1) Multiple Grubbs-Beck Test
c 2) Regional standard deviation
c
c modified 08 apr 2011 fixed MGBT to deal with more than
c 50% zero flows
c
c NEW VERSION 05 Mar 2013 employs
c 1) Inverse Gaussian Quadrature CI
c modified 19 Mar 2014 nu_min increased to 5.d0 from 0.5d0
c
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c n.b. use of ema requires two distinct types of information:
c
c a. "observations," which are described by {ql,qu};
c specifically, the true value of q is assumed to lie
c wihin the interval ql(i) <= q(i) <= qu(i);
c if q(i) is known exactly, then ql(i) = q(i) = qu(i);
c
c {ql,qu} are required by ema to fit p3 distn to data
c
c b. "censoring pattern," which is described by {tl,tu}
c {tl,tu} define the interval on which data get observed:
c if q is not inside {tl,tu}, q is either left or right
c censored.
c
c examples:
c {-inf,+inf} => systematic data (exact value of q
c would be measured)
c {t, +inf} => historical data
c q>=t would be known exactly;
c q<t would be reported as censored ("less-than")
c
c {tl,tu} are required by ema to estimate confidence
c intervals; they are not required for frequency
c curve determination
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c input variables:
c ------------------------------------------------------------------------
c n i*4 number of observations (censored, uncensored, or
c other)
c ql(n) r*8 vector of lower bounds on (log) floods
c qu(n) r*8 vector of upper bounds on (log) floods
c NOTE: Zero flows enter as lmissing=-80.d0
c tl(n) r*8 vector of lower bounds on (log) flood threshold
c tu(n) r*8 vector of upper bounds on (log) flood threshold
c dtype(n) c*4 vector describing input data
c dtype(i) = "Syst" => systematic data
c dtype(i) = "Hist" => historic data
c dtype(i) = "Othr" => other
c reg_SD r*8 regional standard deviation
c reg_SD_mse r*8 mean square error of generalized standard deviation
c notes:
c 1) reg_SD_mse = 0 ("GENERALIZED STD DEV, NO ERROR")
c use fixed parms(*) = reg_SD w/ mse = 0.0
c 2) -999 < reg_SD_mse< 0 ("AT-SITE STD DEV")
c use at-site estimated standard deviation
c 3) 0 < reg_SD_mse < 1.d10 ("WEIGHTED STD. DEV.")
c use sd = weighted average of at-site and
c regional standard deviation
c r_G r*8 regional skew
c r_G_mse r*8 mean square error of regional skew
c this variable encodes four distinct cases:
c 1) r_G_mse = 0 ("GENERALIZED SKEW, NO ERROR")
c use fixed g = r_G w/ mse = 0.0
c 2) -98 < r_G_mse < 0 ("GENERALIZED SKEW, MSE > 0")
c use fixed g = w/ mse = -r_G_mse
c 3) 0 < r_G_mse < 1.d10 ("WEIGHTED SKEW")
c use g = weighted average of at-site and
c regional skew (b17b recommendation)
c 4) 1.d10 < r_G_mse ("STATION SKEW")
c use g = at-site skew
c gbtype c*4 type of Grubbs-Beck test
c = "GBT" => standard GB test
c = "MGBT" => Multiple GB test
c gbthrsh0 r*8 critical value for Grubbs-Beck test
c N.B. gbthrsh0 codes for 3 cases
c 1. gbthrsh0 <= -6 ==> Compute GB critical value
c 2. gbthrsh0 > -6 ==> gbthrsh0 as crit. val.
c 3. gbthrsh0 (small, e.g. -5.9) no low out. test
c weightOpt c*4 weighting option for regional skew. Choices are
c 1) "HWN" - Greg Schwarz's Halloween Method
c 2) "ERL" - Relative effective record length
c weighting (used by HEC-SSP 2.3)
c 3) "" - PeakFQ 7.4 weighting scheme as described
c in Bulletin 17C. Will default to this option
c for any string except "HWN" or "ERL"
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c output variables:
c ------------------------------------------------------------------------
c
c cmoms(3,3) r*8 first 3 central moments
c (,1) using regional info and at-site data
c (,2) using just at-site data
c (,3) using B17B formula for at-site MSE(G)
c mc = {mean, variance, coeff. skew}
c pq(32) r*8 quantiles estimated
c --pq=0.99 corresponds to "100-year flood"
c nq i*4 number of quantiles estimated (32)
c yp(32) r*8 estimated pq-th quantile of fitted p3 distribution
c ci_low(32) r*8 left end of 95% confidence interval for pq-th
c quantile
c ci_high(32)r*8 right end of 95% confidence interval for pq-th
c quantile
cprh (09/2009)
c var_est(*) r*8 variance of estimate for each quantile
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c parameters set in common block data/emablk/ (can be reset if desired)
c ------------------------------------------------------------------------
c /tacg04/at_site_std c*4 formula employed for computing mse of
c estimated at-site skew
c 'B17B' => Bulletin 17B MSE formula
c 'EMA' => First-order EMA MSE
c 'ADJE' => Adjusted first-order (Default)
c
c /MGB001/Alpha(3) r*8 alpha level for MGBT check for low outliers
c Default value is 0.01 [corr. 1% test]
c Step 1. Outward sweep from median (always done).
c alpha level of test = Alphaout Alpha(1)
c number of outliers = J1
c
c Step 2. Inward sweep from largest low outlier identified in Step 1.
c alpha level of test = Alphain Alpha(2)
c number of outliers = J2
c
c Step 3. Inward sweep from smallest observation
c alpha level of test = Alphazeroin Alpha(3)
c number of outliers = J3
c
c
c /tac002/sk141 r*8 minimum value of skew
c Default value is -1.41
c
c /tac002/bcf r*8 bias correction factor to use for S2, G
c 1997 => Cohn [1997] factors (Default)
c 2004 => Griffis et al. [2004] factors
c 0 => None
c
c /tac002/lskewXmax r*8 do you want to test fitted support to
c include Qmax?
c Default value is .FALSE.
c
c /tacR01/VarS2opt c*4 formula employed for computing weighting of
c at-site and regional info for computing S2
c (the problem: MSE of \hat{S^2} proportional
c to \sigma^4; we have two estimates of \sigma
c at-site and regional. What should weight
c reflect?)
c 'DF' => inversely to degrees of freedom
c df = 2*(S^4)/MSE[S^2] (DEFAULT)
c 'S2' => inversely to MSE[S^2_{at-site}]
c and MSE[S^2_{regional}]
c 'S1' => inversely to MSE[S_{at-site}]
c and MSE[S_{reg.}] (NOT AVAIL.)
c
c /tacci1/eps r*8 nominal conf. int. coverage (90% def.)
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
subroutine emafit(n,ql,qu,tl,tu,dtype,
1 reg_SD,reg_SD_mse,r_G,r_G_mse,
1 gbtype,gbthrsh0,weightOpt,
1 cmoms,pq,nq,yp,ci_low,ci_high,var_est)
implicit none
integer
1 i,n,nq,nqd ! input variables
double precision
1 ql(*),qu(*),tl(*),tu(*),reg_SD,reg_SD_mse,r_G,r_G_mse, ! input variables
2 cmoms(3,3),pq(*),yp(*),ci_low(*),ci_high(*),var_est(*),! output vars
3 pqd,eps,gbthrsh0,
4 r_M,r_M_mse,r_S2,r_S2_mse
character*4
1 dtype(*),gbtype,weightOpt
common /tacpq1/pqd(100),nqd
common /tacci1/eps
c
c set regional information for mean equal to zero
c
r_M = 0.d0
r_M_mse = -99.d0 ! ignore regional info on M
c
c calculate stats for variances (S^2) from standard deviations input
c
r_S2 = reg_SD**2
r_S2_mse = 4.d0 * r_S2 * reg_SD_mse ! first order approximation
nq = nqd
do 10 i=1,nq
pq(i) = pqd(i)
10 continue
call emafitb(n,ql,qu,tl,tu,dtype,
1 r_M,r_M_mse,r_S2,r_S2_mse,r_G,r_G_mse,
1 eps,gbtype,gbthrsh0,pq,nq,weightOpt,
1 cmoms,yp,ci_low,ci_high,var_est)
c
c correction to adjust for small sample sizes (see tac notes 17 feb 2007)
c
do 20 i=(nq-1)/2,1,-1
ci_low(i) = min(ci_low(i),ci_low(i+1))
ci_high(i) = min(ci_high(i),ci_high(i+1))
20 continue
do 30 i=(nq+3)/2,nq
ci_low(i) = max(ci_low(i),ci_low(i-1))
ci_high(i) = max(ci_high(i),ci_high(i-1))
30 continue
return
end
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c****|subroutine emafitpr
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c REVISED VERSION FROM 2011
c
c this routine fits the pearson type iii distribution to
c a data set using the ema algorithm.
c it includes the ability to employ regional information on 3 parameters
c otherwise it works the same as emafit
c
c so far this feature is not adequately documented
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c development history
c
c timothy a. cohn 26 sep 2011 added regional estimate for M
c this is analogous to region S2, G
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c input variables:
c ------------------------------------------------------------------------
c n i*4 number of observations (censored, uncensored, or
c other)
c ql(n) r*8 vector of lower bounds on (log) floods
c qu(n) r*8 vector of upper bounds on (log) floods
c NOTE: Zero flows enter as lmissing=-80.d0
c tl(n) r*8 vector of lower bounds on (log) flood threshold
c tu(n) r*8 vector of upper bounds on (log) flood threshold
c dtype(n) c*4 vector describing input data
c dtype(i) = "Syst" => systematic data
c dtype(i) = "Hist" => historic data
c dtype(i) = "Othr" => other
c reg_M r*8 regional mean (M)
c reg_M_mse r*8 mean square error of generalized mean
c notes:
c 1) reg_M_mse = 0 ("GENERALIZED MEAN, NO ERROR")
c use fixed parms(*) = reg_M w/ mse = 0.0
c 2) -999 < reg_M_mse< 0 ("AT-SITE MEAN")
c use at-site estimated mean
c 3) 0 < reg_M_mse < 1.d10 ("WEIGHTED MEAN")
c use M = weighted average of at-site and
c regional mean
c reg_SD r*8 regional standard deviation
c reg_SD_mse r*8 mean square error of generalized standard deviation
c notes:
c 1) reg_SD_mse = 0 ("GENERALIZED STD DEV, NO ERROR")
c use fixed parms(*) = reg_SD w/ mse = 0.0
c 2) -999 < reg_SD_mse< 0 ("AT-SITE STD DEV")
c use at-site estimated standard deviation
c 3) 0 < reg_SD_mse < 1.d10 ("WEIGHTED STD. DEV.")
c use sd = weighted average of at-site and
c regional standard deviation
c r_G r*8 regional skew
c r_G_mse r*8 mean square error of regional skew
c this variable encodes four distinct cases:
c 1) r_G_mse = 0 ("GENERALIZED SKEW, NO ERROR")
c use fixed g = r_G w/ mse = 0.0
c 2) -98 < r_G_mse < 0 ("GENERALIZED SKEW, MSE > 0")
c use fixed g = w/ mse = -r_G_mse
c 3) 0 < r_G_mse < 1.d10 ("WEIGHTED SKEW")
c use g = weighted average of at-site and
c regional skew (b17b recommendation)
c 4) 1.d10 < r_G_mse ("STATION SKEW")
c use g = at-site skew
c gbtype c*4 type of Grubbs-Beck test
c = "GBT" => standard GB test
c = "MGBT" => Multiple GB test
c gbthrsh0 r*8 critical value for Grubbs-Beck test
c N.B. gbthrsh0 codes for 3 cases
c 1. gbthrsh0 <= -6 ==> Compute GB critical value
c 2. gbthrsh0 > -6 ==> gbthrsh0 as crit. val.
c 3. gbthrsh0 (small, e.g. -5.9) no low out. test
c weightOpt c*4 weighting option for regional skew. Choices are
c 1) "HWN" - Greg Schwarz's Halloween Method
c 2) "ERL" - Relative effective record length
c weighting (used by HEC-SSP 2.3)
c 3) "" - PeakFQ 7.4 weighting scheme as described
c in Bulletin 17C. Will default to this option
c for any string except "HWN" or "ERL"
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c output variables:
c ------------------------------------------------------------------------
c
c cmoms(3,3) r*8 first 3 central moments
c (,1) using regional info and at-site data
c (,2) using just at-site data
c (,3) using B17B formula for at-site MSE(G)
c mc = {mean, variance, coeff. skew}
c pq(32) r*8 quantiles estimated
c --pq=0.99 corresponds to "100-year flood"
c nq i*4 number of quantiles estimated (32)
c yp(32) r*8 estimated pq-th quantile of fitted p3 distribution
c ci_low(32) r*8 left end of 95% confidence interval for pq-th
c quantile
c ci_high(32)r*8 right end of 95% confidence interval for pq-th
c quantile
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
subroutine emafitpr(n,ql,qu,tl,tu,dtype,
1 reg_M,reg_M_mse,reg_SD,reg_SD_mse,r_G,r_G_mse,
2 gbtype,gbthrsh0,weightOpt,
2 cmoms,pq,nq,yp,ci_low,ci_high,var_est)
implicit none
integer
1 i,n,nq,nqd ! input variables
double precision
1 ql(*),qu(*),tl(*),tu(*),reg_SD,reg_SD_mse,r_G,r_G_mse, ! input vars
2 cmoms(3,3),pq(*),yp(*),ci_low(*),ci_high(*),var_est(*),! output vars
3 pqd,eps,gbthrsh0,
4 reg_M,reg_M_mse,r_M,r_M_mse,r_S2,r_S2_mse
character*4
1 dtype(*),gbtype,weightOpt
ctac data eps/0.90d0/,nqd/32/
common /tacpq1/pqd(100),nqd
common /tacci1/eps
c 1. bring in regional information for M, S, and g
c 1a. set regional information for mean
r_M_mse = reg_M_mse
if(r_M_mse .le. 0.d0) then
r_M = 0.d0
r_M_mse = -99.d0
else
r_M = reg_M
r_M_mse = reg_M_mse
endif
c 1b. set regional information for variance
c
c calculate stats for variances (S^2) from standard deviations input
c N.B. This is an approximation. The results would be slightly different
c if regionalization were done on S^2 or if estimation weighting
c were done on S. However, this is more convenient given the
c parametrization based on (M, S2, G) rather than (M, S, G)
c
r_S2 = reg_SD**2
r_S2_mse = 4.d0 * r_S2 * reg_SD_mse ! first order approximation
c 1c. regional information for skew must be supplied by user as argument
nq = nqd
do 10 i=1,nq
pq(i) = pqd(i)
10 continue
call emafitb(n,ql,qu,tl,tu,dtype,
1 r_M,r_M_mse,r_S2,r_S2_mse,r_G,r_G_mse,
1 eps,gbtype,gbthrsh0,pq,nq,weightOpt,
1 cmoms,yp,ci_low,ci_high,var_est)
c
c correction to adjust for small sample sizes (see tac notes 17 feb 2007)
c
do 20 i=(nq-1)/2,1,-1
ci_low(i) = min(ci_low(i),ci_low(i+1))
ci_high(i) = min(ci_high(i),ci_high(i+1))
20 continue
do 30 i=(nq+3)/2,nq
ci_low(i) = max(ci_low(i),ci_low(i-1))
ci_high(i) = max(ci_high(i),ci_high(i-1))
30 continue
return
end
c*_*-*~*_*-*~* new program begins here *_*-*~*_*-*~*_*-*~
c****|subroutine emafitb
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c the main subroutine
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c input variables:
c ------------------------------------------------------------------------
c n i*4 number of observations (censored, uncensored, or
c other)
c ql(n) r*8 vector of lower bounds on (log) floods
c qu(n) r*8 vector of upper bounds on (log) floods
c tl(n) r*8 vector of lower bounds on (log) flood threshold
c tu(n) r*8 vector of upper bounds on (log) flood threshold
c dtype(n) c*4 vector describing input data
c dtype(i) = "Syst" => systematic data
c dtype(i) = "Hist" => historic data
c dtype(i) = "Othr" => other
c r_M r*8 regional mean
c r_M_mse r*8 mean square error of regional mean
c 1) r_M_mse = 0 ("GENERALIZED MEAN, NO ERROR")
c use fixed M = r_M w/ mse = 0.0
c 2) -999 < r_M_mse < 0 ("AT-SITE MEAN")
c use at-site estimated mean (M)
c 3) 0 < r_M_mse < 1.d10 ("WEIGHTED MEAN")
c use M = weighted average of at-site and
c regional variance
c r_S2 r*8 regional variance (std.dev^2)
c N.B. Needed because var. of regional S2 scales
c with (E[S2])^2; weights for S2 is based
c on degrees of freedom in Chi-square dstn
c corresponding to each estimator where
c df = (1/2) S2^2/\hat{Var[S^2]}
c r_S2_mse r*8 mean square error of regional variance
c 1) r_S2_mse = 0 ("GENERALIZED S2, NO ERROR")
c use fixed sd = reg_SD w/ mse = 0.0
c 2) -999 < r_S2_mse < 0 ("AT-SITE VARIANCE")
c use at-site estimated variance (S^2)
c 3) 0 < r_S2_mse < 1.d10 ("WEIGHTED VARIANCE")
c use S2 = weighted average of at-site and
c regional variance
c r_G r*8 regional skew
c r_G_mse r*8 mean square error of regional skew
c this variable encodes four distinct cases:
c 1) r_G_mse = 0 ("GENERALIZED SKEW, NO ERROR")
c use fixed g = r_G w/ mse = 0.0
c 2) -98 < r_G_mse < 0 ("GENERALIZED SKEW, MSE > 0")
c use fixed g = r_G w/ mse = -r_G_mse
c 3) 0 < r_G_mse < 1.d10 ("WEIGHTED SKEW")
c use g = weighted average of at-site and
c regional skew (b17b recommendation)
c 4) 1.d10 < r_G_mse ("STATION SKEW")
c use g = at-site skew
c pq r*8 quantile to be estimated
c --pq=0.99 corresponds to "100-year flood"
c nq i*4 number of quantiles estimated (32)
c eps r*8 vector of ci coverages (usually just 0.90)
c gbthrsh0 r*8 critical value for Grubbs-Beck test
c (values < -6 result in computed low outlier
c test criterion)
c weightOpt c*4 weighting option for regional skew. Choices are
c 1) "HWN" - Greg Schwarz's Halloween Method
c 2) "ERL" - Relative effective record length
c weighting (used by HEC-SSP 2.3)
c 3) "" - PeakFQ 7.4 weighting scheme as described
c in Bulletin 17C. Will default to this option
c for any string except "HWN" or "ERL"
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c output variables:
c ------------------------------------------------------------------------
c
c cmoms(3,3) r*8 first 3 central moments
c mc = {mean, variance, coeff. skew}
c yp r*8 estimated pq-th quantile of fitted p3 distribution
c ci_low r*8 left end of 90% confidence interval for pq-th
c quantile
c ci_high r*8 right end of 90% confidence interval for pq-th
c quantile
cprh (09/2009)
c var_est r*8 variance of estimate for each quantile
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
subroutine emafitb(n,ql_in,qu_in,tl_in,tu_in,dtype_in,
1 r_M,r_M_mse,r_S2,r_S2_mse,r_G,r_G_mse,
1 eps,gbtype_in,gbthrsh0,pq,nq,weightOpt,
2 cmoms,yp,ci_low,ci_high,var_est)
implicit none
integer
1 ntmax,nn,nx
parameter (ntmax=100,nn=100,nx=25000)
integer
1 n,i,nt,ns,nlow,nzero,nGBiter,nlow_V,it_max,nq,i1
double precision
1 ql_in(*),qu_in(*),tl_in(*),tu_in(*),
1 r_M,r_M_mse,r_S2,r_S2_mse,r_G,r_G_mse,pq(*), ! input
2 cmoms(3,3),yp(nn),ci_low(nn),ci_high(nn),var_est(nn), ! output
3 yp1(nn),yp2(nn),ci_low1(nn),ci_low2(nn),ci_high1(nn),
4 ci_high2(nn),skewmin,qP3,parms(3),
5 ql,qu,tl,tu,qs,thrmin(1),thrmax(1),
6 cv_yp_syp(2,2,nn),eps,tl2(ntmax),tu2(ntmax),nobs(ntmax),
7 skew,wt,as_M_mse,as_S2_mse,as_G_mse,as_G_mse_Syst,as_G_ERL,
8 eff_n,gbthrsh0,gbcrit,gbthresh,gbcrit_V,gbthresh_V,pvaluew
parameter (skewmin=0.06324555)
double precision
1 mseg_all,mse_ema, detrat, Wd, erlg
character*4
1 at_site_option,at_site_default,at_site_std,gbtype,gbtype_in,
2 dtype,dtype_in(*),VarS2opt, weightOpt
logical cirun
common /tacg01/gbcrit,gbthresh,pvaluew(10000),qs(10000),
1 ns,nlow,nzero,gbtype
common /tacg02/ql(nx),qu(nx),tl(nx),tu(nx),dtype(nx)
common /tacg03/gbcrit_V(10),gbthresh_V(10),nlow_V(10),nGBiter
common /tacg04/at_site_option,at_site_default,at_site_std
common /tacdgb/cirun
common /tac005/as_M_mse,as_S2_mse,as_G_mse,as_G_mse_Syst,as_G_ERL
common /tacR01/VarS2opt
common /sas/Wd !Weighting factor for Halloween skew method
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
i1 = 1
thrmin(1) = -99.0
thrmax(1) = 99.0
c 1. check for low outliers
c a) Identify/recode LOs based on at-site systematic + historical data
c b) Iterate search for LOs using at-site systematic + historical data
c combined with regional skew information
c
do 12 i=1,n
ql(i) = ql_in(i)
qu(i) = qu_in(i)
tl(i) = tl_in(i)
tu(i) = tu_in(i)
12 continue
gbtype = gbtype_in
c loop up to 10 times to see if any new LOs uncovered
if(gbtype .eq. 'GBit') then
it_max = 10
else
it_max = 1
endif
do 15 i=1,it_max
call gbtest(n,ql,qu,tl,tu,dtype_in,gbthrsh0,
1 ql,qu,tl,tu)
gbcrit_V(i) = gbcrit
gbthresh_V(i) = gbthresh
nlow_V(i) = nlow
if(nlow .eq. 0) goto 17 ! any new low outliers?
15 continue
if(gbtype .eq. 'GBit') then
write(*,*) ' Low outlier issues (emafit): nlow = ', nlow,i
write(*,*) ' User may want to specify threshold'
stop ! Should never get here; something is wrong
endif
17 continue
nGBiter = i ! added on JRS recomendation
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c If we have censoring due to low outliers using new MGBT, the MSE of
c at-site skew will tend to stay constant or decline because skew
c will be driven toward zero.
c Thus first-order approximation based on fixed censoring, which would
c suggest increased MSE, is incorrect.
c As a temporary fix, if MGBT is used and low outliers are detected,
c the B17B formula will be used to estimate the MSE of the skew.
c
if(gbtype .eq. "MGBT" .and. nlow .gt. 0) then
at_site_std = "B17B"
else
at_site_std = "ADJE" ! correction due to PRH 12/16/2013
endif
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c 2. organize data for computing ci and set up the tl and tu vectors
c
call compress2(n,tl,tu,nt,nobs,tl2,tu2)
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c 3. begin by fitting the p3 distn to the at-site and regional data
c
c n.b. three calls are necessary:
c 1. compute at-site moments without employing regional skew;
c this is used to compute the MSE(G) and moments for
c the case of only at-site data employing the B17B
c formula for MSE(G) [this is a very poor approximation
c when historical info or left-tail censoring is present].
c However, the moments are needed to compute at-site MSEs
c for call 2.
c 2. EMA/B17B computation using regional skew information
c with B17B MSE(G) formula (B17B eqn 6; p. 13)
c 3. final computation using regional skew information
c with EMA MSE(G)
c
c 1) Get at-site skews
call p3est_ema(n,ql,qu,
1 1.d0, 1.d0, 1.d0, ! set all at-site MSEs to 1
2 r_M, r_S2, r_G,
3 -99.d0, -99.d0, -99.d0, ! set all regional MSEs to Infinity
4 -99.d0, ! skew weighting coefficient (SAS)
5 cmoms(1,2)) ! return moments (only skew is needed)
c 2) EMA computation using weighted regional information/B17B MSEs
at_site_option = "B17B"
as_M_mse = mse_ema(nt,nobs,tl2,tu2,cmoms(1,2),1) ! compute true a-s
as_S2_mse = mse_ema(nt,nobs,tl2,tu2,cmoms(1,2),2) ! MSEs based on at-
as_G_mse = mseg_all(nt,nobs,tl2,tu2,cmoms(1,2)) ! site skew cmoms(1,3)
c
eff_n = dble(max(ns,10)) ! use a reasonable sample size if ns=0
as_G_mse_Syst = mseg_all(i1,eff_n,thrmin,thrmax,cmoms(1,2)) ! ERL Computation
as_G_ERL = dble(eff_n)*(as_G_mse_Syst/as_G_mse)
! weightOpt = "ERL"
if( weightOpt .eq. "HWN" .and. abs(cmoms(3,2)) .ge. 0.04 ) then
!Halloween method weighting from Greg Schwarz
Wd = detrat(cmoms(:,2), n, nt, nobs, tl2, tu2)
else if(weightOpt .eq. "ERL") then
Wd = erlg(cmoms(3,2), as_G_mse) / n !Relative effective record length weighting
else
Wd = 1d0 !For Halloween method skews near zero, Wd reduces to 1.0
endif
call p3est_ema(n,ql,qu,
1 as_M_mse,as_S2_mse,as_G_mse,
2 r_M, r_S2, r_G,
3 r_M_mse, r_S2_mse, r_G_mse,
4 Wd,
5 cmoms(1,3))
c 3) final computation using weighted regional info
if(at_site_std .ne. 'B17B') then
at_site_option = at_site_std
as_M_mse = mse_ema(nt,nobs,tl2,tu2,cmoms(1,2),1) ! compute true a-s
as_S2_mse = mse_ema(nt,nobs,tl2,tu2,cmoms(1,2),2) ! MSEs based on at-
as_G_mse = mseg_all(nt,nobs,tl2,tu2,cmoms(1,2)) ! site skew cmoms(1,3)
as_G_mse_Syst = mseg_all(i1,eff_n,thrmin,thrmax,cmoms(1,2)) ! ERL
as_G_ERL = dble(eff_n)*(as_G_mse_Syst/as_G_mse)
call p3est_ema(n,ql,qu,
1 as_M_mse,as_S2_mse,as_G_mse, ! use at-site MSEs
2 r_M, r_S2, r_G, ! and weighted regional info
3 r_M_mse, r_S2_mse, r_G_mse,
4 Wd,
5 cmoms(1,1)) ! these are the EMA results
else
cmoms(1,1) = cmoms(1,3)
cmoms(2,1) = cmoms(2,3)
cmoms(3,1) = cmoms(3,3)
endif
if(.not. cirun) return
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c 4. compute confidence intervals
c
if( abs(cmoms(3,1)) .gt. skewmin) then
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c 4.1 for skews far from zero
c
call var_emab(nt,nobs,tl2,tu2,cmoms,pq,nq,eps,
1 r_s2, r_m_mse, r_s2_mse, r_g_mse,
3 yp,cv_yp_syp,ci_low,ci_high)
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c 4.2 for skews close to zero; preserves mean and variance
c
else ! compute a weighted sum/interpolate values
skew = cmoms(3,1)
cmoms(3,1) = -skewmin
call m2p(cmoms,parms)
call var_emab(nt,nobs,tl2,tu2,cmoms,pq,nq,eps,
1 r_s2,
2 r_m_mse, r_s2_mse, r_g_mse,
3 yp1,cv_yp_syp,ci_low1,ci_high1)
cmoms(3,1) = skewmin
call m2p(cmoms,parms)
call var_emab(nt,nobs,tl2,tu2,cmoms,pq,nq,eps,
1 r_s2,
2 r_m_mse, r_s2_mse, r_g_mse,
3 yp2,cv_yp_syp,ci_low2,ci_high2)
wt = (skew+skewmin)/(2.d0 * skewmin) ! weight to attach to positive skew
cmoms(3,1) = skew
c compute weighted average of results (assume approx. linear)
do 20 i=1,nq
yp(i) = qP3(pq(i),cmoms)
ci_low(i) = (1.d0-wt) * ci_low1(i) + wt * ci_low2(i)
ci_high(i) = (1.d0-wt) * ci_high1(i) + wt * ci_high2(i)
20 continue
endif
c return estimate of log-quantile variance for each quantile
do 30 i=1,nq
var_est(i) = cv_yp_syp(1,1,i)
30 continue
return
end
c*_*-*~*_*-*~* new program begins here *_*-*~*_*-*~*_*-*~
c****|subroutine gbtest
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c grubbs-beck test (now includes tests for multiple low outliers)
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c development history
c
c timothy a. cohn 07 feb 2007
c modified .. ... 2011 New MGBT procedure added
c modified .. ... 2012 New JLaM/JRS MGBT procedure added
c modified 13 feb 2013 ns put into common block
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c n.b. routine takes input data, uses gb test on systematic data to identify
c low outliers, then recodes low outliers as censored values
c where threshold is set at first above-gb-critical value
c observation.
c routine is not iterative
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c input variables:
c ------------------------------------------------------------------------
c n i*4 number of observations (only systematic data)
c ql_in(n) r*8 vector of lower bounds on (log) floods
c qu_in(n) r*8 vector of upper bounds on (log) floods
c tl_in(n) r*8 vector of lower bounds on (log) flood threshold
c tu_in(n) r*8 vector of upper bounds on (log) flood threshold
c gbthrsh0 r*8 critical value for Grubbs-Beck test
c (0 or negative values => estimate threshold)
c (small value (1e-10) => no low outlier test)
c as_G_mse r*8 mse of at-site skew estimate
c r_G r*8 regional skew
c r_G_mse r*8 mean square error of regional skew
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c output variables:
c ------------------------------------------------------------------------
c
c ql(n) r*8 vector of lower bounds on (log) floods
c qu(n) r*8 vector of upper bounds on (log) floods
c tl(n) r*8 vector of lower bounds on (log) flood threshold
c tu(n) r*8 vector of upper bounds on (log) flood threshold
c
c n.b. routine also returns info (used by PeakfqSA) on number of
c systematic observations, low outliers, etc. through common block
c
c common /tacg01/gbcrit,gbthresh,pvaluew(10000),qs(10000),
c ns,nlow,nzero,gbtype
c
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
subroutine gbtest(n,ql_in,qu_in,tl_in,tu_in,dtype,gbthrsh0,
1 ql,qu,tl,tu)
implicit none
integer
1 n,ns,i,nlow,klow,
2 nzero
integer p_nq
parameter (p_nq=20000)
double precision
1 ql_in(*),qu_in(*),tl_in(*),tu_in(*),
2 ql(*),qu(*),tl(*),tu(*),gbcrit,gbthresh,gbthrsh0,qs,
3 x(p_nq),xm,s,t,cmoms(3),gbtmin,pvaluew,
5 lmissing,qmin
integer MGBTP
character*4
1 gbtype,dtype(*)
common /tacg01/gbcrit,gbthresh,pvaluew(10000),qs(10000),
1 ns,nlow,nzero,gbtype
data gbtmin/-6.d0/,lmissing/-80.d0/
! allows very small positive log(q) values
c****|===|====-====|====-====|====-====|====-====|====-====|====-====|==////////
c
c compute number of systematic flows (ns) and count zero flows (nzero)
c note that zero flows are automatically treated as low outliers in MGBT
ns = 0
nzero = 0
qmin = 99.d99 ! identify the smallest 'Syst'
do 10 i=1,n
if(ql_in(i) .eq. qu_in(i) .and. dtype(i) .eq. 'Syst') then ! N.B. Criterion
ns = ns + 1
x(ns) = ql_in(i)
qmin = min(qmin,x(ns))
qs(ns)= 10**x(ns)
if(qu_in(i) .le. lmissing) nzero = nzero + 1
endif
10 continue
c===
c
c determine if there is a 'less-than' value known to be smaller than smallest
c point value; these are also treated as systematic observations
if(qmin .lt. 99.d99) then
do 15 i=1,n
if( (ql_in(i) .ne. qu_in(i)) .and.
1 (dtype(i) .eq. 'Syst') .and.
2 (qu_in(i) .le. qmin) ) THEN
ns = ns + 1
x(ns) = qu_in(i)
qs(ns)= 10**x(ns)
endif
15 continue
endif
c
c===
c no low outlier test
if(gbtype .eq. "NONE") then
gbthresh = -99.d0
gbcrit = gbthresh
goto 25
endif
c specified low outlier threshold?
if(gbtype .eq. "FIXE") then
gbthresh = gbthrsh0
gbcrit = gbthrsh0
cPRH 09/2017
c sort values for fixed LO threshold so calling code reports properly
call dsvrgn(ns,x,x)
call dsvrgn(ns,qs,qs)
goto 25
endif
call dsvrgn(ns,x,x)
call dsvrgn(ns,qs,qs)
if( (ns-nzero) .le. 5) then ! not enough data>0 to apply low-outlier test
write(*,*) 'inadequate data for grubbs-beck test',ns-nzero
write(*,*) 'all zeroes recoded as smaller than: ',qs(nzero+1)
write(*,*) 'this is the smallest non-zero systematic obs.'
gbcrit = x(nzero+1)
gbthresh = x(nzero+1)
goto 25
endif
c
c Limit of use of MGBT Test; substitute GB when over half zeros
c
if( (gbtype .eq. 'MGBT') .and. (nzero .ge. ns/2) ) then
gbtype = 'GBT'
write(*,*) 'too many zeros for MGBT test (ns, nzero) ',ns,nzero
write(*,*) 'traditional Grubbs-Beck (GB) used instead'
endif
c
c Traditional Grubbs-Beck Test (error noted on 3/24/2013; n where ns needed)
c
if(gbtype .eq. 'GBT') then ! B17B Grubbs-Beck test for 1 outlier
call p3est_ema(ns-nzero,x(nzero+1),x(nzero+1), ! changed to ns from n
1 1.d0, 1.d0, 1.d0, ! At-site MSEs
2 0.d0, 1.d0, 0.d0, ! Regional parameters (dumb)
3 -99.d0,-99.d0,-99.d0, ! use no regional info
4 -99.d0, ! skew weighting coefficient (SAS)
5 cmoms) ! get moms
xm = cmoms(1)
s = sqrt(cmoms(2))
t = -0.9043+3.345*sqrt(log10(dble(ns-nzero))) ! N.B. ns, not n
1 -0.4046*log10(dble(ns-nzero)) ! Lu formula [JRS]
gbcrit = xm - s*t
gbthresh= 1.d10 ! starting value to find gbthresh
do 20 i=1,ns
if(x(i) .ge. gbcrit) then
gbthresh = min(x(i),gbthresh)
endif
20 continue
c
c Multiple Grubbs-Beck Test
c
else if(gbtype .eq. "MGBT") then ! Multiple Grubbs-Beck (Cohn, 2011)
klow = MGBTP(qs,ns,pvaluew)
if(klow .gt. 0) then
gbcrit = x(klow+1)
gbthresh = gbcrit
else
if(nzero .gt. 0) then ! zero flows are low outliers
klow = nzero
gbcrit = x(klow+1)/2.d0 ! smallest obs > 0 divided by 2...why not?
gbthresh = x(klow+1)
else
gbcrit = -99.d0 ! no low outlier issues
gbthresh = gbcrit
endif
endif
endif
c fill in various arrays and compute nlow
25 continue
nlow = 0
do 30 i=1,n
c added check of data type conditional; change made 16 Mar 15 (PRH)
c if(qu_in(i) .lt. gbcrit .and. dtype(i).eq.'Syst') then ! note:
if(qu_in(i) .lt. gbcrit) then ! note: