<?php // 標準偏差の関数 function stats_standard_deviation($ary) { // 平均取得 $avg = round(array_sum($ary) / count($ary), 1); // 各値の平均値との差の二乗【(値-平均値)^2】を算出 $diff_ary = array(); foreach ($ary as $val) { $diff = $val - $avg; $diff_ary[] = pow($diff, 2); } // 上記差の二乗の合計を算出 $diff_total = array_sum($diff_ary); // 平均を算出 $diff_avg = $diff_total / count($diff_ary); // 平方根を取る(標準偏差) $stdev = sqrt($diff_avg); // 標準偏差を返す return $stdev; } // N個の整数をセット $Numbers = array(90, 30, 53, 45, 60); $count = count($Numbers); $sum = array_sum($Numbers); $max = max($Numbers); $min = min($Numbers); $average = round($sum / $count, 1); echo "セットされた数字の<BR>個数:" . $count . "最大:" . $max . "最小:" . $min . "平均:" . $average . "標準偏差:" . round(stats_standard_deviation($Numbers), 1);
/** * @param array $a * @param bool $sample [optional] Defaults to false * @return float|bool The standard deviation or false on error. */ public static function standardDeviation(array $a, $sample = false) { return stats_standard_deviation($a, $sample); }
function retSD($x) { $answer = stats_standard_deviation($x); return $answer; }
$a = [[1, 2], [3, 4], [5, 6]]; print_r($a); $b = array_filter($a, function ($k) { return $k[0]; }); print_r($b); function get_heights($i, $k) { return $i[0]; } $c = array_walk($a, 'get_heights'); print_r($c); $d = array(); foreach ($a as $e) { array_push($d, $e[0]); } print_r($d); function sum($g) { return $g[0] + $g[1]; } $f = array_map('sum', $a); print_r($f); $ret = array(); $mean = array_sum($f) / count($f); $stddev = stats_standard_deviation($f); $outlier = 3 * $stddev; print_r($mean); ?> </pre>
$d = (double) $val - $mean; $carry += $d * $d; } if ($sample) { --$n; } return sqrt($carry / $n); } $input = array($_POST); // カンマ区切り、PHP、配列 // N個の数値を入力し、配列に取得する方法がわからない。。。 $N = count($input); $max = max($input); $min = min($input); $ave = array_sum($input) / $N; $sd = stats_standard_deviation($input); var_dump($input); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>統計量の計算</title> </head> <body> <h1>統計量の計算</h1> <form action="" method="post"> <input type="text" name="n" min="0"> <input type="submit" value="送信"><br> データ個数:<?php
function GetView() { global $TMP_PATH; $req = $this->req->CreateDataRequest(); $x = $req->GetProp("view_x", false); if (!$x) { throw new ADEIException(translate("Parameter view_x is not set")); } list($x_gid, $x_id) = explode(":", $x); if ($this->object) { $width = $req->GetProp($this->object . "_width", $this->min_width + 20) - 20; if ($width < $this->min_width) { $width = $this->min_width; } $height = $width - 40; //$req->GetProp($this->object . "_height", $this->min_height); } else { $width = $req->GetProp("page_width", $this->min_width + 5) - 5; $height = $req->GetProp("page_height", $this->min_height); if ($width < $this->min_width) { $width = $this->min_width; } if ($height < $this->min_height) { $height = $this->min_height; } } $rdr = $req->CreateReader(); $group = $rdr->CreateGroup(); $caches = $rdr->CreateCacheSet($group, $mask); $myreq = $this->req->CreateDataRequest(); $iv = $caches->CreateInterval($req, true); $window_size = $iv->GetWindowSize(); $window_start = $iv->GetWindowStart(); $window_end = $iv->GetWindowEnd(); $rescfg = array('limit' => $this->max_points, 'resolution' => $res); $gid = 0; $res = array(); foreach ($caches as $key => $cachewrap) { if ($gid != $x_gid) { $gid++; continue; } $resolution = $cachewrap->GetResolution(); $r = $resolution->Get($iv, $width); $size = $resolution->GetWindowSize($r); if ($size > 0 && $window_size / $size > $this->max_points) { $new_r = $resolution->Larger($r); if ($new_r !== false) { $r = $new_r; } $size = $resolution->GetWindowSize($r); } $rescfg['resolution'] = $r; $points = $cachewrap->GetIntervals($iv, $rescfg, CACHE::TRUNCATE_INTERVALS); $operation_info = $points->GetOperationInfo(); if ($gid == $x_gid) { $res_x = $size; } foreach ($points as $t => $v) { /* if (($t < $window_start)||(($t + $size) > $window_end)) { continue; }*/ if ($gid == $x_gid && is_numeric($v['mean' . $x_id])) { if (!is_array($res[$t])) { $res[$t] = array(); } $res[$t]['x'] = $v['mean' . $x_id]; $res[$t]['t'] = $t; } } $gid++; } $x = array(); $t = array(); foreach ($res as $val) { if (isset($val['x'])) { array_push($x, $val['x']); } } if (!$x) { throw new ADEIException(translate("No data found")); } $bins = $req->GetProp("view_bins", 0); if (!$bins) { $bins = ceil(sqrt(sizeof($x))); } $norm = $req->GetProp("view_hist_norm", 0); $fit = $req->GetProp("view_hist_fit", 0); $min = min($x); $max = max($x); $step = ($max - $min) / $bins; $coef = $norm ? 1 / ($step * sizeof($x)) : 1; $h = array_fill(0, $bins, 0); foreach ($x as $val) { $idx = ($val - $min) / $step; if ($idx == $bins) { $idx--; } $h[$idx] += $coef; } for ($i = 0; $i < $bins; $i++) { array_push($t, sprintf("%3.1e", $min + $i * $step)); } $tmp_file = ADEI::GetTmpFile(); $graph = new Graph($width, $height); /* $title = "Resolution: $res_x"; $graph->title->SetFont(FF_ARIAL,FS_BOLD,10); $graph->title->Set($title);*/ $graph->SetTickDensity(TICKD_SPARSE, TICKD_SPARSE); $graph->img->SetMargin(55, 5, 10, 20); $graph->SetScale("textlin"); $graph->xaxis->SetPos("min"); $graph->yaxis->SetPos("min"); // $graph->xaxis->SetLabelFormat('%3.1e'); // if (abs(max($h))<9999 && (abs(min($h))>0.01)) $graph->yaxis->SetLabelFormat('%01.2f'); // else $graph->yaxis->SetLabelFormat('%3.1e'); $graph->xaxis->SetFont(FF_ARIAL, FS_NORMAL, 8); $graph->yaxis->SetFont(FF_ARIAL, FS_NORMAL, 8); // $graph->yaxis->HideFirstTickLabel(); $graph->xaxis->title->SetFont(FF_ARIAL, FS_BOLD); $graph->yaxis->title->SetFont(FF_ARIAL, FS_BOLD); //$graph->xaxis->title->Set($arr[0]['select']['options'][$x_idg]['label']); if ($bins > 8) { $graph->xaxis->SetTextLabelInterval(ceil($bins / 6)); } $graph->xaxis->SetTickLabels($t); $bplot = new BarPlot($h); $bplot->SetWidth(1); $graph->Add($bplot); $graph->yaxis->scale->SetGrace(14); $mean = array_sum($x) / sizeof($x); $stddev = stats_standard_deviation($x); $var = stats_variance($x); $sigma = sqrt($var); $re = 100 * $sigma / $mean; sort($x); if (sizeof($x) % 2) { $median = $x[(sizeof($x) - 1) / 2]; } else { $median = ($x[sizeof($x) / 2 - 1] + $x[sizeof($x) / 2]) / 2; } // Gaussian fitting if ($fit) { $ydata = array(); $xdata = array(); if ($norm) { $coef = 1 / sqrt(2 * pi() * $var); } else { $coef = sizeof($x) * $step / sqrt(2 * pi() * $var); } $xi2 = 0; for ($i = 0; $i <= $bins; $i++) { $offset = $i * $step; $y = $coef * exp(-pow($min + $offset - $mean, 2) / (2 * $var)); array_push($xdata, $i); array_push($ydata, $y); $xi2 += pow($y - $h[$i], 2) / $y; } $xi2 /= $bins; $lineplot = new LinePlot($ydata, $xdata); $graph->Add($lineplot); } $char_sigma = SymChar::Get('sigma', false); /* $txt = new Text(); $txt->SetFont(FF_ARIAL,FS_BOLD,10); if( $req->GetProp("view_GFit", false) == "true") $txt->Set("m=$mean\n$char_sigma=$sigma\nRE=$RE%\nx^2=$xi2");//\ns=$stdDev else $txt->Set("m=$mean\n$char_sigma=$sigma\nRE=$RE%");//\ns=$stdDev $txt->ParagraphAlign('right'); $txt->SetPos(0.96,0.1,'right'); //$txt->SetBox('white'); $graph->Add($txt); */ $graph->Stroke("{$TMP_PATH}/{$tmp_file}"); if ($this->object) { $res = array(array("img" => array("id" => $tmp_file)), array("info" => array(array("title" => _("From"), "value" => date('c', $iv->GetWindowStart())), array("title" => _("To"), "value" => date('c', $iv->GetWindowEnd())), array("title" => _("Resolution"), "value" => $res_x), array("title" => _("Bins"), "value" => $bins), array("title" => _("First Bin"), "value" => $min), array("title" => _("Last Bin"), "value" => $min + $bins * $step), array("title" => _("Mean"), "value" => $mean), array("title" => _("Median"), "value" => $median), array("title" => _("StdDev"), "value" => $stddev), array("title" => _("Sigma"), "value" => $sigma), array("title" => _("RE"), "value" => $re . "%")))); if ($fit) { array_push($res[1]["info"], array("title" => _("xi2"), "value" => $xi2)); } return $res; } else { return array("img" => array("id" => $tmp_file)); } }
function calculateRegression($project_id, $time, $trend_value) { $trends = getTrendsTimeArray($project_id); $times = getTrendsTimeArray($project_id); $sx = stats_standard_deviation($times); $sy = stats_standard_deviation($trends); $r = Corr($x, $y); $b = $r * ($sy / $sx); $a = $trend_value - $b * $time; return $b * $time + $a; }
function calculateRegression($project_id, $time, $trend_value) { $trends = getTrendsTimeArray($project_id); $times = getTrendsTimeArray($project_id); $sx = stats_standard_deviation($times); $sy = stats_standard_deviation($trends); $r = Corr($sx, $sy); //removed to check error can be uncomment later. //$r = 2;//random value need to remove $b = $r * ($sy / $sx); $a = $trend_value - $b * $time; return $b * $time + $a; }