stats.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 882 行 · 第 1/2 页

PHP
882
字号
<?php//// +----------------------------------------------------------------------+// | PHP Version 4                                                        |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2003 The PHP Group                                |// +----------------------------------------------------------------------+// | This source file is subject to version 2.0 of the PHP license,       |// | that is bundled with this package in the file LICENSE, and is        |// | available at through the world-wide-web at                           |// | http://www.php.net/license/2_02.txt.                                 |// | If you did not receive a copy of the PHP license and are unable to   |// | obtain it through the world-wide-web, please send a note to          |// | license@php.net so we can mail you a copy immediately.               |// +----------------------------------------------------------------------+// | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |// +----------------------------------------------------------------------+//// $Id: Stats.php,v 1.10 2003/05/16 22:03:03 jmcastagnetto Exp $//include_once "PEAR.php";/** * @package Math_Stats */// Constants for defining the statistics to calculate /*{{{*//** * STATS_BASIC to generate the basic descriptive statistics */define("STATS_BASIC", 1);/** * STATS_FULL to generate also higher moments, mode, median, etc. */define("STATS_FULL", 2);/*}}}*/// Constants describing the data set format /*{{{*//** * STATS_DATA_SIMPLE for an array of numeric values * e.g. $data = array(2,3,4,5,1,1,6); */define("STATS_DATA_SIMPLE", 0);/** * STATS_DATA_CUMMULATIVE for an associative array of frequency values, * where in each array entry, the index is the data point and the * value the count (frequency): * e.g. $data = array(3=>4, 2.3=>5, 1.25=>6, 0.5=>3) */define("STATS_DATA_CUMMULATIVE", 1);/*}}}*/// Constants defining how to handle nulls /*{{{*//** * STATS_REJECT_NULL, reject data sets with null values. * Any non-numeric value is considered a null in this context. */define("STATS_REJECT_NULL", -1);/** * STATS_IGNORE_NULL, ignore null values and prune them from the data. * Any non-numeric value is considered a null in this context. */define("STATS_IGNORE_NULL", -2);/** * STATS_USE_NULL_AS_ZERO, assign the value of 0 (zero) to null values. * Any non-numeric value is considered a null in this context. */define("STATS_USE_NULL_AS_ZERO", -3);/*}}}*//** * A class to calculate descriptive statistics from a data set. * Data sets can be simple arrays of data, or a cummulative hash. * The second form is useful when passing large data set, * for example the data set: * * <pre> * $data1 = array (1,2,1,1,1,1,3,3,4.1,3,2,2,4.1,1,1,2,3,3,2,2,1,1,2,2); * </pre> * * can be epxressed more compactly as: * * <pre> * $data2 = array("1"=>9, "2"=>8, "3"=>5, "4.1"=>2); * </pre> * * Example of use: * * <pre> * include_once "Math/Stats.php"; * $s = new Math_Stats(); * $s->setData($data1); * // or * // $s->setData($data2, STATS_DATA_CUMMULATIVE); * $stats = $s->calcBasic(); * echo "Mean: ".$stats["mean"]." StDev: ".$stats["stdev"]." <br />\n"; *  * // using data with nulls * // first ignoring them: * $data3 = array(1.2, "foo", 2.4, 3.1, 4.2, 3.2, null, 5.1, 6.2); * $s->setNullOption(STATS_IGNORE_NULL); * $s->setData($data3); * $stats3 = $s->calcFull(); * * // and then assuming nulls == 0 * $s->setNullOption(STATS_USE_NULL_AS_ZERO); * $s->setData($data3); * $stats3 = $s->calcFull(); * </pre> * * Originally this class was part of NumPHP (Numeric PHP package) * * @author  Jesus M. Castagnetto <jmcastagnetto@php.net> * @version 0.8 * @access  public * @package Math_Stats */class Math_Stats {/*{{{*/    // properties /*{{{*/        /**     * The simple or cummulative data set.     * Null by default.     *     * @access  private     * @var array     */    var $_data = null;    /**     * Flag for data type, one of STATS_DATA_SIMPLE or     * STATS_DATA_CUMMULATIVE. Null by default.     *     * @access  private     * @var int     */    var $_dataOption = null;    /**     * Flag for null handling options. One of STATS_REJECT_NULL,     * STATS_IGNORE_NULL or STATS_USE_NULL_AS_ZERO     *     * @access  private     * @var int     */    var $_nullOption;    /*}}}*/        /**     * Constructor for the class     *     * @access  public     * @param   optional    int $nullOption how to handle null values     * @return  object  Math_Stats     */    function Math_Stats($nullOption=STATS_REJECT_NULL) {/*{{{*/        $this->_nullOption = $nullOption;    }/*}}}*/    /**     * Sets and verifies the data, checking for nulls and using     * the current null handling option     *     * @access public     * @param   array   $arr    the data set     * @param   optional    int $opt    data format: STATS_DATA_CUMMULATIVE or STATS_DATA_SIMPLE (default)     * @return  mixed   true on success, a PEAR_Error object otherwise     */    function setData($arr, $opt=STATS_DATA_SIMPLE) {/*{{{*/        $this->_data = null;        $this->_dataOption = null;        if (!is_array($arr))            return PEAR::raiseError("invalid data, an array of numeric data was expected");        if ($opt == STATS_DATA_SIMPLE) {            $this->_dataOption = $opt;            $this->_data = array_values($arr);        } else if ($opt == STATS_DATA_CUMMULATIVE) {            $this->_dataOption = $opt;            $this->_data = $arr;        }         return $this->_validate();    }/*}}}*/    /**     * Returns the data which might have been modified     * according to the current null handling options.     *     * @access  public     * @return  mixed   array of data on success, a PEAR_Error object otherwise     * @see _validate()     */    function getData() {/*{{{*/        if ($this->_data == null)            return PEAR::raiseError("data has not been set");        return $this->_data;    }/*}}}*/    /**     * Sets the null handling option.     * Must be called before assigning a new data set containing null values     *      * @access  public     * @return  mixed   true on success, a PEAR_Error object otherwise     * @see _validate()     */    function setNullOption($nullOption) {/*{{{*/        if ($nullOption == STATS_REJECT_NULL            || $nullOption == STATS_IGNORE_NULL            || $nullOption == STATS_USE_NULL_AS_ZERO) {            $this->_nullOption = $nullOption;            return true;        } else {            return PEAR::raiseError("invalid null handling option expecting: ".                        "STATS_REJECT_NULL, STATS_IGNORE_NULL or STATS_USE_NULL_AS_ZERO");        }    }/*}}}*/    /**     * Calculates the basic or full statistics for the data set     *      * @access  public     * @param   int $mode   one of STATS_BASIC or STATS_FULL     * @return  mixed   an associative array of statistics on success, a PEAR_Error object otherwise     * @see calcBasic()     * @see calcFull()     */     function calc($mode) {/*{{{*/        if ($this->_data == null)            return PEAR::raiseError("data has not been set");        if ($mode == STATS_BASIC) {            return array (                "min" => $this->min(),                "max" => $this->max(),                "sum" => $this->sum(),                "sum2" => $this->sum2(),                "count" => $this->count(),                "mean" => $this->mean(),                "stdev" => $this->stDev(),                "variance" => $this->variance()            );        } else if ($mode == STATS_FULL) {            return array (                "min" => $this->min(),                "max" => $this->max(),                "sum" => $this->sum(),                "sum2" => $this->sum2(),                "count" => $this->count(),                "mean" => $this->mean(),                "median" => $this->median(),                "mode" => $this->mode(),                "midrange" => $this->midrange(),                "stdev" => $this->stDev(),                "absdev" => $this->absDev(),                "variance" => $this->variance(),                "std_error_of_mean" => $this->stdErrorOfMean(),                "skewness" => $this->skewness(),                "kurtosis" => $this->kurtosis(),                "coeff_of_variation" => $this->coeffOfVariation(),                "sample_central_moments" => array (                            1 => $this->sampleCentralMoment(1),                            2 => $this->sampleCentralMoment(2),                            3 => $this->sampleCentralMoment(3),                            4 => $this->sampleCentralMoment(4),                            5 => $this->sampleCentralMoment(5)                            ),                "sample_raw_moments" => array (                            1 => $this->sampleRawMoment(1),                            2 => $this->sampleRawMoment(2),                            3 => $this->sampleRawMoment(3),                            4 => $this->sampleRawMoment(4),                            5 => $this->sampleRawMoment(5)                            ),                "frequency" => $this->frequency()            );        } else {            return PEAR::raiseError("incorrect mode, expected STATS_BASIC or STATS_FULL");        }    }/*}}}*/    /**     * Calculates a basic set of statistics     *     * @access  public     * @return  mixed   an associative array of statistics on success, a PEAR_Error object otherwise     * @see calc()     * @see calcFull()     */    function calcBasic() {/*{{{*/        return $this->calc(STATS_BASIC);    }/*}}}*/    /**     * Calculates a full set of statistics     *     * @access  public     * @return  mixed   an associative array of statistics on success, a PEAR_Error object otherwise     * @see calc()     * @see calcBasic()     */    function calcFull() {/*{{{*/        return $this->calc(STATS_FULL);    }/*}}}*/    /**     * Calculates the minimum of a data set.     * Handles cummulative data sets correctly     *     * @access  public     * @return  mixed   the minimum value on success, a PEAR_Error object otherwise     * @see calc()     * @see max()     */    function min() {/*{{{*/        if ($this->_data == null)            return PEAR::raiseError("data has not been set");        if ($this->_dataOption == STATS_DATA_CUMMULATIVE)            return min(array_keys($this->_data));        else            return min($this->_data);    }/*}}}*/    /**     * Calculates the maximum of a data set.     * Handles cummulative data sets correctly     *     * @access  public     * @return  mixed   the maximum value on success, a PEAR_Error object otherwise     * @see calc()     * @see min()     */    function max() {/*{{{*/        if ($this->_data == null)            return PEAR::raiseError("data has not been set");        if ($this->_dataOption == STATS_DATA_CUMMULATIVE)            return max(array_keys($this->_data));        else            return max($this->_data);    }/*}}}*/    /**     * Calculates SUM { xi }     * Handles cummulative data sets correctly     *     * @access  public     * @return  mixed   the sum on success, a PEAR_Error object otherwise        * @see calc()     * @see sum2()     * @see sumN()     */    function sum() {/*{{{*/        return $this->sumN(1);    }/*}}}*/    /**     * Calculates SUM { (xi)^2 }     * Handles cummulative data sets correctly     *     * @access  public     * @return  mixed   the sum on success, a PEAR_Error object otherwise        * @see calc()     * @see sum()     * @see sumN()     */    function sum2() {/*{{{*/        return $this->sumN(2);    }/*}}}*/    /**     * Calculates SUM { (xi)^n }     * Handles cummulative data sets correctly     *     * @access  public     * @param   numeric $n  the exponent     * @return  mixed   the sum on success, a PEAR_Error object otherwise        * @see calc()     * @see sum()     * @see sum2()     */    function sumN($n) {/*{{{*/        if ($this->_data == null)            return PEAR::raiseError("data has not been set");        $sumN = 0;        if ($this->_dataOption == STATS_DATA_CUMMULATIVE) {            foreach($this->_data as $val=>$freq)                $sumN += $freq * pow((double)$val, (double)$n);        } else {            foreach($this->_data as $val)                $sumN += pow((double)$val, (double)$n);        }        return $sumN;    }/*}}}*/    /**     * Calculates the number of data points in the set     * Handles cummulative data sets correctly     *     * @access  public     * @return  mixed   the count on success, a PEAR_Error object otherwise      * @see calc()     */    function count() {/*{{{*/        if ($this->_data == null)            return PEAR::raiseError("data has not been set");        if ($this->_dataOption == STATS_DATA_CUMMULATIVE) {            foreach($this->_data as $freq)                $count += $freq;        } else {            $count = count($this->_data);        }        return $count;    }/*}}}*/    /**     * Calculates the mean (average) of the data points in the set     * Handles cummulative data sets correctly     *     * @access  public     * @return  mixed   the mean value on success, a PEAR_Error object otherwise         * @see calc()     * @see sum()     * @see count()     */    function mean() {/*{{{*/        if ($this->_data == null)            return PEAR::raiseError("data has not been set");        return ($this->sum() / $this->count());    }/*}}}*/    /**     * Calculates the variance (unbiased) of the data points in the set     * Handles cummulative data sets correctly     *     * @access  public     * @return  mixed   the variance value on success, a PEAR_Error object otherwise         * @see calc()     * @see __sumdiff()     * @see count()     */    function variance() {/*{{{*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?