Skip to content

jelli.utils.distributions

combine_distributions_numerically(constraints, n_points=1000)

Combine multiple distributions into a single numerical distribution by summing their logpdfs on a common support.

Parameters:

Name Type Description Default
constraints Dict[str, Dict[str, ndarray]]

A dictionary where keys are distribution types (e.g., NumericalDistribution, NormalDistribution, etc.) and values are dictionaries containing distribution information such as central_value, standard_deviation, etc.

required
n_points int

Number of points in the common support for the output distribution. Default is 1000.

1000

Returns:

Type Description
Dict[str, ndarray]

A dictionary containing the combined numerical distribution information, including measurement_name, observables, observable_indices, x, y, and log_y.

Examples:

>>> constraints = {
...     'NormalDistribution': {
...         'measurement_name': np.array(['measurement1']),
...         'observables': np.array(['observable1']),
...         'observable_indices': np.array([0]),
...         'central_value': np.array([1.0]),
...         'standard_deviation': np.array([0.8])
...     },
...     'HalfNormalDistribution': {
...         'measurement_name': np.array(['measurement2', 'measurement3']),
...         'observables': np.array(['observable1', 'observable1']),
...         'observable_indices': np.array([0, 0]),
...         'standard_deviation': np.array([0.3, 0.4])
...     }
... }
>>> combine_distributions(constraints, n_points=1000)
{
    'measurement_name': np.array(['measurement1, measurement2, measurement3']),
    'observables': np.array(['observable1']),
    'observable_indices': np.array([0]),
    'x': np.array([...]),  # combined support
    'y': np.array([...]),  # combined pdf values
    'log_y': np.array([...])  # combined log pdf values
}
Source code in jelli/utils/distributions.py
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
def combine_distributions_numerically(
        constraints: Dict[str, Dict[str, np.ndarray]],
        n_points: int = 1000,
) -> Dict[str, np.ndarray]:
    '''
    Combine multiple distributions into a single numerical distribution by summing their logpdfs on a common support.

    Parameters
    ----------
    constraints : Dict[str, Dict[str, np.ndarray]]
        A dictionary where keys are distribution types (e.g., `NumericalDistribution`, `NormalDistribution`, etc.)
        and values are dictionaries containing distribution information such as `central_value`, `standard_deviation`, etc.
    n_points : int, optional
        Number of points in the common support for the output distribution. Default is `1000`.

    Returns
    -------
    Dict[str, np.ndarray]
        A dictionary containing the combined numerical distribution information, including `measurement_name`, `observables`,
        `observable_indices`, `x`, `y`, and `log_y`.

    Examples
    --------
    >>> constraints = {
    ...     'NormalDistribution': {
    ...         'measurement_name': np.array(['measurement1']),
    ...         'observables': np.array(['observable1']),
    ...         'observable_indices': np.array([0]),
    ...         'central_value': np.array([1.0]),
    ...         'standard_deviation': np.array([0.8])
    ...     },
    ...     'HalfNormalDistribution': {
    ...         'measurement_name': np.array(['measurement2', 'measurement3']),
    ...         'observables': np.array(['observable1', 'observable1']),
    ...         'observable_indices': np.array([0, 0]),
    ...         'standard_deviation': np.array([0.3, 0.4])
    ...     }
    ... }
    >>> combine_distributions(constraints, n_points=1000)
    {
        'measurement_name': np.array(['measurement1, measurement2, measurement3']),
        'observables': np.array(['observable1']),
        'observable_indices': np.array([0]),
        'x': np.array([...]),  # combined support
        'y': np.array([...]),  # combined pdf values
        'log_y': np.array([...])  # combined log pdf values
    }
    '''

    # get universal parameters for output
    dist_info = next(iter(constraints.values()))
    observables_out = dist_info['observables'][:1]
    observable_indices_out = dist_info['observable_indices'][:1]

    # get measurement names in each constraint and supports of distributions
    measurement_names = []
    supports = []
    for dist_type, dist_info in constraints.items():
        supports.append(
            get_distribution_support(dist_type, dist_info)
        )
        measurement_names.append(dist_info['measurement_name'])

    # combine measurement names for output
    measurement_name_out = np.expand_dims(', '.join(np.unique(np.concatenate(measurement_names))), axis=0)

    # common support for all distributions
    support_min = np.min(np.concatenate([s[0] for s in supports]))
    support_max = np.max(np.concatenate([s[1] for s in supports]))
    xp_out = np.linspace(support_min, support_max, n_points)

    # sum the logpdfs of all distributions on the common support
    log_fp_out = np.zeros_like(xp_out)
    for dist_type, dist_info in constraints.items():
        unique_observables = np.unique(dist_info['observables'])
        if len(unique_observables) > 1 or unique_observables[0] != observables_out[0]:
            raise ValueError(f"Only distributions constraining the same observable can be combined.")
        n_constraints = len(dist_info['observables'])
        x = np.broadcast_to(xp_out, (n_constraints, n_points)).reshape(-1)
        observable_indices = np.arange(len(x))
        selector_matrix = np.concatenate([np.eye(n_points)]*n_constraints, axis=1)
        if dist_type == 'NumericalDistribution':
            xp = dist_info['x']
            log_fp = dist_info['log_y']
            xp = np.broadcast_to(xp[:, None, :], (xp.shape[0], n_points, xp.shape[1]))
            xp = xp.reshape(-1, xp.shape[2])
            log_fp = np.broadcast_to(log_fp[:, None, :], (log_fp.shape[0], n_points, log_fp.shape[1]))
            log_fp = log_fp.reshape(-1, log_fp.shape[2])
            log_fp_out += logpdf_functions_summed[dist_type](
                x,
                selector_matrix,
                observable_indices,
                xp,
                log_fp,
            )
        elif dist_type == 'NormalDistribution':
            central_value = np.broadcast_to(dist_info['central_value'], (n_points, n_constraints)).T.reshape(-1)
            standard_deviation = np.broadcast_to(dist_info['standard_deviation'], (n_points, n_constraints)).T.reshape(-1)
            log_fp_out += logpdf_functions_summed[dist_type](
                x,
                selector_matrix,
                observable_indices,
                central_value,
                standard_deviation,
            )
        elif dist_type == 'HalfNormalDistribution':
            standard_deviation = np.broadcast_to(dist_info['standard_deviation'], (n_points, n_constraints)).T.reshape(-1)
            log_fp_out += logpdf_functions_summed[dist_type](
                x,
                selector_matrix,
                observable_indices,
                standard_deviation,
            )
        elif dist_type == 'GammaDistributionPositive':
            a = np.broadcast_to(dist_info['a'], (n_points, n_constraints)).T.reshape(-1)
            loc = np.broadcast_to(dist_info['loc'], (n_points, n_constraints)).T.reshape(-1)
            scale = np.broadcast_to(dist_info['scale'], (n_points, n_constraints)).T.reshape(-1)
            log_fp_out += logpdf_functions_summed[dist_type](
                x,
                selector_matrix,
                observable_indices,
                a,
                loc,
                scale,
            )
        else:
            raise NotImplementedError(f"Combining distributions not implemented for {dist_type}.")

    # normalize the output distribution
    log_fp_out -= log_trapz_exp(log_fp_out, xp_out)

    return {
        'measurement_name': measurement_name_out,
        'observables': observables_out,
        'observable_indices': observable_indices_out,
        'x': xp_out,
        'y': np.exp(log_fp_out),
        'log_y': log_fp_out,
    }

combine_normal_distributions(measurement_name, observables, observable_indices, central_value, standard_deviation)

Combine multiple normal distributions into a single normal distribution.

Parameters:

Name Type Description Default
measurement_name ndarray

Names of the measurements.

required
observables ndarray

Names of the observables.

required
observable_indices ndarray

Indices of the observables.

required
central_value ndarray

Central values of the normal distributions.

required
standard_deviation ndarray

Standard deviations of the normal distributions.

required

Returns:

Type Description
Dict[str, ndarray]

A dictionary containing the combined measurement name, observables, observable indices, central value, and standard deviation.

Examples:

>>> combine_normal_distributions(
...     measurement_name=np.array(['measurement1', 'measurement2']),
...     observables=np.array(['observable1', 'observable1']),
...     observable_indices=np.array([3, 3]),
...     central_value=np.array([1.0, 2.0]),
...     standard_deviation=np.array([0.1, 0.2])
... )
{
    'measurement_name': np.array(['measurement1, measurement2']),
    'observables': np.array(['observable1']),
    'observable_indices': np.array([3]),
    'central_value': np.array([1.2]),
    'standard_deviation': np.array([0.08944272])
}
Source code in jelli/utils/distributions.py
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
def combine_normal_distributions(
        measurement_name: np.ndarray,
        observables: np.ndarray,
        observable_indices: np.ndarray,
        central_value: np.ndarray,
        standard_deviation: np.ndarray,
    ) -> Dict[str, np.ndarray]:
    '''
    Combine multiple normal distributions into a single normal distribution.

    Parameters
    ----------
    measurement_name : np.ndarray
        Names of the measurements.
    observables : np.ndarray
        Names of the observables.
    observable_indices : np.ndarray
        Indices of the observables.
    central_value : np.ndarray
        Central values of the normal distributions.
    standard_deviation : np.ndarray
        Standard deviations of the normal distributions.

    Returns
    -------
    Dict[str, np.ndarray]
        A dictionary containing the combined measurement name, observables, observable indices,
        central value, and standard deviation.

    Examples
    --------
    >>> combine_normal_distributions(
    ...     measurement_name=np.array(['measurement1', 'measurement2']),
    ...     observables=np.array(['observable1', 'observable1']),
    ...     observable_indices=np.array([3, 3]),
    ...     central_value=np.array([1.0, 2.0]),
    ...     standard_deviation=np.array([0.1, 0.2])
    ... )
    {
        'measurement_name': np.array(['measurement1, measurement2']),
        'observables': np.array(['observable1']),
        'observable_indices': np.array([3]),
        'central_value': np.array([1.2]),
        'standard_deviation': np.array([0.08944272])
    }
    '''

    if len(measurement_name) > 1:
        if len(np.unique(observables)) > 1:
            raise ValueError(f"Only distributions constraining the same observable can be combined.")
        measurement_name = np.expand_dims(', '.join(np.unique(measurement_name)), axis=0)
        observables = observables[:1]
        observable_indices = observable_indices[:1]
        weights = 1 / standard_deviation**2
        central_value = np.average(central_value, weights=weights, keepdims=True)
        standard_deviation = np.sqrt(1 / np.sum(weights, keepdims=True))
    return {
        'measurement_name': measurement_name,
        'observables': observables,
        'observable_indices': observable_indices,
        'central_value': central_value,
        'standard_deviation': standard_deviation,
    }

