-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhorn_numeric.py
306 lines (262 loc) · 9.62 KB
/
horn_numeric.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
294
295
296
297
298
299
300
301
302
303
304
305
306
# -*- coding: utf-8 -*-
import numpy as np
from mpmath import mp, fp
mp_ctx = mp
def phi1_T1_x(m, a_, b_, g_, x_, y_):
r""" Series terms for an expansion of ::math::`Phi_1`
in powers of ::math::`x`.
"""
res = mp_ctx.rf(a_, m) / mp_ctx.rf(g_, m)
res *= mp_ctx.power(x_, m) / mp_ctx.fac(m)
res *= mp_ctx.hyp2f1(b_, a_ + m, g_ + m, y_)
return res
def phi1_T1_x_gen(a_, b_, g_, x_, y_, pow_terms=True):
r""" Generator of series terms for an expansion of ::math::`Phi_1`
in powers of ::math::`x`.
"""
coef = 1
x_pow = 1
F_term = mp_ctx.hyp2f1(b_, a_, g_, y_)
m = 0
while True:
res = coef * x_pow * F_term
yield res
m += 1
coef *= (a_ + m - 1) / (g_ + m - 1) / m
if pow_terms:
x_pow *= x_
F_term = mp_ctx.hyp2f1(b_, a_ + m, g_ + m, y_)
def phi1_T2_x(m, a_, b_, g_, x_, y_):
r""" Regularized series terms for an expansion of ::math::`Phi_1`
in powers of ::math::`-x`.
FYI: 'regularized' means that the ::math::`\exp(-x)` term is excluded.
"""
res = mp_ctx.rf(g_ - a_, m) / mp_ctx.rf(g_, m)
res *= mp_ctx.power(-x_, m) / mp_ctx.fac(m)
res *= mp_ctx.hyp2f1(b_, a_, g_ + m, y_)
return res
def phi1_T2_x_gen(a_, b_, g_, x_, y_, pow_terms=True):
r""" Generator of regularized series terms for an expansion of
::math::`Phi_1` in powers of ::math::`-x`.
FYI: 'regularized' means that the ::math::`\exp(-x)` term is excluded.
"""
coef = 1.
x_pow = 1.
F_term = mp_ctx.hyp2f1(b_, a_, g_, y_)
m = 0
while True:
res = coef * x_pow * F_term
yield res
m += 1
coef *= (g_ - a_ + m - 1) / (g_ + m - 1) / m
if pow_terms:
x_pow *= -x_
F_term = mp_ctx.hyp2f1(b_, a_, g_ + m, y_)
def phi1_T3_y(n, a_, b_, g_, x_, y_):
r""" Series terms for an expansion of ::math::`Phi_1`
in powers of ::math::`y`.
"""
try:
res = mp_ctx.rf(a_, n) * mp_ctx.rf(b_, n) / mp_ctx.rf(g_, n)
res *= mp_ctx.power(y_, n) / mp_ctx.fac(n)
except OverflowError:
res = mp_ctx.inf
try:
res *= mp_ctx.hyp1f1(a_ + n, g_ + n, x_)
except OverflowError:
# XXX, FIXME: This is hack. When using finite precision,
# `math.exp(x)` will overflow for large `x`, instead of returing
# `float('inf')`. That's why we do the following:
if x_ > 0:
res = mp_ctx.inf
else:
raise
return res
def phi1_T3_y_gen(a_, b_, g_, x_, y_, pow_terms=True):
r""" Generator of series terms for an expansion of ::math::`Phi_1`
in powers of ::math::`y`.
"""
coef = 1
y_pow = 1
F_term = mp_ctx.hyp1f1(a_, g_, x_)
n = 0
while True:
res = coef * y_pow * F_term
yield res
n += 1
coef *= (a_ + n - 1) * (b_ + n - 1) / (g_ + n - 1) / n
if pow_terms:
y_pow *= y_
F_term = mp_ctx.hyp1f1(a_ + n, g_ + n, x_)
def phi1_T4_y(n, a_, b_, g_, x_, y_):
r""" Regularized series terms for an expansion of ::math::`Phi_1` in
powers of ::math::`y`.
FYI: 'regularized' means that the ::math::`\exp(-x)` term is excluded.
"""
try:
res = mp_ctx.rf(a_, n) * mp_ctx.rf(b_, n) / mp_ctx.rf(g_, n)
res *= mp_ctx.power(y_, n) / mp_ctx.fac(n)
except OverflowError:
res = mp_ctx.inf
try:
res *= mp_ctx.hyp1f1(g_ - a_, g_ + n, -x_)
except OverflowError:
# XXX, FIXME: This is hack. When using finite precision,
# `math.exp(x)` will overflow for large `x`, instead of returing
# `float('inf')`. That's why we do the following:
if -x_ > 0:
res = mp_ctx.inf
else:
raise
return res
def phi1_T4_y_gen(a_, b_, g_, x_, y_, pow_terms=True):
r""" Generator of regularized series terms for an expansion of
::math::`Phi_1` in powers of ::math::`y`.
FYI: 'regularized' means that the ::math::`\exp(-x)` term is excluded.
"""
coef = 1
# coef *= mp_ctx.exp(y_)
y_pow = 1
F_term = mp_ctx.hyp1f1(g_ - a_, g_, -x_)
n = 0
while True:
res = coef * y_pow * F_term
yield res
n += 1
coef *= (a_ + n - 1) * (b_ + n - 1) / (g_ + n - 1) / n
if pow_terms:
y_pow *= y_
F_term = mp_ctx.hyp1f1(g_ - a_, g_ + n, -x_)
def horn_phi1_gordy_single(a, b, g, x, y,
keep_exp_const=True,
**kwargs):
r""" Infinite precision computation of the Horn :math:`Phi_1` function.
Uses the approach of [Gordy]_.
.. math:
\Phi_1(\alpha, \beta; \gamma; x, y) =
\sum_{m=0}^\infty \sum_{n=0}^\infty
\frac{(\alpha)_{m+n} (\beta)_n}{(\gamma)_{m+n} m! n!}
y^n x^m
The general expression in `mpmath` is
```
nsum(lambda m,n: rf(a,m+n)*rf(b,n)/rf(g,m+n)*\
x**m*y**n/fac(m)/fac(n), [0,inf], [0,inf])
```
Parameters
==========
a: float
The ::math::`\alpha` parameter. This value must satisfy
::math::`0 < \alpha < \gamma`.
b: float
The ::math::`\beta` parameter.
g: float
The ::math::`\gamma` parameter.
x: float
The ::math::`x` parameter.
y: float
The ::math::`y` parameter. This value must satisfy
::math::`y < 1`.
keep_exp_const: bool (optional)
Include constant multipliers terms like ::math::`\exp(x)` and
::math::`(1-y)^{-\beta}`. Set this to `False` when computing
ratios with equal ``b``, ``x`` and ``y`` parameters.
Returns
=======
ndarray (mpmath.mpf)
The real value of the Horn Phi1 function at the given points.
.. [Gordy] Gordy, Michael B. “A Generalization of Generalized Beta
Distributions,” 1998.
"""
if not (0 <= a and a < g):
raise ValueError("Parameter a must be 0 < a < g")
if y > 1:
raise ValueError("Parameter y must be 0 <= y < 1")
elif y == 1:
return mp_ctx.inf
if (0 <= y and y < 1):
phi_args = (a, b, g, x, y)
if x < 0:
if x > -1:
# -1 < x < 0
series_gen = phi1_T2_x_gen(*phi_args)
res = mp_ctx.nsum(lambda n: next(series_gen),
[0, mp_ctx.inf])
if keep_exp_const and not mp_ctx.isinf(res):
res *= mp_ctx.exp(x)
else:
# x <= -1
if y == 0 and mp_ctx is fp:
# FIXME: Yet another finite precision hack.
return mp_ctx.inf
else:
series_gen = phi1_T4_y_gen(*phi_args)
res = mp_ctx.nsum(lambda n: next(series_gen),
[0, mp_ctx.inf])
if keep_exp_const and not mp_ctx.isinf(res):
res *= mp_ctx.exp(x)
else:
# x > 0
if x >= 1:
if y == 0 and mp_ctx is fp:
# FIXME: Yet another finite precision hack.
return mp_ctx.inf
else:
series_gen = phi1_T4_y_gen(*phi_args)
res = mp_ctx.nsum(lambda n: next(series_gen),
[0, mp_ctx.inf])
if keep_exp_const and not mp_ctx.isinf(res):
res *= mp_ctx.exp(x)
else:
# 0 <= x <= 1
if y < 0.5:
if x < 0.5:
series_gen = phi1_T1_x_gen(*phi_args)
res = mp_ctx.nsum(lambda n: next(series_gen),
[0, mp_ctx.inf])
else:
series_gen = phi1_T3_y_gen(*phi_args)
res = mp_ctx.nsum(lambda n: next(series_gen),
[0, mp_ctx.inf])
else:
if x < 0.5:
series_gen = phi1_T2_x_gen(*phi_args)
res = mp_ctx.nsum(lambda n: next(series_gen),
[0, mp_ctx.inf])
if keep_exp_const and not mp_ctx.isinf(res):
res *= mp_ctx.exp(x)
else:
series_gen = phi1_T1_x_gen(*phi_args)
res = mp_ctx.nsum(lambda n: next(series_gen),
[0, mp_ctx.inf])
elif mp.isfinite(y):
res = horn_phi1_gordy_single(g - a, b, g, -x, y / (y - 1.),
keep_exp_const=keep_exp_const)
if keep_exp_const:
res *= mp_ctx.power(1 - y, -b) * mp_ctx.exp(x)
else:
raise ValueError("Unhandled y value: {}". format(y))
return res
def horn_phi1_quad_single(a, b, g, x, y, **kwargs):
r""" Finite precision quadrature computation of Humbert :math:`\Phi_1`.
See Also
--------
horn_phi1_gordy_single: Series computation of Humbert :math:`\Phi_1`.
"""
if not (0 <= a and a < g):
# FIXME: Too restrictive: c-a < 0 can be non-integer.
raise ValueError("Parameter a must be 0 < a < g")
if y >= 1:
raise ValueError("Parameter y must be 0 <= y < 1")
def phi_1_integrand_num(t):
res_ = t**(a-1.) * (1.-t)**(g-a-1.)
res_ *= fp.exp(y * t) / (1.-x*t)**b
return res_
try:
res = fp.gamma(g)/fp.gamma(a)/fp.gamma(g-a)
res *= fp.quad(phi_1_integrand_num, [0, 1])
return res
except:
# TODO: Could check |y| >> 1 and guess at the result being inf.
return fp.nan
horn_phi1 = np.vectorize(horn_phi1_gordy_single)
horn_phi1_quad = np.vectorize(horn_phi1_quad_single)