/* Dirichlet process (DP), see https://en.wikipedia.org/wiki/Dirichlet_process Samples are drawn from a base distribution. New samples have a nonzero probability of being equal to already sampled values. The process depends on a parameter alpha (concentration parameter): with alpha->0, a single value is sampled, with alpha->infinite the distribution is equal to the base distribution. In this example the base distribution is a Gaussian with mean 0 and variance 1, as in https://en.wikipedia.org/wiki/Dirichlet_process#/media/File:Dirichlet_process_draws.svg To model the process, this example uses the Chinese Restaurant Process: # Draw X_{1} from the base distribution H. # For n>1: :::: a) With probability \frac{\alpha}{\alpha+n-1} draw X_{n} from H. :::: b) With probability \frac{n_{x}}{\alpha+n-1} set X_{n}=x, where n_{x} is the number of previous observations X_{j}, j, such that X_{j}=x. The example queries show both the distribution of indexes and values of the DP. Moreover, they show the distribution of unique indexes as in http://www.robots.ox.ac.uk/~fwood/anglican/examples/viewer/?worksheet=nonparametrics/dp-mixture-model */ /** ?- hist_val(2000,100). % show the distribution of values over 2000 samples from a DP with concentration parameter 10. Should look % like row 2 of https://en.wikipedia.org/wiki/Dirichlet_process#/media/File:Dirichlet_process_draws.svg */ :- use_module(library(mcintyre)). :- use_module(library(cplint_r)). :- mc. :- begin_lpad. % dp_n_values(N0,N,Alpha,L,Counts0,Counts) % returns in L a list of N-N0 samples from the DP with concentration parameter % Alpha and initial counts Counts0. Also returns the updated counts Counts dp_n_values(N,N,_Alpha,[],Counts,Counts):-!. % dp_value(NV,Alpha,V) % returns in V the NVth sample from the DP with concentration parameter % Alpha dp_n_values(N0,N,Alpha,[[V]-1|Vs],Counts0,Counts):- N0