convert_GeneralGammaDistributionPositive(a, loc, scale, gaussian_standard_deviation)

Convert a GeneralGammaDistributionPositive to either a GammaDistributionPositive or a NumericalDistribution.

Parameters:

Name Type Description Default
a float

Shape parameter of the Generalized Gamma distribution.

required
loc float

Location parameter of the Generalized Gamma distribution.

required
scale float

Scale parameter of the Generalized Gamma distribution.

required
gaussian_standard_deviation float

Standard deviation of the Gaussian smearing. If zero, no smearing is applied.

required

Returns:

Type Description
tuple

A tuple containing the type of the resulting distribution ('GammaDistributionPositive' or 'NumericalDistribution') and its parameters.

Source code in jelli/utils/distributions.py
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
def convert_GeneralGammaDistributionPositive(a, loc, scale, gaussian_standard_deviation):
    '''
    Convert a `GeneralGammaDistributionPositive` to either a `GammaDistributionPositive` or a `NumericalDistribution`.

    Parameters
    ----------
    a : float
        Shape parameter of the Generalized Gamma distribution.
    loc : float
        Location parameter of the Generalized Gamma distribution.
    scale : float
        Scale parameter of the Generalized Gamma distribution.
    gaussian_standard_deviation : float
        Standard deviation of the Gaussian smearing. If zero, no smearing is applied.

    Returns
    -------
    tuple
        A tuple containing the type of the resulting distribution (`'GammaDistributionPositive'` or `'NumericalDistribution'`) and its parameters.
    '''
    loc_scaled = loc/scale
    if gaussian_standard_deviation == 0:
        distribution_type = 'GammaDistributionPositive'
        parameters = {'a': a, 'loc': loc_scaled, 'scale': 1}
    else:
        distribution_type = 'NumericalDistribution'
        gamma_unscaled = GammaDistribution(a = a, loc = loc_scaled, scale = 1)
        norm_bg = NormalDistribution(0, gaussian_standard_deviation)
        numerical = [NumericalDistribution.from_pd(p, nsteps=1000) for p in [gamma_unscaled, norm_bg]]
        num_unscaled = _convolve_numerical(numerical, central_values='sum')
        x = np.array(num_unscaled.x)
        y = np.array(num_unscaled.y_norm)
        if loc_scaled in x:
            to_mirror = y[x<=loc_scaled][::-1]
            y_pos = y[len(to_mirror)-1:len(to_mirror)*2-1]
            y[len(to_mirror)-1:len(to_mirror)*2-1] += to_mirror[:len(y_pos)]
        else:
            to_mirror = y[x<loc_scaled][::-1]
            y_pos = y[len(to_mirror):len(to_mirror)*2]
            y[len(to_mirror):len(to_mirror)*2] += to_mirror[:len(y_pos)]
        y = y[x >= 0]
        x = x[x >= 0]
        if x[0] != 0:  #  make sure the PDF at 0 exists
            x = np.insert(x, 0, 0.)  # add 0 as first element
            y = np.insert(y, 0, y[0])  # copy first element
        x = x * scale
        y = np.maximum(0, y)  # make sure PDF is positive
        y = y /  np.trapz(y, x=x)  # normalize PDF to 1
        # ignore warning from log(0)=-np.inf
        with np.errstate(divide='ignore', invalid='ignore'):
            log_y = np.log(y)
        # replace -np.inf with a large negative number
        log_y[np.isneginf(log_y)] = LOG_ZERO
        parameters = {
            'x': x,
            'y': y,
            'log_y': log_y,
        }
    return distribution_type, parameters

cov_coeff_to_cov_obs(par_monomials, cov_th_scaled)

Convert a covariance matrix in the space of parameters to a covariance matrix in the space of observables.

Parameters:

Name Type Description Default
par_monomials List[array]

List of parameter monomials for each sector.

required
cov_th_scaled List[List[array]]

Covariance matrix in the space of parameters, scaled by the SM+exp standard deviations.

required

Returns:

Type Description
array

Covariance matrix in the space of observables.

Source code in jelli/utils/distributions.py
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
def cov_coeff_to_cov_obs(par_monomials, cov_th_scaled): # TODO (maybe) optimize
    '''
    Convert a covariance matrix in the space of parameters to a covariance matrix in the space of observables.

    Parameters
    ----------
    par_monomials : List[jnp.array]
        List of parameter monomials for each sector.
    cov_th_scaled : List[List[jnp.array]]
        Covariance matrix in the space of parameters, scaled by the SM+exp standard deviations.

    Returns
    -------
    jnp.array
        Covariance matrix in the space of observables.
    '''
    n_sectors = len(par_monomials)

    cov = np.empty((n_sectors,n_sectors), dtype=object).tolist()

    for i in range(n_sectors):
        for j in range(n_sectors):
            if i>= j:
                cov[i][j] = jnp.einsum('ijkl,k,l->ij',cov_th_scaled[i][j],par_monomials[i],par_monomials[j])
            else:
                shape = cov_th_scaled[j][i].shape
                cov[i][j] = jnp.zeros((shape[1], shape[0]))
    cov_matrix_tril = jnp.tril(jnp.block(cov))
    return cov_matrix_tril + cov_matrix_tril.T - jnp.diag(jnp.diag(cov_matrix_tril))

get_distribution_samples(dist_type, dist_info, n_samples, seed=None)

Generate samples from a specified distribution type using the provided distribution information.

Parameters:

Name Type Description Default
dist_type str

Type of the distribution (e.g., NumericalDistribution, NormalDistribution, etc.).

required
dist_info Dict[str, ndarray]

Information about the distribution, such as central_value, standard_deviation, etc.

required
n_samples int

Number of samples to generate.

required
seed int

Random seed for reproducibility. Default is None.

None

Returns:

Type Description
List[ndarray] or ndarray

A list of arrays in case of MultivariateNormalDistribution, where the length of the list is the number of constraints, and each array is of shape (n_observables, n_samples). For other distributions, returns a single array of shape (n_constraints, n_samples).

Examples:

