Truncated Gaussian#
Conditioning a probabilistic circuit on a random event requires conditioning of any kind of distribution to an interval on the real line. Unfortunately, the literature regarding the usage of a truncated Gaussian in a probabilistic circuit is not very extensive. In this notebook, we will explore the usage of a truncated Gaussian in a probabilistic circuit.
First, let us create a Normal distribution:
from random_events.interval import closed
from random_events.variable import Continuous
from random_events.product_algebra import Event, SimpleEvent
from probabilistic_model.distributions import GaussianDistribution, TruncatedGaussianDistribution
import plotly
plotly.offline.init_notebook_mode()
import plotly.graph_objects as go
variable = Continuous("x")
distribution = GaussianDistribution(variable, location=0, scale=1)
fig = go.Figure(distribution.plot())
fig.update_layout(title="Normal Distribution", xaxis_title=distribution.variable.name)
fig.show()
Whenever one conditions a Gaussian Distribution to an event, the result of that action will give a truncated Gaussian. Let us see what happens:
evidence = SimpleEvent({variable: closed(0.5, 2)}).as_composite_set()
conditional_distribution, evidence_probability = distribution.truncated(evidence)
fig = go.Figure(conditional_distribution.plot())
fig.update_layout(title="Normal Distribution", xaxis_title=distribution.variable.name)
fig.show()
Perhaps it is a bit surprising that computing (non-centered) moments for a truncated Gaussian is actually pretty hard. For this, one has to calculate the integral
While this integral shares some similarities with the ones from a Gaussian distribution, notice how the Expectation operator does not enjoy a full support (i.e., the real line), indeed, the above integral would be equivalent to:
A solution to this problem could be found by recursively using the raw moments of the truncated distribution and the binomial expansion of the inner factor inside the operator, as in:
And so on. Thus, one can easily obtain the following result:
Moreover, [Jaw03] has shown that the following result holds for raw moments:
Where \(\mu\) and \(\sigma\) are the mean and the variance of the starting Gaussian distribution, and the variable \(y\) is obtained by the following change of variables:
and
However, it is known that conventional recursive formulas for truncated moments tend to suffer from subtracted cancellation errors associated, with the differences of large values giving inaccuracies as their orders become high [PSA19]. Furthermore, the computation time of higher order moments can explode whenever one deals with recursive formulas. That is why our implementations follow the paper by [Oga22], as a non-recursive method is defined for dealing with orders of any kind (this includes real-valued orders).
For the simple case of a double truncated Gaussian, the formula proposed by [Oga22] is as follows:
Where:
for \(k, \nu = 0,1,\ldots\).
The term \(F_{\Gamma}(x,a)\) denotes the cdf of the gamma distribution at \(x\) when the shape parameter is \(a\) with the unit scale parameter.