Up

Scattergrams of Samples from Dirichlet Distributions

 

Probability density function of Dirichlet distribution is given as follows (Gelman et al., 2014).

where

We note that

 

Consider a multinomial distribution

When we use Dirichlet distribution (1) as a prior distribution for this multinomial distribution (2), we get the following posterior distribution.

Parameters of Dirichlet distribution are updated as follows:

That is, Dirichlet distribution is the conjugate prior for multinomial distribution, and equation (3) means that the parameters of Dirichlet distribution may be interpreted to represent some prior information about frequencies.

 

The following Python script displays scattergrams of samples from a Dirichlet distribution, whose parameter values are given when the script starts.

 

import numpy.random as nprn

import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sb

import numpy as np

 

print('Set the three positive values, a1, a2, a3...')

a1 = float(input('a1 = '))

a2 = float(input('a2 = '))

a3 = float(input('a3 = '))

 

raw_data = nprn.dirichlet([a1, a2, a3], 5000)

data = pd.DataFrame({'theta_1': raw_data.T[0], 'theta_2': raw_data.T[1],

                        'theta_3': raw_data.T[2]})

sb.pairplot(data)

plt.show()

 

 

p1 = np.array([0.0, (3.0**0.5) - 1.0])

p2 = np.array([-1.0, -1.0])

p3 = np.array([1.0, -1.0])

plt.figure(figsize = (5, 5))

plt.plot([-1.0, 1.0, 0.0, -1.0],

         [-1.0, -1.0, (3.0**0.5) - 1.0, -1.0])

 

 

points = []

for t1, t2, t3 in raw_data:

    points.append(p1 * t1 + p2 * t2 + p3 * t3)

points = np.array(points)

plt.title('a = [{0}, {1}, {2}]'.format(a1, a2, a3), fontsize = 16)

plt.xlim(-1.2, 1.2)

plt.ylim(-1.2, 1.2)

plt.scatter(points.T[0], points.T[1], edgecolors = '#ffffff', alpha = 0.5)

plt.text(p1[0]-0.1, p1[1]+0.1, 'P1', fontsize = 16)

plt.text(p2[0]-0.1, p2[1]-0.15, 'P2', fontsize = 16)

plt.text(p3[0], p3[1]-0.15, 'P3', fontsize = 16)

plt.xticks([])

plt.yticks([])

plt.show()

 

The file of this script is archived into a file prgs.zip.

Run this script, parameter values, a1, a2, a3, which denote the parameters ,, in model (1),are required to be set (Figure 1).

Figure 1

 

After the three values are set, 5000 samples are taken from the Dirichlet distribution by the following code.

raw_data = nprn.dirichlet([a1, a2, a3], 5000)

The sampled data is converted into a pandas DataFrame, and for this converted data, pairplot function of seaborn module is called

pg = sb.pairplot(data)

to display graphs shown in Figure 2.

Figure 2

 

Graphs in Figure 2 are for Dirichlet distribution (1) with parameters

When you close the window by clicking the X icon at the upper right corner, a graph in Figure 3 will be shown.

Figure 3

 

Let the three vertices of a triangle be P1, P2, and P3. For a sample (, , ), calculate the weighted average of the points as follows:

Scattergram of these weighted average of the points is shown in Figure 3. The points are distributed uniformly over the triangle.

 

If we set

we get Figures 4 and 5. The points gather together in the center (Figure 5).

Figure 4

 

Figure 5

 

If we set , we have Figures 6 and 7.

図6

 

図7

 

The points leave apex P1, and approach to apex P3.

 

Reference

Gelman, A., Carlin, J. B., Stern, H. S., Dunson, D. B., Vehtari, A., & Rubin, D. B. (2014). Bayesian Data Analysis, third edition. CRC Press.

 

Up