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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use crate::prelude::*;
use bigdecimal::FromPrimitive;
use nu_data::value::compute_values;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{hir::Operator, Dictionary, Primitive, Signature, UntaggedValue, Value};

pub struct SubCommand;

impl WholeStreamCommand for SubCommand {
    fn name(&self) -> &str {
        "math variance"
    }

    fn signature(&self) -> Signature {
        Signature::build("math variance").switch("sample", "calculate sample variance", Some('s'))
    }

    fn usage(&self) -> &str {
        "Finds the variance of a list of numbers or tables"
    }

    fn run(&self, mut args: CommandArgs) -> Result<OutputStream, ShellError> {
        let sample: bool = args.has_flag("sample");
        let values: Vec<Value> = args.input.drain_vec();
        let name = args.call_info.name_tag;

        let n = if sample {
            values.len() - 1
        } else {
            values.len()
        };

        let res = if values.iter().all(|v| v.is_primitive()) {
            compute_variance(&values, n, &name)
        } else {
            // If we are not dealing with Primitives, then perhaps we are dealing with a table
            // Create a key for each column name
            let mut column_values = IndexMap::new();
            for value in values {
                if let UntaggedValue::Row(row_dict) = &value.value {
                    for (key, value) in &row_dict.entries {
                        column_values
                            .entry(key.clone())
                            .and_modify(|v: &mut Vec<Value>| v.push(value.clone()))
                            .or_insert(vec![value.clone()]);
                    }
                }
            }
            // The mathematical function operates over the columns of the table
            let mut column_totals = IndexMap::new();
            for (col_name, col_vals) in column_values {
                if let Ok(out) = compute_variance(&col_vals, n, &name) {
                    column_totals.insert(col_name, out);
                }
            }

            if column_totals.keys().len() == 0 {
                return Err(ShellError::labeled_error(
                    "Attempted to compute values that can't be operated on",
                    "value appears here",
                    name.span,
                ));
            }

            Ok(UntaggedValue::Row(Dictionary {
                entries: column_totals,
            })
            .into_untagged_value())
        }?;

        if res.value.is_table() {
            Ok(OutputStream::from(
                res.table_entries().cloned().collect::<Vec<_>>(),
            ))
        } else {
            Ok(OutputStream::one(res))
        }
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Get the variance of a list of numbers",
                example: "echo [1 2 3 4 5] | math variance",
                result: Some(vec![UntaggedValue::decimal_from_float(
                    2.0,
                    Span::unknown(),
                )
                .into()]),
            },
            Example {
                description: "Get the sample variance of a list of numbers",
                example: "echo [1 2 3 4 5] | math variance -s",
                result: Some(vec![UntaggedValue::decimal_from_float(
                    2.5,
                    Span::unknown(),
                )
                .into()]),
            },
        ]
    }
}

fn sum_of_squares(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
    let n = BigDecimal::from_usize(values.len()).ok_or_else(|| {
        ShellError::labeled_error(
            "could not convert to big decimal",
            "could not convert to big decimal",
            &name.span,
        )
    })?;
    let mut sum_x = UntaggedValue::int(0).into_untagged_value();
    let mut sum_x2 = UntaggedValue::int(0).into_untagged_value();
    for value in values {
        let v = match value {
            Value {
                value: UntaggedValue::Primitive(Primitive::Filesize(num)),
                ..
            } => {
                UntaggedValue::from(Primitive::Int(*num as i64))
            },
            Value {
                value: UntaggedValue::Primitive(num),
                ..
            } => {
                UntaggedValue::from(num.clone())
            },
            _ => {
                return Err(ShellError::labeled_error(
                    "Attempted to compute the sum of squared values of a value that cannot be summed or squared.",
                    "value appears here",
                    value.tag.span,
                ))
            }
        };
        let v_squared = compute_values(Operator::Multiply, &v, &v);
        match v_squared {
            // X^2
            Ok(x2) => {
                sum_x2 = match compute_values(Operator::Plus, &sum_x2, &x2) {
                    Ok(v) => v.into_untagged_value(),
                    Err((left_type, right_type)) => {
                        return Err(ShellError::coerce_error(
                            left_type.spanned(name.span),
                            right_type.spanned(name.span),
                        ))
                    }
                };
            }
            Err((left_type, right_type)) => {
                return Err(ShellError::coerce_error(
                    left_type.spanned(value.tag.span),
                    right_type.spanned(value.tag.span),
                ))
            }
        };
        sum_x = match compute_values(Operator::Plus, &sum_x, &v) {
            Ok(v) => v.into_untagged_value(),
            Err((left_type, right_type)) => {
                return Err(ShellError::coerce_error(
                    left_type.spanned(name.span),
                    right_type.spanned(name.span),
                ))
            }
        };
    }

    let sum_x_squared = match compute_values(Operator::Multiply, &sum_x, &sum_x) {
        Ok(v) => v.into_untagged_value(),
        Err((left_type, right_type)) => {
            return Err(ShellError::coerce_error(
                left_type.spanned(name.span),
                right_type.spanned(name.span),
            ))
        }
    };
    let sum_x_squared_div_n = match compute_values(Operator::Divide, &sum_x_squared, &n.into()) {
        Ok(v) => v.into_untagged_value(),
        Err((left_type, right_type)) => {
            return Err(ShellError::coerce_error(
                left_type.spanned(name.span),
                right_type.spanned(name.span),
            ))
        }
    };
    let ss = match compute_values(Operator::Minus, &sum_x2, &sum_x_squared_div_n) {
        Ok(v) => v.into_untagged_value(),
        Err((left_type, right_type)) => {
            return Err(ShellError::coerce_error(
                left_type.spanned(name.span),
                right_type.spanned(name.span),
            ))
        }
    };

    Ok(ss)
}

#[cfg(test)]
pub fn variance(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
    compute_variance(values, values.len(), name)
}

pub fn compute_variance(values: &[Value], n: usize, name: &Tag) -> Result<Value, ShellError> {
    let ss = sum_of_squares(values, name)?;
    let n = BigDecimal::from_usize(n).ok_or_else(|| {
        ShellError::labeled_error(
            "could not convert to big decimal",
            "could not convert to big decimal",
            &name.span,
        )
    })?;
    let variance = compute_values(Operator::Divide, &ss, &n.into());
    match variance {
        Ok(value) => Ok(value.into_value(name)),
        Err((_, _)) => Err(ShellError::labeled_error(
            "could not calculate variance of non-integer or unrelated types",
            "source",
            name,
        )),
    }
}

#[cfg(test)]
mod tests {
    use super::ShellError;
    use super::SubCommand;

    #[test]
    fn examples_work_as_expected() -> Result<(), ShellError> {
        use crate::examples::test as test_examples;

        test_examples(SubCommand {})
    }
}