Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions python/paddle/distribution/beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

import numbers
from typing import TYPE_CHECKING

import paddle
from paddle.distribution import dirichlet, exponential_family

if TYPE_CHECKING:
from paddle import Tensor


class Beta(exponential_family.ExponentialFamily):
r"""
Expand Down Expand Up @@ -85,8 +91,10 @@ class Beta(exponential_family.ExponentialFamily):
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[-1.91923141, -0.38095081])
"""
alpha: Tensor
beta: Tensor

def __init__(self, alpha, beta):
def __init__(self, alpha: float | Tensor, beta: float | Tensor) -> None:
if isinstance(alpha, numbers.Real):
alpha = paddle.full(shape=[], fill_value=alpha)

Expand All @@ -102,17 +110,17 @@ def __init__(self, alpha, beta):
super().__init__(self._dirichlet._batch_shape)

@property
def mean(self):
def mean(self) -> Tensor:
"""Mean of beta distribution."""
return self.alpha / (self.alpha + self.beta)

@property
def variance(self):
def variance(self) -> Tensor:
"""Variance of beat distribution"""
sum = self.alpha + self.beta
return self.alpha * self.beta / (sum.pow(2) * (sum + 1))

def prob(self, value):
def prob(self, value: Tensor) -> Tensor:
"""Probability density function evaluated at value

Args:
Expand All @@ -123,7 +131,7 @@ def prob(self, value):
"""
return paddle.exp(self.log_prob(value))

def log_prob(self, value):
def log_prob(self, value: Tensor) -> Tensor:
"""Log probability density function evaluated at value

Args:
Expand All @@ -146,7 +154,7 @@ def sample(self, shape=()):
shape = shape if isinstance(shape, tuple) else tuple(shape)
return paddle.squeeze(self._dirichlet.sample(shape)[..., 0], axis=-1)

def entropy(self):
def entropy(self) -> Tensor:
"""Entropy of dirichlet distribution

Returns:
Expand All @@ -155,8 +163,8 @@ def entropy(self):
return self._dirichlet.entropy()

@property
def _natural_parameters(self):
def _natural_parameters(self) -> tuple[Tensor, Tensor]:
return (self.alpha, self.beta)

def _log_normalizer(self, x, y):
def _log_normalizer(self, x: Tensor, y: Tensor) -> Tensor:
return paddle.lgamma(x) + paddle.lgamma(y) - paddle.lgamma(x + y)