当前位置:网站首页>Implementing StdevP function of Excel with PHP

Implementing StdevP function of Excel with PHP

2022-06-23 02:49:00 jwj

stay Excel in ,stdevp Is a function of the population standard deviation of the sample , It reflects the degree of dispersion relative to the average . But in PHP There is no such function in , To calculate the standard deviation , You can only write your own algorithm , It's very inconvenient . So we can query relevant data and formulas , The following code is summarized .

The formula

First , Check Wikipedia , The complete formula and detailed calculation steps are obtained , The following image is captured in Wikipedia

Complete formulas and detailed calculation steps

Reference material : Wikipedia - Standard deviation

Encapsulate as a function

Then split according to the formula and steps , Write the following functions

/**
 *  The standard deviation of the sample population 
 * @param array $list  sample 
 * @return float
 */
function stdevp($list)
{
    //  Number of samples 
    $count = count($list);

    //  Average 
    $avg = array_sum($list) / $count;

    //  Sum of the squares of the differences between all samples and the mean 
    $sum = 0.0;
    foreach($list as $item) $sum += ($item - $avg) ** 2;

    //  Radical sign ( Sum of squares  /  Sample size )
    return sqrt($sum / $count);
}

test run

$list = [5,6,8,9];
var_dump(stdevp($list));

result

float(1.5811388300842)

You can see that the result is the same as the example in Wikipedia , stay Excel Medium test stdevp Function is the same result .

It's not exactly the same , Because the decimal length is inconsistent , But I believe the result is relatively accurate . If you want to get the specified decimal length , have access to round() Round the result

MySQL

Of course , In development , Most of our data comes from databases , The database has its own function for calculating the population standard deviation of samples , By the way, record MySQL Use example of .

according to uid Group data , And then based on Standard deviation Sort the process from large to small .

SELECT `uid`, STDDEV_POP(score) ` Standard deviation `, AVG(score) ` Average ` FROM `test` GROUP BY `uid` ORDER BY ` Standard deviation ` DESC

This article is synchronously published to Zero blog .


Last, last , There's also a benefit . developers , Welcome to join us Tengyun pioneer (TDP) Feedback communication group , There are plenty of activities in the group to gain points and growth value , Exchange for surprise benefits . How to join :https://cloud.tencent.com/developer/article/1855195

We are the vanguard of Tengyun (TDP) The team , Tencent cloud GTS Officially established and operated technology developer group . There are the most professional developers in & Customer , Be able to have close contact with product personnel , Proprietary questions & Demand feedback channels , There are a group of like-minded brothers and sisters , Looking forward to your joining !

原网站

版权声明
本文为[jwj]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201281532232191.html