Skip to content

Commit 9ed9df3

Browse files
committed
Update: resolution tensor
1 parent 101ac1c commit 9ed9df3

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

‎test/retrieve/oe/test_oe_jax.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_line(self):
6666
self.assertTrue(np.allclose(result.xunc, 0.5, atol=ATOL))
6767
self.assertTrue(np.allclose(result.zvar, 0.0, atol=ATOL))
6868
self.assertTrue(np.allclose(result.cost, 0.0, atol=ATOL))
69-
self.assertIsNone(result.get("resolution"))
69+
self.assertIsNone(result.get("xres"))
7070

7171
def test_line_and_prior(self):
7272
"""The line function is exemplary for linear forward models."""
@@ -109,8 +109,8 @@ def test_line_and_prior(self):
109109
self.assertTrue(np.allclose(result.xunc, np.sqrt(0.5), atol=ATOL))
110110
self.assertTrue(np.all(np.isfinite(result.zvar)))
111111
self.assertTrue(np.all(np.isfinite(result.cost)))
112-
self.assertIsNotNone(result.get("resolution"))
113-
self.assertAlmostEqual(500.0, result.get("resolution").sum())
112+
self.assertIsNotNone(result.get("xres"))
113+
self.assertAlmostEqual(500.0, result.get("xres").sum())
114114

115115
def test_sphere(self):
116116
"""The sphere function has a unique minimum at zero."""

‎uncertaintyx/retrieve/oe/jax.py‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def post(x: Array) -> tuple[Array, Array]:
113113
u = jnp.sqrt(jnp.diag(U))
114114
return U.reshape(x.shape + x.shape), u.reshape(x.shape)
115115

116-
def res(x: Array, U: Array) -> Array:
116+
def resolution(x: Array, U: Array, H: Array) -> Array: # noqa: N806
117117
r"""
118118
Computes the resolution tensor.
119119
@@ -124,15 +124,13 @@ def res(x: Array, U: Array) -> Array:
124124
the effective degrees of freedom.
125125
126126
:param x: The posterior :math:`\hat{x} \in \mathbb{R}^{m}`.
127-
:param U: The posterior uncertainty tensor.
127+
:param x: The posterior uncertainty tensor.
128+
:param H: The inverse prior uncertainty tensor.
128129
:returns: The resolution tensor.
129130
"""
130-
R = jnp.eye(x.size) - ( # noqa: N806
131-
U.reshape(x.size, -1) * hx.reshape(-1)
132-
if hx.ndim == x.ndim
133-
else U.reshape(x.size, -1) @ hx.reshape(x.size, -1)
131+
return jnp.eye(x.size).reshape(U.shape) - (
132+
jnp.tensordot(U, H, x.ndims) if H.ndim != x.ndim else U * H
134133
)
135-
return R.reshape(x.shape + x.shape)
136134

137135
def invert(u: Array, t: Array) -> Array:
138136
"""
@@ -163,11 +161,11 @@ def make_minimizer():
163161
)
164162
xopt = optimum.value
165163
xcov, xunc = post(xopt)
164+
xres = resolution(xopt, xcov, hx) if hx is not None else None
166165
cost = misfit(xopt)
167166
info = jnp.where(optimum.result == optimistix.RESULTS.successful, 0, 1)
168-
rslv = res(xopt, xcov) if hx is not None else None
169167

170-
return xopt, xcov, xunc, cost, info, rslv
168+
return (xopt, xcov, xunc, cost, info, xres)
171169

172170

173171
@jax.jit(static_argnums=(0,), static_argnames=("max_steps",))
@@ -266,7 +264,7 @@ def retrieve(
266264
:param max_steps: The maximum number of steps the optimizer can take.
267265
:returns: The retrieved result.
268266
"""
269-
xopt, xcov, xunc, cost, info, rslv = _batch(
267+
xopt, xcov, xunc, cost, info, xres = _batch(
270268
f.f,
271269
jnp.asarray(x),
272270
jnp.asarray(y),
@@ -285,5 +283,5 @@ def retrieve(
285283
zvar=zvar,
286284
cost=np.asarray(cost),
287285
info=np.asarray(info),
288-
resolution=np.asarray(rslv) if rslv is not None else None,
286+
xres=np.asarray(xres) if xres is not None else None,
289287
)

0 commit comments

Comments
 (0)