1
1
import sys
2
- import json
3
- import os
4
2
5
3
import numpy as np
6
4
from numpy import inf
10
8
ContinuousDistribution , _RealDomain , _RealParameter , _Parameterization ,
11
9
_combine_docs )
12
10
13
- __all__ = ['Normal' ]
11
+ __all__ = ['Normal' , 'Uniform' ]
14
12
15
13
16
14
class Normal (ContinuousDistribution ):
@@ -269,8 +267,7 @@ def _moment_raw_formula(self, order, log_a, log_b, **kwargs):
269
267
return t1 * t2
270
268
271
269
272
- # currently for testing only
273
- class _Uniform (ContinuousDistribution ):
270
+ class Uniform (ContinuousDistribution ):
274
271
r"""Uniform distribution.
275
272
276
273
The probability density function of the uniform distribution is:
@@ -284,7 +281,7 @@ class _Uniform(ContinuousDistribution):
284
281
285
282
_a_domain = _RealDomain (endpoints = (- inf , inf ))
286
283
_b_domain = _RealDomain (endpoints = ('a' , inf ))
287
- _x_support = _RealDomain (endpoints = ('a' , 'b' ), inclusive = (False , False ))
284
+ _x_support = _RealDomain (endpoints = ('a' , 'b' ), inclusive = (True , True ))
288
285
289
286
_a_param = _RealParameter ('a' , domain = _a_domain , typical = (1e-3 , 0.9 ))
290
287
_b_param = _RealParameter ('b' , domain = _b_domain , typical = (1.1 , 1e3 ))
@@ -304,15 +301,56 @@ def _process_parameters(self, a=None, b=None, ab=None, **kwargs):
304
301
kwargs .update (dict (a = a , b = b , ab = ab ))
305
302
return kwargs
306
303
304
+ def _logpdf_formula (self , x , * , ab , ** kwargs ):
305
+ return np .where (np .isnan (x ), np .nan , - np .log (ab ))
306
+
307
307
def _pdf_formula (self , x , * , ab , ** kwargs ):
308
- return np .full (x .shape , 1 / ab )
308
+ return np .where (np .isnan (x ), np .nan , 1 / ab )
309
+
310
+ def _logcdf_formula (self , x , * , a , ab , ** kwargs ):
311
+ with np .errstate (divide = 'ignore' ):
312
+ return np .log (x - a ) - np .log (ab )
313
+
314
+ def _cdf_formula (self , x , * , a , ab , ** kwargs ):
315
+ return (x - a ) / ab
316
+
317
+ def _logccdf_formula (self , x , * , b , ab , ** kwargs ):
318
+ with np .errstate (divide = 'ignore' ):
319
+ return np .log (b - x ) - np .log (ab )
320
+
321
+ def _ccdf_formula (self , x , * , b , ab , ** kwargs ):
322
+ return (b - x ) / ab
309
323
310
- def _icdf_formula (self , x , a , b , ab , ** kwargs ):
311
- return a + ab * x
324
+ def _icdf_formula (self , p , * , a , ab , ** kwargs ):
325
+ return a + ab * p
326
+
327
+ def _iccdf_formula (self , p , * , b , ab , ** kwargs ):
328
+ return b - ab * p
329
+
330
+ def _entropy_formula (self , * , ab , ** kwargs ):
331
+ return np .log (ab )
312
332
313
333
def _mode_formula (self , * , a , b , ab , ** kwargs ):
314
334
return a + 0.5 * ab
315
335
336
+ def _median_formula (self , * , a , b , ab , ** kwargs ):
337
+ return a + 0.5 * ab
338
+
339
+ def _moment_raw_formula (self , order , a , b , ab , ** kwargs ):
340
+ np1 = order + 1
341
+ return (b ** np1 - a ** np1 ) / (np1 * ab )
342
+
343
+ def _moment_central_formula (self , order , ab , ** kwargs ):
344
+ return ab ** 2 / 12 if order == 2 else None
345
+
346
+ _moment_central_formula .orders = [2 ] # type: ignore[attr-defined]
347
+
348
+ def _sample_formula (self , sample_shape , full_shape , rng , a , b , ab , ** kwargs ):
349
+ try :
350
+ return rng .uniform (a , b , size = full_shape )[()]
351
+ except OverflowError : # happens when there are NaNs
352
+ return rng .uniform (0 , 1 , size = full_shape )* ab + a
353
+
316
354
317
355
class _Gamma (ContinuousDistribution ):
318
356
# Gamma distribution for testing only
@@ -331,28 +369,7 @@ def _pdf_formula(self, x, *, a, **kwargs):
331
369
332
370
# Distribution classes need only define the summary and beginning of the extended
333
371
# summary portion of the class documentation. All other documentation, including
334
- # examples, is generated automatically. This may be time-consuming for distributions
335
- # with slow methods, so we generate the documentation offline and store it as a static
336
- # `_new_distributions_docs.json` file. After making updates to the documentation of
337
- # a class, execute this file as a script to re-generate `_new_distribution_docs.json`.
338
- # Improvements to this system are welcome.
339
- _docfile = "_new_distribution_docs.json"
340
- _docdir = os .path .dirname (__file__ )
341
- _docpath = os .path .abspath (os .path .join (_docdir , _docfile ))
372
+ # examples, is generated automatically.
342
373
_module = sys .modules [__name__ ].__dict__
343
-
344
- if __name__ == "__main__" :
345
- # When executed as a script, generate the complete docstring for each distribution
346
- # class (`_combine_docs`), store them in a dictionary, and write to a file.
347
- docs = {}
348
- for dist_name in __all__ :
349
- docs [dist_name ] = _combine_docs (_module [dist_name ])
350
- with open (_docpath , 'w' ) as f :
351
- json .dump (docs , f , indent = " " )
352
-
353
- # When imported, load the dictionary from the file, and assign to each distribution
354
- # class's `__doc__` attribute the corresponding docstring.
355
- with open (_docpath ) as f :
356
- docs = json .load (f )
357
- for dist_name in __all__ :
358
- _module [dist_name ].__doc__ = docs [dist_name ]
374
+ for dist_name in __all__ :
375
+ _module [dist_name ].__doc__ = _combine_docs (_module [dist_name ])
0 commit comments