Accessors
In [1]: from dit import Distribution
In [2]: d = Distribution(['000', '011', '101', '110'], [1/4]*4)
In [3]: d.set_rv_names('XYZ')
Indexing
Outcomes can be looked up as concatenated strings or as tuples of coordinate
values. sel() selects by named coordinate:
In [4]: d['000']
Out[4]: 0.25
In [5]: d[('0', '0', '0')]
Out[5]: 0.25
In [6]: d.sel(X='0', Y='0', Z='0')
Out[6]: 0.25
An event (a set of outcomes) is summed by
event_probability():
In [7]: d.event_probability([('0', '0', '0'), ('0', '1', '1')])
Out[7]: 0.5
Arrays and tables
In [8]: d.outcomes
Out[8]: (('0', '0', '0'), ('0', '1', '1'), ('1', '0', '1'), ('1', '1', '0'))
In [9]: d.pmf
Out[9]: array([0.25, 0.25, 0.25, 0.25])
In [10]: d.alphabet
Out[10]: (('0', '1'), ('0', '1'), ('0', '1'))
In [11]: d.to_dict()
Out[11]:
{('0', '0', '0'): 0.25,
('0', '1', '1'): 0.25,
('1', '0', '1'): 0.25,
('1', '1', '0'): 0.25}
The underlying DataArray is d.data.
to_numpy() returns the dense ndarray.
Base, copy, sampling
set_base() converts between linear probabilities and log
probabilities (base 2, 'e', or any positive float).
copy() duplicates a distribution, optionally changing
base. rand() draws outcomes.
normalize() renormalizes the free-variable slices.
Queries
is_conditional()— whethergiven_varsis nonemptyis_symbolic()— sympy probabilitiesis_numerical()— numeric probabilitiesis_approx_equal()— compare two distributions
API
- Distribution.__getitem__(key)[source]
Index by dict, outcome tuple, or string.
- Parameters:
key (dict, tuple, or str) – If a dict, performs label-based selection via
sel(). If a string with length matching dims, each character is one variable’s value. If a tuple with the same length asdims, looks up the probability of that outcome.- Returns:
result
- Return type:
float or Distribution
- Raises:
InvalidOutcome – If the outcome is not in the sample space.
- Distribution.sel(**kwargs)[source]
Fix variables to specific values (label-based selection).
- Parameters:
**kwargs – Variable-name to value mappings.
- Returns:
result – If all dimensions are selected, returns a float (probability or log probability, depending on the distribution’s base). Otherwise returns a reduced Distribution.
- Return type:
Distribution or float
Examples
>>> p_xyz.sel(Y='0') # p(X,Z) at Y=0 (un-normalised slice) >>> p_xyz.sel(X='0', Y='1') # p(Z) at X=0,Y=1
- Distribution.event_probability(event)[source]
Compute the probability of an event (subset of outcomes).
- Parameters:
event (iterable of tuples) – Outcomes in the event.
- Returns:
p
- Return type: