forked from johannesgerer/jburkardt-f
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbdmlib.f90
3027 lines (2680 loc) · 68.5 KB
/
bdmlib.f90
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
subroutine amino_print ( acid_num, acid_sym )
!*****************************************************************************80
!
!! AMINO_PRINT prints the amino acid parameters.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 23 November 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, integer ( kind = 4 ) ACID_NUM, the number of amino acids.
!
! Input, character ACID_SYM(ACID_NUM), the one letter amino acid codes.
!
implicit none
integer ( kind = 4 ) acid_num
integer ( kind = 4 ) acid_i
character ( len = 27 ) acid_name
character acid_sym(acid_num)
character c
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' I Amino Acid Symbol'
write ( *, '(a)' ) ' '
do acid_i = 1, acid_num
c = acid_sym(acid_i)
call ch_to_amino_name ( c, acid_name )
write ( *, '(i3,2x,a,2x,a)' ) acid_i, acid_sym(acid_i), acid_name
end do
return
end
subroutine binomial_sample ( a, b, seed, x )
!*****************************************************************************80
!
!! BINOMIAL_SAMPLE samples the Binomial PDF.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 02 February 1999
!
! Author:
!
! John Burkardt
!
! Reference:
!
! William Kennedy, James Gentle,
! Algorithm BU,
! Statistical Computing,
! Dekker, 1980.
!
! Parameters:
!
! Input, integer ( kind = 4 ) A, the number of trials.
! 1 <= A.
!
! Input, real ( kind = 8 ) B, the probability of success on one trial.
! 0.0D+00 <= B <= 1.0.
!
! Input/output, integer ( kind = 4 ) SEED, a seed for the random
! number generator.
!
! Output, integer ( kind = 4 ) X, a sample of the PDF.
!
implicit none
integer ( kind = 4 ) a
real ( kind = 8 ) b
real ( kind = 8 ) r8_uniform_01
integer ( kind = 4 ) i
integer ( kind = 4 ) seed
real ( kind = 8 ) u
integer ( kind = 4 ) x
x = 0
do i = 1, a
u = r8_uniform_01 ( seed )
if ( u <= b ) then
x = x + 1
end if
end do
return
end
subroutine ch_cap ( c )
!*****************************************************************************80
!
!! CH_CAP capitalizes a single character.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 19 July 1998
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input/output, character C, the character to capitalize.
!
implicit none
character c
integer ( kind = 4 ) itemp
itemp = ichar ( c )
if ( 97 <= itemp .and. itemp <= 122 ) then
c = char ( itemp - 32 )
end if
return
end
function ch_eqi ( c1, c2 )
!*****************************************************************************80
!
!! CH_EQI is a case insensitive comparison of two characters for equality.
!
! Example:
!
! CH_EQI ( 'A', 'a' ) is .TRUE.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 14 August 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character C1, C2, the characters to compare.
!
! Output, logical CH_EQI, the result of the comparison.
!
implicit none
logical ch_eqi
character c1
character c2
character cc1
character cc2
cc1 = c1
cc2 = c2
call ch_cap ( cc1 )
call ch_cap ( cc2 )
if ( cc1 == cc2 ) then
ch_eqi = .true.
else
ch_eqi = .false.
end if
return
end
subroutine ch_next ( line, cval, done )
!*****************************************************************************80
!
!! CH_NEXT "reads" space-separated characters from a string, one at a time.
!
! Example:
!
! Input:
!
! LINE = ' A B, C DE F'
!
! Output:
!
! 'A', 'B', 'C', 'D', 'E', 'F', and then blanks.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 18 November 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character ( len = * ) LINE, a string, presumably containing
! characters, possibly separated by spaces or commas.
!
! Output, character CVAL. If DONE is FALSE, then CVAL contains the
! "next" character read from LINE. If DONE is TRUE, then
! CVAL is blank.
!
! Input/output, logical DONE.
! On input with a fresh value of LINE, the user should set
! DONE to TRUE.
! On output, the routine sets DONE to FALSE if another character
! was read, or TRUE if no more characters could be read.
!
implicit none
character cval
logical done
integer ( kind = 4 ) i
character ( len = * ) line
integer ( kind = 4 ), save :: next = 1
if ( done ) then
next = 1
done = .false.
end if
do i = next, len(line)
if ( line(i:i) /= ' ' .and. line(i:i) /= ',' ) then
cval = line(i:i)
next = i + 1
return
end if
end do
done = .true.
next = 1
cval = ' '
return
end
subroutine ch_to_amino_name ( c, amino_name )
!*****************************************************************************80
!
!! CH_TO_AMINO_NAME converts a character to an amino acid name.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 16 June 2000
!
! Author:
!
! John Burkardt
!
! Reference:
!
! Carl Branden, John Tooze,
! Introduction to Protein Structure,
! Garland Publishing, 1991.
!
! Parameters:
!
! Input, character C, the one letter code for an amino acid.
! Lower and upper case letters are treated the same.
!
! Output, character ( len = * ) AMINO_NAME, the full name of the
! corresponding amino acid. The longest name is 27 characters.
! If the input code is not recognized, then AMINO_NAME will be
! set to '???'.
!
implicit none
integer ( kind = 4 ), parameter :: n = 23
character ( len = * ) amino_name
character ( len = 27 ), dimension ( n ) :: amino_table = (/ &
'Alanine ', &
'Aspartic acid or Asparagine', &
'Cysteine ', &
'Aspartic acid ', &
'Glutamic acid ', &
'Phenylalanine ', &
'Glycine ', &
'Histidine ', &
'Isoleucine ', &
'Lysine ', &
'Leucine ', &
'Methionine ', &
'Asparagine ', &
'Proline ', &
'Glutamine ', &
'Arginine ', &
'Serine ', &
'Threonine ', &
'Valine ', &
'Tryptophan ', &
'Undetermined amino acid ', &
'Tyrosine ', &
'Glutamic acid or Glutamine ' /)
character c
logical ch_eqi
character, dimension ( n ) :: c_table = (/ &
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', &
'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', &
'X', 'Y', 'Z' /)
integer ( kind = 4 ) i
do i = 1, n
if ( ch_eqi ( c, c_table(i) ) ) then
amino_name = amino_table(i)
return
end if
end do
amino_name = '???'
return
end
subroutine ch_to_digit ( c, digit )
!*****************************************************************************80
!
!! CH_TO_DIGIT returns the value of a base 10 digit.
!
! Example:
!
! C DIGIT
! --- -----
! '0' 0
! '1' 1
! ... ...
! '9' 9
! ' ' 0
! 'X' -1
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 04 August 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character C, the decimal digit, '0' through '9' or blank
! are legal.
!
! Output, integer ( kind = 4 ) DIGIT, the corresponding value. If C was
! 'illegal', then DIGIT is -1.
!
implicit none
character c
integer ( kind = 4 ) digit
if ( lge ( c, '0' ) .and. lle ( c, '9' ) ) then
digit = ichar ( c ) - 48
else if ( c == ' ' ) then
digit = 0
else
digit = -1
end if
return
end
subroutine comp_param_print ( acid_num, acid_sym, comp_max, comp_num, beta, &
beta_sum, comp_weight )
!*****************************************************************************80
!
!! COMP_PARAM_PRINT prints the parameters for the mixture components.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 24 January 2000
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, integer ( kind = 4 ) ACID_NUM, the number of amino acids.
!
! Input, character ACID_SYM(ACID_NUM), the one letter amino acid codes.
!
! Input, integer ( kind = 4 ) COMP_MAX, the maximum number of Dirichlet
! mixture components.
!
! Input, integer ( kind = 4 ) COMP_NUM, the number of components in the
! Dirichlet mixture.
!
! Input, real ( kind = 8 ) BETA(ACID_NUM,COMP_MAX); BETA(I,J) is the
! parameter for the J-th acid in the I-th Dirichlet mixture component.
!
! Input, real ( kind = 8 ) BETA_SUM(COMP_MAX), the sum of the values of
! BETA(ACID_I,COMP_I) for a given component COMP_I.
!
! Input, real ( kind = 8 ) COMP_WEIGHT(COMP_NUM), the mixture weight of each
! component. These values should be nonnegative, and sum to 1. They
! represent the relative proportion of each component in the mixture.
!
implicit none
integer ( kind = 4 ) acid_num
integer ( kind = 4 ) comp_max
integer ( kind = 4 ) acid_i
character acid_sym(acid_num)
integer ( kind = 4 ) comp_i
real ( kind = 8 ) beta(acid_num,comp_max)
real ( kind = 8 ) beta_sum(comp_max)
integer ( kind = 4 ) comp_num
real ( kind = 8 ) comp_weight(comp_max)
write ( *, '(a)' ) ' '
write ( *, '(a,i6)' ) ' Number of components = ', comp_num
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' '
write ( *, '(''Compon:'',20i8)' ) ( comp_i, comp_i = 1, comp_num )
write ( *, '(''Weight:'',20f8.4)' ) comp_weight(1:comp_num)
write ( *, '(a)' ) ' '
do acid_i = 1, acid_num
write ( *, '(i2,2x,a1,2x,20f8.4)' ) acid_i, acid_sym(acid_i), &
beta(acid_i,1:comp_num)
end do
write ( *, '(a)' ) ' '
write ( *, '(a3,4x,20f8.4)' ) 'Sum', beta_sum(1:comp_num)
return
end
subroutine dirichlet_mean ( n, a, mean )
!*****************************************************************************80
!
!! DIRICHLET_MEAN returns the means of the Dirichlet PDF.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 23 November 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, integer ( kind = 4 ) N, the number of components.
!
! Input, real ( kind = 8 ) A(N), the probabilities for each component.
! Each A(I) should be nonnegative, and at least one should be positive.
!
! Output, real ( kind = 8 ) MEAN(N), the means of the PDF.
!
implicit none
integer ( kind = 4 ) n
real ( kind = 8 ) a(n)
real ( kind = 8 ) mean(n)
if ( any ( a(1:n) < 0.0D00 ) ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'DIRICHLET_MEAN - Fatal error!'
write ( *, '(a)' ) ' At least one entry of A is negative!'
stop
end if
if ( all ( a(1:n) == 0.0D+00 ) ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'DIRICHLET_MEAN - Fatal error!'
write ( *, '(a)' ) ' All entries of A are zero!'
stop
end if
mean(1:n) = a(1:n)
call r8vec_unit_sum ( n, mean )
return
end
subroutine dirichlet_mix_check ( comp_num, elem_max, elem_num, a, comp_weight )
!*****************************************************************************80
!
!! DIRICHLET_MIX_CHECK checks the parameters of a Dirichlet mixture PDF.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 13 December 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, integer ( kind = 4 ) COMP_NUM, the number of components in the
! Dirichlet mixture density, that is, the number of distinct Dirichlet PDF's
! that are mixed together.
!
! Input, integer ( kind = 4 ) ELEM_MAX, the leading dimension of A, which
! must be at least ELEM_NUM.
!
! Input, integer ( kind = 4 ) ELEM_NUM, the number of elements of an
! observation.
!
! Input, real ( kind = 8 ) A(ELEM_MAX,COMP_NUM), the probabilities for
! element ELEM_NUM in component COMP_NUM.
! Each A(I,J) should be greater than or equal to 0.0.
!
! Input, integer ( kind = 4 ) COMP_WEIGHT(COMP_NUM), the mixture weights of
! the densities. These do not need to be normalized. The weight of a given
! component is the relative probability that that component will be used
! to generate the sample.
!
implicit none
integer ( kind = 4 ) comp_num
integer ( kind = 4 ) elem_max
integer ( kind = 4 ) elem_num
real ( kind = 8 ) a(elem_max,comp_num)
integer ( kind = 4 ) comp_i
real ( kind = 8 ) comp_weight(comp_num)
integer ( kind = 4 ) elem_i
logical positive
do comp_i = 1, comp_num
do elem_i = 1, elem_num
if ( a(elem_i,comp_i) < 0.0D+00 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'DIRICHLET_MIX_CHECK - Fatal error!'
write ( *, '(a)' ) ' A(ELEM,COMP) < 0.'
write ( *, '(a,i6)' ) ' COMP = ', comp_i
write ( *, '(a,i6)' ) ' ELEM = ', elem_i
write ( *, '(a,g14.6)' ) ' A(COMP,ELEM) = ', a(elem_i,comp_i)
stop
end if
end do
end do
positive = .false.
do comp_i = 1, comp_num
if ( comp_weight(comp_i) < 0.0D+00 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'DIRICHLET_MIX_CHECK - Fatal error!'
write ( *, '(a)' ) ' COMP_WEIGHT(COMP) < 0.'
write ( *, '(a,i6)' ) ' COMP = ', comp_i
write ( *, '(a,g14.6)' ) ' COMP_WEIGHT(COMP) = ', comp_weight(comp_i)
stop
else if ( 0.0D+00 < comp_weight(comp_i) ) then
positive = .true.
end if
end do
if ( .not. positive ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'DIRICHLET_MIX_CHECK - Fatal error!'
write ( *, '(a)' ) ' All component weights are zero.'
stop
end if
return
end
subroutine dirichlet_multinomial_pdf ( x, a, b, c, pdf )
!*****************************************************************************80
!
!! DIRICHLET_MULTINOMIAL_PDF evaluates a Dirichlet Multinomial PDF.
!
! Discussion:
!
! PDF(X)(A,B,C) = Comb(A,B,X) * ( Gamma(C_Sum) / Gamma(C_Sum+A) )
! Product ( 1 <= I <= B ) Gamma(C(I)+X(I)) / Gamma(C(I))
!
! where:
!
! Comb(A,B,X) is the multinomial coefficient C( A; X(1), X(2), ..., X(B) ),
! C_Sum = Sum ( 1 <= I <= B ) C(I)
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 17 December 1999
!
! Author:
!
! John Burkardt
!
! Reference:
!
! Kenneth Lange,
! Mathematical and Statistical Methods for Genetic Analysis,
! Springer, 1997, page 45.
!
! Parameters:
!
! Input, integer ( kind = 4 ) X(B); X(I) counts the number of occurrences of
! outcome I, out of the total of A trials.
!
! Input, integer ( kind = 4 ) A, the total number of trials.
!
! Input, integer ( kind = 4 ) B, the number of different possible outcomes
! on one trial.
!
! Input, integer ( kind = 4 ) C(B); C(I) is the Dirichlet parameter
! associated with outcome I.
!
! Output, real ( kind = 8 ) PDF, the value of the Dirichlet multinomial PDF.
!
implicit none
integer ( kind = 4 ) b
integer ( kind = 4 ) a
real ( kind = 8 ) c(b)
real ( kind = 8 ) c_sum
integer ( kind = 4 ) i
real ( kind = 8 ) pdf
real ( kind = 8 ) pdf_log
real ( kind = 8 ) r8_gamma_log
integer ( kind = 4 ) x(b)
c_sum = sum ( c(1:b) )
pdf_log = - r8_gamma_log ( c_sum + real ( a, kind = 8 ) ) &
+ r8_gamma_log ( c_sum ) &
+ r8_gamma_log ( real ( a + 1, kind = 8 ) )
do i = 1, b
pdf_log = pdf_log + r8_gamma_log ( c(i) + real ( x(i), kind = 8 ) ) &
- r8_gamma_log ( c(i) ) - r8_gamma_log ( real ( x(i) + 1, kind = 8 ) )
end do
pdf = exp ( pdf_log )
return
end
subroutine dirichlet_sample ( n, a, seed, x )
!*****************************************************************************80
!
!! DIRICHLET_SAMPLE samples the Dirichlet PDF.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 23 November 1999
!
! Author:
!
! John Burkardt
!
! Reference:
!
! Jerry Banks, editor,
! Handbook of Simulation,
! Engineering and Management Press Books, 1998, page 169.
!
! Parameters:
!
! Input, integer ( kind = 4 ) N, the number of components.
!
! Input, real ( kind = 8 ) A(N), the probabilities for each component.
! Each A(I) should be nonnegative, and at least one should be
! positive.
!
! Input/output, integer ( kind = 4 ) SEED, a seed for the random
! number generator.
!
! Output, real ( kind = 8 ) X(N), a sample of the PDF. The entries
! of X should sum to 1.
!
implicit none
integer ( kind = 4 ) n
real ( kind = 8 ) a(n)
real ( kind = 8 ) a2
real ( kind = 8 ) b2
real ( kind = 8 ) c2
integer ( kind = 4 ) i
integer ( kind = 4 ) seed
real ( kind = 8 ) x(n)
a2 = 0.0D+00
b2 = 1.0D+00
do i = 1, n
c2 = a(i)
call gamma_sample ( a2, b2, c2, seed, x(i) )
end do
!
! Rescale the vector to have unit sum.
!
call r8vec_unit_sum ( n, x )
return
end
subroutine discrete_cdf_inv ( cdf, a, b, x )
!*****************************************************************************80
!
!! DISCRETE_CDF_INV inverts the Discrete CDF.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 05 December 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, real ( kind = 8 ) CDF, the value of the CDF.
! 0.0 <= CDF <= 1.0.
!
! Input, integer ( kind = 4 ) A, the number of probabilities assigned.
!
! Input, real ( kind = 8 ) B(A), the relative probabilities of outcomes
! 1 through A. Each entry must be nonnegative.
!
! Output, integer ( kind = 4 ) X, the corresponding argument for which
! CDF(X-1) < CDF <= CDF(X)
!
implicit none
integer ( kind = 4 ) a
real ( kind = 8 ) b(a)
real ( kind = 8 ) b_sum
real ( kind = 8 ) cdf
real ( kind = 8 ) cum
integer ( kind = 4 ) j
integer ( kind = 4 ) x
if ( cdf < 0.0D+00 .or. 1.0D+00 < cdf ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'DISCRETE_CDF_INV - Fatal error!'
write ( *, '(a)' ) ' CDF < 0 or 1 < CDF.'
stop
end if
b_sum = sum ( b(1:a) )
cum = 0.0D+00
do j = 1, a
cum = cum + b(j) / b_sum
if ( cdf <= cum ) then
x = j
return
end if
end do
x = a
return
end
subroutine discrete_sample ( a, b, seed, x )
!*****************************************************************************80
!
!! DISCRETE_SAMPLE samples the Discrete PDF.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 05 December 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, integer ( kind = 4 ) A, the number of probabilities assigned.
!
! Input, real ( kind = 8 ) B(A), the relative probabilities of
! outcomes 1 through A. Each entry must be nonnegative.
!
! Input/output, integer ( kind = 4 ) SEED, a seed for the random
! number generator.
!
! Output, integer ( kind = 4 ) X, a sample of the PDF.
!
implicit none
integer ( kind = 4 ) a
real ( kind = 8 ) b(a)
real ( kind = 8 ) b_sum
real ( kind = 8 ) cdf
real ( kind = 8 ) r8_uniform_01
integer ( kind = 4 ) seed
integer ( kind = 4 ) x
b_sum = sum ( b(1:a) )
cdf = r8_uniform_01 ( seed )
call discrete_cdf_inv ( cdf, a, b, x )
return
end
subroutine event_process ( acid_num, alpha, beta, comp_num, p, p_hat, &
site_num, x_sample )
!*****************************************************************************80
!
!! EVENT_PROCESS updates the mixture weight distribution parameters.
!
! Discussion:
!
! This routine updates the values of ALPHA. It does this by
! considering the results of the most recent event. If we knew
! which component PDF had generated the event, then we would
! simply add 1 to the ALPHA for that component. Instead, we
! use Bayesian analysis to estimate the proportion of the event
! that is to be added to each ALPHA.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 18 December 1999
!
! Author:
!
! John Burkardt
!
! Reference:
!
! BS Everitt, DJ Hand,
! Finite Mixture Distributions,
! Chapman and Hall, 1981.
!
! AFM Smith, UE Makov,
! A Quasi-Bayes Sequential Procedure for Mixtures,
! Journal of the Royal Statistical Society,
! Volume 40, Number 1, B, 1978, pages 106-112.
!
! Parameters:
!
! Input, integer ( kind = 4 ) ACID_NUM, the number of amino acids.
!
! Input/output, real ( kind = 8 ) ALPHA(COMP_NUM), the Dirichlet parameters
! for the weights.
!
! Input, real ( kind = 8 ) BETA(ACID_NUM,COMP_MAX); BETA(I,J) is the
! multinomial Dirichlet parameter for the J-th acid in the I-th Dirichlet
! mixture component.
!
! Input, integer ( kind = 4 ) COMP_NUM, the number of components in the
! Dirichlet mixture.
!
! Input/output, real ( kind = 8 ) P(COMP_NUM); P(I) is the Bayesian
! posterior probability of component I, given the observation of the most
! recent event, which is proportional to the probability of the event under
! the component I PDF, times the prior probability of component I.
!
! Input/output, real ( kind = 8 ) P_HAT(COMP_NUM), the prior probablities
! of the components.
!
! Input, integer ( kind = 4 ) SITE_NUM, the number of sites observed for
! this event. This value might change from call to call, although in the
! demonstration it is kept fixed.
!
! Input, integer ( kind = 4 ) X_SAMPLE(ACID_NUM), the "current event",
! namely, the count vector for the number of occurrences of each acid out
! of the total of SITE_NUM sites analyzed. This is the evidence used to
! update the "theory" for the value of ALPHA.
!
implicit none
integer ( kind = 4 ) acid_num
integer ( kind = 4 ) comp_num
real ( kind = 8 ) alpha(comp_num)
real ( kind = 8 ) alpha_sum
real ( kind = 8 ) beta(acid_num,comp_num)
integer ( kind = 4 ) comp_i
real ( kind = 8 ) comp_pdf
real ( kind = 8 ) p(comp_num)
real ( kind = 8 ) p_hat(comp_num)
integer ( kind = 4 ) site_num
integer ( kind = 4 ) x_sample(acid_num)
!
! Sum the parameters.
!
alpha_sum = sum ( alpha(1:comp_num) )
!
! Update P_HAT.
!
do comp_i = 1, comp_num
p_hat(comp_i) = ( ( alpha_sum - 1.0D+00 ) * p_hat(comp_i) + p(comp_i) ) &
/ alpha_sum
end do
!
! Generate the new P's.
! P(COMP_I) = the Bayesian posterior probability of component I,
! given the observation of event EVENT_I, which is proportional
! to the probability of event EVENT_I in the component I PDF,
! times the prior probability of component I.
!
do comp_i = 1, comp_num
call dirichlet_multinomial_pdf ( x_sample, site_num, acid_num, &
beta(1,comp_i), comp_pdf )
p(comp_i) = comp_pdf * p_hat(comp_i)
end do
!
! Normalize the P's.
!
if ( sum ( p(1:comp_num) ) == 0.0D+00 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'EVENT_PROCESS - Fatal error!'
write ( *, '(a)' ) ' The P''s sum to 0.'
stop
end if