Standard Deviation

StandardDeviation is a curried constructor used to create a JS/JSON configuration object that represents a standard deviation from the mean operation.

const StandardDeviation =
  (datatype = "Number") =>
  (usePopulation = false) =>
  (operands = []) => ({
    tag: "StandardDeviation",
    operands,
    datatype,
    usePopulation,
  })
 
export default StandardDeviation
 

The first parameter to StandardDeviation determines the datatype of the return value of the standard deviation operation: Number (float) or Integer.

The second parameter determines toggles between standard deviation and population standard deviation. Defaults to false.

The third parameter is an array of operations, each of which must eventually evaluate to a numerical value. These are the operands for the function. They are squared, added together, and then the square root of the sum is returned.

The operand operations (add, subtract, etc.) may be nested to any depth, but the bottom (leaf) nodes must return values. Values are injected with injectors.

Because StandardDeviation is curried, we can simplify our code by partial application, for example, const Sd = StandardDeviation("Number"). This returns a function that takes the array of operand operations and returns a StandardDeviation configuration object for working with floating point numbers.

See below for an example.

The standardDeviation function

We pass our configuration object to composeOperators, which composes the operations and returns a single function. This function takes an optional argument and returns the result of the calculation as a Right, e.g., { right: 42 }, or a Left with an array of Error objects, { left: [Error] }.

const standardDeviation = operation => (arg? => Either<Array<Error>, result>)

composeOperators works by recursing down through the operation object, calling the correct operator function based on the tag (e.g., "StandardDeviation"), and composing the functions returned. composeOperators then returns this composed function. See composeOperators for a more detailed explanation.

The injectors are not called until this composed calculate function is run. Hence, evaluation is lazy: the values are not injected until the last moment.

See the injectors for a complete list of how values may be injected. See the list of operators for the full range of available mathematical operations.

Example

We use composeOperators to create a calculate function, passing it our StandardDeviation operation object. We call the calculate function and pass it an (optional) argument to run our operation(s) and calculate the product.

Here our operation includes five operands, all integer constants (using Constant)

Try leaving one of the numbers undefined to see an Error returned.