Skip to content

jelli.core.experimental_correlations

ExperimentalCorrelations

A class to represent experimental correlations.

Parameters:

Name Type Description Default
hash_val str

A unique hash value representing the combination of measurements and observables.

required
data_type str

The type of data stored in the instance. It can be correlations, central, or uncertainties.

required
data ndarray

The data array containing the correlation matrix, central values, or uncertainties.

required
row_names Iterable[str]

The names of the observables corresponding to the rows of the data array.

required
col_names Iterable[str]

The names of the observables corresponding to the columns of the data array.

required

Attributes:

Name Type Description
hash_val str

A unique hash value representing the combination of measurements and observables.

data_type str

The type of data stored in the instance. It can be correlations, central, or uncertainties.

data ndarray

The data array containing the correlation matrix, central values, or uncertainties.

row_names Iterable[str]

The names of the observables corresponding to the rows of the data array.

col_names Iterable[str]

The names of the observables corresponding to the columns of the data array.

_instances Dict[str, Dict[str, ExperimentalCorrelations]]

A class-level dictionary to hold instances of ExperimentalCorrelations for each data type.

_covariance_scaled Dict[str, ndarray]

A class-level dictionary to hold scaled covariance matrices for each unique combination of measurements and observables.

_observable_names Iterable[Iterable[str]]

A class-level iterable to hold lists of observable names for each observable sector.

Methods:

Name Description
load

Load observable names from all correlated observable sectors and initialize class-level attributes.

compute

Compute the correlation matrices, central values, and uncertainties from the specified measurements.

get_data

Retrieve the data array for the specified data type and combination of observables and measurements.

get_cov_scaled

Retrieve the scaled covariance matrix for the specified combination of observables and measurements.

Examples:

Load observable names from all correlated observable sectors:

>>> ExperimentalCorrelations.load()

Compute the correlation matrices, central values, and uncertainties from the specified measurements:

>>> ExperimentalCorrelations.compute(include_measurements=['measurement1', 'measurement2'])

Retrieve the data array for a specific data type and combination of observables and measurements:

>>> data = ExperimentalCorrelations.get_data(data_type='correlations', include_measurements=['measurement1'], row_names=['observable1'], col_names=['observable2'])

Retrieve the scaled covariance matrix for a specific combination of observables and measurements:

