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() — whether given_vars is nonempty

  • is_symbolic() — sympy probabilities

  • is_numerical() — numeric probabilities

  • is_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 as dims, 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:

float

Distribution.set_rv_names(rv_names)[source]

Rename the dimensions (random variables).

Parameters:

rv_names (list of str) – New names, one per dimension.

Distribution.set_base(base)[source]

Change the probability base in-place.

Parameters:

base (str or float) – 'linear', 2, 'e', or any positive float.

Distribution.copy(base=None)[source]

Return a deep copy of this distribution.

Parameters:

base (str or float, optional) – If given, the copy will be converted to this base.

Returns:

c

Return type:

Distribution

Distribution.rand(size=None, rand=None, prng=None)[source]

Return a random sample from the distribution.

Parameters:
  • size (int or None) – Number of samples. None for a single sample.

  • rand (float, array, or None) – Pre-generated random numbers. None to generate internally.

  • prng (random state, optional) – Random number generator. Defaults to self.prng.

Distribution.normalize()[source]

Normalise the distribution in-place.

For a joint distribution, divides by the total sum. For a conditional, normalises each conditional slice.

Return type:

None