Function Repository Resource:

# ConditionalProductDistribution

Represent a product of distributions where some distributions can depend on others

Contributed by: Sjoerd Smit
 ResourceFunction["ConditionalProductDistribution"][Distributed[var1,dist1],Distributed[var2,dist2],…] represents the multivariate distribution of {var1,var2, …} where each disti may depend on varj for all i

## Details

ResourceFunction["ConditionalProductDistribution"] is used to define a multivariate distribution in terms of conditional probabilities using the conditional product rule (where f𝒟 denotes the PDF of a distribution 𝒟).
ResourceFunction["ConditionalProductDistribution"] is like a hybrid between ProductDistribution (which allows you to draw from multiple independent distributions at once) and ParameterMixtureDistribution (which allows you to define new distributions in terms of existing ones).
ResourceFunction["ConditionalProductDistribution"] works with RandomVariate, PDF, Likelihood and LogLikelihood and Mean. It does not necessarily work in all Wolfram Language functions for distributions.
Graph can be used to create a dependency graph of the variables vari.
ResourceFunction["ConditionalProductDistribution"] will return \$Failed if one of the disti depends on some varj for ij.

## Examples

### Basic Examples (2)

Define a BinomialDistribution where the probability of success is drawn from a BetaDistribution:

 In[1]:=
 Out[1]=

Compute the PDF:

 In[2]:=
 Out[2]=

Compute the Mean:

 In[3]:=
 Out[3]=

Draw random samples from the distribution. Each sample returns the values of k and p:

 In[4]:=
 Out[5]=

Generate multiple samples:

 In[6]:=
 Out[6]=

Calculate the LogLikelihood of the samples:

 In[7]:=
 Out[7]=

Samples with lower values of k are generally produced by samples with lower values of p:

 In[8]:=
 Out[8]=

### Scope (2)

Graph can be used to generate a dependency graph of the individual random variables. Each edge x y should be read as "x influences y":

 In[9]:=
 Out[10]=

The graph is acyclic:

 In[11]:=
 Out[11]=

Sample the distribution and compute the expected value. Note that the coordinates are listed in order of appearance of the symbols:

 In[12]:=
 Out[12]=
 In[13]:=
 Out[13]=
 In[14]:=
 Out[14]=

When a ConditionalProductDistribution is defined using unprotected symbols for the bound variables, they will be replaced with symbols of the form x[i]:

 In[15]:=
 Out[15]=

### Applications (3)

Define a Normal-inverse-Wishart distribution, which is the conjugate prior to a multivariate normal distribution of unknown mean and covariance:

 In[16]:=

Draw samples from it:

 In[17]:=
 Out[17]=

Draw samples from the posterior predictive distribution:

 In[18]:=
 Out[12]=

### Properties and Relations (3)

A ParameterMixtureDistribution is the marginal distribution of a ConditionalProductDistribution:

 In[19]:=
 Out[19]=

Compute the marginal over k by integrating out p from the PDF of the corresponding ConditionalProductDistribution:

 In[20]:=
 Out[20]=

The result is the same:

 In[21]:=
 Out[21]=

### Possible Issues (3)

The distributions need to be ordered correctly and cannot be circular:

 In[22]:=
 Out[22]=
 In[23]:=
 Out[23]=

In some cases PDF cannot distinguish between evaluating a single coordinate or threading over multiple coordinates. In the following example, the second argument will be interpreted as a pair of coordinates rather than a single 2×2 coordinate:

 In[24]:=
 Out[24]=

In this case, wrap the second argument in a list to remove the ambiguity:

 In[25]:=
 Out[25]=

Bound variables should not have definitions at the time of defining a ConditionalProductDistribution, since this can have unexpected results:

 In[26]:=
 Out[27]=
 In[28]:=
 Out[28]=

Use Clear or Block to make sure p has no value at the moment of definition. After that, the variable will have been localized so that p can be used again:

 In[29]:=
 Out[29]=
 In[30]:=
 Out[31]=

### Neat Examples (3)

Define a function that can compute the Expectation of a ConditionalProductDistribution:

 In[32]:=

Define a distribution using formal symbols (to ensure they do not get renamed) and compute an expected value:

 In[33]:=
 Out[34]=

Check the result with a Monte Carlo simulation:

 In[35]:=
 Out[36]=
 In[37]:=
 Out[37]=

Sjoerd Smit

## Version History

• 1.1.0 – 10 February 2023
• 1.0.0 – 14 August 2020