>>> cov_scaled = ExperimentalCorrelations.get_cov_scaled(include_measurements=['measurement1'], row_names=['observable1'], col_names=['observable2'], std_exp_scaled_row=np.array([0.5]), std_exp_scaled_col=np.array([1.2]))
Source code in jelli/core/experimental_correlations.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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
162
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
193
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
223
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
258
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
342
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
class ExperimentalCorrelations:
    '''
    A class to represent experimental correlations.

    Parameters
    ----------
    hash_val : str
        A unique hash value representing the combination of measurements and observables.
    data_type : str
        The type of data stored in the instance. It can be `correlations`, `central`, or `uncertainties`.
    data : np.ndarray
        The data array containing the correlation matrix, central values, or uncertainties.
    row_names : Iterable[str]
        The names of the observables corresponding to the rows of the data array.
    col_names : Iterable[str]
        The names of the observables corresponding to the columns of the data array.

    Attributes
    ----------
    hash_val : str
        A unique hash value representing the combination of measurements and observables.
    data_type : str
        The type of data stored in the instance. It can be `correlations`, `central`, or `uncertainties`.
    data : np.ndarray
        The data array containing the correlation matrix, central values, or uncertainties.
    row_names : Iterable[str]
        The names of the observables corresponding to the rows of the data array.
    col_names : Iterable[str]
        The names of the observables corresponding to the columns of the data array.
    _instances : Dict[str, Dict[str, 'ExperimentalCorrelations']]
        A class-level dictionary to hold instances of `ExperimentalCorrelations` for each data type.
    _covariance_scaled : Dict[str, jnp.ndarray]
        A class-level dictionary to hold scaled covariance matrices for each unique combination of measurements and observables.
    _observable_names : Iterable[Iterable[str]]
        A class-level iterable to hold lists of observable names for each observable sector.

    Methods
    -------
    load() -> None
        Load observable names from all correlated observable sectors and initialize class-level attributes.
    compute(include_measurements: Iterable[str], n_samples: int = int(1e6), seed: int = None) -> None
        Compute the correlation matrices, central values, and uncertainties from the specified measurements.
    get_data(data_type: str, include_measurements: Iterable[str], row_names: Iterable[str], col_names: Optional[Iterable[str]] = []) -> Optional[jnp.ndarray]
        Retrieve the data array for the specified data type and combination of observables and measurements.
    get_cov_scaled(include_measurements: Iterable[str], row_names: Iterable[str], col_names: Iterable[str], std_exp_scaled_row: np.ndarray, std_exp_scaled_col: np.ndarray) -> jnp.ndarray
        Retrieve the scaled covariance matrix for the specified combination of observables and measurements.

    Examples
    --------
    Load observable names from all correlated observable sectors:

    >>> ExperimentalCorrelations.load()

    Compute the correlation matrices, central values, and uncertainties from the specified measurements:

    >>> ExperimentalCorrelations.compute(include_measurements=['measurement1', 'measurement2'])

    Retrieve the data array for a specific data type and combination of observables and measurements:

    >>> data = ExperimentalCorrelations.get_data(data_type='correlations', include_measurements=['measurement1'], row_names=['observable1'], col_names=['observable2'])

    Retrieve the scaled covariance matrix for a specific combination of observables and measurements:

    >>> cov_scaled = ExperimentalCorrelations.get_cov_scaled(include_measurements=['measurement1'], row_names=['observable1'], col_names=['observable2'], std_exp_scaled_row=np.array([0.5]), std_exp_scaled_col=np.array([1.2]))
    '''

    _instances: Dict[str, Dict[str, 'ExperimentalCorrelations']] = {
        'correlations': {},
        'central': {},
        'uncertainties': {}
    }  # Dictionary to hold instances of ExperimentalCorrelations
    _covariance_scaled: Dict[str, jnp.ndarray] = {}
    _observable_names: Iterable[Iterable[str]] = []

    def __init__(
        self,
        hash_val: str,
        data_type: str,
        data: np.ndarray,
        row_names: Iterable[str],
        col_names: Iterable[str],
    ) -> None:
        '''
        Initialize an instance of the ExperimentalCorrelations class.

        Parameters
        ----------
        hash_val : str
            A unique hash value representing the combination of measurements and observables.
        data_type : str
            The type of data stored in the instance. It can be `correlations`, `central`, or `uncertainties`.
        data : np.ndarray
            The data array containing the correlation matrix, central values, or uncertainties.
        row_names : Iterable[str]
            The names of the observables corresponding to the rows of the data array.
        col_names : Iterable[str]
            The names of the observables corresponding to the columns of the data array.

        Returns
        -------
        None

        Examples
        --------
        Initialize an instance of the ExperimentalCorrelations class:

        >>> exp_corr = ExperimentalCorrelations(
        ...     hash_val='unique_hash_value',
        ...     data_type='correlations',
        ...     data=np.array([[1.0, 0.5], [0.5, 1.0]]),
        ...     row_names=['observable1', 'observable2'],
        ...     col_names=['observable1', 'observable2']
        ... )
        '''
        self.hash_val = hash_val
        self.data_type = data_type
        self.data = data
        self.row_names = row_names
        self.col_names = col_names
        self._instances[data_type][hash_val] = self

    @classmethod
    def load(cls) -> None:
        '''
        Load observable names from all correlated observable sectors and initialize class-level attributes.

        Returns
        -------
        None

        Examples
        --------
        Load observable names from all correlated observable sectors:

        >>> ExperimentalCorrelations.load()
        '''
        observable_names = []
        for observable_sector in ObservableSector.get_all():
            if observable_sector.observable_uncertainties is not None:
                observable_names.append(observable_sector.observable_names)
        cls._observable_names = observable_names
        cls._instances = {
            'correlations': {},
            'central': {},
            'uncertainties': {}
        }
        cls._covariance_scaled = {}

    @classmethod
    def compute(
        cls,
        include_measurements: Iterable[str],
        n_samples: int = int(1e6),
        seed: int = None
    ) -> None:
        '''
        Compute the correlation matrices, central values, and uncertainties from the specified measurements.

        Parameters
        ----------
        include_measurements : Iterable[str]
            A list of measurement names to include in the computation.
        n_samples : int, optional
            The number of samples to draw for numerical approximations of non-Gaussian distributions. Default is 1e6.
        seed : int, optional
            A random seed for reproducibility of the samples. Default is None.

        Returns
        -------
        None

        Examples
        --------
        Compute the correlation matrices, central values, and uncertainties from the specified measurements:

        >>> ExperimentalCorrelations.compute(include_measurements=['measurement1', 'measurement2'])
        '''

        observables = list(chain.from_iterable(cls._observable_names))

        # get univariate constraints and combine them for each observable
        constraints_univariate = Measurement.get_constraints(
            observables,
            distribution_types=[k for k in logpdf_functions.keys() if k not in ['MultivariateNormalDistribution']],
            include_measurements=include_measurements,
            )
        constraints_list = []
        for observable in observables:
            constraints_observable = {}
            for dist_type, dist_info in constraints_univariate.items():
                mask = dist_info['observables'] == observable
                if np.any(mask):
                    constraints_observable[dist_type] = {
                        k: v[mask] for k, v in dist_info.items()
                    }
            if constraints_observable:
                constraints_list.append(constraints_observable)
        constraints_univariate = Measurement.combine_constraints(constraints_list)

        # construct covariance matrix and mean vector from univariate constraints
        cov = np.diag([np.inf] * len(observables))
        mean = np.zeros(len(observables))
        for dist_type, dist_info in constraints_univariate.items():
            if dist_type == 'NormalDistribution' or dist_type == 'HalfNormalDistribution':  # replace HalfNormalDistribution with zero-mean NormalDistribution
                central_value = dist_info['central_value']
                standard_deviation = dist_info['standard_deviation']
                observable_indices = dist_info['observable_indices']
            else:  # numerically obtain the Gaussian approximation
                samples = get_distribution_samples(dist_type, dist_info, n_samples, seed)
                central_value = np.mean(samples, axis=1)
                standard_deviation = np.std(samples, axis=1)
                observable_indices = dist_info['observable_indices']
            cov[observable_indices, observable_indices] = standard_deviation**2
            mean[observable_indices] = central_value

        # get multivariate constraints
        constraints_multivariate = Measurement.get_constraints(
            observables,
            distribution_types=['MultivariateNormalDistribution'],
            include_measurements=include_measurements,
        )
        if constraints_multivariate:
            # combine all covariance matrices and mean vectors using the weighted average
            weights = [np.diag(1/np.diag(cov))]
            means = [mean]
            constraints_multivariate = constraints_multivariate['MultivariateNormalDistribution']
            for i in range(len(constraints_multivariate['central_value'])):
                weight_i = np.zeros((len(observables), len(observables)))
                mean_i = np.zeros(len(observables))
                observable_indices_i = constraints_multivariate['observable_indices'][i]
                central_value_i = constraints_multivariate['central_value'][i]
                standard_deviation_i = constraints_multivariate['standard_deviation'][i]
                inverse_correlation_i = constraints_multivariate['inverse_correlation'][i]
                weight_i[np.ix_(observable_indices_i, observable_indices_i)] = inverse_correlation_i / np.outer(standard_deviation_i, standard_deviation_i)
                mean_i[observable_indices_i] = central_value_i
                weights.append(weight_i)
                means.append(mean_i)
            inv_cov = np.sum(weights, axis=0)
            # regularize inversion asuming inv_cov = D R D where R has unit diagonal, then invert R instead of inv_cov
            d = np.sqrt(np.diag(inv_cov))
            nonzero = d != 0  # unconstrained observables have zeros
            inv_cov = inv_cov[np.ix_(nonzero, nonzero)]
            d = d[nonzero]
            d2 = np.outer(d, d)
            R = inv_cov / d2
            inv_R = np.linalg.inv(R)
            cov = np.diag([np.nan] * len(nonzero))
            cov[np.ix_(nonzero, nonzero)] = inv_R / d2
            mean = cov @ np.sum([w @ m for w, m in zip(weights, means)], axis=0)
        else:
            cov[cov == np.inf] = np.nan
        std = np.sqrt(np.diag(cov))
        corr = cov / np.outer(std, std)

        for i, row_names in enumerate(cls._observable_names):
            row_measurements = Measurement.get_measurements(row_names, include_measurements=include_measurements)
            row_idx = [observables.index(o) for o in row_names]
            hash_val = hash_names(row_measurements, row_names)
            cls(
                hash_val=hash_val,
                data_type='central',
                data=jnp.array(mean[row_idx], dtype=jnp.float64),
                row_names=row_names,
                col_names=[],
            )
            cls(
                hash_val=hash_val,
                data_type='uncertainties',
                data=jnp.array(std[row_idx], dtype=jnp.float64),
                row_names=row_names,
                col_names=[],
            )
            for j in range(i, len(cls._observable_names)):
                col_names = cls._observable_names[j]
                col_measurements = Measurement.get_measurements(col_names, include_measurements=include_measurements)
                col_idx = [observables.index(o) for o in col_names]
                hash_val = hash_names(row_measurements, col_measurements, row_names, col_names)
                cls(
                    hash_val=hash_val,
                    data_type='correlations',
                    data=jnp.array(corr[np.ix_(row_idx, col_idx)], dtype=jnp.float64),
                    row_names=row_names,
                    col_names=col_names,
                )

    @classmethod
    def get_data(
        cls,
        data_type: str,
        include_measurements: Iterable[str],
        row_names: Iterable[str],
        col_names: Optional[Iterable[str]] = []
    ):
        '''
        Retrieve the data array for the specified data type and combination of observables and measurements.

        Parameters
        ----------
        data_type : str
            The type of data to retrieve. It can be `correlations`, `central`, or `uncertainties`.
        include_measurements : Iterable[str]
            A list of measurement names to include in the retrieval.
        row_names : Iterable[str]
            The names of the observables corresponding to the rows of the data array.
        col_names : Iterable[str], optional
            The names of the observables corresponding to the columns of the data array. Default is an empty list.

        Returns
        -------
        Optional[jnp.ndarray]
            The data array for the specified data type and combination of observables and measurements, or `None` if not found.

        Examples
        --------
        Retrieve the data array for a specific data type and combination of observables and measurements:

        >>> data = ExperimentalCorrelations.get_data(
        ...     data_type='correlations',
        ...     include_measurements=['measurement1'],
        ...     row_names=['observable1'],
        ...     col_names=['observable2']
        ... )
        '''
        row_measurements = Measurement.get_measurements(row_names, include_measurements=include_measurements)
        col_measurements = Measurement.get_measurements(col_names, include_measurements=include_measurements)
        hash_val = hash_names(row_measurements, col_measurements, row_names, col_names)
        hash_val_transposed = hash_names(col_measurements, row_measurements, col_names, row_names)
        if hash_val not in cls._instances[data_type] and hash_val_transposed not in cls._instances[data_type]:
            cls.compute(include_measurements)
        if hash_val in cls._instances[data_type]:
            return cls._instances[data_type][hash_val].data
        elif hash_val_transposed in cls._instances[data_type]:
            return cls._instances[data_type][hash_val_transposed].data.T
        else:
            return None

    @classmethod
    def get_cov_scaled(
        cls,
        include_measurements: Iterable[str],
        row_names: Iterable[str],
        col_names: Iterable[str],
        std_exp_scaled_row: np.ndarray,
        std_exp_scaled_col: np.ndarray,
    ):
        '''
        Retrieve the scaled covariance matrix for the specified combination of observables and measurements.

        Parameters
        ----------
        include_measurements : Iterable[str]
            A list of measurement names to include in the retrieval.
        row_names : Iterable[str]
            The names of the observables corresponding to the rows of the covariance matrix.
        col_names : Iterable[str]
            The names of the observables corresponding to the columns of the covariance matrix.
        std_exp_scaled_row : np.ndarray
            The experimental standard deviations for the row observables, scaled by any additional factors.
        std_exp_scaled_col : np.ndarray
            The experimental standard deviations for the column observables, scaled by any additional factors.

        Returns
        -------
        jnp.ndarray
            The scaled covariance matrix for the specified combination of observables and measurements.

        Examples
        --------
        Retrieve the scaled covariance matrix for a specific combination of observables and measurements:

        >>> cov_scaled = ExperimentalCorrelations.get_cov_scaled(
        ...     include_measurements=['measurement1'],
        ...     row_names=['observable1'],
        ...     col_names=['observable2'],
        ...     std_exp_scaled_row=np.array([0.5]),
        ...     std_exp_scaled_col=np.array([1.2])
        ... )
        '''
        row_measurements = Measurement.get_measurements(row_names, include_measurements=include_measurements)
        col_measurements = Measurement.get_measurements(col_names, include_measurements=include_measurements)
        hash_val = hash_names(row_measurements, col_measurements, row_names, col_names)
        if hash_val in cls._covariance_scaled:
            cov_scaled = cls._covariance_scaled[hash_val]
        else:
            corr = cls.get_data('correlations', include_measurements, row_names, col_names)
            if corr is None:
                raise ValueError(f"Correlation data for {row_names} and {col_names} not found.")
            cov_scaled = corr * np.outer(std_exp_scaled_row, std_exp_scaled_col)
            cov_scaled = jnp.array(cov_scaled, dtype=jnp.float64)
            cls._covariance_scaled[hash_val] = cov_scaled
        return cov_scaled

