|
| 1 | +import numpy as np |
1 | 2 | import pytensor.tensor as pt |
2 | 3 |
|
3 | 4 | from distributions.helper import ( |
4 | 5 | cdf_bounds, |
5 | | - continuous_entropy, |
6 | | - continuous_kurtosis, |
7 | | - continuous_mean, |
8 | | - continuous_skewness, |
9 | | - continuous_variance, |
10 | 6 | ppf_bounds_cont, |
11 | 7 | ) |
12 | 8 | from distributions.normal import ppf as normal_ppf |
13 | 9 |
|
14 | | -# Support bounds for logitnormal (open interval (0, 1)) |
15 | | -_LOWER = 0.001 |
16 | | -_UPPER = 0.999 |
17 | | - |
18 | 10 |
|
19 | 11 | def _logit(x): |
20 | 12 | return pt.log(x) - pt.log1p(-x) |
21 | 13 |
|
22 | 14 |
|
23 | | -def _expit(y): |
24 | | - return pt.sigmoid(y) |
| 15 | +def _ghq_moments(mu, sigma, order=1, mean_val=None, n_points=70): |
| 16 | + """ |
| 17 | + Compute moments of the logit-normal using Gauss-Hermite quadrature. |
| 18 | +
|
| 19 | + Based on https://en.wikipedia.org/wiki/Logit-normal_distribution#Moments |
| 20 | + but using Gauss-Hermite quadrature for better accuracy. |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | + mu : tensor |
| 25 | + Mean of underlying normal distribution |
| 26 | + sigma : tensor |
| 27 | + Standard deviation of underlying normal distribution |
| 28 | + order : int |
| 29 | + Order of the moment |
| 30 | + mean_val : tensor, optional |
| 31 | + If provided, compute central moment around this mean |
| 32 | + n_points : int |
| 33 | + Number of Gauss–Hermite nodes |
| 34 | +
|
| 35 | + Returns |
| 36 | + ------- |
| 37 | + tensor |
| 38 | + Estimated moment |
| 39 | + """ |
| 40 | + gh_x, gh_w = np.polynomial.hermite.hermgauss(n_points) |
| 41 | + gh_x = pt.as_tensor_variable(gh_x) |
| 42 | + gh_w = pt.as_tensor_variable(gh_w) |
| 43 | + |
| 44 | + broadcast_shape = pt.broadcast_arrays(mu, sigma)[0] |
| 45 | + |
| 46 | + gh_x_bc = gh_x.reshape((-1,) + (1,) * broadcast_shape.ndim) |
| 47 | + gh_w_bc = gh_w.reshape((-1,) + (1,) * broadcast_shape.ndim) |
| 48 | + |
| 49 | + z = pt.sqrt(2.0) * sigma * gh_x_bc + mu |
| 50 | + x_vals = pt.sigmoid(z) |
| 51 | + |
| 52 | + if mean_val is not None: |
| 53 | + integrand = (x_vals - mean_val) ** order |
| 54 | + else: |
| 55 | + integrand = x_vals**order |
| 56 | + |
| 57 | + result = pt.sum(gh_w_bc * integrand, axis=0) / pt.sqrt(pt.pi) |
| 58 | + |
| 59 | + return result |
25 | 60 |
|
26 | 61 |
|
27 | 62 | def mean(mu, sigma): |
28 | | - return continuous_mean(_LOWER, _UPPER, logpdf, mu, sigma) |
| 63 | + return _ghq_moments(mu, sigma, order=1) |
29 | 64 |
|
30 | 65 |
|
31 | 66 | def mode(mu, sigma): |
32 | | - return _expit(mu) |
| 67 | + return pt.sigmoid(mu) |
33 | 68 |
|
34 | 69 |
|
35 | 70 | def median(mu, sigma): |
36 | 71 | shape = pt.broadcast_arrays(mu, sigma)[0] |
37 | | - return pt.full_like(shape, _expit(mu)) |
| 72 | + return pt.full_like(shape, pt.sigmoid(mu)) |
38 | 73 |
|
39 | 74 |
|
40 | 75 | def var(mu, sigma): |
41 | | - return continuous_variance(_LOWER, _UPPER, logpdf, mu, sigma) |
| 76 | + mean_val = _ghq_moments(mu, sigma, order=1) |
| 77 | + return _ghq_moments(mu, sigma, order=2, mean_val=mean_val) |
42 | 78 |
|
43 | 79 |
|
44 | 80 | def std(mu, sigma): |
45 | 81 | return pt.sqrt(var(mu, sigma)) |
46 | 82 |
|
47 | 83 |
|
48 | 84 | def skewness(mu, sigma): |
49 | | - return continuous_skewness(_LOWER, _UPPER, logpdf, mu, sigma) |
| 85 | + mean_val = _ghq_moments(mu, sigma, order=1) |
| 86 | + variance = _ghq_moments(mu, sigma, order=2, mean_val=mean_val) |
| 87 | + third_central = _ghq_moments(mu, sigma, order=3, mean_val=mean_val) |
| 88 | + return third_central / (pt.sqrt(variance) ** 3) |
50 | 89 |
|
51 | 90 |
|
52 | 91 | def kurtosis(mu, sigma): |
53 | | - return continuous_kurtosis(_LOWER, _UPPER, logpdf, mu, sigma) |
| 92 | + mean_val = _ghq_moments(mu, sigma, order=1) |
| 93 | + variance = _ghq_moments(mu, sigma, order=2, mean_val=mean_val) |
| 94 | + fourth_central = _ghq_moments(mu, sigma, order=4, mean_val=mean_val) |
| 95 | + return fourth_central / (variance**2) - 3 |
54 | 96 |
|
55 | 97 |
|
56 | 98 | def entropy(mu, sigma): |
57 | | - return continuous_entropy(_LOWER, _UPPER, logpdf, mu, sigma) |
| 99 | + gh_x, gh_w = np.polynomial.hermite.hermgauss(70) |
| 100 | + gh_x = pt.as_tensor_variable(gh_x) |
| 101 | + gh_w = pt.as_tensor_variable(gh_w) |
| 102 | + |
| 103 | + broadcast_shape = pt.broadcast_arrays(mu, sigma)[0] |
| 104 | + |
| 105 | + gh_x_bc = gh_x.reshape((-1,) + (1,) * broadcast_shape.ndim) |
| 106 | + gh_w_bc = gh_w.reshape((-1,) + (1,) * broadcast_shape.ndim) |
| 107 | + |
| 108 | + z = pt.sqrt(2.0) * sigma * gh_x_bc + mu |
| 109 | + x_vals = pt.sigmoid(z) |
| 110 | + |
| 111 | + integrand = -logpdf(x_vals, mu, sigma) |
| 112 | + |
| 113 | + result = pt.sum(gh_w_bc * integrand, axis=0) / pt.sqrt(pt.pi) |
| 114 | + |
| 115 | + return result |
58 | 116 |
|
59 | 117 |
|
60 | 118 | def pdf(x, mu, sigma): |
@@ -121,12 +179,12 @@ def logsf(x, mu, sigma): |
121 | 179 |
|
122 | 180 |
|
123 | 181 | def ppf(q, mu, sigma): |
124 | | - return ppf_bounds_cont(_expit(normal_ppf(q, mu, sigma)), q, 0, 1) |
| 182 | + return ppf_bounds_cont(pt.sigmoid(normal_ppf(q, mu, sigma)), q, 0, 1) |
125 | 183 |
|
126 | 184 |
|
127 | 185 | def isf(q, mu, sigma): |
128 | 186 | return ppf(1 - q, mu, sigma) |
129 | 187 |
|
130 | 188 |
|
131 | 189 | def rvs(mu, sigma, size=None, random_state=None): |
132 | | - return _expit(pt.random.normal(mu, sigma, rng=random_state, size=size)) |
| 190 | + return pt.sigmoid(pt.random.normal(mu, sigma, rng=random_state, size=size)) |
0 commit comments