1
2
3
4
5
6
7
8
9
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
use smartnoise_validator::errors::*;
use crate::NodeArguments;
use smartnoise_validator::base::ReleaseNode;
use smartnoise_validator::utilities::take_argument;
use crate::components::Evaluable;
use smartnoise_validator::{proto, Float};
use ndarray::ArrayD;
use crate::components::mean::mean;
use std::convert::TryFrom;
impl Evaluable for proto::RawMoment {
fn evaluate(&self, _privacy_definition: &Option<proto::PrivacyDefinition>, mut arguments: NodeArguments) -> Result<ReleaseNode> {
let data = take_argument(&mut arguments, "data")?.array()?.float()?;
Ok(ReleaseNode::new(raw_moment(&data, self.order)?.into()))
}
}
pub fn raw_moment(data: &ArrayD<Float>, order: u32) -> Result<ArrayD<Float>> {
let mut data = data.clone();
let k = i32::try_from(order)
.map_err(|_| Error::from("order: invalid size"))?;
data.gencolumns_mut().into_iter()
.for_each(|mut column| column.iter_mut()
.for_each(|v| *v = v.powi(k)));
mean(&data)
}