2021-11-05 17:58:40 +00:00
|
|
|
use super::variance::compute_variance as variance;
|
|
|
|
use crate::math::utils::run_with_function;
|
|
|
|
use nu_protocol::ast::Call;
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
2022-11-09 21:55:05 +00:00
|
|
|
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Span, Type, Value};
|
2021-11-05 17:58:40 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SubCommand;
|
|
|
|
|
|
|
|
impl Command for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"math stddev"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2021-11-17 04:22:37 +00:00
|
|
|
Signature::build("math stddev")
|
2022-11-09 21:55:05 +00:00
|
|
|
.input_output_types(vec![(Type::List(Box::new(Type::Number)), Type::Number)])
|
|
|
|
.switch(
|
|
|
|
"sample",
|
|
|
|
"calculate sample standard deviation (i.e. using N-1 as the denominator)",
|
|
|
|
Some('s'),
|
|
|
|
)
|
2021-11-17 04:22:37 +00:00
|
|
|
.category(Category::Math)
|
2021-11-05 17:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2023-03-01 05:33:02 +00:00
|
|
|
"Returns the standard deviation of a list of numbers, or of each column in a table."
|
2021-11-05 17:58:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 21:33:32 +00:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
2022-08-24 09:16:47 +00:00
|
|
|
vec![
|
|
|
|
"SD",
|
|
|
|
"standard",
|
|
|
|
"deviation",
|
|
|
|
"dispersion",
|
|
|
|
"variation",
|
|
|
|
"statistics",
|
|
|
|
]
|
2022-04-18 21:33:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 17:58:40 +00:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
_engine_state: &EngineState,
|
|
|
|
_stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
2023-02-05 21:17:46 +00:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-11-05 17:58:40 +00:00
|
|
|
let sample = call.has_flag("sample");
|
|
|
|
run_with_function(call, input, compute_stddev(sample))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
2022-11-09 21:55:05 +00:00
|
|
|
description: "Compute the standard deviation of a list of numbers",
|
2021-11-05 17:58:40 +00:00
|
|
|
example: "[1 2 3 4 5] | math stddev",
|
2022-12-24 13:41:57 +00:00
|
|
|
result: Some(Value::test_float(std::f64::consts::SQRT_2)),
|
2021-11-05 17:58:40 +00:00
|
|
|
},
|
|
|
|
Example {
|
2022-11-09 21:55:05 +00:00
|
|
|
description: "Compute the sample standard deviation of a list of numbers",
|
2023-10-19 16:17:42 +00:00
|
|
|
example: "[1 2 3 4 5] | math stddev --sample",
|
2022-12-24 13:41:57 +00:00
|
|
|
result: Some(Value::test_float(1.5811388300841898)),
|
2021-11-05 17:58:40 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:47:46 +00:00
|
|
|
pub fn compute_stddev(sample: bool) -> impl Fn(&[Value], Span, Span) -> Result<Value, ShellError> {
|
|
|
|
move |values: &[Value], span: Span, head: Span| {
|
2023-09-03 14:27:29 +00:00
|
|
|
// variance() produces its own usable error, so we can use `?` to propagated the error.
|
|
|
|
let variance = variance(sample)(values, span, head)?;
|
|
|
|
let val_span = variance.span();
|
2021-11-05 17:58:40 +00:00
|
|
|
match variance {
|
2023-09-03 14:27:29 +00:00
|
|
|
Value::Float { val, .. } => Ok(Value::float(val.sqrt(), val_span)),
|
|
|
|
Value::Int { val, .. } => Ok(Value::float((val as f64).sqrt(), val_span)),
|
|
|
|
other => Ok(other),
|
2021-11-05 17:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(SubCommand {})
|
|
|
|
}
|
|
|
|
}
|