__init__(hash_val, data_type, data, row_names, col_names)

Initialize an instance of the ExperimentalCorrelations class.

Parameters:

Name Type Description Default
hash_val str

A unique hash value representing the combination of measurements and observables.

required
data_type str

The type of data stored in the instance. It can be correlations, central, or uncertainties.

required
data ndarray

The data array containing the correlation matrix, central values, or uncertainties.

required
row_names Iterable[str]

The names of the observables corresponding to the rows of the data array.

required
col_names Iterable[str]

The names of the observables corresponding to the columns of the data array.

required

Returns:

Type Description
None

Examples:

Initialize an instance of the ExperimentalCorrelations class:

>>> exp_corr = ExperimentalCorrelations(
...     hash_val='unique_hash_value',
...     data_type='correlations',
...     data=np.array([[1.0, 0.5], [0.5, 1.0]]),
...     row_names=['observable1', 'observable2'],
...     col_names=['observable1', 'observable2']
... )
Source code in jelli/core/experimental_correlations.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def __init__(
    self,
    hash_val: str,
    data_type: str,
    data: np.ndarray,
    row_names: Iterable[str],
    col_names: Iterable[str],
) -> None:
    '''
    Initialize an instance of the ExperimentalCorrelations class.

    Parameters
    ----------
    hash_val : str
        A unique hash value representing the combination of measurements and observables.
    data_type : str
        The type of data stored in the instance. It can be `correlations`, `central`, or `uncertainties`.
    data : np.ndarray
        The data array containing the correlation matrix, central values, or uncertainties.
    row_names : Iterable[str]
        The names of the observables corresponding to the rows of the data array.
    col_names : Iterable[str]
        The names of the observables corresponding to the columns of the data array.

    Returns
    -------
    None

    Examples
    --------
    Initialize an instance of the ExperimentalCorrelations class:

    >>> exp_corr = ExperimentalCorrelations(
    ...     hash_val='unique_hash_value',
    ...     data_type='correlations',
    ...     data=np.array([[1.0, 0.5], [0.5, 1.0]]),
    ...     row_names=['observable1', 'observable2'],
    ...     col_names=['observable1', 'observable2']
    ... )
    '''
    self.hash_val = hash_val
    self.data_type = data_type
    self.data = data
    self.row_names = row_names
    self.col_names = col_names
    self._instances[data_type][hash_val] = self

