Skip to contents

A function to sample Dirichlet random variables, based on the representation as ratios of Gamma. Note that the RNG will generate on the full simplex and the sum to one constraint is respected here

Usage

rdir(n, alpha, normalize = TRUE)

Arguments

n

sample size

alpha

vector of parameter

normalize

boolean. If FALSE, the function returns Gamma variates with parameter alpha.

Value

sample of dimension d (size of alpha) from the Dirichlet distribution.

Examples

rdir(n=100, alpha=c(0.5,0.5,2),TRUE)
#>                [,1]         [,2]       [,3]
#>   [1,] 1.200983e-02 4.172676e-01 0.57072260
#>   [2,] 5.469671e-02 2.054171e-01 0.73988622
#>   [3,] 6.406752e-02 1.706156e-01 0.76531684
#>   [4,] 1.049807e-01 1.090282e-01 0.78599105
#>   [5,] 2.131522e-01 4.015647e-01 0.38528307
#>   [6,] 4.937827e-01 2.493132e-01 0.25690403
#>   [7,] 1.893922e-01 3.699422e-02 0.77361354
#>   [8,] 2.674865e-01 3.825638e-02 0.69425710
#>   [9,] 1.523943e-01 6.924892e-03 0.84068085
#>  [10,] 2.801341e-02 1.648385e-01 0.80714804
#>  [11,] 3.402999e-03 2.185654e-02 0.97474046
#>  [12,] 4.130989e-01 4.723748e-02 0.53966358
#>  [13,] 3.538784e-01 1.413089e-01 0.50481263
#>  [14,] 8.780496e-03 9.022994e-02 0.90098956
#>  [15,] 1.246059e-02 4.378388e-02 0.94375553
#>  [16,] 4.114750e-01 1.988259e-03 0.58653677
#>  [17,] 1.176403e-01 2.721815e-01 0.61017814
#>  [18,] 6.047398e-03 9.486561e-04 0.99300395
#>  [19,] 2.084432e-03 2.942638e-01 0.70365178
#>  [20,] 2.088064e-03 1.198983e-01 0.87801367
#>  [21,] 1.312465e-02 1.018949e-01 0.88498041
#>  [22,] 8.138247e-02 4.552035e-01 0.46341402
#>  [23,] 9.229277e-02 6.089298e-02 0.84681425
#>  [24,] 3.564092e-01 4.141133e-02 0.60217951
#>  [25,] 8.003441e-01 1.125936e-01 0.08706232
#>  [26,] 7.149568e-02 1.226917e-01 0.80581260
#>  [27,] 2.222732e-01 2.205181e-02 0.75567496
#>  [28,] 1.125834e-01 1.550446e-01 0.73237200
#>  [29,] 6.711140e-02 4.486071e-02 0.88802789
#>  [30,] 5.717953e-02 5.289418e-02 0.88992630
#>  [31,] 8.568339e-03 6.136955e-01 0.37773615
#>  [32,] 1.630189e-01 2.921480e-01 0.54483315
#>  [33,] 8.074463e-02 1.431286e-02 0.90494250
#>  [34,] 3.278688e-02 4.894133e-02 0.91827179
#>  [35,] 2.480135e-01 2.443238e-01 0.50766277
#>  [36,] 3.125562e-05 9.249583e-02 0.90747291
#>  [37,] 6.226968e-02 3.535658e-01 0.58416455
#>  [38,] 4.789866e-02 4.614443e-02 0.90595691
#>  [39,] 2.071593e-02 4.542368e-02 0.93386039
#>  [40,] 4.342041e-01 8.308693e-02 0.48270895
#>  [41,] 2.615101e-01 1.435479e-03 0.73705446
#>  [42,] 1.132727e-01 6.174095e-01 0.26931783
#>  [43,] 1.847804e-01 3.010621e-01 0.51415749
#>  [44,] 7.500331e-02 2.235102e-02 0.90264566
#>  [45,] 5.287830e-01 2.525525e-02 0.44596172
#>  [46,] 1.644847e-01 9.673928e-03 0.82584135
#>  [47,] 1.581462e-02 2.638467e-01 0.72033867
#>  [48,] 3.119425e-01 1.836264e-01 0.50443115
#>  [49,] 4.605569e-01 7.662288e-03 0.53178081
#>  [50,] 4.177635e-02 3.376489e-02 0.92445876
#>  [51,] 3.422355e-01 2.409590e-02 0.63366864
#>  [52,] 1.218716e-01 2.720594e-02 0.85092250
#>  [53,] 7.769923e-04 5.652049e-01 0.43401808
#>  [54,] 5.811493e-03 3.373798e-02 0.96045052
#>  [55,] 3.300086e-02 2.534860e-01 0.71351309
#>  [56,] 5.335497e-02 4.110373e-02 0.90554130
#>  [57,] 1.109180e-01 7.903796e-01 0.09870238
#>  [58,] 2.574708e-01 6.970949e-03 0.73555826
#>  [59,] 3.068043e-01 7.025678e-02 0.62293887
#>  [60,] 6.149685e-02 5.288879e-01 0.40961520
#>  [61,] 5.187238e-01 1.859628e-02 0.46267992
#>  [62,] 2.089576e-03 1.170371e-01 0.88087334
#>  [63,] 3.723216e-01 7.901065e-03 0.61977737
#>  [64,] 2.089106e-01 2.081650e-01 0.58292444
#>  [65,] 1.798348e-03 5.386103e-01 0.45959131
#>  [66,] 4.095432e-03 8.521753e-02 0.91068703
#>  [67,] 1.733650e-01 4.671569e-01 0.35947812
#>  [68,] 2.609678e-05 1.636740e-01 0.83629987
#>  [69,] 3.447685e-02 1.874008e-01 0.77812236
#>  [70,] 3.430355e-01 3.144238e-01 0.34254067
#>  [71,] 1.562232e-03 6.978821e-02 0.92864956
#>  [72,] 5.126192e-02 2.040448e-02 0.92833360
#>  [73,] 4.103396e-04 5.497340e-01 0.44985570
#>  [74,] 3.654622e-03 2.577588e-01 0.73858661
#>  [75,] 2.162556e-01 1.050761e-01 0.67866826
#>  [76,] 5.692530e-02 2.399276e-02 0.91908194
#>  [77,] 1.884541e-01 2.266699e-03 0.80927920
#>  [78,] 1.780667e-01 2.487067e-01 0.57322659
#>  [79,] 1.070752e-02 5.786320e-03 0.98350616
#>  [80,] 7.981752e-03 3.128351e-01 0.67918312
#>  [81,] 2.393843e-01 9.281915e-03 0.75133378
#>  [82,] 5.047663e-01 3.500419e-02 0.46022952
#>  [83,] 2.729660e-02 7.430474e-05 0.97262909
#>  [84,] 1.032517e-01 2.359163e-01 0.66083195
#>  [85,] 1.178731e-05 6.747181e-01 0.32527010
#>  [86,] 8.487109e-05 8.851786e-02 0.91139727
#>  [87,] 1.780460e-03 1.259592e-03 0.99695995
#>  [88,] 6.573323e-03 2.694464e-01 0.72398027
#>  [89,] 1.281572e-01 1.340206e-03 0.87050259
#>  [90,] 7.597475e-03 6.696830e-02 0.92543422
#>  [91,] 5.634050e-02 6.151003e-02 0.88214948
#>  [92,] 2.008203e-01 7.968956e-02 0.71949018
#>  [93,] 2.423393e-01 3.425374e-01 0.41512326
#>  [94,] 2.224921e-01 4.221808e-03 0.77328608
#>  [95,] 2.147221e-01 3.606076e-02 0.74921715
#>  [96,] 1.708264e-01 4.086208e-01 0.42055276
#>  [97,] 9.673919e-02 1.500722e-01 0.75318862
#>  [98,] 1.159670e-01 9.652595e-02 0.78750708
#>  [99,] 4.442472e-01 1.686341e-01 0.38711876
#> [100,] 2.817304e-01 2.004118e-03 0.71626550
rdir(n=100, alpha=c(3,1,2),FALSE)
#>             [,1]       [,2]      [,3]
#>   [1,] 3.5806223 0.16429088 1.4363330
#>   [2,] 3.5863489 0.48675001 0.2945734
#>   [3,] 0.5191856 0.56571851 1.3476165
#>   [4,] 3.0572454 0.75870964 0.4957867
#>   [5,] 0.6712289 2.81031922 1.3739499
#>   [6,] 1.7766274 0.51491199 5.7753507
#>   [7,] 5.1980112 0.74680870 0.8712667
#>   [8,] 0.7832317 0.32076218 1.8251169
#>   [9,] 1.8164491 1.50911787 1.4199056
#>  [10,] 3.8437073 0.63321007 0.1707316
#>  [11,] 3.0454307 3.16483584 3.1156613
#>  [12,] 5.8064077 0.14074295 3.6765402
#>  [13,] 1.8641454 0.84557895 7.4137553
#>  [14,] 1.1736300 1.00919732 0.7443287
#>  [15,] 2.9713201 1.65073095 1.7703679
#>  [16,] 4.1716465 1.01726562 1.8056013
#>  [17,] 2.9324668 0.13107347 1.4503016
#>  [18,] 5.2781363 1.82068474 3.9430276
#>  [19,] 2.2138287 0.44560083 4.5642070
#>  [20,] 3.4872290 1.06530574 1.3334971
#>  [21,] 0.5402611 1.32500832 0.7437200
#>  [22,] 4.9117105 0.72817266 2.2953126
#>  [23,] 5.2280549 1.98474551 4.3790928
#>  [24,] 5.2264994 0.53324337 0.7037596
#>  [25,] 1.3405926 0.43628516 5.1107455
#>  [26,] 3.4540471 1.36200549 1.3822771
#>  [27,] 1.5716870 3.12210603 3.6317457
#>  [28,] 2.6902309 0.36198147 4.3218787
#>  [29,] 6.0405311 1.10781875 1.7560398
#>  [30,] 5.9088640 0.74108451 0.3513223
#>  [31,] 1.7903857 0.41823483 0.1108663
#>  [32,] 4.0502163 1.18281067 1.4690982
#>  [33,] 3.9763782 1.74477696 4.7056762
#>  [34,] 1.5329652 4.01119593 1.0538613
#>  [35,] 1.9134197 1.62256869 1.4834122
#>  [36,] 3.8662030 0.29514039 4.5781610
#>  [37,] 1.4242328 3.06456252 0.6588286
#>  [38,] 2.3109077 0.58534591 0.8717445
#>  [39,] 3.8799819 0.17628655 0.4648718
#>  [40,] 1.0383487 0.69333225 0.6367161
#>  [41,] 1.7879741 1.00494232 4.4566953
#>  [42,] 4.0967402 0.62548480 5.0710935
#>  [43,] 4.3214311 0.62970742 1.6134154
#>  [44,] 2.0034124 1.29790982 3.2159645
#>  [45,] 1.6896058 0.70339678 1.5912472
#>  [46,] 1.1215118 1.00394040 1.2300292
#>  [47,] 2.5165104 0.51222061 1.3831104
#>  [48,] 3.8222020 0.23210909 1.2181405
#>  [49,] 6.8946794 1.43513583 1.4746009
#>  [50,] 1.0387056 0.53340385 4.0083240
#>  [51,] 4.5091487 0.13756207 2.0511536
#>  [52,] 1.3306141 0.58306527 3.5368835
#>  [53,] 1.7000354 0.16237637 0.9035040
#>  [54,] 4.5675359 1.62943492 0.5030574
#>  [55,] 3.7579045 4.35371832 0.5061478
#>  [56,] 4.5876322 0.58013214 0.3782676
#>  [57,] 7.0260941 0.12751991 1.7001996
#>  [58,] 4.4090710 0.47543202 6.3471173
#>  [59,] 2.1176563 1.75183389 0.9398455
#>  [60,] 1.5590015 0.41056802 4.1161434
#>  [61,] 1.9336569 1.33021813 5.0938611
#>  [62,] 0.9604132 0.31577534 1.0847309
#>  [63,] 3.2304251 1.80456514 2.4803022
#>  [64,] 1.1478336 1.52828673 1.4989679
#>  [65,] 3.8511378 1.12096715 1.3281819
#>  [66,] 3.1761339 3.12547140 1.8640920
#>  [67,] 3.5362214 1.10382615 2.0277034
#>  [68,] 0.8296751 0.07163743 1.2411745
#>  [69,] 1.6655442 7.83223917 0.7619994
#>  [70,] 4.4486161 2.66071054 2.4260625
#>  [71,] 2.7995609 3.80713900 1.2952952
#>  [72,] 6.9166534 0.02190433 1.6299354
#>  [73,] 2.9564156 3.01301721 3.4486462
#>  [74,] 2.3180321 0.40314724 6.5958056
#>  [75,] 3.0984318 0.44313427 1.4009734
#>  [76,] 4.1619789 1.06433016 3.3788266
#>  [77,] 2.2406345 2.37179573 0.5126196
#>  [78,] 2.8405126 1.36827498 1.0031464
#>  [79,] 3.0856193 0.89704088 3.0025300
#>  [80,] 3.2910129 0.86574998 0.5113335
#>  [81,] 4.0228616 1.46537447 1.9375124
#>  [82,] 0.4335552 2.09720027 1.9818001
#>  [83,] 0.4600798 0.97915794 1.3472679
#>  [84,] 1.7154897 1.48739214 1.1390135
#>  [85,] 1.4581621 0.49433370 1.6054939
#>  [86,] 7.2077960 0.62156778 1.7869377
#>  [87,] 2.3611707 2.50124587 0.4119828
#>  [88,] 1.4592564 1.53732454 0.3379988
#>  [89,] 0.9967300 0.34538431 1.5476422
#>  [90,] 0.7037616 3.28107375 1.0323901
#>  [91,] 7.5508363 1.26627765 1.9617336
#>  [92,] 4.4722093 0.37999313 4.5241559
#>  [93,] 1.5447284 0.07216783 1.3333920
#>  [94,] 0.8987009 0.86676044 1.5147813
#>  [95,] 1.5747097 0.27642754 1.4067947
#>  [96,] 1.8487052 0.39434759 1.3526483
#>  [97,] 2.3353823 3.36257705 0.8735704
#>  [98,] 4.9237932 0.74601446 1.0115178
#>  [99,] 6.2148871 1.98701041 0.5484934
#> [100,] 1.6386062 0.93499838 0.5562714