/*
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 from the base distribution .
# For :
:::: a) With probability draw from .
:::: b) With probability set , where is the number of previous observations
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