prodigy.distribution.generating_function

class GeneratingFunction(function, *variables, closed=None, finite=None)[source]

Bases: Distribution

This class represents a generating function. It wraps the sympy library. A GeneratingFunction object is designed to be immutable. This class does not ensure to be in a healthy state (i.e., every coefficient is non-negative).

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

  • variables (str | Symbol)

  • closed (bool | None)

  • finite (bool | None)

use_simplification = False
use_latex_output = False
static factory()[source]

Returns a factory for this subclass of distribution

Return type:

Type[CommonDistributionsFactory]

hadamard_product(other)[source]

Computes the Hadamard product of two univariate rational functions, i.e. the point-wise product of the coefficients in their formal power series representation.

Parameters:

other (Distribution)

Return type:

GeneratingFunction

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

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:

GeneratingFunction

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

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

copy(deep=True)[source]

Returns a full copy of itself.

Parameters:

deep (bool)

Return type:

GeneratingFunction

set_variables(*variables)[source]

Sets the free variables in a distribution.

Parameters:

variables (str) – The variables.

Return type:

GeneratingFunction

Returns:

The distribution with free variables variables.

set_parameters(*parameters)[source]

Sets the parameters in a distribution.

Parameters:

parameters (str) – The parameters.

Return type:

GeneratingFunction

Returns:

The distribution with parameters parameters.

approximate(threshold)[source]

Generate an approximation of a generating function, until threshold percent or terms of the probability mass is captured.

Parameters:

threshold (str | int) – The probability percentage threshold of the probability mass of the distribution.

Return type:

Generator[GeneratingFunction]

Returns:

A Generating Function generator.

coefficient_sum()[source]
Return type:

Expr

get_probability_mass()[source]

Returns the probability mass of the distribution.

get_parameters()[source]

Returns the parameters of the distribution.

Return type:

Set[str]

get_variables()[source]

Returns the free variables of the distribution.

Return type:

Set[str]

is_zero_dist()[source]

Returns whether the distribution encodes the 0 distribution.

Return type:

bool

normalize()[source]

Normalizes the probability mass of the distribution.

Return type:

GeneratingFunction

is_finite()[source]

Checks whether the generating function is finite.

Returns:

True if the GF is a polynomial, False otherwise.

static evaluate(expression, state)[source]

Evaluates the expression in a given state.

Parameters:
Return type:

Expr

classmethod evaluate_condition(condition, state)[source]

Evaluates whether the condition holds in the specified state.

Parameters:
  • condition (BinopExpr)

  • state (State)

Return type:

bool

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:

GeneratingFunction

Returns:

The marginal 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:

GeneratingFunction

get_symbols()[source]

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

Return type:

Set[str]

class SympyPGF[source]

Bases: CommonDistributionsFactory

Implements PGFs of standard distributions.

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:

GeneratingFunction

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:

GeneratingFunction

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:

GeneratingFunction

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:

GeneratingFunction

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:

GeneratingFunction

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:

GeneratingFunction

static zero(*variables)[source]
Parameters:

variables (str | Symbol)

Return type:

GeneratingFunction

static undefined(*variables)[source]

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

Parameters:

variables (str | VarExpr)

Return type:

GeneratingFunction

static one(*variables)[source]

A distribution where all variables are initialized with 0.

Parameters:

variables (str | VarExpr)

Return type:

Distribution

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