Skip to content

Commit

Permalink
v0.4.3 (#32)
Browse files Browse the repository at this point in the history
* log_p and log_x in entropy_pooling loop. (#30)

* v0.4.3rc (#31)

* Poetry update

* Docs update

Co-authored-by: ft-cama <[email protected]>
  • Loading branch information
antonvorobets and ft-cama authored May 25, 2022
1 parent 654548d commit 2651fcf
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 102 deletions.
Binary file modified docs/build/doctrees/environment.pickle
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/build/html/contributing.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<a href="index.html" class="icon icon-home"> fortitudo.tech
</a>
<div class="version">
v0.4.2
v0.4.3
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<a href="index.html" class="icon icon-home"> fortitudo.tech
</a>
<div class="version">
v0.4.2
v0.4.3
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<a href="index.html" class="icon icon-home"> fortitudo.tech
</a>
<div class="version">
v0.4.2
v0.4.3
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<a href="index.html" class="icon icon-home"> fortitudo.tech
</a>
<div class="version">
v0.4.2
v0.4.3
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<a href="#" class="icon icon-home"> fortitudo.tech
</a>
<div class="version">
v0.4.2
v0.4.3
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
Expand Down
Binary file modified docs/build/html/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/build/html/py-modindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<a href="index.html" class="icon icon-home"> fortitudo.tech
</a>
<div class="version">
v0.4.2
v0.4.3
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/references.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<a href="index.html" class="icon icon-home"> fortitudo.tech
</a>
<div class="version">
v0.4.2
v0.4.3
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<a href="index.html" class="icon icon-home"> fortitudo.tech
</a>
<div class="version">
v0.4.2
v0.4.3
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="#" method="get">
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
author = 'Fortitudo Technologies'

# The full version, including alpha/beta/rc tags
version = 'v0.4.2'
version = 'v0.4.3'


# -- General configuration ---------------------------------------------------
Expand Down
39 changes: 21 additions & 18 deletions fortitudo/tech/entropy_pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,81 +34,84 @@ def entropy_pooling(
Returns:
Posterior probability vector with shape (S, 1).
"""
log_p = np.log(p)
if G is None:
solution = minimize(
_dual_equality, x0=np.zeros(A.shape[0]), args=(p, A, b),
_dual_equality, x0=np.zeros(A.shape[0]), args=(log_p, A, b),
method='Newton-CG', jac=True, hess=_hessian_equality,
options={'maxiter': 10000})
q = np.exp(np.log(p) - 1 - A.T @ solution.x[:, np.newaxis])
q = np.exp(log_p - 1 - A.T @ solution.x[:, np.newaxis])
else:
len_b = len(b)
len_h = len(h)
len_bh = len_b + len_h
bounds = Bounds([-np.inf] * len_b + [0] * len_h, [np.inf] * len_bh)
lhs = np.vstack((A, G))
solution = minimize(
_dual_inequality, x0=np.zeros(len_bh), args=(p, lhs, np.vstack((b, h))),
_dual_inequality, x0=np.zeros(len_bh), args=(log_p, lhs, np.vstack((b, h))),
method='TNC', jac=True, bounds=bounds, options={'maxiter': 10000})
q = np.exp(np.log(p) - 1 - lhs.T @ solution.x[:, np.newaxis])
q = np.exp(log_p - 1 - lhs.T @ solution.x[:, np.newaxis])
return q


def _dual_equality(
equality_multipliers: np.ndarray, p: np.ndarray, A: np.ndarray,
equality_multipliers: np.ndarray, log_p: np.ndarray, A: np.ndarray,
b: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""Function computing equality constrained objective and gradient.
Args:
equality_multipliers: Lagrange multipliers with shape (M,).
p: Prior probability vector with shape (S, 1).
log_p: Log of prior probability vector with shape (S, 1).
A: Equality constraint matrix with shape (M, S).
b: Equality constraint vector with shape (M, 1).
Returns:
Tuple containing the dual objective value and gradient.
"""
equality_multipliers = equality_multipliers[:, np.newaxis]
x = np.exp(np.log(p) - 1 - A.T @ equality_multipliers)
log_x = log_p - 1 - A.T @ equality_multipliers
x = np.exp(log_x)
gradient = b - A @ x
dual_objective = x.T @ (np.log(x) - np.log(p)) - equality_multipliers.T @ gradient
dual_objective = x.T @ (log_x - log_p) - equality_multipliers.T @ gradient
return -dual_objective, gradient.flatten()


def _dual_inequality(
lagrange_multipliers: np.ndarray, p: np.ndarray, lhs: np.ndarray,
lagrange_multipliers: np.ndarray, log_p: np.ndarray, lhs: np.ndarray,
rhs: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""Function computing inequality constrained objective and gradient.
Args:
lagrange_multipliers: Lagrange multipliers with shape (N + M,).
p: Prior probability vector with shape (S, 1).
lhs: Matrix with shape (N + M, S).
rhs: Vector with shape (N + M, 1).
lagrange_multipliers: Lagrange multipliers with shape (M + N,).
log_p: Log of prior probability vector with shape (S, 1).
lhs: Matrix with shape (M + N, S).
rhs: Vector with shape (M + N, 1).
Returns:
Tuple containing the dual objective value and gradient.
"""
lagrange_multipliers = lagrange_multipliers[:, np.newaxis]
x = np.exp(np.log(p) - 1 - lhs.T @ lagrange_multipliers)
log_x = log_p - 1 - lhs.T @ lagrange_multipliers
x = np.exp(log_x)
gradient = rhs - lhs @ x
dual_objective = x.T @ (np.log(x) - np.log(p)) - lagrange_multipliers.T @ gradient
dual_objective = x.T @ (log_x - log_p) - lagrange_multipliers.T @ gradient
return -1000 * dual_objective, 1000 * gradient


def _hessian_equality(
equality_multipliers: np.ndarray, p: np.ndarray,
equality_multipliers: np.ndarray, log_p: np.ndarray,
A: np.ndarray, b: np.ndarray) -> np.ndarray:
"""Function computing equality constrained hessian.
Args:
equality_multipliers: Lagrange multipliers with shape (M,).
p: Prior probability vector with shape (S, 1).
log_p: Log of prior probability vector with shape (S, 1).
A: Equality constraint matrix with shape (M, S).
b: Equality constraint vector with shape (M, 1).
Returns:
Hessian matrix with shape (M, M).
"""
x = np.exp(np.log(p) - 1 - A.T @ equality_multipliers[:, np.newaxis])
x = np.exp(log_p - 1 - A.T @ equality_multipliers[:, np.newaxis])
hessian = A @ (x @ np.ones((1, A.shape[0])) * A.T)
return hessian
Loading

0 comments on commit 2651fcf

Please sign in to comment.