1:- module(prob_tagless,
2 [ uniform01//1
3 , uniform//2, uniformP//2
4 , normal//1
5 , gaussian//3
6 , exponential//1
7 , poisson//2
8 , stable//3
9 , dirichlet//2
10 , discrete//2
11 , discrete//3
12 , binomial//3
13 , beta//3
14 , zeta//2
15 , gamma//2
16 , inv_gamma//2
17 , bernoulli//2
18 , students_t//2
19 , mixture//3
20 , pair//3
21 ]).
32:- module_transparent stream/2. 33 34:- use_module(library(dcg_core)). 35:- use_module(library(dcg_pair)). 36:- use_module(library(plrand),[]). 37 38term_expansion(stub(Arity,Name,Pred), Head --> plrand:Body) :- 39 length(Args, Arity), 40 Head =.. [Name | Args], 41 Body =.. [Pred | Args].
45bernoulli(P,X) --> plrand:sample_Uniform01(U), {U<P->X=1;X=0}.
52stub(3,binomial,sample_Binomial).
57stub(2,poisson,sample_Poisson).
61discrete(Ps,I) --> {length(Ps,N)}, plrand:sample_Discrete(N,Ps,I).
65discrete(Xs,Ps,X) --> {length(Ps,N)}, plrand:sample_Discrete(N,Ps,I), {nth1(I,Xs,X)}.
70stub(1,uniform01,sample_Uniform01).
74stub(1,normal,sample_Normal).
78stub(1,exponential,sample_Exponential).
82stub(3,stable,sample_Stable).
86dirichlet(A,X) --> {length(A,N)}, plrand:sample_Dirichlet(N,A,X).
list(A)
-> expr(A)
.
94uniform(O,X) -->
95 {length(O,N)},
96 plrand:sample_Uniform01(U),
97 {I is 1+floor(N*U), nth1(I,O,X)}.
call(P,X)
.101:- meta_predicate uniformP( , , , ). 102uniformP(P,X) --> 103 {findall(Y,call(P,Y),YY)}, 104 uniform(YY,X).
108stub(3,beta,sample_Beta).
113stub(2,zeta,sample_Zeta).
117stub(2,gamma,sample_Gamma). 118 119 120% ^ above use plrand samplers and need randstate 121% ---------------------- DERIVED DISTRIBUTIONS --------------------- 122% V below do not use state directly.
expr(float)
.
Sample from Gaussian with given mean and variance.
127gaussian(Mean, Var, X) --> normal(U), {X is Mean + Var*U}.
131inv_gamma(A,X) --> gamma(A,Y), {X is 1/Y}.
135pareto(A,X) --> uniform01(Y), { X is (1-Y)**(-1/A) }.
139students_t(V,X)--> {V1 is V/2}, normal(Z), gamma(V1,Y), {X is Z*sqrt(V1/Y)}.
144pair(F,G,X-Y) --> call(F,X), call(G,Y).
mixture :: \(list(expr(A))
, list(prob)
) -> expr(A)
.
153mixture( Sources, Dist, X) -->
154 discrete(Dist,I),
155 {nth1(Sources,I,S)},
156 call(S,X)
Random predicates, untagged RNG state
This module provides a set of predicates for sampling from various distributions. The state of the random generator is threaded through using the DCG idiom.