|
| 1 | +"""Tests for the LogitNormalVariable factory.""" |
| 2 | + |
| 3 | +import jax |
| 4 | +import jax.numpy as jnp |
| 5 | +import numpyro |
| 6 | +import numpyro.distributions as dist |
| 7 | +import pytest |
| 8 | +from numpy.testing import assert_allclose |
| 9 | +from numpyro.infer import Predictive |
| 10 | +from numpyro.infer.reparam import LocScaleReparam |
| 11 | + |
| 12 | +import pyrenew.transformation as transformation |
| 13 | +from pyrenew.randomvariable import ( |
| 14 | + LogitNormalVariable, |
| 15 | + StaticDistributionalVariable, |
| 16 | + TransformedVariable, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def test_logit_normal_variable_construction(): |
| 21 | + """The factory constructs the expected transformed Normal variable.""" |
| 22 | + median = 0.004 |
| 23 | + scale = 0.3 |
| 24 | + |
| 25 | + rv = LogitNormalVariable(name="iedr", median=median, scale=scale) |
| 26 | + |
| 27 | + assert isinstance(rv, TransformedVariable) |
| 28 | + assert isinstance(rv.base_rv, StaticDistributionalVariable) |
| 29 | + assert isinstance(rv.base_rv.distribution, dist.Normal) |
| 30 | + assert_allclose( |
| 31 | + rv.base_rv.distribution.loc, |
| 32 | + transformation.SigmoidTransform().inv(median), |
| 33 | + ) |
| 34 | + assert_allclose(rv.base_rv.distribution.scale, scale) |
| 35 | + assert len(rv.transforms) == 1 |
| 36 | + assert isinstance(rv.transforms[0], transformation.SigmoidTransform) |
| 37 | + |
| 38 | + |
| 39 | +def test_logit_normal_variable_samples_are_probabilities(): |
| 40 | + """Samples from a logit-normal variable lie strictly between zero and one.""" |
| 41 | + rv = LogitNormalVariable(name="iedr", median=0.004, scale=0.3) |
| 42 | + |
| 43 | + def model(): # numpydoc ignore=GL08 |
| 44 | + return rv.sample(record=True) |
| 45 | + |
| 46 | + samples = Predictive(model, num_samples=100)(jax.random.key(0)) |
| 47 | + values = samples["iedr"][0] |
| 48 | + |
| 49 | + assert (values > 0).all() |
| 50 | + assert (values < 1).all() |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + ("base_name", "expected_base_name"), |
| 55 | + [ |
| 56 | + (None, "logit_iedr"), |
| 57 | + ("iedr_unconstrained", "iedr_unconstrained"), |
| 58 | + ], |
| 59 | +) |
| 60 | +def test_logit_normal_variable_names(base_name, expected_base_name): |
| 61 | + """Default and explicit names are used for object and trace sites.""" |
| 62 | + rv = LogitNormalVariable( |
| 63 | + name="iedr", |
| 64 | + median=0.004, |
| 65 | + scale=0.3, |
| 66 | + base_name=base_name, |
| 67 | + ) |
| 68 | + |
| 69 | + assert rv.name == "iedr" |
| 70 | + assert rv.base_rv.name == expected_base_name |
| 71 | + |
| 72 | + with numpyro.handlers.seed(rng_seed=0), numpyro.handlers.trace() as trace: |
| 73 | + rv.sample(record=True) |
| 74 | + |
| 75 | + assert trace[expected_base_name]["type"] == "sample" |
| 76 | + assert trace["iedr"]["type"] == "deterministic" |
| 77 | + |
| 78 | + |
| 79 | +def test_logit_normal_variable_reparameterization(): |
| 80 | + """A reparameterizer is applied to the underlying Normal sample site.""" |
| 81 | + reparam = LocScaleReparam(0) |
| 82 | + rv = LogitNormalVariable( |
| 83 | + name="iedr", |
| 84 | + median=0.004, |
| 85 | + scale=0.3, |
| 86 | + reparam=reparam, |
| 87 | + ) |
| 88 | + |
| 89 | + assert rv.base_rv.reparam_dict == {"logit_iedr": reparam} |
| 90 | + |
| 91 | + with numpyro.handlers.seed(rng_seed=0), numpyro.handlers.trace() as trace: |
| 92 | + value = rv.sample() |
| 93 | + |
| 94 | + assert trace["logit_iedr_decentered"]["type"] == "sample" |
| 95 | + assert trace["logit_iedr"]["type"] == "deterministic" |
| 96 | + assert 0 < value < 1 |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.parametrize( |
| 100 | + "median", |
| 101 | + [ |
| 102 | + -0.1, |
| 103 | + 0.0, |
| 104 | + 1.0, |
| 105 | + 1.1, |
| 106 | + float("-inf"), |
| 107 | + float("inf"), |
| 108 | + float("nan"), |
| 109 | + jnp.array([0.2, 1.0]), |
| 110 | + ], |
| 111 | +) |
| 112 | +def test_logit_normal_variable_rejects_invalid_median(median): |
| 113 | + """Medians must be finite and strictly inside the unit interval.""" |
| 114 | + with pytest.raises(ValueError, match="median must contain only finite values"): |
| 115 | + LogitNormalVariable(name="invalid", median=median, scale=0.3) |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.parametrize( |
| 119 | + "scale", |
| 120 | + [ |
| 121 | + -0.1, |
| 122 | + 0.0, |
| 123 | + float("-inf"), |
| 124 | + float("inf"), |
| 125 | + float("nan"), |
| 126 | + jnp.array([0.3, 0.0]), |
| 127 | + ], |
| 128 | +) |
| 129 | +def test_logit_normal_variable_rejects_invalid_scale(scale): |
| 130 | + """Scales must be finite and strictly positive.""" |
| 131 | + with pytest.raises(ValueError, match="scale must contain only finite positive"): |
| 132 | + LogitNormalVariable(name="invalid", median=0.4, scale=scale) |
0 commit comments