23
23
//! // sample 10_000 times per iteration
24
24
//! for _ in 0..10_000 {
25
25
//! grid.sample(&mut rng, &mut sample);
26
- //!
26
+ //!
27
27
//! if let Sample::Continuous(_cont_weight, xs) = &sample {
28
28
//! grid.add_training_sample(&sample, f(xs)).unwrap();
29
29
//! }
30
30
//! }
31
- //!
31
+ //!
32
32
//! grid.update(1.5, 1.5);
33
- //!
33
+ //!
34
34
//! println!(
35
35
//! "Integral at iteration {}: {}",
36
36
//! iteration,
39
39
//! }
40
40
//! ```
41
41
42
+ use bincode:: { Decode , Encode } ;
42
43
use rand:: { Rng , RngCore , SeedableRng } ;
43
44
use rand_xoshiro:: Xoshiro256StarStar ;
44
45
use serde:: { Deserialize , Serialize } ;
@@ -60,7 +61,7 @@ use crate::domains::float::{ConstructibleFloat, Real, RealNumberLike};
60
61
///
61
62
/// The accumulator also stores which samples yielded the highest weight thus far.
62
63
/// This can be used to study the input that impacted the average and error the most.
63
- #[ derive( Debug , Default , Clone , Serialize , Deserialize ) ]
64
+ #[ derive( Debug , Default , Clone , Serialize , Deserialize , Encode , Decode ) ]
64
65
pub struct StatisticsAccumulator < T : Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd >
65
66
{
66
67
sum : T ,
@@ -383,7 +384,7 @@ impl<T: Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd> Statisti
383
384
/// and contains the weight and the list of sample points.
384
385
/// If the sample comes from a [DiscreteGrid], it is the variant [Discrete](Sample::Discrete) and contains
385
386
/// the weight, the bin and the subsample if the bin has a nested grid.
386
- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
387
+ #[ derive( Debug , Clone , Serialize , Deserialize , Encode , Decode ) ]
387
388
pub enum Sample < T : Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd > {
388
389
Continuous ( T , Vec < T > ) ,
389
390
Discrete ( T , usize , Option < Box < Sample < T > > > ) ,
@@ -453,22 +454,22 @@ impl<T: Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd> Sample<T
453
454
/// // sample 10_000 times per iteration
454
455
/// for _ in 0..10_000 {
455
456
/// grid.sample(&mut rng, &mut sample);
456
- ///
457
+ ///
457
458
/// if let Sample::Continuous(_cont_weight, xs) = &sample {
458
459
/// grid.add_training_sample(&sample, f(xs)).unwrap();
459
460
/// }
460
461
/// }
461
- ///
462
+ ///
462
463
/// grid.update(1.5, 1.5);
463
- ///
464
+ ///
464
465
/// println!(
465
466
/// "Integral at iteration {}: {}",
466
467
/// iteration,
467
468
/// grid.get_statistics().format_uncertainty()
468
469
/// );
469
470
/// }
470
471
/// ```
471
- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
472
+ #[ derive( Debug , Clone , Serialize , Deserialize , Encode , Decode ) ]
472
473
pub enum Grid < T : Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd > {
473
474
Continuous ( ContinuousGrid < T > ) ,
474
475
Discrete ( DiscreteGrid < T > ) ,
@@ -539,7 +540,7 @@ impl<T: Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd> Grid<T>
539
540
}
540
541
}
541
542
/// A bin of a discrete grid, which may contain a subgrid.
542
- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
543
+ #[ derive( Debug , Clone , Serialize , Deserialize , Encode , Decode ) ]
543
544
pub struct Bin < T : Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd > {
544
545
pub pdf : T ,
545
546
pub accumulator : StatisticsAccumulator < T > ,
@@ -578,7 +579,7 @@ impl<T: Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd> Bin<T> {
578
579
/// of a sample from the grid landing in a bin is proportional to its
579
580
/// average value if training happens on the average, or to its
580
581
/// variance (recommended).
581
- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
582
+ #[ derive( Debug , Clone , Serialize , Deserialize , Encode , Decode ) ]
582
583
pub struct DiscreteGrid < T : Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd > {
583
584
pub bins : Vec < Bin < T > > ,
584
585
pub accumulator : StatisticsAccumulator < T > ,
@@ -797,7 +798,7 @@ impl<T: Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd> Discrete
797
798
/// of a sample from the grid landing in a bin is proportional to its
798
799
/// average value if training happens on the average, or to its
799
800
/// variance (recommended).
800
- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
801
+ #[ derive( Debug , Clone , Serialize , Deserialize , Encode , Decode ) ]
801
802
pub struct ContinuousGrid < T : Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd > {
802
803
pub continuous_dimensions : Vec < ContinuousDimension < T > > ,
803
804
pub accumulator : StatisticsAccumulator < T > ,
@@ -925,7 +926,7 @@ impl<T: Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd> Continuo
925
926
}
926
927
927
928
/// A dimension in a continuous grid that contains a partitioning.
928
- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
929
+ #[ derive( Debug , Clone , Serialize , Deserialize , Encode , Decode ) ]
929
930
pub struct ContinuousDimension < T : Real + ConstructibleFloat + Copy + RealNumberLike + PartialOrd > {
930
931
pub partitioning : Vec < T > ,
931
932
bin_accumulator : Vec < StatisticsAccumulator < T > > ,
0 commit comments