forked from google/ml-fairness-gym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributions.py
123 lines (91 loc) · 3.32 KB
/
distributions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# coding=utf-8
# Copyright 2020 The ML Fairness Gym Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
"""Classes for building distributions."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from absl import logging
import attr
import numpy as np
from typing import Sequence
@attr.s
class Distribution(object):
"""Base distribution class.
Inheriting classes should fill in the sample method and initialize dim.
"""
dim = attr.ib(init=False)
def sample(self, rng):
raise NotImplementedError
def _check_sum_to_one(instance, attribute, value):
"""Raises ValueError if the value does not sum to one."""
del instance, attribute # Unused.
value = np.array(value)
if not np.isclose(np.sum(value), 1):
raise ValueError("Array must sum to one. Got %s." % np.sum(value))
def _check_nonnegative(instance, attribute, value):
"""Raises ValueError if the value elements are negative."""
del instance, attribute # Unused.
value = np.array(value)
if np.any(value < 0):
raise ValueError("Array must be nonnegative. Got %s." % value)
def _check_in_zero_one_range(instance, attribute, value):
"""Raises ValueError if value is not in [0, 1]."""
del instance, attribute # Unused.
value = np.array(value)
if np.any(value < 0) or np.any(value > 1):
raise ValueError("Value must be in [0, 1]. Got %s." % value)
@attr.s
class Mixture(Distribution):
"""A mixture distribution."""
components = attr.ib(factory=list) # type: Sequence[Distribution]
weights = attr.ib(
factory=list, validator=[_check_sum_to_one,
_check_nonnegative]) # type: Sequence[float]
def sample(self, rng):
logging.debug("Sampling from a mixture with %d components. Weights: %s",
len(self.components), self.weights)
component = rng.choice(self.components, p=self.weights)
return component.sample(rng)
def __attrs_post_init__(self):
for component in self.components:
if component.dim != self.components[0].dim:
raise ValueError("Components do not have the same dimensionality.")
self.dim = self.components[0].dim
@attr.s
class Gaussian(Distribution):
"""A Gaussian Distribution."""
mean = attr.ib()
std = attr.ib()
def __attrs_post_init__(self):
self.dim = len(self.mean)
def sample(self, rng):
return rng.normal(self.mean, self.std)
@attr.s
class Bernoulli(Distribution):
"""A Bernoulli Distribution."""
p = attr.ib(validator=[_check_in_zero_one_range])
def __attrs_post_init__(self):
self.dim = 1
def sample(self, rng):
return rng.rand() < self.p
@attr.s
class Constant(Distribution):
"""A Constant Distribution."""
mean = attr.ib()
def __attrs_post_init__(self):
self.dim = len(self.mean)
def sample(self, rng):
del rng # Unused.
return self.mean