compute(include_measurements, n_samples=int(1000000.0), seed=None) classmethod

Compute the correlation matrices, central values, and uncertainties from the specified measurements.

Parameters:

Name Type Description Default
include_measurements Iterable[str]

A list of measurement names to include in the computation.

required
n_samples int

The number of samples to draw for numerical approximations of non-Gaussian distributions. Default is 1e6.

int(1000000.0)
seed int

A random seed for reproducibility of the samples. Default is None.

None

Returns:

Type Description
None

Examples:

Compute the correlation matrices, central values, and uncertainties from the specified measurements:

>>> ExperimentalCorrelations.compute(include_measurements=['measurement1', 'measurement2'])
Source code in jelli/core/experimental_correlations.py
158
159
160
161
162
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
193
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
223
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
258
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
285
286
287
288
289
290
291
292
293
@classmethod
def compute(
    cls,
    include_measurements: Iterable[str],
    n_samples: int = int(1e6),
    seed: int = None
) -> None:
    '''
    Compute the correlation matrices, central values, and uncertainties from the specified measurements.

    Parameters
    ----------
    include_measurements : Iterable[str]
        A list of measurement names to include in the computation.
    n_samples : int, optional
        The number of samples to draw for numerical approximations of non-Gaussian distributions. Default is 1e6.
    seed : int, optional
        A random seed for reproducibility of the samples. Default is None.

    Returns
    -------
    None

    Examples
    --------
    Compute the correlation matrices, central values, and uncertainties from the specified measurements:

    >>> ExperimentalCorrelations.compute(include_measurements=['measurement1', 'measurement2'])
    '''

    observables = list(chain.from_iterable(cls._observable_names))

    # get univariate constraints and combine them for each observable
    constraints_univariate = Measurement.get_constraints(
        observables,
        distribution_types=[k for k in logpdf_functions.keys() if k not in ['MultivariateNormalDistribution']],
        include_measurements=include_measurements,
        )
    constraints_list = []
    for observable in observables:
        constraints_observable = {}
        for dist_type, dist_info in constraints_univariate.items():
            mask = dist_info['observables'] == observable
            if np.any(mask):
                constraints_observable[dist_type] = {
                    k: v[mask] for k, v in dist_info.items()
                }
        if constraints_observable:
            constraints_list.append(constraints_observable)
    constraints_univariate = Measurement.combine_constraints(constraints_list)

    # construct covariance matrix and mean vector from univariate constraints
    cov = np.diag([np.inf] * len(observables))
    mean = np.zeros(len(observables))
    for dist_type, dist_info in constraints_univariate.items():
        if dist_type == 'NormalDistribution' or dist_type == 'HalfNormalDistribution':  # replace HalfNormalDistribution with zero-mean NormalDistribution
            central_value = dist_info['central_value']
            standard_deviation = dist_info['standard_deviation']
            observable_indices = dist_info['observable_indices']
        else:  # numerically obtain the Gaussian approximation
            samples = get_distribution_samples(dist_type, dist_info, n_samples, seed)
            central_value = np.mean(samples, axis=1)
            standard_deviation = np.std(samples, axis=1)
            observable_indices = dist_info['observable_indices']
        cov[observable_indices, observable_indices] = standard_deviation**2
        mean[observable_indices] = central_value

    # get multivariate constraints
    constraints_multivariate = Measurement.get_constraints(
        observables,
        distribution_types=['MultivariateNormalDistribution'],
        include_measurements=include_measurements,
    )
    if constraints_multivariate:
        # combine all covariance matrices and mean vectors using the weighted average
        weights = [np.diag(1/np.diag(cov))]
        means = [mean]
        constraints_multivariate = constraints_multivariate['MultivariateNormalDistribution']
        for i in range(len(constraints_multivariate['central_value'])):
            weight_i = np.zeros((len(observables), len(observables)))
            mean_i = np.zeros(len(observables))
            observable_indices_i = constraints_multivariate['observable_indices'][i]
            central_value_i = constraints_multivariate['central_value'][i]
            standard_deviation_i = constraints_multivariate['standard_deviation'][i]
            inverse_correlation_i = constraints_multivariate['inverse_correlation'][i]
            weight_i[np.ix_(observable_indices_i, observable_indices_i)] = inverse_correlation_i / np.outer(standard_deviation_i, standard_deviation_i)
            mean_i[observable_indices_i] = central_value_i
            weights.append(weight_i)
            means.append(mean_i)
        inv_cov = np.sum(weights, axis=0)
        # regularize inversion asuming inv_cov = D R D where R has unit diagonal, then invert R instead of inv_cov
        d = np.sqrt(np.diag(inv_cov))
        nonzero = d != 0  # unconstrained observables have zeros
        inv_cov = inv_cov[np.ix_(nonzero, nonzero)]
        d = d[nonzero]
        d2 = np.outer(d, d)
        R = inv_cov / d2
        inv_R = np.linalg.inv(R)
        cov = np.diag([np.nan] * len(nonzero))
        cov[np.ix_(nonzero, nonzero)] = inv_R / d2
        mean = cov @ np.sum([w @ m for w, m in zip(weights, means)], axis=0)
    else:
        cov[cov == np.inf] = np.nan
    std = np.sqrt(np.diag(cov))
    corr = cov / np.outer(std, std)

    for i, row_names in enumerate(cls._observable_names):
        row_measurements = Measurement.get_measurements(row_names, include_measurements=include_measurements)
        row_idx = [observables.index(o) for o in row_names]
        hash_val = hash_names(row_measurements, row_names)
        cls(
            hash_val=hash_val,
            data_type='central',
            data=jnp.array(mean[row_idx], dtype=jnp.float64),
            row_names=row_names,
            col_names=[],
        )
        cls(
            hash_val=hash_val,
            data_type='uncertainties',
            data=jnp.array(std[row_idx], dtype=jnp.float64),
            row_names=row_names,
            col_names=[],
        )
        for j in range(i, len(cls._observable_names)):
            col_names = cls._observable_names[j]
            col_measurements = Measurement.get_measurements(col_names, include_measurements=include_measurements)
            col_idx = [observables.index(o) for o in col_names]
            hash_val = hash_names(row_measurements, col_measurements, row_names, col_names)
            cls(
                hash_val=hash_val,
                data_type='correlations',
                data=jnp.array(corr[np.ix_(row_idx, col_idx)], dtype=jnp.float64),
                row_names=row_names,
                col_names=col_names,
            )

