我想用我的标准偏差结果过滤我将添加多少级别。这些是我需要做的步骤:
根据数组值计算标准偏差。(解决)
找到数组值的平均值并与标准偏差结果相加。例如:平均值 = -49.7 标准差 = 3.37
所以,我需要继续添加这个值,直到我得到一个新的数字列表。例如:-49.7、-46.33、-42.96、-39.59
之后,我只需要过滤打印从 -49.7 到 -39.59 的数组值。
有人可以帮我吗?
我想用我的标准偏差结果过滤我将添加多少级别。这些是我需要做的步骤:
根据数组值计算标准偏差。(解决)
找到数组值的平均值并与标准偏差结果相加。例如:平均值 = -49.7 标准差 = 3.37
所以,我需要继续添加这个值,直到我得到一个新的数字列表。例如:-49.7、-46.33、-42.96、-39.59
之后,我只需要过滤打印从 -49.7 到 -39.59 的数组值。
有人可以帮我吗?
这是我能理解的问题的脚本。
如果您需要任何解释或代码不是您想要的,请在评论部分告诉我。
<?php
$array = [2,4,6,8,10];
$resultList = "";
$resultList_rounded = "";
function standardDeviation($array){
//sum of all values
$sum = array_sum($array);
//sum of (square of values)
$sum_of_square = array_sum(array_map(function($value){return $value*$value;},$array));
//X Bar
$xBar = average($array);
//variance
$variance = ($sum_of_square/count($array))-pow($xBar,2);
//standard deviation
return sqrt($variance);
}
function average($array){
return (array_sum($array)/count($array));
}
function newList($array,$rounded=false,$round_digits = 4){
$newarray = [];
$sd = standardDeviation($array);
$avg = average($array);
for($i=1;$i<=count($array);$i++){
if(empty($newarray)){
array_push($newarray,$avg);
continue;
}
if(!$rounded){
array_push($newarray,$array[$i-1]+$sd);
}else{
array_push($newarray,number_format((float) ($array[$i-1]+$sd), $round_digits, '.', ''));
}
}
return implode(',',$newarray);
}
function getRange($array){
return $array[0]." to ".$array[count($array)-1];
}
//get new list
$resultList = newList($array,true,0);
/*In the line above this, replace false with true if you want the rounded version and false if you want non rounded version. 4 means the number of digits to round.
examples:
$resultList = newList($array,true,4); will print 6,6.8284,8.8284,10.8284,12.8284
$resultList = newList($array,false,4); will print 6,6.8284271247462,8.8284271247462,10.828427124746,12.828427124746
$resultList = newList($array,true,0); will print 6,7,9,11,13
$resultList = newList($array,true,1); will print 6,6.8,8.8,10.8,12.8
*/
//print the new result list
echo $resultList;