-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmixins_example.py
More file actions
42 lines (35 loc) · 1.43 KB
/
mixins_example.py
File metadata and controls
42 lines (35 loc) · 1.43 KB
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
import numpy as np
class ProbabilityDensityFunction(np.lib.mixins.NDArrayOperatorsMixin):
"""
"""
def __init__(self, **kwargs):
self._pdf_ = kwargs.get('probabilities', np.tile(0.5, kwargs.get('shape', (2, 2))))
self._HANDLED_TYPES_ = (np.ndarray, type(self))
if not np.allclose(self._pdf_.sum(), 1.0):
self._pdf_ /= self._pdf_.sum()
self._pdf_ = np.nan_to_num(self._pdf_)
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
out = kwargs.get('out', ())
for x in inputs + out:
if not isinstance(x, self._HANDLED_TYPES_):
return NotImplemented
inputs = tuple(x._pdf_ if isinstance(x, ProbabilityDensityFunction) else x for x in inputs)
if out:
kwargs['out'] = (x._pdf_ if isinstance(x, ProbabilityDensityFunction) else x for x in out)
result = getattr(ufunc, method)(*inputs, **kwargs)
if type(result) is tuple:
return tuple(type(self)(probabilities = x) for x in result)
elif method == 'at':
return None
else:
return type(self)(probabilities = result)
def __call__(self, **kwargs):
return self._pdf_
def __repr__(self):
''' '''
repr_string = 'PDF: \n%s' % (
self._pdf_.__str__()
)
return repr_string
pdf = ProbabilityDensityFunction()
print(np.subtract(pdf, pdf))