|
9 | 9 |
|
10 | 10 | from .dist_math import * |
11 | 11 | from numpy.random import uniform as runiform, normal as rnormal |
| 12 | +from .transforms import logtransform, logoddstransform, interval_transform |
12 | 13 |
|
13 | 14 | __all__ = ['Uniform', 'Flat', 'Normal', 'Beta', 'Exponential', 'Laplace', |
14 | 15 | 'T', 'StudentT', 'Cauchy', 'HalfCauchy', 'Gamma', 'Weibull','Bound', |
15 | 16 | 'Tpos', 'Lognormal', 'ChiSquared', 'HalfNormal', 'Wald', |
16 | 17 | 'Pareto', 'InverseGamma'] |
17 | 18 |
|
| 19 | +class PositiveContinuous(Continuous): |
| 20 | + """Base class for positive continuous distributions""" |
| 21 | + def __init__(self, transform=logtransform, *args, **kwargs): |
| 22 | + super(PositiveContinuous, self).__init__(transform=transform, *args, **kwargs) |
| 23 | + |
| 24 | +class UnitContinuous(Continuous): |
| 25 | + """Base class for continuous distributions on [0,1]""" |
| 26 | + def __init__(self, transform=logoddstransform, *args, **kwargs): |
| 27 | + super(UnitContinuous, self).__init__(transform=transform, *args, **kwargs) |
| 28 | + |
18 | 29 | def get_tau_sd(tau=None, sd=None): |
19 | 30 | """ |
20 | 31 | Find precision and standard deviation |
@@ -70,13 +81,17 @@ class Uniform(Continuous): |
70 | 81 | upper : float |
71 | 82 | Upper limit (defaults to 1) |
72 | 83 | """ |
73 | | - def __init__(self, lower=0, upper=1, *args, **kwargs): |
| 84 | + def __init__(self, lower=0, upper=1, transform='interval', *args, **kwargs): |
74 | 85 | super(Uniform, self).__init__(*args, **kwargs) |
| 86 | + |
75 | 87 | self.lower = lower |
76 | 88 | self.upper = upper |
77 | 89 | self.mean = (upper + lower) / 2. |
78 | 90 | self.median = self.mean |
79 | 91 |
|
| 92 | + if transform is 'interval': |
| 93 | + self.transform = interval_transform(lower, upper) |
| 94 | + |
80 | 95 | def logp(self, value): |
81 | 96 | lower = self.lower |
82 | 97 | upper = self.upper |
@@ -142,7 +157,7 @@ def logp(self, value): |
142 | 157 | ) |
143 | 158 |
|
144 | 159 |
|
145 | | -class HalfNormal(Continuous): |
| 160 | +class HalfNormal(PositiveContinuous): |
146 | 161 | """ |
147 | 162 | Half-normal log-likelihood, a normal distribution with mean 0 limited |
148 | 163 | to the domain :math:`x \in [0, \infty)`. |
@@ -206,7 +221,7 @@ def logp(self, value): |
206 | 221 |
|
207 | 222 |
|
208 | 223 |
|
209 | | -class Beta(Continuous): |
| 224 | +class Beta(UnitContinuous): |
210 | 225 | """ |
211 | 226 | Beta log-likelihood. The conjugate prior for the parameter |
212 | 227 | :math:`p` of the binomial distribution. |
@@ -268,7 +283,7 @@ def logp(self, value): |
268 | 283 | beta > 0) |
269 | 284 |
|
270 | 285 |
|
271 | | -class Exponential(Continuous): |
| 286 | +class Exponential(PositiveContinuous): |
272 | 287 | """ |
273 | 288 | Exponential distribution |
274 | 289 |
|
@@ -320,7 +335,7 @@ def logp(self, value): |
320 | 335 | return -log(2 * b) - abs(value - mu) / b |
321 | 336 |
|
322 | 337 |
|
323 | | -class Lognormal(Continuous): |
| 338 | +class Lognormal(PositiveContinuous): |
324 | 339 | """ |
325 | 340 | Log-normal log-likelihood. |
326 | 341 |
|
@@ -409,7 +424,7 @@ def logp(self, value): |
409 | 424 | StudentT = T |
410 | 425 |
|
411 | 426 |
|
412 | | -class Pareto(Continuous): |
| 427 | +class Pareto(PositiveContinuous): |
413 | 428 | """ |
414 | 429 | Pareto log-likelihood. The Pareto is a continuous, positive |
415 | 430 | probability distribution with two parameters. It is often used |
@@ -482,7 +497,7 @@ def logp(self, value): |
482 | 497 | value - alpha) / beta) ** 2), |
483 | 498 | beta > 0) |
484 | 499 |
|
485 | | -class HalfCauchy(Continuous): |
| 500 | +class HalfCauchy(PositiveContinuous): |
486 | 501 | """ |
487 | 502 | Half-Cauchy log-likelihood. Simply the absolute value of Cauchy. |
488 | 503 |
|
@@ -510,7 +525,7 @@ def logp(self, value): |
510 | 525 | value >= 0) |
511 | 526 |
|
512 | 527 |
|
513 | | -class Gamma(Continuous): |
| 528 | +class Gamma(PositiveContinuous): |
514 | 529 | """ |
515 | 530 | Gamma log-likelihood. |
516 | 531 |
|
@@ -575,7 +590,7 @@ def logp(self, value): |
575 | 590 | beta > 0) |
576 | 591 |
|
577 | 592 |
|
578 | | -class InverseGamma(Continuous): |
| 593 | +class InverseGamma(PositiveContinuous): |
579 | 594 | """ |
580 | 595 | Inverse gamma log-likelihood, the reciprocal of the gamma distribution. |
581 | 596 |
|
@@ -634,7 +649,7 @@ def __init__(self, nu, *args, **kwargs): |
634 | 649 | super(ChiSquared, self).__init__(alpha=nu/2., beta=0.5, *args, **kwargs) |
635 | 650 |
|
636 | 651 |
|
637 | | -class Weibull(Continuous): |
| 652 | +class Weibull(PositiveContinuous): |
638 | 653 | """ |
639 | 654 | Weibull log-likelihood |
640 | 655 |
|
|
0 commit comments