pub struct BayesianQuadrature { /* private fields */ }Expand description
Bayesian quadrature with an RBF covariance kernel and Gaussian integration measure.
The current implementation assumes a zero Gaussian-process prior mean. This is intentionally explicit rather than hidden behind a generic prior-mean abstraction.
Implementations§
Source§impl BayesianQuadrature
impl BayesianQuadrature
Sourcepub const fn new(
kernel: RbfKernel,
measure: GaussianMeasure,
jitter: f64,
) -> Self
pub const fn new( kernel: RbfKernel, measure: GaussianMeasure, jitter: f64, ) -> Self
Construct the first supported Bayesian quadrature configuration.
Examples found in repository?
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 // Prior over the integrand: zero-mean Gaussian process with an RBF kernel.
9 let kernel = RbfKernel::new(1.0, 1.0)?; // signal variance, length scale
10 // Integration measure p(x) = N(0, 1).
11 let measure = GaussianMeasure::new(0.0, 1.0)?;
12 // Jitter is an explicit, fixed diagonal regularizer. It is never escalated silently.
13 let quadrature = BayesianQuadrature::new(kernel, measure, 1.0e-10);
14
15 // Observe f(x) = cos(x) at seven nodes. Exactly, E[cos X] = exp(-1/2) for X ~ N(0, 1).
16 let nodes = [-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0];
17 let values: Vec<f64> = nodes.iter().copied().map(f64::cos).collect();
18 let exact = (-0.5_f64).exp();
19
20 let posterior = quadrature.posterior(&nodes, &values)?;
21 let standardized_error = (posterior.mean() - exact).abs() / posterior.standard_deviation();
22
23 println!("E[I | y] = {:.6}", posterior.mean());
24 println!(
25 "sd[I | y] = {:.3e}",
26 posterior.standard_deviation()
27 );
28 println!("exact integral = {exact:.6}");
29 println!("standardized error = {standardized_error:.3}");
30
31 // The posterior is honest about its own error here: the exact value lies well
32 // inside the reported uncertainty.
33 assert!((posterior.mean() - exact).abs() < 3.0 * posterior.standard_deviation());
34 Ok(())
35}More examples
15fn main() -> Result<(), Box<dyn std::error::Error>> {
16 let kernel = RbfKernel::new(1.0, 1.0)?;
17 let measure = GaussianMeasure::new(0.0, 1.0)?;
18 let quadrature = BayesianQuadrature::new(kernel, measure, 1.0e-10);
19 let active = ActiveBayesianQuadrature::new(quadrature);
20
21 // Start from three evaluations; allow up to six more from a fixed candidate grid,
22 // stopping early once the posterior variance of the integral drops below 1e-6.
23 let initial_nodes = [-1.0, 0.0, 1.0];
24 let initial_values: Vec<f64> = initial_nodes.iter().copied().map(integrand).collect();
25 let candidates: Vec<f64> = (0..=23).map(|i| -2.875 + 0.25 * f64::from(i)).collect();
26
27 let initial = quadrature.posterior(&initial_nodes, &initial_values)?;
28 println!("initial posterior variance = {:.3e}", initial.variance());
29
30 let result = active.run(
31 &initial_nodes,
32 &initial_values,
33 &candidates,
34 6,
35 1.0e-6,
36 integrand,
37 )?;
38
39 for step in result.steps() {
40 println!(
41 "x = {:+.3} predicted reduction = {:.3e} posterior variance = {:.3e}",
42 step.point(),
43 step.predicted_variance_reduction(),
44 step.posterior_variance(),
45 );
46 }
47
48 let exact = (1.0_f64 / 3.0).sqrt() * (-0.25_f64 / 3.0).exp();
49 let posterior = result.posterior();
50 println!("stopped because: {:?}", result.termination());
51 println!(
52 "E[I | y] = {:.6} ± {:.3e} (exact {exact:.6})",
53 posterior.mean(),
54 posterior.standard_deviation(),
55 );
56 Ok(())
57}Sourcepub const fn measure(&self) -> GaussianMeasure
pub const fn measure(&self) -> GaussianMeasure
Return the Gaussian integration measure.
Sourcepub const fn jitter(&self) -> f64
pub const fn jitter(&self) -> f64
Return the fixed diagonal jitter used by Gaussian conditioning.
Sourcepub fn posterior(
&self,
nodes: &[f64],
values: &[f64],
) -> Result<ScalarNormalPosterior, BayesianQuadratureError>
pub fn posterior( &self, nodes: &[f64], values: &[f64], ) -> Result<ScalarNormalPosterior, BayesianQuadratureError>
Compute the posterior distribution of the integral from observed function values.
For observations y = f(X) and zero prior mean,
posterior_mean = z^T (K + jitter I)^(-1) y
posterior_var = kappa - z^T (K + jitter I)^(-1) zThe inverse is never formed explicitly; both systems are solved from one reusable Cholesky factorization.
§Errors
Returns BayesianQuadratureError for invalid observations, conditioning
failures, or an invalid posterior variance.
Examples found in repository?
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 // Prior over the integrand: zero-mean Gaussian process with an RBF kernel.
9 let kernel = RbfKernel::new(1.0, 1.0)?; // signal variance, length scale
10 // Integration measure p(x) = N(0, 1).
11 let measure = GaussianMeasure::new(0.0, 1.0)?;
12 // Jitter is an explicit, fixed diagonal regularizer. It is never escalated silently.
13 let quadrature = BayesianQuadrature::new(kernel, measure, 1.0e-10);
14
15 // Observe f(x) = cos(x) at seven nodes. Exactly, E[cos X] = exp(-1/2) for X ~ N(0, 1).
16 let nodes = [-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0];
17 let values: Vec<f64> = nodes.iter().copied().map(f64::cos).collect();
18 let exact = (-0.5_f64).exp();
19
20 let posterior = quadrature.posterior(&nodes, &values)?;
21 let standardized_error = (posterior.mean() - exact).abs() / posterior.standard_deviation();
22
23 println!("E[I | y] = {:.6}", posterior.mean());
24 println!(
25 "sd[I | y] = {:.3e}",
26 posterior.standard_deviation()
27 );
28 println!("exact integral = {exact:.6}");
29 println!("standardized error = {standardized_error:.3}");
30
31 // The posterior is honest about its own error here: the exact value lies well
32 // inside the reported uncertainty.
33 assert!((posterior.mean() - exact).abs() < 3.0 * posterior.standard_deviation());
34 Ok(())
35}More examples
15fn main() -> Result<(), Box<dyn std::error::Error>> {
16 let kernel = RbfKernel::new(1.0, 1.0)?;
17 let measure = GaussianMeasure::new(0.0, 1.0)?;
18 let quadrature = BayesianQuadrature::new(kernel, measure, 1.0e-10);
19 let active = ActiveBayesianQuadrature::new(quadrature);
20
21 // Start from three evaluations; allow up to six more from a fixed candidate grid,
22 // stopping early once the posterior variance of the integral drops below 1e-6.
23 let initial_nodes = [-1.0, 0.0, 1.0];
24 let initial_values: Vec<f64> = initial_nodes.iter().copied().map(integrand).collect();
25 let candidates: Vec<f64> = (0..=23).map(|i| -2.875 + 0.25 * f64::from(i)).collect();
26
27 let initial = quadrature.posterior(&initial_nodes, &initial_values)?;
28 println!("initial posterior variance = {:.3e}", initial.variance());
29
30 let result = active.run(
31 &initial_nodes,
32 &initial_values,
33 &candidates,
34 6,
35 1.0e-6,
36 integrand,
37 )?;
38
39 for step in result.steps() {
40 println!(
41 "x = {:+.3} predicted reduction = {:.3e} posterior variance = {:.3e}",
42 step.point(),
43 step.predicted_variance_reduction(),
44 step.posterior_variance(),
45 );
46 }
47
48 let exact = (1.0_f64 / 3.0).sqrt() * (-0.25_f64 / 3.0).exp();
49 let posterior = result.posterior();
50 println!("stopped because: {:?}", result.termination());
51 println!(
52 "E[I | y] = {:.6} ± {:.3e} (exact {exact:.6})",
53 posterior.mean(),
54 posterior.standard_deviation(),
55 );
56 Ok(())
57}Trait Implementations§
Source§impl Clone for BayesianQuadrature
impl Clone for BayesianQuadrature
Source§fn clone(&self) -> BayesianQuadrature
fn clone(&self) -> BayesianQuadrature
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for BayesianQuadrature
Source§impl Debug for BayesianQuadrature
impl Debug for BayesianQuadrature
Source§impl PartialEq for BayesianQuadrature
impl PartialEq for BayesianQuadrature
impl StructuralPartialEq for BayesianQuadrature
Auto Trait Implementations§
impl Freeze for BayesianQuadrature
impl RefUnwindSafe for BayesianQuadrature
impl Send for BayesianQuadrature
impl Sync for BayesianQuadrature
impl Unpin for BayesianQuadrature
impl UnsafeUnpin for BayesianQuadrature
impl UnwindSafe for BayesianQuadrature
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Scalar for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.