class meansdev


This class has been designed for practical use in a statistical processing environment. Lengthy run-time checks have therefore been avoided. It calculates means, population and sample variances, and population and sample standard deviations. It is quick and lightweight to use.


This class was originally inspired by a function (by an unnamed third party) that worked over large arrays and generated huge arrays of x, x-squared etc. to generate the results. By contrast, this class uses very little memory and coughs up its answers cheaply.

It was first developed as a C++ class, with slightly different efficiency and usability goals. C++ buffs will know what I mean when I say that all its functions were inline. It was pretty slick.

This version aims to sit well within the PHP environment, whilst emulating the efficiency of its C++ ancestor. To that end, there is relatively little error checking: users of classes like these know what they want and they know the data they're dealing with.

Using the class

You should see meansdev_test.php for an example program.

Note that if you want to use meansdev at the command-line interface, you must use its namespace qualification explicitly. This is because namespace aliases work only within self-contained PHP scripts, which are compiled as a unit.

Customization: conversion to string and array

You do not need to use the asString() or as_array() methods, but if you do, you might find the following helpful.

Both conversion functions convert some or all of the calculations this class offers to a result set. In the case of the __toString() method, they are converted to a single string, as you might expect. as_array() converts to an associative array. Each instance of meansdev can be customized to do this as you prefer - calculating just the results you need and (in the case of string conversion) into your chosen format.

Both string and array conversions are based on the $formats public member variable of the meansdev instance. This member variable can be set in three ways, depending on how you want to do things:

  1. Generate all meansdev instances alike
    This is achieved by customizing the static class variable meansdev::$default_format.
  2. Generate some meansdev instances according to one form, and some in another
    This is best achieved by making subclasses and overriding the constructor argument.
  3. Generate a one-off special meansdev instance with unique behaviour
    This is best achieved either by providing a constructor argument or by assigning to the public $formats member variable directly.

In the case of array conversion, the class uses only the keys of the $formats array, to generate the keys of the result array that is returned. In the case of string conversion, the array keys determine the value to be converted and the array values are sprintf formats.