123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353(** Type signatures. *)(* ------------------------------------------------------------------------- *)(** The type of (inclusive) ranges. *)typerange={min:float;max:float}(* ------------------------------------------------------------------------- *)(** An ['a iterator] abstracts a finite sequence of elements. *)type'aiterator=('a->unit)->unit(* ------------------------------------------------------------------------- *)(** Primitive {e representations} of distributions: empirical, generative or
finitely supported. *)(** ['a emp] is the type of empirical measures *)type'aemp='aarray(** ['a gen] is the type of mutable samplers of type ['a] with state of type ['s] *)type('s,'a)gen='s->'atype('a,'r)fin_fun=('aiterator,'a,'r)Vec.vec(** [fin_mes] is the type of finitely supported measures. *)type('a,'r)fin_mes=Mof{total_mass:'r;fn:('a,'r)fin_fun}(** [fin_prb] is the type of finitely supported probability measures. *)type('a,'r)fin_prb=Pof{fn:('a,'r)fin_fun}(* ------------------------------------------------------------------------- *)(** PRNG interface, subset of {!Random.State} and [Pringo]. *)moduletypeStateful_PRNG=sigtypetvalfloat:t->float->floatvalint:t->int->intvalbool:t->boolend(* ------------------------------------------------------------------------- *)(** Type classes. *)(** [Gen] allows to manipulate generative probabilities (ie samplers). *)moduletypeGen=sigtypestate(** Follows the module type of a sampling-based monad *)includeBasic_intf.Monadwithtype'at=(state,'a)genandtype'ares=(state,'a)gen(** [iid gen] transforms a sampler into a sequence sampler. *)valiid:'at->'aSeq.tt(** [float bound] samples uniformly in [0; bound] *)valfloat:float->floatt(** [int bound] samples uniformly in [0; bound-1] *)valint:int->intt(** [bool] samples a boolean uniformly *)valbool:boolt(** [shuffle a] samples a uniform permutation of [a]. Uses Fisher-Yates shuffle algorithm. *)valshuffle:'aarray->'aarrayt(** [uniform elts] samples an element of the [elts] array by sampling an index uniformly.
@raise Invalid_argument if [elts] has length equal to 0 *)valuniform:'aarray->'at(** [bernoulli alpha] samples [true] with probability [alpha].
@raise Invalid_argument if [alpha] is not in the [0;1] interval. *)valbernoulli:float->boolt(** [geometric p] samples a nonnegative integer according to the geometric law of parameter [p].
@raise Invalid_argument if [p <= 0 || p > 1] *)valgeometric:float->intt(** [subsample ~n gen] samples one out of [n] samples from [gen]. *)valsubsample:n:int->'at->'at(** [of_empirical emp] samples from [emp] matching exactly the empirical frequencies. *)valof_empirical:'aemp->'at(** Exponential distribution via inverse CDF.
@raise Invalid_argument if [rate <=. 0.0]. *)valexponential:rate:float->floatt(** Gaussian distribution via Box-Muller transform.
Returns a pair of {e independent} gaussian variates with
prescribed mean and standard deviation.
@raise Invalid_argument if [std <= 0.0] *)valbox_muller:mean:float->std:float->(float*float)t(** Gaussian distribution (wrapper over box-muller transform).
@raise Invalid_argument if [std <= 0.0] *)valgaussian:mean:float->std:float->floatt(** Poisson distribution via inverse transform. Consider using other methods
for large [lambda].
@raise Invalid_argument if [lambda <= 0.0] *)valpoisson:lambda:float->intt(** Samples uniformly in the given [range].
@raise Invalid_argument if [range] is empty. *)valrange:range->floatt(** Samples uniformly in the n-dimensional axis-aligned box defined by [min] and [max].
@raise Invalid_argument if [Array.length min != Array.length max], if the length of [min]
or [max] is zero or if there exists an index [i] such that [min.(i) > max.(i)]. *)valrectangle:min:floatarray->max:floatarray->floatarrayt(** Gamma distribution. *)valgamma:shape:float->scale:float->floatt(** Categorical distribution. Total mass need not be one. Does not aggregate mass of equal elements.
@raise Invalid_argument if some weights are negative or if the total mass is zero. *)valcategorical:('a*float)array->'at(** [without_replacement n l] samples a subset of size [n] from [l] without replacement.
@raise Invalid_argument if [n > List.length l]. *)valwithout_replacement:int->'alist->('alist*'alist)t(** [tup2 g1 g2] samples the component of a tuple independently from [g1] and [g2]. *)valtup2:'at->'bt->('a*'b)t(** See {!tup2} *)valtup3:'at->'bt->'ct->('a*'b*'c)t(** See {!tup2} *)valtup4:'at->'bt->'ct->'dt->('a*'b*'c*'d)t(** See {!tup2} *)valtup5:'at->'bt->'ct->'dt->'et->('a*'b*'c*'d*'e)t(** See {!tup2} *)valtup6:'at->'bt->'ct->'dt->'et->'ft->('a*'b*'c*'d*'e*'f)t(** [mixture weights gens] samples from the convex combination of [weights].
[weights] need to be normalized, non-negative floats.
@raise Invalid_argument if [mixture] is empty or if [weights] is not normalized. *)valmixture:floatarray->(int->'bt)->'btmoduleRational:sig(** Categorical distribution. Total mass need not be one. Does not aggregate mass of equal elements.
@raise Invalid_argument if some weights are negative or if the total mass is zero. *)valcategorical:('a*Q.t)array->'atendendmoduletypeFin_dist=sigtypestatetyper(** The type of finite probability measures with domain ['a] and range [r]. *)type'aprb=('a,r)fin_prb(** The type of finite measures with domain ['a] and range [r]. *)type'ames=('a,r)fin_mes(** {2:finite_generic Constructing [r]-valued finitely supported functions.} *)(** [from_fun len f] constructs a finite function with support [0; ...; len-1].
@raise Invalid_argument if [len < 0]
*)valof_fun:int->(int->r)->(int,r)fin_fun(** [from_array a] returns a finite function wrapping the array [a]. *)valof_array:rarray->(int,r)fin_fun(** [from_assoc h a] returns a finite function constructed from the bindings in [a].
The finite function is backed by a hash table whose implementation is given
through the first class module [h]. The behaviour of the function if elements
in the support appear more than once is unspecified. *)valof_assoc:(moduleHashtbl.Swithtypekey='k)->('k*r)array->('k,r)fin_fun(** Constructing measures and probabilities *)(** Creates a finitely supported {e measure} from a finite function.
A measure is not necessarily normalized.
@raise Invalid_argument if a point as negative mass. *)valmeasure:('a,r)fin_fun->'ames(** Creates a finitely supported {e probability} from a finite function.
A probability is normalized.
@raise Invalid_argument if a point as negative mass or if the total mass does not sum up to one. *)valprobability:('a,r)fin_fun->'aprb(** Forgetful map from the type of finite probabilities to the type of measures. *)valas_measure:'aprb->'ames(** Normalize a measure to obtain a probability measure.
@raise Invalid_argument if the measure has zero mass. *)valnormalize:'tmes->'tprb(** Computes the empirical measure of an array of elements. Each element
present in the array is mapped to its count. *)valcounts_of_empirical:(moduleHashtbl.Swithtypekey='t)->'tarray->'tmes(** Finitely supported uniform distribution. *)valuniform:'tarray->'tprb(** Biased coin. Raises an error if [bias] is not in [0,1]. *)valcoin:bias:r->boolprb(** Binomial distribution.
[binomial p n] returns the probability of having
[k] successes over [n] experiments, according to
a biased coin [p]. *)valbinomial:boolprb->int->intprb(** Using measures and probabilities *)(** Integrates a function against a finitely supported measure. *)valintegrate:'tmes->('t->r)->r(** Evaluates a finitely supported probability on argument. Returns 0 if
the argument is out of the support. *)valeval_prb:'tprb->'t->r(** Evaluates a finitely supported measure on argument. Returns 0 if
the argument is out of the support. *)valeval_mes:'tmes->'t->r(** Iterates the given function on the support of the probability. *)valiter_prb:'tprb->('t->r->unit)->unit(** Iterates the given function on the support of the measure. *)valiter_mes:'tmes->('t->r->unit)->unit(** Samples from a finitely supported distribution presented as an unnormalized
measure. This is mostly useful when sampling only once or twice from a
distribution: consider converting to a categorical sampler when sampling
repeatedly. Complexity: O(n) with [n] the cardinality of the support. *)valsample:'tmes->(state,'t)gen(** Returns the total mass associated to a finitely supported measure. *)valtotal_mass:'tmes->r(** Compute the mean of a finite measure supported on an [Intf.Module].*)valmean_generic:(moduleBasic_intf.Modulewithtypet='tandtypeR.t=r)->'tmes->'t(** Compute the mean of a finite measure supported by r. *)valmean:rmes->r(** Compute the variance of a finite measure supported by r. *)valvariance:rmes->r(** [quantile ord mes p] computes the [p]th quantile of [mes].
The underlying data is totally ordered by [ord].
@raise Invalid_argument if [p < 0 || p > 1] or if the total mass of [mes] is zero *)valquantile:(moduleBasic_intf.Orderedwithtypet='elt)->'eltmes->r->'elt(** Returns the raw data underlying a finitely supported measure. *)vallist_of_measure:'tmes->[>`Measureof('t*r)list](** Returns the raw data underlying a finitely supported probability. *)vallist_of_probability:'tprb->[>`Probabilityof('t*r)list]type'app:=Format.formatter->'a->unit(** Pretty print a measure, with elements sorted according to
the order relation on the support. *)valpp_fin_mes:'app->'amespp(** Pretty print a measure, with elements sorted by increasing measure.. *)valpp_fin_mes_by_measure:'app->'amespp(** Fold over the union of the supports of the given measures. *)valfold_union:(moduleHashtbl.Swithtypekey='t)->('t->r->r->'a->'a)->'tmes->'tmes->'a->'aend(** We use an OCamlgraph-compatible module type to describe
undirected graphs. We assume that all graphs are undirected
and simple. *)moduletypeGraph=sigtypetmoduleV:Basic_intf.Stdtypevertex=V.ttypeedgevalnb_vertex:t->intvalnb_edges:t->intvalout_degree:t->vertex->intvalmem_vertex:t->vertex->boolvalmem_edge:t->vertex->vertex->boolvalsucc:t->vertex->vertexlistvalsucc_e:t->vertex->edgelistvaliter_vertex:(vertex->unit)->t->unitvalfold_vertex:(vertex->'a->'a)->t->'a->'avaliter_edges:(vertex->vertex->unit)->t->unitvalfold_edges:(vertex->vertex->'a->'a)->t->'a->'avaliter_succ:(vertex->unit)->t->vertex->unitvalfold_succ:(vertex->'a->'a)->t->vertex->'a->'aend