-
Notifications
You must be signed in to change notification settings - Fork 2
/
watson_distribution.py
293 lines (228 loc) · 8.89 KB
/
watson_distribution.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
r"""
Dimroth-Watson distribution class
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
from astropy.utils.misc import NumpyRNGContext
from scipy.stats import rv_continuous
from scipy.special import erf, erfi
from warnings import warn
__all__ = ('DimrothWatson')
__author__ = ('Duncan Campbell')
class DimrothWatson(rv_continuous):
r"""
A Dimroth-Watson distribution of :math:`\cos(\theta)'
Parameters
----------
k : float
shape paramater
Notes
-----
The Dimroth-Watson distribution is defined as:
.. math::
p(\cos(\theta)) = B(k)\exp[-k\cos(\theta)^2]\mathrm{d}\cos(\theta)
where
.. math::
B(k) = \frac{1}{2}int_0^1\exp(-k t^2)\mathrm{d}t
We assume the ISO convention for spherical coordinates, where :math:`\theta`
is the polar angle, bounded between :math:`[-\pi, \pi]`, and :math:`\phi`
is the azimuthal angle, where for a Dimroth-Watson distribution, :math:`phi'
is a uniform random variable between :math:`[0, 2\pi]`: for all `k`.
For :math:`k<0`, the distribution of points on a sphere is bipolar.
For :math:`k=0`, the distribution of points on a sphere is uniform.
For :math:`k>0`, the distribution of points on a sphere is girdle.
Note that as :math:`k \rarrow \infty`:
.. math::
p(\cos(\theta)) = \frac{1}{2}\left[ \delta(\cos(\theta) + 1) + \delta(\cos(\theta) - 1) \right]\mathrm{d}\cos(\theta)
and as :math:`k \rarrow -\infty`:
.. math::
p(\cos(\theta)) = \frac{1}{2}\delta(\cos(\theta))\mathrm{d}\cos(\theta)
Needless to say, for large :math:`|k|`, the attributes of this class are approximate and not well tested.
"""
def _argcheck(self, k):
r"""
check arguments
"""
k = np.asarray(k)
self.a = -1.0 # lower bound
self.b = 1.0 # upper bound
return (k == k)
def _norm(self, k):
r"""
normalization constant
"""
k = np.atleast_1d(k)
# mask for positive and negative k cases
negative_k = (k < 0) & (k != 0)
positive_k = (k != 0)
# after masking, ignore the sign of k
k = np.fabs(k)
# create an array to store the result
norm = np.zeros(len(k))
# for k>0
norm[positive_k] = 4.0*np.sqrt(np.pi)*erf(np.sqrt(k[positive_k]))/(4.0*np.sqrt(k[positive_k]))
# for k<0
norm[negative_k] = 4.0*np.sqrt(np.pi)*erfi(np.sqrt(k[negative_k]))/(4.0*np.sqrt(k[negative_k]))
# ignore divide by zero in the where statement
with np.errstate(divide='ignore', invalid='ignore'):
return np.where(k == 0, 0.5, 1.0/norm)
def _pdf(self, x, k):
r"""
probability distribution function
Parameters
----------
k : float
shape parameter
Notes
-----
See the 'notes' section of the class for a discussion of large :math:`|k|`.
"""
# process arguments
k = np.atleast_1d(k).astype(np.float64)
x = np.atleast_1d(x).astype(np.float64)
with np.errstate(over='ignore', invalid='ignore'):
norm = self._norm(k)
p = norm*np.exp(-1.0*k*x**2)
p = np.nan_to_num(p)
# deal with the edge cases
epsilon = np.finfo(float).eps
edge_mask = (p >= 1.0/epsilon) | (p == 0.0)
p[edge_mask] = 0.0
# large positive k (bipolar)
bipolar = (x >= (1.0 - epsilon)) | (x <= (-1.0 + epsilon))
p[bipolar & edge_mask & (k>1)] = 1.0/(2.0*epsilon)
# large negative k (girdle)
girdle = (x >= (0.0 - epsilon)) & (x <= (0.0 + epsilon))
p[girdle & edge_mask & (k <- 1)] = 1.0/(2.0*epsilon)
return p
def _rvs(self, k, max_iter=100):
r"""
random variate sampling
Parameters
----------
k : array_like
array of shape parameters
size : int, optional
integer indicating the number of samples to draw.
if not given, the number of samples will be equal to len(k).
max_iter : int, optional
integer indicating the maximum number of times to iteratively draw from
the proposal distribution until len(s) points are accepted.
Notes
-----
The random variate sampling for this distribution is an implementation
of the rejection-sampling technique.
The Proposal distributions are taken from Best & Fisher (1986).
"""
k = np.atleast_1d(k).astype(np.float64)
size = self._size[0]
if size != 1:
if len(k) == size:
pass
elif len(k) == 1:
k = np.ones(size)*k
else:
msg = ('if `size` argument is given, len(k) must be 1 or equal to size.')
raise ValueError(msg)
else:
size = len(k)
# vector to store random variates
result = np.zeros(size)
# take care of k=0 case
zero_k = (k == 0)
uran0 = np.random.random(np.sum(zero_k))*2 - 1.0
result[zero_k] = uran0
# take care of edge cases, i.e. |k| very large
with np.errstate(over='ignore'):
x = np.exp(k)
inf_mask = np.array([False]*size)
edge_mask = ((x == np.inf) | (x == 0.0))
result[edge_mask & (k>0)] = np.random.choice([1,-1], size=np.sum(edge_mask & (k>0)))
result[edge_mask & (k<0)] = 0.0
# apply rejection sampling technique to sample from pdf
n_sucess = np.sum(zero_k) + np.sum(edge_mask) # number of sucesessful draws from pdf
n_remaining = size - n_sucess # remaining draws necessary
n_iter = 0 # number of sample-reject iterations
kk = k[(~zero_k) & (~edge_mask)] # store subset of k values that still need to be sampled
mask = np.array([False]*size) # mask indicating which k values have a sucessful sample
mask[zero_k] = True
while (n_sucess < size) & (n_iter < max_iter):
# get three uniform random numbers
uran1 = np.random.random(n_remaining)
uran2 = np.random.random(n_remaining)
uran3 = np.random.random(n_remaining)
# masks indicating which envelope function is used
negative_k = (kk < 0.0)
positive_k = (kk > 0.0)
# sample from g(x) to get y
y = np.zeros(n_remaining)
y[positive_k] = self.g1_isf(uran1[positive_k], kk[positive_k])
y[negative_k] = self.g2_isf(uran1[negative_k], kk[negative_k])
y[uran3 < 0.5] = -1.0*y[uran3 < 0.5] # account for one-sided isf function
# calculate M*g(y)
g_y = np.zeros(n_remaining)
m = np.zeros(n_remaining)
g_y[positive_k] = self.g1_pdf(y[positive_k], kk[positive_k])
g_y[negative_k] = self.g2_pdf(y[negative_k], kk[negative_k])
m[positive_k] = self.m1(kk[positive_k])
m[negative_k] = self.m2(kk[negative_k])
# calulate f(y)
f_y = self.pdf(y, kk)
# accept or reject y
keep = ((f_y/(g_y*m)) > uran2)
# count the number of succesful samples
n_sucess += np.sum(keep)
# store y values
result[~mask] = y
# update mask indicating which values need to be redrawn
mask[~mask] = keep
# get subset of k values which need to be sampled.
kk = kk[~keep]
n_iter += 1
n_remaining = np.sum(~keep)
if (n_iter == max_iter):
msg = ('The maximum number of iterations reached, random variates may not be represnetitive.')
raise warn(msg)
return result
def g1_pdf(self, x, k):
r"""
proposal distribution for pdf for k>0
"""
k = -1*k
eta = np.sqrt(-1*k)
C = eta/(np.arctan(eta))
return (C/(1+eta**2*x**2))/2.0
def g1_isf(self, y, k):
r"""
inverse survival function of proposal distribution for pdf for k>0
"""
k = -1*k
eta = np.sqrt(-1*k)
return (1.0/eta)*(np.tan(y*np.arctan(eta)))
def m1(self, k):
r"""
eneveloping factor for proposal distribution for k>0
"""
return 2.0*np.ones(len(k))
def g2_pdf(self, x, k):
r"""
proposal distribution for pdf for k<0
"""
k = -1*k
norm = 2.0*(np.exp(k)-1)/k
return (np.exp(k*np.fabs(x)))/norm
def g2_isf(self, y, k):
r"""
inverse survival function of proposal distribution for pdf for k<0
"""
k = -1.0*k
C = k/(np.exp(k)-1.0)
return np.log(k*y/C+1)/k
def m2(self, k):
r"""
eneveloping factor for proposal distribution for pdf for k<0
"""
k = -1.0*k
C = k*(np.exp(k)-1)**(-1)
norm = 2.0*(np.exp(k)-1)/k
return C*norm