|
1 | 1 | """ |
2 | 2 | This module provides functions for computing distances between observation samples and reference samples with distance |
3 | 3 | distributions within the reference samples for hypothesis testing. |
4 | | -
|
5 | | -Functions: |
6 | | ----------- |
7 | | -- bootstrap_comparison: Computes distance between observed and reference samples and generates a distribution of null |
8 | | - sample distances by bootstrapping for hypothesis testing. |
9 | | -- mmd_comparison_from_summaries: Computes the Maximum Mean Discrepancy (MMD) between observed and reference summaries |
10 | | - and generates a distribution of MMD values under the null hypothesis to assess model misspecification. |
11 | | -- mmd_comparison: Computes the Maximum Mean Discrepancy (MMD) between observed and reference data and generates a |
12 | | - distribution of MMD values under the null hypothesis to assess model misspecification. |
13 | | -
|
14 | | -Dependencies: |
15 | | -------------- |
16 | | -- numpy: For numerical operations. |
17 | | -- keras.ops: For converting data to numpy and tensor formats. |
18 | | -- bayesflow.networks: Provides the `SummaryNetwork` class for extracting summary statistics. |
19 | | -- bayesflow.approximators: Provides the `Approximator` class for extracting summary statistics. |
20 | | -- bayesflow.metrics: Provides the `maximum_mean_discrepancy` function for computing the MMD. |
21 | 4 | """ |
22 | 5 |
|
23 | | -import typing |
| 6 | +from collections.abc import Mapping, Callable |
24 | 7 |
|
25 | 8 | import numpy as np |
26 | 9 | from keras.ops import convert_to_numpy, convert_to_tensor |
27 | 10 |
|
28 | 11 | from bayesflow.approximators import ContinuousApproximator |
29 | 12 | from bayesflow.metrics.functional import maximum_mean_discrepancy |
30 | | -from bayesflow.networks import SummaryNetwork |
31 | 13 | from bayesflow.types import Tensor |
32 | 14 |
|
33 | 15 |
|
34 | 16 | def bootstrap_comparison( |
35 | 17 | observed_samples: np.ndarray, |
36 | 18 | reference_samples: np.ndarray, |
37 | | - comparison_fn: typing.Callable[[Tensor, Tensor], Tensor], |
| 19 | + comparison_fn: Callable[[Tensor, Tensor], Tensor], |
38 | 20 | num_null_samples: int = 100, |
39 | 21 | ) -> tuple[float, np.ndarray]: |
40 | | - """Compute distance between observed and reference samples and generated a distribution of null sample distances by |
41 | | - bootstrapping for hypothesis testing. |
| 22 | + """Computes the distance between observed and reference samples and generates a distribution of null sample |
| 23 | + distances by bootstrapping for hypothesis testing. |
42 | 24 |
|
43 | 25 | Parameters |
44 | 26 | ---------- |
45 | 27 | observed_samples : np.ndarray) |
46 | 28 | Observed samples, shape (num_observed, ...). |
47 | 29 | reference_samples : np.ndarray |
48 | 30 | Reference samples, shape (num_reference, ...). |
49 | | - comparison_fn : typing.Callable[[Tensor, Tensor], Tensor] |
| 31 | + comparison_fn : Callable[[Tensor, Tensor], Tensor] |
50 | 32 | Function to compute the distance metric. |
51 | 33 | num_null_samples : int |
52 | 34 | Number of null samples to generate for hypothesis testing. Default is 100. |
@@ -98,108 +80,76 @@ def bootstrap_comparison( |
98 | 80 | return distance_observed, distance_null_samples |
99 | 81 |
|
100 | 82 |
|
101 | | -def mmd_comparison_from_summaries( |
102 | | - observed_summaries: np.ndarray, |
103 | | - reference_summaries: np.ndarray, |
| 83 | +def summary_space_comparison( |
| 84 | + observed_data: Mapping[str, np.ndarray], |
| 85 | + reference_data: Mapping[str, np.ndarray], |
| 86 | + approximator: ContinuousApproximator, |
104 | 87 | num_null_samples: int = 100, |
| 88 | + comparison_fn: Callable = maximum_mean_discrepancy, |
| 89 | + **kwargs, |
105 | 90 | ) -> tuple[float, np.ndarray]: |
106 | | - """Computes the Maximum Mean Discrepancy (MMD) between observed and reference summaries and generates a distribution |
107 | | - of MMD values under the null hypothesis to assess model misspecification. |
108 | | -
|
109 | | - [1] M. Schmitt, P.-C. Bürkner, U. Köthe, and S. T. Radev, "Detecting model misspecification in amortized Bayesian |
110 | | - inference with neural networks," arXiv e-prints, Dec. 2021, Art. no. arXiv:2112.08866. |
111 | | - URL: https://arxiv.org/abs/2112.08866 |
| 91 | + """Computes the distance between observed and reference data in the summary space and |
| 92 | + generates a distribution of distance values under the null hypothesis to assess model misspecification. |
112 | 93 |
|
113 | | -
|
114 | | - Parameters |
115 | | - ---------- |
116 | | - observed_summary : np.ndarray |
117 | | - Summary statistics of observed data, shape (num_observed, ...). |
118 | | - reference_summary : np.ndarray |
119 | | - Summary statistics of reference data, shape (num_reference, ...). |
120 | | - num_null_samples : int |
121 | | - Number of null samples to generate for hypothesis testing. Default is 100. |
122 | | -
|
123 | | - Returns |
124 | | - ------- |
125 | | - mmd_observed : float |
126 | | - The MMD value between observed and reference summaries. |
127 | | - mmd_null : np.ndarray |
128 | | - A distribution of MMD values under the null hypothesis. |
129 | | - """ |
130 | | - mmd_observed, mmd_null_samples = bootstrap_comparison( |
131 | | - observed_samples=observed_summaries, |
132 | | - reference_samples=reference_summaries, |
133 | | - comparison_fn=maximum_mean_discrepancy, |
134 | | - num_null_samples=num_null_samples, |
135 | | - ) |
136 | | - |
137 | | - return mmd_observed, mmd_null_samples |
138 | | - |
139 | | - |
140 | | -def mmd_comparison( |
141 | | - observed_data: np.ndarray, |
142 | | - reference_data: np.ndarray, |
143 | | - approximator: ContinuousApproximator | SummaryNetwork, |
144 | | - num_null_samples: int = 100, |
145 | | -) -> tuple[float, np.ndarray]: |
146 | | - """Computes the Maximum Mean Discrepancy (MMD) between observed and reference data and generates a distribution of |
147 | | - MMD values under the null hypothesis to assess model misspecification. |
| 94 | + By default, the Maximum Mean Discrepancy (MMD) is used as a distance function. |
148 | 95 |
|
149 | 96 | [1] M. Schmitt, P.-C. Bürkner, U. Köthe, and S. T. Radev, "Detecting model misspecification in amortized Bayesian |
150 | 97 | inference with neural networks," arXiv e-prints, Dec. 2021, Art. no. arXiv:2112.08866. |
151 | 98 | URL: https://arxiv.org/abs/2112.08866 |
152 | 99 |
|
153 | | -
|
154 | 100 | Parameters |
155 | 101 | ---------- |
156 | | - observed_data : np.ndarray |
157 | | - Observed data, shape (num_observed, ...). |
158 | | - reference_data : np.ndarray |
159 | | - Reference data, shape (num_reference, ...). |
160 | | - approximator : ContinuousApproximator or SummaryNetwork |
161 | | - An instance of the ContinuousApproximator or SummaryNetwork class use to extract summary statistics from data. |
162 | | - num_null_samples : int |
| 102 | + observed_data : dict[str, np.ndarray] |
| 103 | + Dictionary of observed data as NumPy arrays, which will be preprocessed by the approximators adapter and passed |
| 104 | + through its summary network. |
| 105 | + reference_data : dict[str, np.ndarray] |
| 106 | + Dictionary of reference data as NumPy arrays, which will be preprocessed by the approximators adapter and passed |
| 107 | + through its summary network. |
| 108 | + approximator : ContinuousApproximator |
| 109 | + An instance of :py:class:`~bayesflow.approximators.ContinuousApproximator` used to compute summary statistics |
| 110 | + from the data. |
| 111 | + num_null_samples : int, optional |
163 | 112 | Number of null samples to generate for hypothesis testing. Default is 100. |
| 113 | + comparison_fn : Callable, optional |
| 114 | + Distance function to compare the data in the summary space. |
| 115 | + **kwargs : dict |
| 116 | + Additional keyword arguments for the adapter and sampling process. |
164 | 117 |
|
165 | 118 | Returns |
166 | 119 | ------- |
167 | | - mmd_observed : float |
168 | | - The MMD value between observed and reference data. |
169 | | - mmd_null : np.ndarray |
| 120 | + distance_observed : float |
| 121 | + The MMD value between observed and reference summaries. |
| 122 | + distance_null : np.ndarray |
170 | 123 | A distribution of MMD values under the null hypothesis. |
171 | 124 |
|
172 | | - Raises: |
| 125 | + Raises |
173 | 126 | ------ |
174 | 127 | ValueError |
175 | | - - If the shapes of observed and reference data do not match on dimensions besides the first one. |
176 | | - - If approximator is not an instance of ContinuousApproximator or SummaryNetwork. |
| 128 | + If approximator is not an instance of ContinuousApproximator or does not have a summary network. |
177 | 129 | """ |
178 | | - if observed_data.shape[1:] != reference_data.shape[1:]: |
| 130 | + |
| 131 | + if not isinstance(approximator, ContinuousApproximator): |
| 132 | + raise ValueError("The approximator must be an instance of ContinuousApproximator.") |
| 133 | + |
| 134 | + if not hasattr(approximator, "summary_network") or approximator.summary_network is None: |
| 135 | + comparison_fn_name = ( |
| 136 | + "bayesflow.metrics.functional.maximum_mean_discrepancy" |
| 137 | + if comparison_fn is maximum_mean_discrepancy |
| 138 | + else comparison_fn.__name__ |
| 139 | + ) |
179 | 140 | raise ValueError( |
180 | | - f"Expected observed and reference data to have the same shape, " |
181 | | - f"but got {observed_data.shape[1:]} != {reference_data.shape[1:]}." |
| 141 | + "The approximator must have a summary network. If you have manually crafted summary " |
| 142 | + "statistics, or want to compare raw data and not summary statistics, please use the " |
| 143 | + f"`bootstrap_comparison` function with `comparison_fn={comparison_fn_name}` on the respective arrays." |
182 | 144 | ) |
| 145 | + observed_summaries = convert_to_numpy(approximator.summary_outputs(observed_data)) |
| 146 | + reference_summaries = convert_to_numpy(approximator.summary_outputs(reference_data)) |
183 | 147 |
|
184 | | - if isinstance(approximator, ContinuousApproximator): |
185 | | - if approximator.summary_network is not None: |
186 | | - observed_data_tensor: Tensor = convert_to_tensor(observed_data) |
187 | | - reference_data_tensor: Tensor = convert_to_tensor(reference_data) |
188 | | - observed_summaries: np.ndarray = convert_to_numpy(approximator.summary_network(observed_data_tensor)) |
189 | | - reference_summaries: np.ndarray = convert_to_numpy(approximator.summary_network(reference_data_tensor)) |
190 | | - else: |
191 | | - observed_summaries: np.ndarray = observed_data |
192 | | - reference_summaries: np.ndarray = reference_data |
193 | | - elif isinstance(approximator, SummaryNetwork): |
194 | | - observed_data_tensor: Tensor = convert_to_tensor(observed_data) |
195 | | - reference_data_tensor: Tensor = convert_to_tensor(reference_data) |
196 | | - observed_summaries: np.ndarray = convert_to_numpy(approximator(observed_data_tensor)) |
197 | | - reference_summaries: np.ndarray = convert_to_numpy(approximator(reference_data_tensor)) |
198 | | - else: |
199 | | - raise ValueError("The approximator must be an instance of ContinuousApproximator or SummaryNetwork.") |
200 | | - |
201 | | - mmd_observed, mmd_null = mmd_comparison_from_summaries( |
202 | | - observed_summaries, reference_summaries, num_null_samples=num_null_samples |
| 148 | + distance_observed, distance_null = bootstrap_comparison( |
| 149 | + observed_samples=observed_summaries, |
| 150 | + reference_samples=reference_summaries, |
| 151 | + comparison_fn=comparison_fn, |
| 152 | + num_null_samples=num_null_samples, |
203 | 153 | ) |
204 | 154 |
|
205 | | - return mmd_observed, mmd_null |
| 155 | + return distance_observed, distance_null |
0 commit comments