-
Notifications
You must be signed in to change notification settings - Fork 1
/
dirichlet.py
106 lines (84 loc) · 3.51 KB
/
dirichlet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from __future__ import division
import numpy as np
na = np.newaxis
import scipy.special
from simplex import proj_to_2D, mesh
def _dirichlet_support_check(x,alpha):
x = np.array(x,ndmin=2)
alpha = np.array(alpha,ndmin=1)
assert alpha.ndim == 1
if len(alpha) == 1:
alpha = alpha * np.ones(x.shape[1])
else:
assert x.shape[1] == len(alpha)
retvals = np.zeros(x.shape[0])
goodindices = np.logical_and((x >= 0).all(1),np.abs(x.sum(1)-1.) < 1e-6)
badindices = np.logical_not(goodindices)
retvals[badindices] = -np.inf
return retvals, goodindices, badindices, x, alpha
def log_censored_dirichlet_density(x,alpha,data=None):
'''
UNNORMALIZED symmetric censored dirichlet density
x is an NxP set of query points on which to evaluate the P-dimensional density
data is an optional PxP matrix where the ijth entry is the number of times
face j came up during the i-censored rounds. therefore np.all(np.diag(data)==0)
'''
retvals, goodindices, badindices, x, alpha = _dirichlet_support_check(x,alpha)
if goodindices.sum() > 0:
x = x[goodindices]
logvals = ((alpha-1) * np.log(x)).sum(axis=1)
if data is not None:
assert (np.diag(data) == 0).all(), 'censored!'
for idx, row in enumerate(data):
ins = row*(np.log(x) - np.log(1.-x[:,idx])[:,na])
logvals += np.where(np.isnan(ins),0.,ins).sum(1)
retvals[goodindices] = logvals
return retvals
def log_dirichlet_density(x,alpha,data=None):
retvals, goodindices, badindices, x, alpha = _dirichlet_support_check(x,alpha)
if goodindices.sum() > 0:
x = x[goodindices]
logvals = ((alpha-1) * np.log(x)).sum(axis=1) - scipy.special.gammaln(alpha).sum() + scipy.special.gammaln(alpha.sum())
if data is not None:
assert data.ndim == 1 and data.shape[0] == x.shape[1]
ins = data*np.log(x)
logvals += np.where(np.isnan(ins),0.,ins).sum(axis=1) # 0^0 := 1 => 0*log(0) = 0.
retvals[goodindices] = logvals
return retvals
def test_pcolor_heatmap():
# import matplotlib.tri as tri
from matplotlib import pyplot as plt
mesh3D = mesh(100,edges=True)
mesh2D = proj_to_2D(mesh3D)
# triangulation = tri.Triangulation(mesh2D) # this is called in tripcolor
data = np.zeros((3,3))
data[0,1] += 1
vals = np.exp(log_dirichlet_density(mesh3D,2.,data=data.sum(0)))
temp = log_censored_dirichlet_density(mesh3D,2.,data=data)
censored_vals = np.exp(temp - temp.max())
plt.figure()
plt.tripcolor(mesh2D[:,0],mesh2D[:,1],vals)
plt.title('uncensored')
plt.figure()
plt.tripcolor(mesh2D[:,0],mesh2D[:,1],censored_vals)
plt.title('censored')
def test_imshow_heatmap():
from scipy.interpolate import griddata
from matplotlib import pyplot as plt
mesh3D = mesh(200)
mesh2D = proj_to_2D(mesh3D)
data = np.zeros((3,3))
data[0,1] += 2
vals = np.exp(log_dirichlet_density(mesh3D,2.,data=data.sum(0)))
temp = log_censored_dirichlet_density(mesh3D,2.,data=data)
censored_vals = np.exp(temp - temp.max())
xi = np.linspace(-1,1,1000)
yi = np.linspace(-0.5,1,1000)
plt.figure()
plt.imshow(griddata((mesh2D[:,0],mesh2D[:,1]),vals,(xi[None,:],yi[:,None]),method='cubic'))
plt.axis('off')
plt.title('uncensored likelihood')
plt.figure()
plt.imshow(griddata((mesh2D[:,0],mesh2D[:,1]),censored_vals,(xi[None,:],yi[:,None]),method='cubic'))
plt.axis('off')
plt.title('censored likelihood')