>>> dist_info = {
...     'central_value': np.array([0.0, 1.0]),
...     'standard_deviation': np.array([1.0, 2.0])
... }
>>> get_distribution_samples('NormalDistribution', dist_info, n_samples=1000)
array([[ 0.12345678,  1.23456789, ...
       [-0.98765432,  2.34567890, ...]])
Source code in jelli/utils/distributions.py
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
def get_distribution_samples(
        dist_type: str,
        dist_info: Dict[str, np.ndarray],
        n_samples: int,
        seed: Optional[int] = None,
) -> Union[List[np.ndarray], np.ndarray]:
    """
    Generate samples from a specified distribution type using the provided distribution information.

    Parameters
    ----------
    dist_type : str
        Type of the distribution (e.g., `NumericalDistribution`, `NormalDistribution`, etc.).
    dist_info : Dict[str, np.ndarray]
        Information about the distribution, such as `central_value`, `standard_deviation`, etc.
    n_samples : int
        Number of samples to generate.
    seed : int, optional
        Random seed for reproducibility. Default is `None`.

    Returns
    -------
    List[np.ndarray] or np.ndarray
        A list of arrays in case of `MultivariateNormalDistribution`, where the length of the list is the number of constraints,
        and each array is of shape `(n_observables, n_samples)`.
        For other distributions, returns a single array of shape `(n_constraints, n_samples)`.

    Examples
    --------
    >>> dist_info = {
    ...     'central_value': np.array([0.0, 1.0]),
    ...     'standard_deviation': np.array([1.0, 2.0])
    ... }
    >>> get_distribution_samples('NormalDistribution', dist_info, n_samples=1000)
    array([[ 0.12345678,  1.23456789, ...
           [-0.98765432,  2.34567890, ...]])
    """
    if seed is not None:
        np.random.seed(seed)
    if dist_type == 'GammaDistributionPositive':
        a = dist_info['a']
        loc = dist_info['loc']
        scale = dist_info['scale']
        n_constraints = len(a)
        ppf = get_ppf_gamma_distribution_positive(a, loc, scale)
        return get_inverse_transform_samples(ppf, n_samples, n_constraints)
    elif dist_type == 'NumericalDistribution':
        xp = dist_info['x']
        fp = dist_info['y']
        ppf = get_ppf_numerical_distribution(xp, fp)
        n_constraints = len(xp)
        return get_inverse_transform_samples(ppf, n_samples, n_constraints)
    elif dist_type == 'NormalDistribution':
        central_value = dist_info['central_value']
        standard_deviation = dist_info['standard_deviation']
        n_constraints = len(central_value)
        return np.random.normal(central_value, standard_deviation, size=(n_samples, n_constraints)).T
    elif dist_type == 'MultivariateNormalDistribution':
        central_value = dist_info['central_value']
        standard_deviation = dist_info['standard_deviation']
        inverse_correlation = dist_info['inverse_correlation']
        samples = []
        n_constraints = len(central_value)
        for i in range(n_constraints):
            correlation = np.linalg.inv(inverse_correlation[i])  # TODO: think about saving the correlation matrix also in the constraint dict
            samples.append(
                np.random.multivariate_normal(
                    central_value[i],
                    correlation * np.outer(standard_deviation[i], standard_deviation[i]), n_samples).T
            )
        return samples
    elif dist_type == 'HalfNormalDistribution':
        standard_deviation = dist_info['standard_deviation']
        n_constraints = len(standard_deviation)
        return np.abs(np.random.normal(0, standard_deviation, size=(n_samples, n_constraints)).T)
    else:
        raise ValueError(f"Sampling not implemented for distribution type: {dist_type}")

get_distribution_support(dist_type, dist_info)

Get the support of one or more distributions based on the distribution parameters.

Parameters:

Name Type Description Default
dist_type str

Type of the distribution (e.g., NumericalDistribution, NormalDistribution, etc.).

required
dist_info Dict[str, ndarray]

Information about the distribution, such as central_value, standard_deviation, etc.

required

Returns:

Type Description
Tuple[ndarray, ndarray]

A tuple containing the minimum and maximum values of the support of the distributions.

Examples:

>>> get_distribution_support('NormalDistribution', {'central_value': np.array([0.0, 1.0]), 'standard_deviation': np.array([1.0, 2.0])})
(array([-6., -11.]), array([6., 13.]))
Source code in jelli/utils/distributions.py
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
def get_distribution_support(
        dist_type: str,
        dist_info: Dict[str, np.ndarray]
    ) -> Tuple[np.ndarray, np.ndarray]:
    '''
    Get the support of one or more distributions based on the distribution parameters.

    Parameters
    ----------
    dist_type : str
        Type of the distribution (e.g., `NumericalDistribution`, `NormalDistribution`, etc.).
    dist_info : Dict[str, np.ndarray]
        Information about the distribution, such as `central_value`, `standard_deviation`, etc.

    Returns
    -------
    Tuple[np.ndarray, np.ndarray]
        A tuple containing the minimum and maximum values of the support of the distributions.

    Examples
    --------
    >>> get_distribution_support('NormalDistribution', {'central_value': np.array([0.0, 1.0]), 'standard_deviation': np.array([1.0, 2.0])})
    (array([-6., -11.]), array([6., 13.]))

    '''

    if dist_type == 'NumericalDistribution':
        xp = dist_info['x']
        return np.min(xp, axis=1), np.max(xp, axis=1)
    elif dist_type == 'NormalDistribution':
        central_value = dist_info['central_value']
        standard_deviation = dist_info['standard_deviation']
        return central_value - 6*standard_deviation, central_value + 6*standard_deviation
    elif dist_type == 'HalfNormalDistribution':
        standard_deviation = dist_info['standard_deviation']
        return np.zeros_like(standard_deviation), 6*standard_deviation
    elif dist_type == 'GammaDistributionPositive':
        a = dist_info['a']
        loc = dist_info['loc']
        scale = dist_info['scale']
        mode = np.maximum(loc + (a-1)*scale, 0)
        gamma = sp.stats.gamma(a, loc, scale)
        support_min = np.maximum(np.minimum(gamma.ppf(1e-9), mode), 0)
        support_max = gamma.ppf(1-1e-9*(1-gamma.cdf(0)))
        return support_min, support_max
    else:
        raise NotImplementedError(f"Computing the support not implemented for {dist_type}.")

get_inverse_transform_samples(ppf, n_samples, n_constraints)

Generate samples from a distribution using inverse transform sampling.

Parameters:

Name Type Description Default
ppf Callable

The percent-point function (PPF) of the distribution.

required
n_samples int

The number of samples to generate.

required
n_constraints int

The number of constraints.

required

Returns:

Type Description
ndarray

An array of samples drawn from the distribution defined by the PPF.

Source code in jelli/utils/distributions.py
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
def get_inverse_transform_samples(
        ppf: Callable,
        n_samples: int,
        n_constraints: int
    ) -> np.ndarray:
    """
    Generate samples from a distribution using inverse transform sampling.

    Parameters
    ----------
    ppf : Callable
        The percent-point function (PPF) of the distribution.
    n_samples : int
        The number of samples to generate.
    n_constraints : int
        The number of constraints.

    Returns
    -------
    np.ndarray
        An array of samples drawn from the distribution defined by the PPF.
    """
    return ppf(np.random.uniform(0, 1, (n_samples, n_constraints))).T

get_mode_and_uncertainty(dist_type, dist_info)

Get the mode and uncertainty of one or more distributions based on the distribution parameters.

A Gaussian approximation or an upper limit based on the 95% confidence level is used, depending on the distribution type and parameters.

In case of the upper limit, the mode is set to nan.

Parameters:

Name Type Description Default
dist_type str

Type of the distribution (e.g., NumericalDistribution, NormalDistribution, etc.).

required
dist_info Dict[str, ndarray]

Information about the distribution, such as central_value, standard_deviation, etc.

required

Returns:

Type Description
Tuple[ndarray, ndarray]

A tuple containing the mode and uncertainty of the distributions.

Examples:

>>> get_mode_and_uncertainty('NormalDistribution', {'central_value': np.array([0.0, 1.0]), 'standard_deviation': np.array([1.0, 2.0])})
(array([0., 1.]), array([1., 2.]))
>>> get_mode_and_uncertainty('HalfNormalDistribution', {'standard_deviation': np.array([0.3, 0.4])})
(array([nan, nan]), array([0.588, 0.784]))
>>> get_mode_and_uncertainty('GammaDistributionPositive', {'a': np.array([2.0, 4.0]), 'loc': np.array([-1.0, 0.0]), 'scale': np.array([1.0, 2.0])})
(array([nan,  6.]), array([4.11300328, 3.46410162]))
>>> central_value = np.array([[0.0], [6.4]])
>>> standard_deviation = np.array([[1.0], [1.2]])
>>> xp = np.broadcast_to(np.linspace(0, 10, 10000), (2, 10000))
>>> fp = sp.stats.norm.pdf(xp, loc=central_value, scale=standard_deviation)
>>> get_mode_and_uncertainty('NumericalDistribution', {'x': xp, 'y': fp, 'log_y': np.log(fp)})
(array([nan, 6.4]), array([1.96, 1.2]))
Source code in jelli/utils/distributions.py
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
def get_mode_and_uncertainty(
        dist_type: str,
        dist_info: Dict[str, np.ndarray],
) -> Tuple[np.ndarray, np.ndarray]:
    '''
    Get the mode and uncertainty of one or more distributions based on the distribution parameters.

    A Gaussian approximation or an upper limit based on the 95% confidence level is used, depending on the distribution type and parameters.

    In case of the upper limit, the mode is set to `nan`.

    Parameters
    ----------
    dist_type : str
        Type of the distribution (e.g., `NumericalDistribution`, `NormalDistribution`, etc.).
    dist_info : Dict[str, np.ndarray]
        Information about the distribution, such as `central_value`, `standard_deviation`, etc.

    Returns
    -------
    Tuple[np.ndarray, np.ndarray]
        A tuple containing the mode and uncertainty of the distributions.

    Examples
    --------
    >>> get_mode_and_uncertainty('NormalDistribution', {'central_value': np.array([0.0, 1.0]), 'standard_deviation': np.array([1.0, 2.0])})
    (array([0., 1.]), array([1., 2.]))
    >>> get_mode_and_uncertainty('HalfNormalDistribution', {'standard_deviation': np.array([0.3, 0.4])})
    (array([nan, nan]), array([0.588, 0.784]))
    >>> get_mode_and_uncertainty('GammaDistributionPositive', {'a': np.array([2.0, 4.0]), 'loc': np.array([-1.0, 0.0]), 'scale': np.array([1.0, 2.0])})
    (array([nan,  6.]), array([4.11300328, 3.46410162]))
    >>> central_value = np.array([[0.0], [6.4]])
    >>> standard_deviation = np.array([[1.0], [1.2]])
    >>> xp = np.broadcast_to(np.linspace(0, 10, 10000), (2, 10000))
    >>> fp = sp.stats.norm.pdf(xp, loc=central_value, scale=standard_deviation)
    >>> get_mode_and_uncertainty('NumericalDistribution', {'x': xp, 'y': fp, 'log_y': np.log(fp)})
    (array([nan, 6.4]), array([1.96, 1.2]))
    '''
    if dist_type == 'NormalDistribution':
        mode = dist_info['central_value']
        uncertainty = dist_info['standard_deviation']
        return mode, uncertainty
    elif dist_type == 'HalfNormalDistribution':
        uncertainty = dist_info['standard_deviation']*1.96  # 95% CL
        return np.full_like(uncertainty, np.nan), uncertainty
    elif dist_type == 'GammaDistributionPositive':
        a = dist_info['a']
        loc = dist_info['loc']
        scale = dist_info['scale']
        mode = np.maximum(loc + (a-1)*scale, 0)

        # if mode is negative, use the 95% CL upper limit, otherwise use the standard deviation at the mode
        upper_limit = mode <= 0
        gaussian = ~upper_limit
        uncertainty = np.empty_like(mode, dtype=float)
        uncertainty[gaussian] = np.sqrt((loc[gaussian]-mode[gaussian])**2 / (a[gaussian]-1))  # standard deviation at the mode, defined as sqrt(-1/(d^2/dx^2 log(gamma(x, a, loc, scale))))
        ppf = get_ppf_gamma_distribution_positive(a[upper_limit], loc[upper_limit], scale[upper_limit])
        uncertainty[upper_limit] = ppf(0.95)  # 95% CL upper limit using the ppf of the gamma distribution restricted to positive values
        mode[upper_limit] = np.nan  # set the modes to nan where they are not defined

        # check if mode/uncertainty is smaller than 1.7 and mode > 0, in this case compute 95% CL upper limit
        # 1.7 is selected as threshold where the gaussian and halfnormal approximation are approximately equally good based on the KL divergence
        upper_limit = (mode/uncertainty < 1.7) & (mode > 0)
        ppf = get_ppf_gamma_distribution_positive(a[upper_limit], loc[upper_limit], scale[upper_limit])
        uncertainty[upper_limit] = ppf(0.95)  # 95% CL upper limit using the ppf of the gamma distribution restricted to positive values
        mode[upper_limit] = np.nan
        return mode, uncertainty
    elif dist_type == 'NumericalDistribution':
        xp = dist_info['x']
        log_fp = dist_info['log_y']
        fp = dist_info['y']
        n_constraints = len(log_fp)
        mode = np.empty(n_constraints, dtype=float)
        uncertainty = np.empty(n_constraints, dtype=float)
        for i in range(n_constraints):
            log_fp_i = log_fp[i]
            fp_i = fp[i]
            xp_i = xp[i]
            fit_points = log_fp_i > np.max(log_fp_i) - 0.5  # points of logpdf within 0.5 of the maximum
            a, b, _ = np.polyfit(xp_i[fit_points], log_fp_i[fit_points], 2)  # fit a quadratic polynomial to the logpdf
            mode_i = -b / (2 * a)
            uncertainty_i = np.sqrt(-1 / (2 * a))
            if np.abs(mode_i/uncertainty_i) > 1.7:  # if mode/uncertainty is larger than 1.7, use gaussian approximation
                mode[i] = mode_i
                uncertainty[i] = uncertainty_i
            else:  # compute 95% CL upper limit using ppf of the numerical distribution
                ppf = get_ppf_numerical_distribution(xp_i, fp_i)
                mode[i] = np.nan
                uncertainty[i] = ppf(0.95)
        return mode, uncertainty

get_ppf_gamma_distribution_positive(a, loc, scale)

Get the percent-point function (PPF) for a gamma distribution restricted to positive values.

Parameters:

Name Type Description Default
a ndarray

Shape parameter of the gamma distribution.

required
loc ndarray

Location parameter of the gamma distribution.

required
scale ndarray

Scale parameter of the gamma distribution.

required

Returns:

Type Description
Callable

The PPF that can be used to compute the quantiles for given probabilities.

Source code in jelli/utils/distributions.py
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
def get_ppf_gamma_distribution_positive(
        a: np.ndarray,
        loc: np.ndarray,
        scale: np.ndarray,
) -> Callable:
    """
    Get the percent-point function (PPF) for a gamma distribution restricted to positive values.

    Parameters
    ----------
    a : np.ndarray
        Shape parameter of the gamma distribution.
    loc : np.ndarray
        Location parameter of the gamma distribution.
    scale : np.ndarray
        Scale parameter of the gamma distribution.

    Returns
    -------
    Callable
        The PPF that can be used to compute the quantiles for given probabilities.
    """
    gamma = sp.stats.gamma(a, loc, scale)
    def ppf(q):
        return gamma.ppf(q + (1-q)*gamma.cdf(0))
    return ppf

get_ppf_numerical_distribution(xp, fp)

Get the percent-point function (PPF) for one or more numerical distributions.

Parameters:

Name Type Description Default
xp ndarray

Points at which the PDF is defined.

required
fp ndarray

PDF values at the points xp.

required

Returns:

Type Description
Callable

The PPF that can be used to compute the quantiles for given probabilities.

Source code in jelli/utils/distributions.py
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
def get_ppf_numerical_distribution(
        xp: np.ndarray,
        fp: np.ndarray,
) -> Callable:
    '''
    Get the percent-point function (PPF) for one or more numerical distributions.

    Parameters
    ----------
    xp : np.ndarray
        Points at which the PDF is defined.
    fp : np.ndarray
        PDF values at the points `xp`.

    Returns
    -------
    Callable
        The PPF that can be used to compute the quantiles for given probabilities.
    '''
    if xp.ndim == 1: # single distribution
        cdf = np.concatenate([[0], np.cumsum((fp[1:] + fp[:-1]) * 0.5 * np.diff(xp))])
        cdf /= cdf[-1]
        return partial(np.interp, xp=cdf, fp=xp)
    elif xp.ndim == 2: # multiple distributions
        dx = np.diff(xp, axis=1)
        avg_fp = 0.5 * (fp[:, 1:] + fp[:, :-1])
        cdf = np.cumsum(avg_fp * dx, axis=1)
        cdf = np.concatenate([np.zeros((cdf.shape[0], 1)), cdf], axis=1)
        cdf /= cdf[:, [-1]]

        def batched_ppf(q: Union[float, np.ndarray]) -> np.ndarray:
            """
            Batched PPF for multiple distributions.

            Parameters
            ----------
            q : float or np.ndarray
                Lower-tail probabilities at which to compute the PPF.

                  - If scalar, computes PPF for that probability across all distributions.

                  - If 1D array of shape (k,), computes PPF at k probabilities for all distributions.

                  - If 2D array of shape (k, m), computes PPF at k probabilities for each of the m distributions.

            Returns
            -------
            np.ndarray
                The quantiles corresponding to the input probabilities.

                  - If input is scalar, returns 1D array of shape (m,)

                  - Otherwise returns array of shape (k, m)
            """

            q = np.asarray(q)
            scalar_input = False
            if q.ndim == 0:  # single probability for all distributions
                q = np.full((1, cdf.shape[0]), q)
                scalar_input = True
            elif q.ndim == 1:  # a vector of probabilities for all distributions
                q = np.tile(q[None, :], (1, cdf.shape[0]))
            result = np.empty_like(q)
            for i in range(q.shape[1]):  # iterate over distributions
                result[:, i] = np.interp(q[:, i], cdf[i], xp[i])
            return result[0] if scalar_input else result
        return batched_ppf

logL_correlated_sectors(predictions_scaled, observable_indices, exp_central_scaled, cov_matrix_exp_scaled, cov_matrix_th_scaled)

Compute the log likelihood values for observables with correlated theoretical and experimental uncertainties.

Parameters:

Name Type Description Default
predictions_scaled array

The predicted values, scaled by the SM+exp standard deviations.

required
observable_indices List[array]

The indices of the constrained observables.

required
exp_central_scaled array

The experimental central values, scaled by the SM+exp standard deviations.

required
cov_matrix_exp_scaled array

The experimental covariance matrix, scaled by the SM+exp standard deviations.

required
cov_matrix_th_scaled array

The theoretical covariance matrix in the space of parameters, scaled by the SM+exp standard deviations.

required

Returns:

Type Description
array

The log likelihood values.

Source code in jelli/utils/distributions.py
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
def logL_correlated_sectors(
    predictions_scaled: jnp.array,
    observable_indices: List[jnp.array],
    exp_central_scaled: jnp.array,
    cov_matrix_exp_scaled: jnp.array,
    cov_matrix_th_scaled: jnp.array,
) -> jnp.array:
    '''
    Compute the log likelihood values for observables with correlated theoretical and experimental uncertainties.

    Parameters
    ----------
    predictions_scaled : jnp.array
        The predicted values, scaled by the SM+exp standard deviations.
    observable_indices : List[jnp.array]
        The indices of the constrained observables.
    exp_central_scaled : jnp.array
        The experimental central values, scaled by the SM+exp standard deviations.
    cov_matrix_exp_scaled : jnp.array
        The experimental covariance matrix, scaled by the SM+exp standard deviations.
    cov_matrix_th_scaled : jnp.array
        The theoretical covariance matrix in the space of parameters, scaled by the SM+exp standard deviations.

    Returns
    -------
    jnp.array
        The log likelihood values.
    '''

    cov_scaled = cov_matrix_th_scaled + cov_matrix_exp_scaled
    std_scaled = jnp.sqrt(jnp.diag(cov_scaled))
    C = cov_scaled / jnp.outer(std_scaled, std_scaled)
    D = (predictions_scaled - exp_central_scaled)/std_scaled

    logL_rows = []
    for i in range(len(observable_indices)):
        logL_total = jnp.zeros_like(predictions_scaled)
        d = jnp.take(D, observable_indices[i])
        c = jnp.take(jnp.take(C, observable_indices[i], axis=0), observable_indices[i], axis=1)
        logL = -0.5 * d * jsp.linalg.cho_solve(jsp.linalg.cho_factor(c), d)
        logL_total = logL_total.at[observable_indices[i]].add(logL)
        logL_rows.append(logL_total)
    return jnp.array(logL_rows)

logL_correlated_sectors_summed(predictions_scaled, selector_matrix, observable_indices, exp_central_scaled, cov_matrix_exp_scaled, cov_matrix_th_scaled)

Compute the summed log likelihood values for observables with correlated theoretical and experimental uncertainties.

Parameters:

Name Type Description Default
predictions_scaled array

The predicted values, scaled by the SM+exp standard deviations.

required
selector_matrix array

The selector matrix to sum the log likelihood values of different unique multivariate normal distributions. Of shape (n_likelihoods, n_distributions).

required
observable_indices List[array]

The indices of the constrained observables.

required
exp_central_scaled array

The experimental central values, scaled by the SM+exp standard deviations.

required
cov_matrix_exp_scaled array

The experimental covariance matrix, scaled by the SM+exp standard deviations.

required
cov_matrix_th_scaled array

The theoretical covariance matrix in the space of parameters, scaled by the SM+exp standard deviations

required

Returns:

Type Description
array

The summed log likelihood values.

Source code in jelli/utils/distributions.py
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
def logL_correlated_sectors_summed(
    predictions_scaled: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: List[jnp.array],
    exp_central_scaled: jnp.array,
    cov_matrix_exp_scaled: jnp.array,
    cov_matrix_th_scaled: jnp.array,
) -> jnp.array:
    '''
    Compute the summed log likelihood values for observables with correlated theoretical and experimental uncertainties.

    Parameters
    ----------
    predictions_scaled : jnp.array
        The predicted values, scaled by the SM+exp standard deviations.
    selector_matrix : jnp.array
        The selector matrix to sum the log likelihood values of different unique multivariate normal distributions. Of shape (n_likelihoods, n_distributions).
    observable_indices : List[jnp.array]
        The indices of the constrained observables.
    exp_central_scaled : jnp.array
        The experimental central values, scaled by the SM+exp standard deviations.
    cov_matrix_exp_scaled : jnp.array
        The experimental covariance matrix, scaled by the SM+exp standard deviations.
    cov_matrix_th_scaled : jnp.array
        The theoretical covariance matrix in the space of parameters, scaled by the SM+exp standard deviations

    Returns
    -------
    jnp.array
        The summed log likelihood values.
    '''
    cov_scaled = cov_matrix_th_scaled + cov_matrix_exp_scaled
    std_scaled = jnp.sqrt(jnp.diag(cov_scaled))
    C = cov_scaled / jnp.outer(std_scaled, std_scaled)
    D = (predictions_scaled - exp_central_scaled)/std_scaled

    logL_rows = []
    for i in range(len(observable_indices)):
        logL_total = jnp.zeros_like(predictions_scaled)
        d = jnp.take(D, observable_indices[i])
        c = jnp.take(jnp.take(C, observable_indices[i], axis=0), observable_indices[i], axis=1)
        logL = -0.5 * jnp.dot(d, jsp.linalg.cho_solve(jsp.linalg.cho_factor(c), d))
        logL_rows.append(logL)
    logL_total = jnp.array(logL_rows)
    return selector_matrix @ logL_total

logL_gamma_distribution_positive(predictions, observable_indices, a, loc, scale)

Compute the log likelihood values of positive gamma distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
observable_indices array

The indices of the constrained observables.

required
a array

The shape parameters for the gamma distributions.

required
loc array

The location parameters for the gamma distributions.

required
scale array

The scale parameters for the gamma distributions.

required

Returns:

Type Description
array

The log likelihood values.

Source code in jelli/utils/distributions.py
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
def logL_gamma_distribution_positive(
    predictions: jnp.array,
    observable_indices: jnp.array,
    a: jnp.array,
    loc: jnp.array,
    scale: jnp.array,
) -> jnp.array:
    '''
    Compute the log likelihood values of positive gamma distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    observable_indices : jnp.array
        The indices of the constrained observables.
    a : jnp.array
        The shape parameters for the gamma distributions.
    loc : jnp.array
        The location parameters for the gamma distributions.
    scale : jnp.array
        The scale parameters for the gamma distributions.

    Returns
    -------
    jnp.array
        The log likelihood values.
    '''
    logL_total = jnp.zeros_like(predictions)
    predictions = jnp.take(predictions, observable_indices)
    mode = jnp.maximum(loc + (a-1)*scale, 0)
    logL_pred = (a-1)*jnp.log((predictions-loc)/scale) - (predictions-loc)/scale
    logL_mode = (a-1)*jnp.log((mode-loc)/scale) - (mode-loc)/scale
    logL = jnp.where(predictions>=0, logL_pred-logL_mode, LOG_ZERO)
    logL_total = logL_total.at[observable_indices].add(logL)
    return logL_total

logL_gamma_distribution_positive_summed(predictions, selector_matrix, observable_indices, a, loc, scale)

Compute the log likelihood values of positive gamma distributions for given predictions and sum them using a selector matrix.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
selector_matrix array

The selector matrix to apply to the log likelihood values. Of shape (n_likelihoods, n_observables).

required
observable_indices array

The indices of the constrained observables.

required
a array

The shape parameters for the gamma distributions.

required
loc array

The location parameters for the gamma distributions.

required
scale array

The scale parameters for the gamma distributions.

required

Returns:

Type Description
array

The summed log likelihood values.

Source code in jelli/utils/distributions.py
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
def logL_gamma_distribution_positive_summed(
    predictions: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: jnp.array,
    a: jnp.array,
    loc: jnp.array,
    scale: jnp.array,
) -> jnp.array:
    '''
    Compute the log likelihood values of positive gamma distributions for given predictions and sum them using a selector matrix.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    selector_matrix : jnp.array
        The selector matrix to apply to the log likelihood values. Of shape (n_likelihoods, n_observables).
    observable_indices : jnp.array
        The indices of the constrained observables.
    a : jnp.array
        The shape parameters for the gamma distributions.
    loc : jnp.array
        The location parameters for the gamma distributions.
    scale : jnp.array
        The scale parameters for the gamma distributions.

    Returns
    -------
    jnp.array
        The summed log likelihood values.
    '''
    return selector_matrix @ logL_gamma_distribution_positive(predictions, observable_indices, a, loc, scale)

logL_half_normal_distribution(predictions, observable_indices, std)

Compute the log likelihood values of half normal distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
observable_indices array

The indices of the constrained observables.

required
std array

The standard deviation values for the half normal distributions.

required

Returns:

Type Description
array

The log likelihood values.

Source code in jelli/utils/distributions.py
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
def logL_half_normal_distribution(
    predictions: jnp.array,
    observable_indices: jnp.array,
    std: jnp.array,
) -> jnp.array:
    '''
    Compute the log likelihood values of half normal distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    observable_indices : jnp.array
        The indices of the constrained observables.
    std : jnp.array
        The standard deviation values for the half normal distributions.

    Returns
    -------
    jnp.array
        The log likelihood values.
    '''
    logL_total = jnp.zeros_like(predictions)
    predictions = jnp.take(predictions, observable_indices)
    logL = -0.5 * (predictions/std)**2
    logL = jnp.where(predictions>=0, logL, LOG_ZERO)
    logL_total = logL_total.at[observable_indices].add(logL)
    return logL_total

logL_half_normal_distribution_summed(predictions, selector_matrix, observable_indices, std)

Compute the log likelihood values of half normal distributions for given predictions and sum them using a selector matrix.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
selector_matrix array

The selector matrix to apply to the log likelihood values. Of shape (n_likelihoods, n_observables).

required
observable_indices array

The indices of the constrained observables.

required
std array

The standard deviation values for the half normal distributions.

required

Returns:

Type Description
array

The summed log likelihood values.

Source code in jelli/utils/distributions.py
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
def logL_half_normal_distribution_summed(
    predictions: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: jnp.array,
    std: jnp.array,
) -> jnp.array:
    '''
    Compute the log likelihood values of half normal distributions for given predictions and sum them using a selector matrix.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    selector_matrix : jnp.array
        The selector matrix to apply to the log likelihood values. Of shape (n_likelihoods, n_observables).
    observable_indices : jnp.array
        The indices of the constrained observables.
    std : jnp.array
        The standard deviation values for the half normal distributions.

    Returns
    -------
    jnp.array
        The summed log likelihood values.
    '''
    return selector_matrix @ logL_half_normal_distribution(predictions, observable_indices, std)

logL_multivariate_normal_distribution(predictions, observable_indices, mean, standard_deviation, inverse_correlation)

Compute the log likelihood values of multivariate normal distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
observable_indices List[array]

The indices of the constrained observables.

required
mean List[array]

The mean values of the multivariate normal distributions.

required
standard_deviation List[array]

The standard deviations of the multivariate normal distributions.

required
inverse_correlation List[array]

The inverse correlation matrices of the multivariate normal distributions.

required

Returns:

Type Description
array

The log likelihood values.

Source code in jelli/utils/distributions.py
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
def logL_multivariate_normal_distribution(
    predictions: jnp.array,
    observable_indices: List[jnp.array],
    mean: List[jnp.array],
    standard_deviation: List[jnp.array],
    inverse_correlation: List[jnp.array],
) -> jnp.array:
    '''
    Compute the log likelihood values of multivariate normal distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    observable_indices : List[jnp.array]
        The indices of the constrained observables.
    mean : List[jnp.array]
        The mean values of the multivariate normal distributions.
    standard_deviation : List[jnp.array]
        The standard deviations of the multivariate normal distributions.
    inverse_correlation : List[jnp.array]
        The inverse correlation matrices of the multivariate normal distributions.

    Returns
    -------
    jnp.array
        The log likelihood values.
    '''
    logLs = []
    for i in range(len(observable_indices)):
        logL_total = jnp.zeros_like(predictions)
        d = (jnp.take(predictions, observable_indices[i]) - mean[i]) / standard_deviation[i]
        logL = -0.5 * d * jnp.dot(inverse_correlation[i], d)
        logL_total = logL_total.at[observable_indices[i]].add(logL)
        logLs.append(logL_total)
    return jnp.stack(logLs)

logL_multivariate_normal_distribution_summed(predictions, selector_matrix, observable_indices, mean, standard_deviation, inverse_correlation)

Compute the summed log likelihood values of multivariate normal distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
selector_matrix array

The selector matrix to sum the log likelihood values of different multivariate normal distributions. Of shape (n_likelihoods, n_distributions).

required
observable_indices List[array]

The indices of the constrained observables.

required
mean List[array]

The mean values of the multivariate normal distributions.

required
standard_deviation List[array]

The standard deviations of the multivariate normal distributions.

required
inverse_correlation List[array]

The inverse correlation matrices of the multivariate normal distributions.

required

Returns:

Type Description
array

The summed log likelihood values.

Source code in jelli/utils/distributions.py
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
def logL_multivariate_normal_distribution_summed(
    predictions: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: List[jnp.array],
    mean: List[jnp.array],
    standard_deviation: List[jnp.array],
    inverse_correlation: List[jnp.array],
) -> jnp.array:
    '''
    Compute the summed log likelihood values of multivariate normal distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    selector_matrix : jnp.array
        The selector matrix to sum the log likelihood values of different multivariate normal distributions. Of shape (n_likelihoods, n_distributions).
    observable_indices : List[jnp.array]
        The indices of the constrained observables.
    mean : List[jnp.array]
        The mean values of the multivariate normal distributions.
    standard_deviation : List[jnp.array]
        The standard deviations of the multivariate normal distributions.
    inverse_correlation : List[jnp.array]
        The inverse correlation matrices of the multivariate normal distributions.

    Returns
    -------
    jnp.array
        The summed log likelihood values.
    '''
    logL_rows = []
    for i in range(len(observable_indices)):
        d = (jnp.take(predictions, observable_indices[i]) - mean[i]) / standard_deviation[i]
        logL = -0.5 * jnp.dot(d, jnp.dot(inverse_correlation[i], d))
        logL_rows.append(logL)
    logL_total = jnp.stack(logL_rows)
    return selector_matrix @ logL_total

logL_normal_distribution(predictions, observable_indices, mean, std)

Compute the log likelihood values of normal distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
observable_indices array

The indices of the constrained observables.

required
mean array

The mean values for the normal distributions.

required
std array

The standard deviation values for the normal distributions.

required

Returns:

Type Description
array

The log likelihood values.

Source code in jelli/utils/distributions.py
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
def logL_normal_distribution(
    predictions: jnp.array,
    observable_indices: jnp.array,
    mean: jnp.array,
    std: jnp.array,
) -> jnp.array:
    '''
    Compute the log likelihood values of normal distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    observable_indices : jnp.array
        The indices of the constrained observables.
    mean : jnp.array
        The mean values for the normal distributions.
    std : jnp.array
        The standard deviation values for the normal distributions.

    Returns
    -------
    jnp.array
        The log likelihood values.
    '''
    logL_total = jnp.zeros_like(predictions)
    predictions = jnp.take(predictions, observable_indices)
    logL = -0.5 * ((predictions-mean)/std)**2
    logL_total = logL_total.at[observable_indices].add(logL)
    return logL_total

logL_normal_distribution_summed(predictions, selector_matrix, observable_indices, mean, std)

Compute the log likelihood values of normal distributions for given predictions and sum them using a selector matrix.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
selector_matrix array

The selector matrix to apply to the log likelihood values. Of shape (n_likelihoods, n_observables).

required
observable_indices array

The indices of the constrained observables.

required
mean array

The mean values for the normal distributions.

required
std array

The standard deviation values for the normal distributions.

required

Returns:

Type Description
array

The summed log likelihood values.

Source code in jelli/utils/distributions.py
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
def logL_normal_distribution_summed(
    predictions: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: jnp.array,
    mean: jnp.array,
    std: jnp.array,
) -> jnp.array:
    '''
    Compute the log likelihood values of normal distributions for given predictions and sum them using a selector matrix.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    selector_matrix : jnp.array
        The selector matrix to apply to the log likelihood values. Of shape (n_likelihoods, n_observables).
    observable_indices : jnp.array
        The indices of the constrained observables.
    mean : jnp.array
        The mean values for the normal distributions.
    std : jnp.array
        The standard deviation values for the normal distributions.

    Returns
    -------
    jnp.array
        The summed log likelihood values.
    '''
    return selector_matrix @ logL_normal_distribution(predictions, observable_indices, mean, std)

logL_numerical_distribution(predictions, observable_indices, x, log_y)

Compute the log likelihood values of numerical distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
observable_indices array

The indices of the constrained observables.

required
x array

The x values for the numerical distributions.

required
log_y array

The log y values for the numerical distributions.

required

Returns:

Type Description
array

The log likelihood values.

Source code in jelli/utils/distributions.py
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
def logL_numerical_distribution(
    predictions: jnp.array,
    observable_indices: jnp.array,
    x: jnp.array,
    log_y: jnp.array,
) -> jnp.array:
    '''
    Compute the log likelihood values of numerical distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    observable_indices : jnp.array
        The indices of the constrained observables.
    x : jnp.array
        The x values for the numerical distributions.
    log_y : jnp.array
        The log y values for the numerical distributions.

    Returns
    -------
    jnp.array
        The log likelihood values.
    '''
    logL_total = jnp.zeros_like(predictions)
    predictions = jnp.take(predictions, observable_indices)
    logL = vmap(interp_log_pdf)(predictions, x, log_y - jnp.max(log_y, axis=1, keepdims=True))
    logL_total = logL_total.at[observable_indices].add(logL)
    return logL_total

logL_numerical_distribution_summed(predictions, selector_matrix, observable_indices, x, log_y)

Compute the log likelihood values of numerical distributions for given predictions and sum them using a selector matrix.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
selector_matrix array

The selector matrix to apply to the log likelihood values. Of shape (n_likelihoods, n_observables).

required
observable_indices array

The indices of the constrained observables.

required
x array

The x values for the numerical distributions.

required
log_y array

The log y values for the numerical distributions.

required

Returns:

Type Description
array

The summed log likelihood values.

Source code in jelli/utils/distributions.py
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
def logL_numerical_distribution_summed(
    predictions: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: jnp.array,
    x: jnp.array,
    log_y: jnp.array,
) -> jnp.array:
    '''
    Compute the log likelihood values of numerical distributions for given predictions and sum them using a selector matrix.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    selector_matrix : jnp.array
        The selector matrix to apply to the log likelihood values. Of shape (n_likelihoods, n_observables).
    observable_indices : jnp.array
        The indices of the constrained observables.
    x : jnp.array
        The x values for the numerical distributions.
    log_y : jnp.array
        The log y values for the numerical distributions.

    Returns
    -------
    jnp.array
        The summed log likelihood values.
    '''
    return selector_matrix @ logL_numerical_distribution(predictions, observable_indices, x, log_y)

log_trapz_exp(log_y, x)

Compute the log of the trapezoidal integral of the exponential of log_y over x.

Parameters:

Name Type Description Default
log_y ndarray

Logarithm of the values to be integrated.

required
x ndarray

Points at which log_y is defined. It is assumed that x is uniformly spaced.

required

Returns:

Type Description
float

The logarithm of the trapezoidal integral of exp(log_y) over x.

Examples:

>>> log_y = np.array([0.1, 0.2, 0.3])
>>> x = np.array([1.0, 2.0, 3.0])
>>> log_trapz_exp(log_y, x)
0.8956461395871966
Source code in jelli/utils/distributions.py
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
def log_trapz_exp(
        log_y: np.ndarray,
        x: np.ndarray,
    ) -> np.float64:
    '''
    Compute the log of the trapezoidal integral of the exponential of `log_y` over `x`.

    Parameters
    ----------
    log_y : np.ndarray
        Logarithm of the values to be integrated.
    x : np.ndarray
        Points at which `log_y` is defined. It is assumed that `x` is uniformly spaced.

    Returns
    -------
    float
        The logarithm of the trapezoidal integral of `exp(log_y)` over `x`.

    Examples
    --------
    >>> log_y = np.array([0.1, 0.2, 0.3])
    >>> x = np.array([1.0, 2.0, 3.0])
    >>> log_trapz_exp(log_y, x)
    0.8956461395871966
    '''
    log_dx = np.log(x[1] - x[0])  # assume uniform spacing
    log_weights = np.zeros(len(x))
    log_weights[[0,-1]] = np.log(0.5)
    return log_dx + sp.special.logsumexp(log_y + log_weights)

logpdf_correlated_sectors(predictions_scaled, std_sm_exp, observable_indices, exp_central_scaled, cov_matrix_exp_scaled, cov_matrix_th_scaled)

Compute the log PDF values for observables with correlated theoretical and experimental uncertainties.

Parameters:

Name Type Description Default
predictions_scaled array

The predicted values, scaled by the SM+exp standard deviations.

required
std_sm_exp array

The SM+exp standard deviations.

required
observable_indices List[array]

The indices of the constrained observables.

required
exp_central_scaled array

The experimental central values, scaled by the SM+exp standard deviations.

required
cov_matrix_exp_scaled array

The experimental covariance matrix, scaled by the SM+exp standard deviations.

required
cov_matrix_th_scaled array

The theoretical covariance matrix in the space of parameters, scaled by the SM+exp standard deviations.

required

Returns:

Type Description
array

The log PDF values.

Source code in jelli/utils/distributions.py
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
def logpdf_correlated_sectors(
    predictions_scaled: jnp.array,
    std_sm_exp: jnp.array,
    observable_indices: List[jnp.array],
    exp_central_scaled: jnp.array,
    cov_matrix_exp_scaled: jnp.array,
    cov_matrix_th_scaled: jnp.array,
) -> jnp.array:
    '''
    Compute the log PDF values for observables with correlated theoretical and experimental uncertainties.

    Parameters
    ----------
    predictions_scaled : jnp.array
        The predicted values, scaled by the SM+exp standard deviations.
    std_sm_exp : jnp.array
        The SM+exp standard deviations.
    observable_indices : List[jnp.array]
        The indices of the constrained observables.
    exp_central_scaled : jnp.array
        The experimental central values, scaled by the SM+exp standard deviations.
    cov_matrix_exp_scaled : jnp.array
        The experimental covariance matrix, scaled by the SM+exp standard deviations.
    cov_matrix_th_scaled : jnp.array
        The theoretical covariance matrix in the space of parameters, scaled by the SM+exp standard deviations.

    Returns
    -------
    jnp.array
        The log PDF values.
    '''
    cov_scaled = cov_matrix_th_scaled + cov_matrix_exp_scaled
    std_scaled = jnp.sqrt(jnp.diag(cov_scaled))
    std = std_scaled  * std_sm_exp
    C = cov_scaled / jnp.outer(std_scaled, std_scaled)
    D = (predictions_scaled - exp_central_scaled)/std_scaled

    logpdf_rows = []
    for i in range(len(observable_indices)):
        logpdf_total = jnp.zeros_like(predictions_scaled)
        d = jnp.take(D, observable_indices[i])
        c = jnp.take(jnp.take(C, observable_indices[i], axis=0), observable_indices[i], axis=1)

        logdet_corr = jnp.linalg.slogdet(c)[1]
        logprod_std2 = 2 * jnp.sum(jnp.log(jnp.take(std, observable_indices[i])))

        logpdf = -0.5 * (
            d * jsp.linalg.cho_solve(jsp.linalg.cho_factor(c), d)
            + (logdet_corr
            + logprod_std2)/len(d)
            + jnp.log(2 * jnp.pi)
        )
        logpdf_total = logpdf_total.at[observable_indices[i]].add(logpdf)
        logpdf_rows.append(logpdf_total)
    return jnp.array(logpdf_rows)

logpdf_correlated_sectors_summed(predictions_scaled, std_sm_exp, selector_matrix, observable_indices, exp_central_scaled, cov_matrix_exp_scaled, cov_matrix_th_scaled)

Compute the summed log PDF values for observables with correlated theoretical and experimental uncertainties.

Parameters:

Name Type Description Default
predictions_scaled array

The predicted values, scaled by the SM+exp standard deviations.

required
std_sm_exp array

The SM+exp standard deviations.

required
selector_matrix array

The selector matrix to sum the log PDF values of different unique multivariate normal distributions. Of shape (n_likelihoods, n_distributions).

required
observable_indices List[array]

The indices of the constrained observables.

required
exp_central_scaled array

The experimental central values, scaled by the SM+exp standard deviations.

required
cov_matrix_exp_scaled array

The experimental covariance matrix, scaled by the SM+exp standard deviations.

required
cov_matrix_th_scaled array

The theoretical covariance matrix in the space of parameters, scaled by the SM+exp standard deviations.

required

Returns:

Type Description
array

The summed log PDF values.

Source code in jelli/utils/distributions.py
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
def logpdf_correlated_sectors_summed(
    predictions_scaled: jnp.array,
    std_sm_exp: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: List[jnp.array],
    exp_central_scaled: jnp.array,
    cov_matrix_exp_scaled: jnp.array,
    cov_matrix_th_scaled: jnp.array,
) -> jnp.array:
    '''
    Compute the summed log PDF values for observables with correlated theoretical and experimental uncertainties.

    Parameters
    ----------
    predictions_scaled : jnp.array
        The predicted values, scaled by the SM+exp standard deviations.
    std_sm_exp : jnp.array
        The SM+exp standard deviations.
    selector_matrix : jnp.array
        The selector matrix to sum the log PDF values of different unique multivariate normal distributions. Of shape (n_likelihoods, n_distributions).
    observable_indices : List[jnp.array]
        The indices of the constrained observables.
    exp_central_scaled : jnp.array
        The experimental central values, scaled by the SM+exp standard deviations.
    cov_matrix_exp_scaled : jnp.array
        The experimental covariance matrix, scaled by the SM+exp standard deviations.
    cov_matrix_th_scaled : jnp.array
        The theoretical covariance matrix in the space of parameters, scaled by the SM+exp standard deviations.

    Returns
    -------
    jnp.array
        The summed log PDF values.
    '''

    cov_scaled = cov_matrix_th_scaled + cov_matrix_exp_scaled
    std_scaled = jnp.sqrt(jnp.diag(cov_scaled))
    std = std_scaled  * std_sm_exp
    C = cov_scaled / jnp.outer(std_scaled, std_scaled)
    D = (predictions_scaled - exp_central_scaled)/std_scaled

    logpdf_rows = []
    for i in range(len(observable_indices)):

        d = jnp.take(D, observable_indices[i])
        c = jnp.take(jnp.take(C, observable_indices[i], axis=0), observable_indices[i], axis=1)

        logdet_corr = jnp.linalg.slogdet(c)[1]
        logprod_std2 = 2 * jnp.sum(jnp.log(jnp.take(std, observable_indices[i])))

        logpdf = -0.5 * (
            jnp.dot(d, jsp.linalg.cho_solve(jsp.linalg.cho_factor(c), d))
            + logdet_corr
            + logprod_std2
            + len(d) * jnp.log(2 * jnp.pi)
        )
        logpdf = jnp.where(jnp.isnan(logpdf), len(d)*LOG_ZERO, logpdf)
        logpdf_rows.append(logpdf)
    logpdf_total = jnp.array(logpdf_rows)
    return selector_matrix @ logpdf_total

logpdf_folded_normal_distribution(predictions, observable_indices, mean, std)

Compute the log PDF values of folded normal distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
observable_indices array

The indices of the constrained observables.

required
mean array

The means of the folded normal distributions.

required
std array

The standard deviations of the folded normal distributions.

required

Returns:

Type Description
array

The log PDF values for the predictions.

Source code in jelli/utils/distributions.py
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def logpdf_folded_normal_distribution(
    predictions: jnp.array,
    observable_indices: jnp.array,
    mean: jnp.array,
    std: jnp.array,
) -> jnp.array:
    '''
    Compute the log PDF values of folded normal distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    observable_indices : jnp.array
        The indices of the constrained observables.
    mean : jnp.array
        The means of the folded normal distributions.
    std : jnp.array
        The standard deviations of the folded normal distributions.

    Returns
    -------
    jnp.array
        The log PDF values for the predictions.
    '''
    logpdf_total = jnp.zeros_like(predictions)
    predictions = jnp.take(predictions, observable_indices)
    folded_logpdf = jnp.log(
        jsp.stats.norm.pdf(predictions, loc=mean, scale=std)
        + jsp.stats.norm.pdf(predictions, loc=-mean, scale=std)
    )
    logpdf = jnp.where(predictions >= 0, folded_logpdf, LOG_ZERO)
    logpdf_total = logpdf_total.at[observable_indices].add(logpdf)
    return logpdf_total

logpdf_folded_normal_distribution_summed(predictions, selector_matrix, observable_indices, mean, std)

Compute the log PDF values of folded normal distributions for given predictions and sum them using a selector matrix.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
selector_matrix array

The selector matrix to sum the log PDF values. Of shape (n_likelihoods, n_observables).

required
observable_indices array

The indices of the constrained observables.

required
mean array

The means of the folded normal distributions.

required
std array

The standard deviations of the folded normal distributions.

required

Returns:

Type Description
array

The summed log PDF values.

Source code in jelli/utils/distributions.py
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
def logpdf_folded_normal_distribution_summed(
    predictions: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: jnp.array,
    mean: jnp.array,
    std: jnp.array,
) -> jnp.array:
    '''
    Compute the log PDF values of folded normal distributions for given predictions and sum them using a selector matrix.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    selector_matrix : jnp.array
        The selector matrix to sum the log PDF values. Of shape (n_likelihoods, n_observables).
    observable_indices : jnp.array
        The indices of the constrained observables.
    mean : jnp.array
        The means of the folded normal distributions.
    std : jnp.array
        The standard deviations of the folded normal distributions.

    Returns
    -------
    jnp.array
        The summed log PDF values.
    '''
    return selector_matrix @ logpdf_folded_normal_distribution(predictions, observable_indices, mean, std)

logpdf_gamma_distribution_positive(predictions, observable_indices, a, loc, scale)

Compute the log PDF values of positive gamma distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
observable_indices array

The indices of the constrained observables.

required
a array

The shape parameters of the gamma distributions.

required
loc array

The location parameters of the gamma distributions.

required
scale array

The scale parameters of the gamma distributions.

required

Returns:

Type Description
array

The log PDF values for the predictions.

Source code in jelli/utils/distributions.py
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
def logpdf_gamma_distribution_positive(
    predictions: jnp.array,
    observable_indices: jnp.array,
    a: jnp.array,
    loc: jnp.array,
    scale: jnp.array,
) -> jnp.array:
    '''
    Compute the log PDF values of positive gamma distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    observable_indices : jnp.array
        The indices of the constrained observables.
    a : jnp.array
        The shape parameters of the gamma distributions.
    loc : jnp.array
        The location parameters of the gamma distributions.
    scale : jnp.array
        The scale parameters of the gamma distributions.

    Returns
    -------
    jnp.array
        The log PDF values for the predictions.
    '''
    logpdf_total = jnp.zeros_like(predictions)
    predictions = jnp.take(predictions, observable_indices)
    log_pdf_scale = jnp.log(1/(1-jsp.stats.gamma.cdf(0, a, loc=loc, scale=scale)))
    positive_logpdf = jsp.stats.gamma.logpdf(
        predictions, a, loc=loc, scale=scale
    ) + log_pdf_scale
    logpdf = jnp.where(predictions>=0, positive_logpdf, LOG_ZERO)
    logpdf_total = logpdf_total.at[observable_indices].add(logpdf)
    return logpdf_total

logpdf_gamma_distribution_positive_summed(predictions, selector_matrix, observable_indices, a, loc, scale)

Compute the log PDF values of positive gamma distributions for given predictions and sum them using a selector matrix.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
selector_matrix array

The selector matrix to sum the log PDF values. Of shape (n_likelihoods, n_observables).

required
observable_indices array

The indices of the constrained observables.

required
a array

The shape parameters of the gamma distributions.

required
loc array

The location parameters of the gamma distributions.

required
scale array

The scale parameters of the gamma distributions.

required

Returns:

Type Description
array

The summed log PDF values.

Source code in jelli/utils/distributions.py
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
def logpdf_gamma_distribution_positive_summed(
    predictions: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: jnp.array,
    a: jnp.array,
    loc: jnp.array,
    scale: jnp.array,
) -> jnp.array:
    '''
    Compute the log PDF values of positive gamma distributions for given predictions and sum them using a selector matrix.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    selector_matrix : jnp.array
        The selector matrix to sum the log PDF values. Of shape (n_likelihoods, n_observables).
    observable_indices : jnp.array
        The indices of the constrained observables.
    a : jnp.array
        The shape parameters of the gamma distributions.
    loc : jnp.array
        The location parameters of the gamma distributions.
    scale : jnp.array
        The scale parameters of the gamma distributions.

    Returns
    -------
    jnp.array
        The summed log PDF values.
    '''
    return selector_matrix @ logpdf_gamma_distribution_positive(predictions, observable_indices, a, loc, scale)

logpdf_half_normal_distribution(predictions, observable_indices, std)

Compute the log PDF values of half normal distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
observable_indices array

The indices of the constrained observables.

required
std array

The standard deviations of the half normal distributions.

required

Returns:

Type Description
array

The log PDF values for the predictions.

Source code in jelli/utils/distributions.py
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
def logpdf_half_normal_distribution(
    predictions: jnp.array,
    observable_indices: jnp.array,
    std: jnp.array,
) -> jnp.array:
    '''
    Compute the log PDF values of half normal distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    observable_indices : jnp.array
        The indices of the constrained observables.
    std : jnp.array
        The standard deviations of the half normal distributions.

    Returns
    -------
    jnp.array
        The log PDF values for the predictions.
    '''
    return logpdf_folded_normal_distribution(predictions, observable_indices, 0, std)

logpdf_half_normal_distribution_summed(predictions, selector_matrix, observable_indices, std)

Compute the log PDF values of half normal distributions for given predictions and sum them using a selector matrix.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
selector_matrix array

The selector matrix to sum the log PDF values. Of shape (n_likelihoods, n_observables).

required
observable_indices array

The indices of the constrained observables.

required
std array

The standard deviations of the half normal distributions.

required

Returns:

Type Description
array

The summed log PDF values.

Source code in jelli/utils/distributions.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def logpdf_half_normal_distribution_summed(
    predictions: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: jnp.array,
    std: jnp.array,
) -> jnp.array:
    '''
    Compute the log PDF values of half normal distributions for given predictions and sum them using a selector matrix.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    selector_matrix : jnp.array
        The selector matrix to sum the log PDF values. Of shape (n_likelihoods, n_observables).
    observable_indices : jnp.array
        The indices of the constrained observables.
    std : jnp.array
        The standard deviations of the half normal distributions.

    Returns
    -------
    jnp.array
        The summed log PDF values.
    '''
    return logpdf_folded_normal_distribution_summed(predictions, selector_matrix, observable_indices, 0, std)

logpdf_multivariate_normal_distribution(predictions, observable_indices, mean, standard_deviation, inverse_correlation, logpdf_normalization_per_observable)

Compute the log PDF values of multivariate normal distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
observable_indices List[array]

The indices of the constrained observables.

required
mean List[array]

The mean values of the multivariate normal distributions.

required
standard_deviation List[array]

The standard deviations of the multivariate normal distributions.

required
inverse_correlation List[array]

The inverse correlation matrices of the multivariate normal distributions.

required
logpdf_normalization_per_observable List[array]

The log PDF normalization constants for each observable.

required

Returns:

Type Description
List[array]

The log PDF values for each observable and distribution.

Source code in jelli/utils/distributions.py
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
def logpdf_multivariate_normal_distribution(
    predictions: jnp.array,
    observable_indices: List[jnp.array],
    mean: List[jnp.array],
    standard_deviation: List[jnp.array],
    inverse_correlation: List[jnp.array],
    logpdf_normalization_per_observable: List[jnp.array],
) -> List[jnp.array]:
    '''
    Compute the log PDF values of multivariate normal distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    observable_indices : List[jnp.array]
        The indices of the constrained observables.
    mean : List[jnp.array]
        The mean values of the multivariate normal distributions.
    standard_deviation : List[jnp.array]
        The standard deviations of the multivariate normal distributions.
    inverse_correlation : List[jnp.array]
        The inverse correlation matrices of the multivariate normal distributions.
    logpdf_normalization_per_observable : List[jnp.array]
        The log PDF normalization constants for each observable.

    Returns
    -------
    List[jnp.array]
        The log PDF values for each observable and distribution.
    '''
    logpdfs = []
    for i in range(len(observable_indices)):
        logpdf_total = jnp.zeros_like(predictions)
        d = (jnp.take(predictions, observable_indices[i]) - mean[i]) / standard_deviation[i]
        logpdf = -0.5 * d * jnp.dot(inverse_correlation[i], d) + logpdf_normalization_per_observable[i]
        logpdf_total = logpdf_total.at[observable_indices[i]].add(logpdf)
        logpdfs.append(logpdf_total)
    return jnp.stack(logpdfs)

logpdf_multivariate_normal_distribution_summed(predictions, selector_matrix, observable_indices, mean, standard_deviation, inverse_correlation, logpdf_normalization_per_observable)

Compute the summed log PDF values of multivariate normal distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
selector_matrix array

The selector matrix to sum the log PDF values of different multivariate normal distributions. Of shape (n_likelihoods, n_distributions).

required
observable_indices List[array]

The indices of the constrained observables.

required
mean List[array]

The mean values of the multivariate normal distributions.

required
standard_deviation List[array]

The standard deviations of the multivariate normal distributions.

required
inverse_correlation List[array]

The inverse correlation matrices of the multivariate normal distributions.

required
logpdf_normalization_per_observable List[array]

The log PDF normalization constants for each observable.

required

Returns:

Type Description
array

The summed log PDF values.

Source code in jelli/utils/distributions.py
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
def logpdf_multivariate_normal_distribution_summed(
    predictions: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: List[jnp.array],
    mean: List[jnp.array],
    standard_deviation: List[jnp.array],
    inverse_correlation: List[jnp.array],
    logpdf_normalization_per_observable: List[jnp.array],
) -> jnp.array:
    '''
    Compute the summed log PDF values of multivariate normal distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    selector_matrix : jnp.array
        The selector matrix to sum the log PDF values of different multivariate normal distributions. Of shape (n_likelihoods, n_distributions).
    observable_indices : List[jnp.array]
        The indices of the constrained observables.
    mean : List[jnp.array]
        The mean values of the multivariate normal distributions.
    standard_deviation : List[jnp.array]
        The standard deviations of the multivariate normal distributions.
    inverse_correlation : List[jnp.array]
        The inverse correlation matrices of the multivariate normal distributions.
    logpdf_normalization_per_observable : List[jnp.array]
        The log PDF normalization constants for each observable.

    Returns
    -------
    jnp.array
        The summed log PDF values.
    '''
    logpdf_rows = []
    for i in range(len(observable_indices)):
        d = (jnp.take(predictions, observable_indices[i]) - mean[i]) / standard_deviation[i]
        n_obs = d.shape[0]
        logpdf = -0.5 * jnp.dot(d, jnp.dot(inverse_correlation[i], d)) + n_obs * logpdf_normalization_per_observable[i]
        logpdf_rows.append(logpdf)
    logpdf_total = jnp.stack(logpdf_rows)
    return selector_matrix @ logpdf_total

logpdf_normal_distribution(predictions, observable_indices, mean, std)

Compute the log PDF values of normal distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
observable_indices array

The indices of the constrained observables.

required
mean array

The means of the normal distributions.

required
std array

The standard deviations of the normal distributions.

required

Returns:

Type Description
array

The log PDF values for the predictions.

Source code in jelli/utils/distributions.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def logpdf_normal_distribution(
    predictions: jnp.array,
    observable_indices: jnp.array,
    mean: jnp.array,
    std: jnp.array,
) -> jnp.array:
    '''
    Compute the log PDF values of normal distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    observable_indices : jnp.array
        The indices of the constrained observables.
    mean : jnp.array
        The means of the normal distributions.
    std : jnp.array
        The standard deviations of the normal distributions.

    Returns
    -------
    jnp.array
        The log PDF values for the predictions.
    '''
    logpdf_total = jnp.zeros_like(predictions)
    predictions = jnp.take(predictions, observable_indices)
    logpdf = jsp.stats.norm.logpdf(predictions, loc=mean, scale=std)
    logpdf_total = logpdf_total.at[observable_indices].add(logpdf)
    return logpdf_total

logpdf_normal_distribution_summed(predictions, selector_matrix, observable_indices, mean, std)

Compute the log PDF values of normal distributions for given predictions and sum them using a selector matrix.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
selector_matrix array

The selector matrix to sum the log PDF values. Of shape (n_likelihoods, n_observables).

required
observable_indices array

The indices of the constrained observables.

required
mean array

The means of the normal distributions.

required
std array

The standard deviations of the normal distributions.

required

Returns:

Type Description
array

The summed log PDF values.

Source code in jelli/utils/distributions.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def logpdf_normal_distribution_summed(
    predictions: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: jnp.array,
    mean: jnp.array,
    std: jnp.array,
) -> jnp.array:
    '''
    Compute the log PDF values of normal distributions for given predictions and sum them using a selector matrix.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    selector_matrix : jnp.array
        The selector matrix to sum the log PDF values. Of shape (n_likelihoods, n_observables).
    observable_indices : jnp.array
        The indices of the constrained observables.
    mean : jnp.array
        The means of the normal distributions.
    std : jnp.array
        The standard deviations of the normal distributions.

    Returns
    -------
    jnp.array
        The summed log PDF values.
    '''
    return selector_matrix @ logpdf_normal_distribution(predictions, observable_indices, mean, std)

logpdf_numerical_distribution(predictions, observable_indices, x, log_y)

Compute the log PDF values of numerical distributions for given predictions.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
observable_indices array

The indices of the constrained observables.

required
x array

The x values of the numerical distributions.

required
log_y array

The log PDF values of the numerical distributions.

required

Returns:

Type Description
array

The log PDF values for the predictions.

Source code in jelli/utils/distributions.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def logpdf_numerical_distribution(
    predictions: jnp.array,
    observable_indices: jnp.array,
    x: jnp.array,
    log_y: jnp.array,
) -> jnp.array:
    '''
    Compute the log PDF values of numerical distributions for given predictions.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    observable_indices : jnp.array
        The indices of the constrained observables.
    x : jnp.array
        The x values of the numerical distributions.
    log_y : jnp.array
        The log PDF values of the numerical distributions.

    Returns
    -------
    jnp.array
        The log PDF values for the predictions.
    '''
    logpdf_total = jnp.zeros_like(predictions)
    predictions = jnp.take(predictions, observable_indices)
    logpdf = vmap(interp_log_pdf)(predictions, x, log_y)
    logpdf_total = logpdf_total.at[observable_indices].add(logpdf)
    return logpdf_total

logpdf_numerical_distribution_summed(predictions, selector_matrix, observable_indices, x, log_y)

Compute the log PDF values of numerical distributions for given predictions and sum them using a selector matrix.

Parameters:

Name Type Description Default
predictions array

The predicted values.

required
selector_matrix array

The selector matrix to sum the log PDF values. Of shape (n_likelihoods, n_observables).

required
observable_indices array

The indices of the constrained observables.

required
x array

The x values of the numerical distributions.

required
log_y array

The log PDF values of the numerical distributions.

required

Returns:

Type Description
array

The summed log PDF values.

Source code in jelli/utils/distributions.py
 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
def logpdf_numerical_distribution_summed(
    predictions: jnp.array,
    selector_matrix: jnp.array,
    observable_indices: jnp.array,
    x: jnp.array,
    log_y: jnp.array,
) -> jnp.array:
    '''
    Compute the log PDF values of numerical distributions for given predictions and sum them using a selector matrix.

    Parameters
    ----------
    predictions : jnp.array
        The predicted values.
    selector_matrix : jnp.array
        The selector matrix to sum the log PDF values. Of shape (n_likelihoods, n_observables).
    observable_indices : jnp.array
        The indices of the constrained observables.
    x : jnp.array
        The x values of the numerical distributions.
    log_y : jnp.array
        The log PDF values of the numerical distributions.

    Returns
    -------
    jnp.array
        The summed log PDF values.
    '''
    return selector_matrix @ logpdf_numerical_distribution(predictions, observable_indices, x, log_y)