probabilistic_model.distributions.distributions#
Classes#
Abstract Base class for Univariate distributions. |
|
Abstract base class for continuous distributions. |
|
Abstract base class for continuous distributions with finite support. |
|
Abstract base class for univariate discrete distributions. |
|
Class for symbolic (categorical) distributions. |
|
Abstract base class for integer distributions. Integer distributions also implement the methods of continuous |
|
Class for Dirac delta distributions. |
Module Contents#
- class probabilistic_model.distributions.distributions.UnivariateDistribution#
Bases:
probabilistic_model.probabilistic_model.ProbabilisticModel,random_events.variable.SubclassJSONSerializer,probabilistic_model.interfaces.drawio.drawio.DrawIOInterfaceAbstract Base class for Univariate distributions.
- variable: random_events.variable.Variable#
- property variables: typing_extensions.Tuple[random_events.variable.Variable, Ellipsis]#
- Returns:
The variables of the model.
- property support: random_events.product_algebra.Event#
- Returns:
The support of the model.
- property univariate_support: random_events.variable.AbstractCompositeSet#
- Abstractmethod:
- Returns:
The univariate support of the distribution. This is not an Event.
- log_mode() typing_extensions.Tuple[random_events.product_algebra.Event, float]#
Calculate the mode of the model.
Check the documentation of mode for more information.
- Returns:
The mode and its log-likelihood.
- abstract univariate_log_mode() typing_extensions.Tuple[random_events.variable.AbstractCompositeSet, float]#
- Returns:
The univariate mode of the distribution and its log-likelihood. The mode is not an Event.
- marginal(variables: typing_extensions.Iterable[random_events.variable.Variable]) random_events.variable.Optional[typing_extensions.Self]#
Calculate the marginal distribution of a set of variables.
- Parameters:
variables – The variables to calculate the marginal distribution on.
- Returns:
The marginal distribution over the variables.
- __eq__(other: typing_extensions.Self)#
- to_json() typing_extensions.Dict[str, typing_extensions.Any]#
- composite_set_from_event(event: random_events.product_algebra.Event) random_events.variable.AbstractCompositeSet#
Extract the composite set from the event that is relevant for this distribution. :param event: The event :return: The composite set
- property abbreviated_symbol: str#
- property drawio_style: typing_extensions.Dict[str, typing_extensions.Any]#
The style of the object.
- class probabilistic_model.distributions.distributions.ContinuousDistribution#
Bases:
UnivariateDistributionAbstract base class for continuous distributions.
- variable: random_events.variable.Continuous#
- property univariate_support: random_events.variable.Interval#
- Abstractmethod:
- Returns:
The univariate support of the distribution. This is not an Event.
- abstract cdf(x: numpy.array) numpy.array#
Calculate the cumulative distribution function at x. :param x: The data :return: The cumulative distribution function at x
- probability_of_simple_event(event: random_events.product_algebra.SimpleEvent) float#
Calculate the probability of a simple event.
The event belongs to the class of marginal queries.
Note
You can read more about queries of this class in Definition 11 in [CVVdB20] or watch the video tutorial. [US20]
- Parameters:
event – The event.
- Returns:
The probability of the event.
- log_truncated(event: random_events.product_algebra.Event) typing_extensions.Tuple[random_events.variable.Optional[typing_extensions.Self], float]#
Calculate the truncated distribution P(*| event) and the probability of the event.
Check the documentation of truncated for more information.
- Parameters:
event – The event to condition on.
- Returns:
The truncated distribution and the log-probability of the event.
- log_conditional(point: typing_extensions.Dict[random_events.variable.Variable, typing_extensions.Any]) typing_extensions.Tuple[random_events.variable.Optional[typing_extensions.Union[probabilistic_model.probabilistic_model.ProbabilisticModel, typing_extensions.Self]], float]#
Calculate the truncated distribution P(*| point) and the probability of the event. Check the documentation of conditional for more information.
- Parameters:
point – A partial point to calculate the truncated distribution on.
- Returns:
The truncated distribution and the log-probability of the point.
- abstract log_conditional_from_simple_interval(interval: random_events.variable.SimpleInterval) typing_extensions.Tuple[typing_extensions.Self, float]#
Calculate the truncated distribution given a simple interval.
- Parameters:
interval – The simple interval
- Returns:
The truncated distribution and the log-probability of the interval.
- abstract log_conditional_from_interval(interval) typing_extensions.Tuple[typing_extensions.Self, float]#
Calculate the truncated distribution given an interval with p(interval) > 0. :param interval: The simple interval :return: The truncated distribution and the log-probability of the interval.
- class probabilistic_model.distributions.distributions.ContinuousDistributionWithFiniteSupport#
Bases:
ContinuousDistributionAbstract base class for continuous distributions with finite support.
- interval: random_events.variable.SimpleInterval#
The interval of the distribution.
- property lower: float#
- property upper: float#
- property univariate_support: random_events.variable.Interval#
- Returns:
The univariate support of the distribution. This is not an Event.
- left_included_condition(x: numpy.array) numpy.array#
Check if x is included in the left bound of the interval. :param x: The data :return: A boolean array
- right_included_condition(x: numpy.array) numpy.array#
Check if x is included in the right bound of the interval. :param x: The data :return: A boolean array
- included_condition(x: numpy.array) numpy.array#
Check if x is included in interval. :param x: The data :return: A boolean array
- log_likelihood(x: numpy.array) numpy.array#
Calculate the log-likelihood of an event.
Check the documentation of likelihood for more information.
- Parameters:
events – The full evidence event with shape (#events, #variables)
- Returns:
The log-likelihood of the event with shape (#events).
- abstract log_likelihood_without_bounds_check(x: numpy.array) numpy.array#
Evaluate the logarithmic likelihood function at x without checking the inclusion into the interval. :param x: x where p(x) > 0 :return: log(p(x))
- translate(translation: typing_extensions.Dict[random_events.variable.Variable, float])#
Translate the model in-place. Translation is done by adding the translation to the variable location influencing values. The translation can be viewed as what happens when you shift the numeric variables of the model by a constant vector.
- Parameters:
translation – The variable value pairs to translate the model by.
- scale(scaling: typing_extensions.Dict[random_events.variable.Variable, float])#
Scale the model in-place. Scaling is done by multiplying the variable location influencing values. The scaling can be viewed as what happens when you multiply the numeric variables of the model by a constant vector.
- Parameters:
scaling – The variable value pairs to scale the model by.
- class probabilistic_model.distributions.distributions.DiscreteDistribution(variable: typing_extensions.Union[random_events.variable.Symbolic, random_events.variable.Integer], probabilities: random_events.variable.Optional[probabilistic_model.utils.MissingDict[typing_extensions.Union[int, random_events.variable.SetElement], float]])#
Bases:
UnivariateDistributionAbstract base class for univariate discrete distributions.
- variable: typing_extensions.Union[random_events.variable.Symbolic, random_events.variable.Integer]#
- probabilities: probabilistic_model.utils.MissingDict[int, float]#
A dict that maps from integers (hash(symbol) for symbols) to probabilities.
- __eq__(other)#
- __hash__()#
- log_likelihood(events: numpy.array) numpy.array#
Calculate the log-likelihood of an event.
Check the documentation of likelihood for more information.
- Parameters:
events – The full evidence event with shape (#events, #variables)
- Returns:
The log-likelihood of the event with shape (#events).
- fit(data: numpy.array) typing_extensions.Self#
Fit the distribution to the data.
The probabilities are set equal to the frequencies in the data. The data contains the indices of the domain elements (if symbolic) or the values (if integer).
- Parameters:
data – The data.
- Returns:
The fitted distribution
- abstract probabilities_for_plotting() typing_extensions.Dict[typing_extensions.Union[int, str], float]#
- Returns:
The probabilities as dict that can be plotted.
- plot(**kwargs) typing_extensions.List[plotly.graph_objects.Bar]#
Plot the distribution.
- to_json() typing_extensions.Dict[str, typing_extensions.Any]#
- classmethod _from_json(data: typing_extensions.Dict[str, typing_extensions.Any]) typing_extensions.Self#
Create a variable from a json dict. This method is called from the from_json method after the correct subclass is determined and should be overwritten by the respective subclass.
- Parameters:
data – The json dict
- Returns:
The deserialized object
- normalize()#
Normalize the distribution.
- log_truncated(event: random_events.product_algebra.Event) typing_extensions.Tuple[random_events.variable.Optional[typing_extensions.Self], float]#
Calculate the truncated distribution P(*| event) and the probability of the event.
Check the documentation of truncated for more information.
- Parameters:
event – The event to condition on.
- Returns:
The truncated distribution and the log-probability of the event.
- log_conditional(point: typing_extensions.Dict[random_events.variable.Variable, typing_extensions.Any]) typing_extensions.Tuple[random_events.variable.Optional[typing_extensions.Self], float]#
Calculate the truncated distribution P(*| point) and the probability of the event. Check the documentation of conditional for more information.
- Parameters:
point – A partial point to calculate the truncated distribution on.
- Returns:
The truncated distribution and the log-probability of the point.
- log_conditional_of_composite_set(event: random_events.variable.AbstractCompositeSet) typing_extensions.Tuple[random_events.variable.Optional[typing_extensions.Self], float]#
- __copy__() typing_extensions.Self#
- __deepcopy__(memo=None) typing_extensions.Self#
- __repr__()#
- sample(amount: int) numpy.array#
Sample from the model.
- Parameters:
amount – The number of samples to draw.
- Returns:
The samples.
- class probabilistic_model.distributions.distributions.SymbolicDistribution(variable: typing_extensions.Union[random_events.variable.Symbolic, random_events.variable.Integer], probabilities: random_events.variable.Optional[probabilistic_model.utils.MissingDict[typing_extensions.Union[int, random_events.variable.SetElement], float]])#
Bases:
DiscreteDistributionClass for symbolic (categorical) distributions.
- variable: random_events.variable.Symbolic#
- univariate_log_mode() typing_extensions.Tuple[random_events.variable.Set, float]#
- Returns:
The univariate mode of the distribution and its log-likelihood. The mode is not an Event.
- log_conditional_of_composite_set(event: random_events.variable.AbstractCompositeSet) typing_extensions.Tuple[random_events.variable.Optional[typing_extensions.Self], float]#
- probabilities_for_plotting() typing_extensions.Dict[typing_extensions.Union[int, str], float]#
- Returns:
The probabilities as dict that can be plotted.
- property univariate_support: random_events.variable.Set#
- Returns:
The univariate support of the distribution. This is not an Event.
- probability_of_simple_event(event: random_events.product_algebra.SimpleEvent) float#
Calculate the probability of a simple event.
The event belongs to the class of marginal queries.
Note
You can read more about queries of this class in Definition 11 in [CVVdB20] or watch the video tutorial. [US20]
- Parameters:
event – The event.
- Returns:
The probability of the event.
- property representation#
The symbol used to represent this distribution.
- property drawio_label#
The label of the object as a drawio compatible string.
- property image#
- fit(data: numpy.array) typing_extensions.Self#
Fit the distribution to the data.
The probabilities are set equal to the frequencies in the data. The data contains the indices of the domain elements (if symbolic) or the values (if integer).
- Parameters:
data – The data.
- Returns:
The fitted distribution
- fit_from_indices(data: numpy.array) typing_extensions.Self#
- to_json() typing_extensions.Dict[str, typing_extensions.Any]#
- classmethod _from_json(data: typing_extensions.Dict[str, typing_extensions.Any]) typing_extensions.Self#
Create a variable from a json dict. This method is called from the from_json method after the correct subclass is determined and should be overwritten by the respective subclass.
- Parameters:
data – The json dict
- Returns:
The deserialized object
- class probabilistic_model.distributions.distributions.IntegerDistribution(variable: random_events.variable.Integer, probabilities: random_events.variable.Optional[probabilistic_model.utils.MissingDict[typing_extensions.Union[int, random_events.variable.SetElement], float]])#
Bases:
ContinuousDistribution,DiscreteDistributionAbstract base class for integer distributions. Integer distributions also implement the methods of continuous distributions.
- variable: random_events.variable.Integer#
- log_truncated(event: random_events.product_algebra.Event) typing_extensions.Tuple[random_events.variable.Optional[typing_extensions.Self], float]#
Calculate the truncated distribution P(*| event) and the probability of the event.
Check the documentation of truncated for more information.
- Parameters:
event – The event to condition on.
- Returns:
The truncated distribution and the log-probability of the event.
- univariate_log_mode() typing_extensions.Tuple[random_events.variable.AbstractCompositeSet, float]#
- Returns:
The univariate mode of the distribution and its log-likelihood. The mode is not an Event.
- probabilities_for_plotting() typing_extensions.Dict[typing_extensions.Union[int, str], float]#
- Returns:
The probabilities as dict that can be plotted.
- property univariate_support: random_events.variable.Interval#
- Returns:
The univariate support of the distribution. This is not an Event.
- cdf(x: numpy.array) numpy.array#
Calculate the cumulative distribution function at x. :param x: The data :return: The cumulative distribution function at x
- probability_of_simple_event(event: random_events.product_algebra.SimpleEvent) float#
Calculate the probability of a simple event.
The event belongs to the class of marginal queries.
Note
You can read more about queries of this class in Definition 11 in [CVVdB20] or watch the video tutorial. [US20]
- Parameters:
event – The event.
- Returns:
The probability of the event.
- property representation#
The symbol used to represent this distribution.
- moment(order: probabilistic_model.probabilistic_model.OrderType, center: probabilistic_model.probabilistic_model.CenterType) probabilistic_model.probabilistic_model.MomentType#
Calculate the (centralized) moment of the distribution.
\[\int_{-\infty}^{\infty} (x - center)^{order} pdf(x) dx\]Note
You can read more about queries of this class in Definition 22 in :cite:p:`choi2020probabilistic`_. [US20]
- Parameters:
order – The orders of the moment as a variable map for every continuous and integer variable.
center – The center of the moment as a variable map for every continuous and integer variable.
- Returns:
The moments of the variables in order.
- plot(**kwargs) typing_extensions.List[plotly.graph_objects.Bar]#
Generate traces that can be plotted with plotly.
- Parameters:
number_of_samples – The number of samples to draw.
surface – If True, plot the model as a surface plot.
mode – If True, plot the mode of the model.
- Returns:
The traces.
- translate(translation: typing_extensions.Dict[random_events.variable.Variable, int])#
Translate the model in-place. Translation is done by adding the translation to the variable location influencing values. The translation can be viewed as what happens when you shift the numeric variables of the model by a constant vector.
- Parameters:
translation – The variable value pairs to translate the model by.
- scale(scaling: typing_extensions.Dict[random_events.variable.Variable, int])#
Scale the model in-place. Scaling is done by multiplying the variable location influencing values. The scaling can be viewed as what happens when you multiply the numeric variables of the model by a constant vector.
- Parameters:
scaling – The variable value pairs to scale the model by.
- class probabilistic_model.distributions.distributions.DiracDeltaDistribution(variable: random_events.variable.Continuous, location: float, density_cap: float = np.inf, tolerance: float = 1e-06)#
Bases:
ContinuousDistributionClass for Dirac delta distributions. The Dirac measure is used whenever evidence is given as a singleton instance.
https://en.wikipedia.org/wiki/Dirac_delta_function
- variable: random_events.variable.Continuous#
- location: float#
The location of the Dirac delta distribution.
- density_cap: float#
The density cap of the Dirac delta distribution. This value will be used to replace infinity in likelihood.
- tolerance: float = 1e-06#
The tolerance of deviations of the location of the Dirac delta distribution. This is used during calculations to take precision problems into account.
- log_likelihood(events: numpy.array) numpy.array#
Calculate the log-likelihood of an event.
Check the documentation of likelihood for more information.
- Parameters:
events – The full evidence event with shape (#events, #variables)
- Returns:
The log-likelihood of the event with shape (#events).
- cdf(x: numpy.array) numpy.array#
Calculate the cumulative distribution function at x. :param x: The data :return: The cumulative distribution function at x
- property abbreviated_symbol: str#
- property univariate_support: random_events.variable.Interval#
- Returns:
The univariate support of the distribution. This is not an Event.
- log_conditional_from_simple_interval(interval: random_events.variable.SimpleInterval) typing_extensions.Tuple[typing_extensions.Self, float]#
Calculate the truncated distribution given a simple interval.
- Parameters:
interval – The simple interval
- Returns:
The truncated distribution and the log-probability of the interval.
- probability_of_simple_event(event: random_events.product_algebra.SimpleEvent) float#
Calculate the probability of a simple event.
The event belongs to the class of marginal queries.
Note
You can read more about queries of this class in Definition 11 in [CVVdB20] or watch the video tutorial. [US20]
- Parameters:
event – The event.
- Returns:
The probability of the event.
- univariate_log_mode() typing_extensions.Tuple[random_events.variable.AbstractCompositeSet, float]#
- Returns:
The univariate mode of the distribution and its log-likelihood. The mode is not an Event.
- sample(amount: int) numpy.array#
Sample from the model.
- Parameters:
amount – The number of samples to draw.
- Returns:
The samples.
- moment(order: probabilistic_model.probabilistic_model.OrderType, center: probabilistic_model.probabilistic_model.CenterType) probabilistic_model.probabilistic_model.MomentType#
Calculate the (centralized) moment of the distribution.
\[\int_{-\infty}^{\infty} (x - center)^{order} pdf(x) dx\]Note
You can read more about queries of this class in Definition 22 in :cite:p:`choi2020probabilistic`_. [US20]
- Parameters:
order – The orders of the moment as a variable map for every continuous and integer variable.
center – The center of the moment as a variable map for every continuous and integer variable.
- Returns:
The moments of the variables in order.
- __eq__(other)#
- __hash__()#
- property representation#
The symbol used to represent this distribution.
- __repr__()#
- __copy__()#
- __deepcopy__(memo=None)#
- to_json() typing_extensions.Dict[str, typing_extensions.Any]#
- classmethod _from_json(data: typing_extensions.Dict[str, typing_extensions.Any]) typing_extensions.Self#
Create a variable from a json dict. This method is called from the from_json method after the correct subclass is determined and should be overwritten by the respective subclass.
- Parameters:
data – The json dict
- Returns:
The deserialized object
- plot(**kwargs) typing_extensions.List#
Generate traces that can be plotted with plotly.
- Parameters:
number_of_samples – The number of samples to draw.
surface – If True, plot the model as a surface plot.
mode – If True, plot the mode of the model.
- Returns:
The traces.
- translate(translation: random_events.product_algebra.VariableMap[random_events.variable.Variable, float])#
Translate the model in-place. Translation is done by adding the translation to the variable location influencing values. The translation can be viewed as what happens when you shift the numeric variables of the model by a constant vector.
- Parameters:
translation – The variable value pairs to translate the model by.
- scale(scaling: random_events.product_algebra.VariableMap[random_events.variable.Variable, float])#
Scale the model in-place. Scaling is done by multiplying the variable location influencing values. The scaling can be viewed as what happens when you multiply the numeric variables of the model by a constant vector.
- Parameters:
scaling – The variable value pairs to scale the model by.