This file contains data structures that implement Dirichlet processes and hierarchical Dirichlet processes. This file does not implement inference algorithms for these structures. See hdp/mcmc.h for examples that use DPs and HDPs and perform inference.
A Dirichlet process (DP) can be understood as a distribution over distributions. That is, samples from a Dirichlet process are themselves distributions. A Dirichlet process is characterized by two parameters: a real-valued concentration parameter $ \alpha > 0 $, and a base distribution $ H $. So if we let $ G $ be a random variable distributed according to a Dirichlet process with parameters $ \alpha $ and $ H $, we can express this as:
There are a handful of equivalent representations of Dirichlet processes. One such representation is the Chinese restaurant process. Imagine a restaurant with an infinite number of tables, numbered $ 0, 1, \ldots $ On each table is an independent sample from the base distribution $ H $. When the first customer walks into the restaurant, they sit at table $ 0 $. When the second customer enters, they either sit at table $ 0 $ with probability $ 1/(1 + \alpha) $ or they sit at table $ 1 $ with probability $ \alpha/(1 + \alpha) $. More generally, when the $ n $-th customer enters the restaurant, they choose to sit at a non-empty table $ i $ with probability proportional to the number of people sitting at that table, or they sit at the next empty table with probability proportional to $ \alpha $.
This process is equivalent to the process of drawing samples from $G$.
It is impossible to write a closed-form expression for the distribution $ G $, since its specification requires an infinite amount of information. But for the useful applications of the Dirichlet process, this is not necessary.
A hierarchical Dirichlet process (HDP) is a hierarchy of random variables, where each random variable is distributed according to a Dirichlet process with base distribution given by the parent in the hierarchy. To be more precise, given a tree $ T $. Every node $ \textbf{n} \in T $ is associated with a random variable $ G_{\textbf{n}} $ such that
The HDP allows statistical information to be shared across groups, and is useful for modeling clustered data.
DPs and HDPs are frequently used in mixture models, where the samples from the DP/HDP are not themselves directly observed. Rather, they are inputs to another distribution, which in turn, provides the observed samples. For example, the following is a simple DP mixture model, where the base distribution is Beta and the likelihood is Bernoulli:
Each node in the HDP hierarchy has a collection of child nodes. Each child node is indexed by an unsigned int
key. This is a special key that represents the set of all keys.
Represents a non-root node in an HDP hierarchy.
To use this structure or hdp, an inference method is required. See hdp/mcmc.h for an example.
K | the generic type of the observations drawn from this distribution. |
V | the type of the probabilities. |
Public members | |
---|---|
array_map< unsigned int, node< K, V > > | children |
V | alpha |
V | log_alpha |
array< K > | observations |
node (const V & alpha) | |
V | get_alpha () const |
V | get_log_alpha () const |
static void | move (const node< K, V > & src, node< K, V > & dst) |
static void | swap (node< K, V > & first, node< K, V > & second) |
static void | free (node< K, V > & n) |
typedef | K atom_type |
typedef | V value_type |
The child nodes of this node in the HDP hierarchy.
The concentration parameter $ \alpha $ at this node.
The natural logarithm of node::alpha.
The observations drawn from this node.
const V & | alpha | ) |
Constructs an HDP node with the given concentration parameter alpha
and no child nodes or observations.
Returns the concentration parameter node::alpha.
Returns the logarithm of the concentration parameter node::log_alpha.
Moves the HDP node from src
to dst
.
Swaps the HDP node in first
and second
.
Releases the memory resources associated with the HDP node n
.
The type of the observations drawn from this distribution.
The type of the probabilities.
Initializes the given HDP node with the concentration parameter alpha
and no child nodes or observations.
const node< K, V > & | src, | |
node< K, V > & | dst, | |
hash_map< const node< K, V > *, node< K, V > * > & | node_map | ) |
Copies the HDP node from src
to dst
(as well as its descendants, recursively), while adding a entries to node_map
mapping the pointers of every source node to the pointer of the corresponding destination node.
A scribe structure useful for reading/writing/printing node and hdp objects.
Public members | |
---|---|
AtomScribe & | atom_scribe |
KeyScribe & | key_scribe |
node_scribe (AtomScribe & atom_scribe, KeyScribe & key_scribe) |
The scribe for reading/writing/printing observations drawn from HDP nodes.
The scribe for reading/writing/printing the keys that index the children at each HDP node.
AtomScribe & | atom_scribe, | |
KeyScribe & | key_scribe | ) |
Constructs the node_scribe with the given atom scribe and the key scribe.
node< K, V > & | node, | |
FILE * | in, | |
node_scribe< AtomReader, KeyReader > & | node_reader | ) |
node
(and all descendants) from in
.AtomScribe | a scribe type for which the function |
KeyScribe | a scribe type for which the function |
const node< K, V > & | node, | |
FILE * | out, | |
node_scribe< AtomWriter, KeyWriter > & | node_writer | ) |
node
(and all descendants) to out
.AtomScribe | a scribe type for which the function |
KeyScribe | a scribe type for which the function |
Represents the root node in an HDP hierarchy (or equivalently, a single Dirichlet process). The hierarchy has a maximum depth, as specified by the hdp::depth variable. When new nodes are added to the HDP using the function add, they are initialized using the concentration parameter in hdp::alpha that corresponds to the level of the node in the tree. However, the concentration parameter can easily be changed by the user after the node is created. In addition, the user can add nodes manually to the hierarchy.
To use this structure, an inference method is required. See hdp/mcmc.h for an example.
BaseDistribution | the type of the base distribution. |
DataDistribution | the type of the likelihood (as in a DP/HDP mixture model). |
K | the generic type of the observations drawn from this distribution. |
V | the type of the probabilities. |
Public members | |
---|---|
BaseDistribution | pi |
unsigned int | depth |
V * | alpha |
V | log_alpha |
array_map< unsigned int, node< K, V > > | children |
array< K > | observations |
hdp (const BaseParameters & base_params, const V * alpha, unsigned int depth) | |
V | get_alpha () const |
V | get_log_alpha () const |
static void | free (hdp< BaseDistribution, DataDistribution, K, V > & h) |
typedef | K atom_type |
typedef | V value_type |
typedef | BaseDistribution base_distribution_type |
typedef | DataDistribution data_distribution_type |
The base distribution.
The depth of the HDP hierarchy, where a depth of 1 indicates that the root node is the only node in the hierarchy.
An array of concentration parameters, with length hdp::depth, one for each level in the HDP.
The natural logarithm of the concentration parameter of this node (i.e. hdp::alpha[0]
).
The child nodes of this root node in the HDP hierarchy.
The observations sampled from this distribution.
const BaseParameters & | base_params, | |
const V * | alpha, | |
unsigned int | depth | ) |
Constructs this HDP root node with the given parameters to the base distribution base_params
, the given array of concentration parameters alpha
(one for every level in the hierarchy), and the given depth
of the hierarchy.
BaseParameters | the type of the parameters passed to the constructor of BaseDistribution. |
Returns the concentration parameter of this root node.
Returns the natural logarithm of the concentration parameter of this root node.
Releases the memory resources of this HDP root node.
The generic type of the observations drawn from this distribution.
The type of the probabilities.
The type of the base distribution.
The type of the likelihood (as in a DP/HDP mixture model).
hdp< BaseDistribution, DataDistribution, K, V > & | h, | |
BaseParameters & | base_params, | |
const V * | alpha, | |
unsigned int | depth | ) |
Initializes the given HDP root node h
with the given parameters to the base distribution base_params
, the given array of concentration parameters alpha
(one for every level in the hierarchy), and the given depth
of the hierarchy.
BaseParameters | the type of the parameters passed to the constructor of BaseDistribution. |
const hdp< BaseDistribution, DataDistribution, K, V > & | src, | |
hdp< BaseDistribution, DataDistribution, K, V > & | dst, | |
hash_map< const node< K, V > *, node< K, V > * > & | node_map | ) |
Copies the HDP root node (as well as its descendants, recursively) from src
to dst
, while adding a entries to node_map
mapping the pointers of every source node to the pointer of the corresponding destination node.
hdp< BaseDistribution, DataDistribution, K, V > & | h, | |
FILE * | in, | |
BaseDistributionScribe & | base_reader, | |
AtomScribe & | atom_reader, | |
KeyScribe & | key_reader | ) |
h
(and all descendants) from in
.BaseDistributionScribe | a scribe type for which the function |
AtomScribe | a scribe type for which the function |
KeyScribe | a scribe type for which the function |
const hdp< BaseDistribution, DataDistribution, K, V > & | h, | |
FILE * | out, | |
BaseDistributionScribe & | base_writer, | |
AtomScribe & | atom_writer, | |
KeyScribe & | key_writer | ) |
h
(and all descendants) to out
.BaseDistributionScribe | a scribe type for which the function |
AtomScribe | a scribe type for which the function |
KeyScribe | a scribe type for which the function |
hdp< BaseDistribution, DataDistribution, K, V > & | h, | |
const unsigned int * | path, | |
unsigned int | depth, | |
const K & | observation | ) |
Adds the given observation
to the HDP hierarchy rooted at h
to the node at the end of the path
. The unsigned int
array path
, with length depth - 1
, provides the indices to follow from each node to its child, in order to locate the node to which the observation
is added. If the node does not exist, it is created, with its concentration parameter copied from the appropriate index in hdp::alpha. Thus, depth
cannot be larger than hdp::depth
of h
. If depth == 1
, the observation is added to the root node.
NodeType & | n, | |
const unsigned int * | path, | |
unsigned int | length, | |
const typename NodeType::atom_type & | observation | ) |
Returns true
if and only if the given observation
is contained in the HDP node at the end of the given path
. The unsigned int
array path
, with given length
, provides the indices to follow from each node to its child. The node at the end of this path is checked to determine if it contains the given observation
. Note that the path may contain IMPLICIT_NODE elements to specify a set of paths. In this case, all nodes at the end of a path in this set are checked.