prodigy.distribution.distribution

constant_value(expression)[source]

The value of an arithmetic expression without variables, or None if the expression contains variables, an operator other than + - * / % ^, or an intermediate result that natural-number arithmetic does not allow (negative difference, division by zero, non-integer modulo or exponent).

Parameters:

expression (VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

Return type:

Fraction | None

class MarginalType(*values)[source]

Bases: Enum

Specifies the type of marginalization.

INCLUDE = 1
EXCLUDE = 2
class State(valuations=dict())[source]

Bases: object

Describes a state of a distribution, i.e., an assignment of variables to values

Parameters:

valuations (Dict[str, int])

valuations: Dict[str, int]

The variable assignment of this state

items()[source]

Provides a view on all (variable, value) pairs

to_monomial()[source]

Provides a string representation of this state in the form of a polynomial. If the state is empty, returns an empty string.

For example, the variable assignment {x: 3, y: 6} gives rise to the string x^3*y^6.

Return type:

str

copy()[source]
extend(other)[source]
Parameters:

other (State)

class Distribution[source]

Bases: ABC

Abstract class that models different representations of probability distributions.

abstractmethod static factory()[source]

Returns a factory for this subclass of distribution

Return type:

Type[CommonDistributionsFactory]

abstractmethod copy(deep=True)[source]

Returns a full copy of itself.

Parameters:

deep (bool)

Return type:

Distribution

get_probability_of(condition)[source]

Returns the probability of a given condition or variable.

Parameters:

condition (VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr | str) – The condition.

Return type:

str

Returns:

The probability that the condition is satisfied.

abstractmethod get_probability_mass()[source]

Returns the probability mass of the distribution.

Return type:

str

abstractmethod get_expected_value_of(expression)[source]

Returns the expected value of the expression expression evaluated in the distribution.

Parameters:

expression (VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr | str)

Return type:

str

abstractmethod normalize()[source]

Normalizes the probability mass of the distribution.

Return type:

Distribution

abstractmethod get_variables()[source]

Returns the free variables of the distribution.

Return type:

Set[str]

abstractmethod get_parameters()[source]

Returns the parameters of the distribution.

Return type:

Set[str]

filter_state(state)[source]

Filters the distribution such that only the specified state is left. If the state does not contain assignments for all variables of the distribution, the resulting distribution contains all extensions of the state.

Parameters:

state (State)

Return type:

Distribution

filter(condition)[source]

Filters the distribution such that only the parts which satisfy the condition are left.

Parameters:

condition (VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

Return type:

Distribution

abstractmethod hadamard_product(other)[source]

Computes the Hadamard product of the distributions, i.e., the point-wise product of the probabilities.

Parameters:

other (Distribution)

Return type:

Distribution

get_symbols()[source]

Returns all symbols that occur in this distribution (a subset of self.get_variables() | self.get_parameters()).

Return type:

Set[str]

abstractmethod static evaluate(expression, state)[source]

Evaluates the expression in a given state.

Parameters:
classmethod evaluate_condition(condition, state)[source]

Evaluates whether the condition holds in the specified state.

Parameters:
  • condition (BinopExpr | UnopExpr)

  • state (State)

Return type:

bool

evaluate_expression(expression, new_var=None)[source]

Creates a new distribution with a single variable that represents all possible results of the given expression.

Parameters:
  • expression (VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

  • new_var (str | None)

Return type:

Distribution

abstractmethod is_zero_dist()[source]

Returns whether the distribution encodes the 0 distribution.

Return type:

bool

abstractmethod is_finite()[source]

Returns whether the distribution has finite support.

Return type:

bool

update(expression, approximate=None)[source]

Updates the current distribution by applying the expression to itself.

Some operations are illegal and will cause this function to raise an error. These operations include subtraction that may cause a variable to have a negative value, division that may cause a variable to have a value that is not an integer, and certain operations on infinite generating functions if the variables involved have an infinite marginal (such as multiplication of two variables) and approximation is disabled.

Every intermediate result that depends on a variable must be a natural number. A constant that is not a natural number may therefore only be a factor or a divisor: p/q * e is evaluated as (p * e) / q and is subject to the same divisibility check as a division, which is exact for infinite marginals of rational generating functions as well. Any other use, such as x + 1/2, raises an error.

The approximate parameter is used to determine up to which precision unilateral approximation should be performed if an update is not possible on infinite distributions.

Parameters are not allowed in an update expression.

Parameters:
  • expression (VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

  • approximate (str | float | None)

Return type:

Distribution

abstractmethod get_fresh_variable(exclude=frozenset({}))[source]

Returns a str that is the name of neither an existing variable nor an existing parameter of this distribution nor contained in the exclude parameter.

Parameters:

exclude (Set[str] | FrozenSet[str])

Return type:

str

abstractmethod update_iid(sampling_dist, count, variable)[source]

Updates the distribution by the the iid-sampling rules.

Parameters:
  • sampling_dist (VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

  • count (VarExpr)

  • variable (str | VarExpr)

Return type:

Distribution

abstractmethod marginal(*variables, method=MarginalType.INCLUDE)[source]

Computes the marginal distribution for the given variables (MarginalType.Include), or for all but the given variables (MarginalType.Exclude).

Parameters:
  • variables (str | VarExpr) – A list of variables for which the marginal distribution should be computed. If this list is empty or contains symbols that are not known variables of this distribution, this function will raise an exception.

  • method (MarginalType) – The method of marginalization.

Return type:

Distribution

Returns:

The marginal distribution.

abstractmethod set_variables(*variables)[source]

Sets the free variables in a distribution.

Parameters:

variables (str) – The variables.

Return type:

Distribution

Returns:

The distribution with free variables variables.

abstractmethod set_parameters(*parameters)[source]

Sets the parameters in a distribution.

Parameters:

parameters (str) – The parameters.

Return type:

Distribution

Returns:

The distribution with parameters parameters.

set_variables_and_parameters(variables, parameters)[source]

todo

abstractmethod approximate(threshold)[source]

Computes the approximation until the given threshold is reached (might not terminate).

Parameters:

threshold (str | int) – The threshold either as a maximum number of states (int), or a certain probability mass (str).

Return type:

Generator[Distribution]

Returns:

The approximated (truncated) probability distribution.

abstractmethod approximate_unilaterally(variable, probability_mass)[source]

Approximates the distribution in one variable via its series expansion, up until the specified probability mass is reached.

Parameters:
Return type:

Distribution

approximate_until_finite(probability_mass)[source]

Unilaterally approximates this distribution in only the variables that have an infinite marginal.

Parameters:

probability_mass (str | float)

Return type:

Distribution

get_state()[source]

Returns a state of this distribution that has probability > 0, and its probability

Return type:

Tuple[State, str]

class CommonDistributionsFactory[source]

Bases: ABC

Abstract Factory Class implementing a Factory for common distributions.

abstractmethod static geometric(var, p)[source]

A geometric distribution with success probability p, i.e., p + p*(1-p)*x + p*(1-p)^2*x^2 + ….

Parameters:
  • var (str | VarExpr)

  • p (str | VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

Return type:

Distribution

abstractmethod static uniform(var, lower, upper)[source]

A uniform distribution with bounds [lower,`upper`].

Parameters:
  • var (str | VarExpr)

  • lower (str | VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

  • upper (str | VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

Return type:

Distribution

abstractmethod static bernoulli(var, p)[source]

A bernoulli distribution with parameter p.

Parameters:
  • var (str | VarExpr)

  • p (str | VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

Return type:

Distribution

abstractmethod static poisson(var, lam)[source]

A poisson distribution with parameter lam.

Parameters:
  • var (str | VarExpr)

  • lam (str | VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

Return type:

Distribution

abstractmethod static log(var, p)[source]

A logarithmic distribution with parameter p.

Parameters:
  • var (str | VarExpr)

  • p (str | VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

Return type:

Distribution

abstractmethod static binomial(var, n, p)[source]

A binomial distribution with parameters n and p.

Parameters:
  • var (str | VarExpr)

  • n (str | VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

  • p (str | VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

Return type:

Distribution

abstractmethod static undefined(*variables)[source]

A distribution where actually no information about the states is given.

Parameters:

variables (str | VarExpr)

Return type:

Distribution

abstractmethod static one(*variables)[source]

A distribution where all variables are initialized with 0.

Parameters:

variables (str | VarExpr)

Return type:

Distribution

abstractmethod static from_expr(expression, *variables, **kwargs)[source]

A distribution represented by the expression.

Parameters:

expression (str | VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr)

Return type:

Distribution