get_cov_scaled(include_measurements, row_names, col_names, std_exp_scaled_row, std_exp_scaled_col) classmethod

Retrieve the scaled covariance matrix for the specified combination of observables and measurements.

Parameters:

Name Type Description Default
include_measurements Iterable[str]

A list of measurement names to include in the retrieval.

required
row_names Iterable[str]

The names of the observables corresponding to the rows of the covariance matrix.

required
col_names Iterable[str]

The names of the observables corresponding to the columns of the covariance matrix.

required
std_exp_scaled_row ndarray

The experimental standard deviations for the row observables, scaled by any additional factors.

required
std_exp_scaled_col ndarray

The experimental standard deviations for the column observables, scaled by any additional factors.

required

Returns:

Type Description
ndarray

The scaled covariance matrix for the specified combination of observables and measurements.

Examples:

Retrieve the scaled covariance matrix for a specific combination of observables and measurements:

>>> cov_scaled = ExperimentalCorrelations.get_cov_scaled(
...     include_measurements=['measurement1'],
...     row_names=['observable1'],
...     col_names=['observable2'],
...     std_exp_scaled_row=np.array([0.5]),
...     std_exp_scaled_col=np.array([1.2])
... )
Source code in jelli/core/experimental_correlations.py
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
@classmethod
def get_cov_scaled(
    cls,
    include_measurements: Iterable[str],
    row_names: Iterable[str],
    col_names: Iterable[str],
    std_exp_scaled_row: np.ndarray,
    std_exp_scaled_col: np.ndarray,
):
    '''
    Retrieve the scaled covariance matrix for the specified combination of observables and measurements.

    Parameters
    ----------
    include_measurements : Iterable[str]
        A list of measurement names to include in the retrieval.
    row_names : Iterable[str]
        The names of the observables corresponding to the rows of the covariance matrix.
    col_names : Iterable[str]
        The names of the observables corresponding to the columns of the covariance matrix.
    std_exp_scaled_row : np.ndarray
        The experimental standard deviations for the row observables, scaled by any additional factors.
    std_exp_scaled_col : np.ndarray
        The experimental standard deviations for the column observables, scaled by any additional factors.

    Returns
    -------
    jnp.ndarray
        The scaled covariance matrix for the specified combination of observables and measurements.

    Examples
    --------
    Retrieve the scaled covariance matrix for a specific combination of observables and measurements:

    >>> cov_scaled = ExperimentalCorrelations.get_cov_scaled(
    ...     include_measurements=['measurement1'],
    ...     row_names=['observable1'],
    ...     col_names=['observable2'],
    ...     std_exp_scaled_row=np.array([0.5]),
    ...     std_exp_scaled_col=np.array([1.2])
    ... )
    '''
    row_measurements = Measurement.get_measurements(row_names, include_measurements=include_measurements)
    col_measurements = Measurement.get_measurements(col_names, include_measurements=include_measurements)
    hash_val = hash_names(row_measurements, col_measurements, row_names, col_names)
    if hash_val in cls._covariance_scaled:
        cov_scaled = cls._covariance_scaled[hash_val]
    else:
        corr = cls.get_data('correlations', include_measurements, row_names, col_names)
        if corr is None:
            raise ValueError(f"Correlation data for {row_names} and {col_names} not found.")
        cov_scaled = corr * np.outer(std_exp_scaled_row, std_exp_scaled_col)
        cov_scaled = jnp.array(cov_scaled, dtype=jnp.float64)
        cls._covariance_scaled[hash_val] = cov_scaled
    return cov_scaled

