Created
March 14, 2018 10:28
-
-
Save franchb/c2912504a181b1d2abb267e40be8faf5 to your computer and use it in GitHub Desktop.
Standart deviation for in-system calculation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $this->addFunction('stats_standard_deviation', | |
| function(array $a, $sample = false) { | |
| $n = count($a); | |
| if ($n === 0) { | |
| return 0; | |
| } | |
| if ($sample && $n === 1) { | |
| return 0; | |
| } | |
| $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); | |
| }, | |
| ['desc' => 'Среднеквадратичное отклонение.'] | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment