我如何使用php计算标准偏差

问题描述 投票:-2回答:1

我如何使用php函数计算标准差

采样结果

<p class="bigtext" style="padding: 15px 0px;">Standard Deviation, σ: <b>0.74833147735479</b></p>
<table cellpadding="0" cellspacing="0"><tbody><tr><td>Count, N:</td><td>5</td></tr><tr><td>Sum, Σx:</td><td>4</td></tr><tr><td>Mean, μ:</td><td>0.8</td></tr><tr><td>Variance, σ<sup>2</sup>:&nbsp;</td><td>0.56</td></tr></tbody></table>
<br>
<p><b>Steps</b></p>
<p><img src="//d26tpo4cm8sb6k.cloudfront.net/img/standard-dev.gif" width="184" height="60"></p>
<table cellpadding="0" cellspacing="0"><tbody><tr><td nowrap="">σ<sup>2</sup> =&nbsp;</td><td><table cellpadding="0" cellspacing="0"><tbody><tr><td align="center">Σ(x<sub>i</sub> - μ)<sup>2</sup></td></tr><tr><td height="1" bgcolor="#000000"></td></tr><tr><td align="center">N</td></tr></tbody></table></td></tr><tr><td align="right">=&nbsp;</td><td><table cellpadding="0" cellspacing="0"><tbody><tr><td align="center">(0 - 0.8)<sup>2</sup> + ... + (2 - 0.8)<sup>2</sup></td></tr><tr><td height="1" bgcolor="#000000"></td></tr><tr><td align="center">5</td></tr></tbody></table></td></tr><tr><td align="right">=&nbsp;</td><td><table cellpadding="0" cellspacing="0"><tbody><tr><td align="center">2.8</td></tr><tr><td height="1" bgcolor="#000000"></td></tr><tr><td align="center">5</td></tr></tbody></table></td></tr><tr><td align="right">=&nbsp;</td><td>0.56</td></tr></tbody></table>
<table><tbody><tr><td nowrap="">σ =&nbsp;</td><td>√<span style="text-decoration:overline;">0.56</span></td></tr><tr><td align="right">=&nbsp;</td><td>0.74833147735479</td></tr></tbody></table>
<p><b>Margin of Error (Confidence Interval)</b></p>
<p>The sampling mean most likely follows a normal distribution. In this case, the standard error of the mean (SEM) can be calculated using the following equation:</p>
<table><tbody><tr><td>σ<sub>x̄</sub> =&nbsp;</td><td><table cellpadding="0" cellspacing="0"><tbody><tr><td align="center">σ</td></tr><tr><td height="1" bgcolor="#000000"></td></tr><tr><td align="center">√<span style="text-decoration:overline;">N</span></td></tr></tbody></table></td><td>=</td><td>0.33466401061363</td></tr></tbody></table>
php standard-deviation
1个回答
0
投票
<?php

if (!function_exists('standard_deviation')) {

        /**
         * This user-land implementation follows the implementation quite strictly;
         * it does not attempt to improve the code or algorithm in any way. It will
         * raise a warning if you have fewer than 2 values in your array, just like
         * the extension does (although as an E_USER_WARNING, not E_WARNING).
         *
         * @param array $a
         * @param bool $sample [optional] Defaults to false
         * @return float|bool The standard deviation or false on error.
         */
        function standard_deviation(array $a, $sample = false) {
            $n = count($a);
            if ($n === 0) {
                trigger_error("The array has zero elements", E_USER_WARNING);
                return false;
            }
            if ($sample && $n === 1) {
                trigger_error("The array has only 1 element", E_USER_WARNING);
                return false;
            }
            $mean = array_sum($a) / $n;
            $carry = 0.0;
            foreach ($a as $val) {
                $d = ((double) $val) - $mean;
                $carry += $d * $d;
            };
            if ($sample) {
                --$n;
            }
            return sqrt($carry / $n);
        }

    }

    echo standard_deviation([4,3,2,1,5]);
© www.soinside.com 2019 - 2024. All rights reserved.