get_data(data_type, include_measurements, row_names, col_names=[]) classmethod

Retrieve the data array for the specified data type and combination of observables and measurements.

Parameters:

Name Type Description Default
data_type str

The type of data to retrieve. It can be correlations, central, or uncertainties.

required
include_measurements Iterable[str]

A list of measurement names to include in the retrieval.

required
row_names Iterable[str]

The names of the observables corresponding to the rows of the data array.

required
col_names Iterable[str]

The names of the observables corresponding to the columns of the data array. Default is an empty list.

[]

Returns:

Type Description
Optional[ndarray]

The data array for the specified data type and combination of observables and measurements, or None if not found.

Examples:

Retrieve the data array for a specific data type and combination of observables and measurements:

>>> data = ExperimentalCorrelations.get_data(
...     data_type='correlations',
...     include_measurements=['measurement1'],
...     row_names=['observable1'],
...     col_names=['observable2']
... )
Source code in jelli/core/experimental_correlations.py
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
342
343
344
@classmethod
def get_data(
    cls,
    data_type: str,
    include_measurements: Iterable[str],
    row_names: Iterable[str],
    col_names: Optional[Iterable[str]] = []
):
    '''
    Retrieve the data array for the specified data type and combination of observables and measurements.

    Parameters
    ----------
    data_type : str
        The type of data to retrieve. It can be `correlations`, `central`, or `uncertainties`.
    include_measurements : Iterable[str]
        A list of measurement names to include in the retrieval.
    row_names : Iterable[str]
        The names of the observables corresponding to the rows of the data array.
    col_names : Iterable[str], optional
        The names of the observables corresponding to the columns of the data array. Default is an empty list.

    Returns
    -------
    Optional[jnp.ndarray]
        The data array for the specified data type and combination of observables and measurements, or `None` if not found.

    Examples
    --------
    Retrieve the data array for a specific data type and combination of observables and measurements:

    >>> data = ExperimentalCorrelations.get_data(
    ...     data_type='correlations',
    ...     include_measurements=['measurement1'],
    ...     row_names=['observable1'],
    ...     col_names=['observable2']
    ... )
    '''
    row_measurements = Measurement.get_measurements(row_names, include_measurements=include_measurements)
    col_measurements = Measurement.get_measurements(col_names, include_measurements=include_measurements)
    hash_val = hash_names(row_measurements, col_measurements, row_names, col_names)
    hash_val_transposed = hash_names(col_measurements, row_measurements, col_names, row_names)
    if hash_val not in cls._instances[data_type] and hash_val_transposed not in cls._instances[data_type]:
        cls.compute(include_measurements)
    if hash_val in cls._instances[data_type]:
        return cls._instances[data_type][hash_val].data
    elif hash_val_transposed in cls._instances[data_type]:
        return cls._instances[data_type][hash_val_transposed].data.T
    else:
        return None

load() classmethod

Load observable names from all correlated observable sectors and initialize class-level attributes.

Returns:

Type Description
None

Examples:

Load observable names from all correlated observable sectors:

>>> ExperimentalCorrelations.load()
Source code in jelli/core/experimental_correlations.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
@classmethod
def load(cls) -> None:
    '''
    Load observable names from all correlated observable sectors and initialize class-level attributes.

    Returns
    -------
    None

    Examples
    --------
    Load observable names from all correlated observable sectors:

    >>> ExperimentalCorrelations.load()
    '''
    observable_names = []
    for observable_sector in ObservableSector.get_all():
        if observable_sector.observable_uncertainties is not None:
            observable_names.append(observable_sector.observable_names)
    cls._observable_names = observable_names
    cls._instances = {
        'correlations': {},
        'central': {},
        'uncertainties': {}
    }
    cls._covariance_scaled = {}