function Hints($set) { $mean = array_sum($set) / count($set); $page[] = 'A szóráshoz először határozzuk meg az adatsor <b>átlagát</b>. Ehhez adjuk össze a számokat:' . '<div class="text-center">$' . implode("\$\$+\$\$", $set) . '=' . strval(array_sum($set)) . '$</div>'; $page[] = 'Most osszuk el az összeget az adatsor méretével:' . '$$\\frac{' . strval(array_sum($set)) . '}{' . count($set) . '}=' . $mean . '$$'; $page[] = 'Tehét az adatsor átlaga: $' . $mean . '$.'; $hints[] = $page; $page = []; $text = 'Vonjuk ki minden számból az átlagot, és emeljük négyzetre a különbséget:'; foreach ($set as $key => $number) { $diff[$key] = $number - $mean; $diffsq[$key] = pow($number - $mean, 2); $text .= '$$(' . $number . '-' . $mean . ')^2=' . ($number >= $mean ? $diff[$key] . '^2' : '(' . $diff[$key] . ')^2') . '=' . strval($diffsq[$key]) . '$$'; } $page[] = $text; $hints[] = $page; $page = []; $page[] = 'Adjuk össze az így kapott számokat:' . '<div class="text-center">$' . implode("\$\$+\$\$", $diffsq) . '=' . strval(array_sum($diffsq)) . '$</div>'; $page[] = 'Most osszuk el az összeget az adatsor méretével:' . '$$\\frac{' . strval(array_sum($diffsq)) . '}{' . count($diffsq) . '}=' . strval(pow(stdev($set), 2)) . '$$'; $page[] = 'Ez az adatsor szórásnégyzete. A szórást úgy kapjuk meg, ha ebből gyököt vonunk:' . '$$\\sqrt{' . strval(pow(stdev($set), 2)) . '}=' . stdev($set) . '$$'; $page[] = 'Tehát az adatsor szórása: <span class="label label-success">$' . stdev($set) . '$</span>.'; $hints[] = $page; return $hints; }
public function colourCalibrate($colours, $expand = 25, $SD = 2) { // $colours lists x-coordinate, y-coordinate, and L, a* and b* values for each colour on the checker chart // $expand = number of pixels plus or minus the reference x and y-corrdinates // $SD = exclude pixels more than $sd standard deviations from the mean for that area if (!imageistruecolor($this->_image)) { return false; } $w = $this->getWidth(); $h = $this->getHeight(); $rgb255 = array(); foreach ($colours as $cname => $c) { // get color from x and y coordinates $imgcolor = array(); for ($x = $c[0] - $expand; $x <= $c[0] + $expand; $x++) { for ($y = $c[1] - $expand; $y <= $c[1] + $expand; $y++) { $rgb = imagecolorat($this->_image, $x, $y); $imgcolor['r'][] = $rgb >> 16 & 0xff; $imgcolor['g'][] = $rgb >> 8 & 0xff; $imgcolor['b'][] = $rgb & 0xff; } } foreach ($imgcolor as $rgb => $valuearray) { $mode = mode($valuearray, 0.25); $stdev = stdev($valuearray); foreach ($valuearray as $i => $v) { if ($v > $mode + $SD * $stdev || $v < $mode - $SD * $stdev) { // remove from list unset($valuearray[$i]); } } $averages[$rgb] = round(mean($valuearray), 12); $stdevs[$rgb] = round(stdev($valuearray), 4); $counts[$rgb] = count($valuearray); } $rgb255[] = $averages; $lab = rgb2lab($averages['r'], $averages['g'], $averages['b']); $deltaE = deltaE($lab, array($c[2], $c[3], $c[4])); $check[$cname] = sprintf("Lab(%.1f, %.1f, %.1f)\n", round($lab['L'], 1), round($lab['a'], 1), round($lab['b'], 1)); /* $check .= "--myLab: (" . implode(',', $lab['lab']) . ")\n"; $check .= "--chartLab: ($c[2], $c[3], $c[4])\n"; $check .= "--deltaE = $deltaE\n"; $check .= "--RGB: (" . implode(',', $averages) . ")\n"; $check .= "--SD: (" . implode(',', $stdevs) . ")\n"; $check .= "--N: (" . implode(',', $counts) . ")\n"; */ if (!is_nan($deltaE)) { $deltaEs[] = $deltaE; } } $meanDeltaE = round(mean($deltaEs), 4); $maxDeltaE = round(max($deltaEs), 4); $patch = $expand * 2 + 1; $this->setDescription(array("colour calibrate" => array("path dimension" => "{$patch} x {$patch}", "SD" => $SD, "deltaE" => array("M" => $meanDeltaE, "max" => $maxDelta), "check" => $check))); /* MATLAB code ************************************ % Calculate camera Characterisation - Least Squares fit of rgb to Lab patches: rgb = Rgb_vals/255; for i = 1:length(rgb) train(i,:) = rgb2rgbpoly(rgb(i,:),11); end R=train\spectrod65; % Calculate predicted lab values from characterisation lab_calc = train*R; squaredDifference = (lab_calc-spectrod65).^2; meanDeltaE = mean(sqrt(sum(squaredDifference'))) maxDeltaE = max(sqrt(sum(squaredDifference'))) **************************************************/ include_once "Math/Matrix.php"; foreach ($colours as $v) { $spectrod65[] = array($v[2], $v[3], $v[4]); } foreach ($rgb255 as $i => $v) { foreach ($v as $j => $c) { $rgb1[$i][] = $c / 255; } } foreach ($rgb1 as $v) { $train[] = rgb2rgbpoly($v, 11); } $rgbM = new Math_Matrix($rgb1); $trainM = new Math_matrix($train); return $this; }
// update as necessary $stored[$date][$user_id][$exchange][$account_id][$currency]['min'] = min($balance['balance'], $stored[$date][$user_id][$exchange][$account_id][$currency]['min']); $stored[$date][$user_id][$exchange][$account_id][$currency]['max'] = max($balance['balance'], $stored[$date][$user_id][$exchange][$account_id][$currency]['max']); $stored[$date][$user_id][$exchange][$account_id][$currency]['close'] = $balance['balance']; $stored[$date][$user_id][$exchange][$account_id][$currency]['samples']++; $stored[$date][$user_id][$exchange][$account_id][$currency]['values'][] = $balance['balance']; } crypto_log("Processed " . number_format($count) . " balances entries"); // we now have lots of data; insert it // danger! danger! five nested loops! $insert_count = 0; foreach ($stored as $date => $a) { foreach ($a as $user_id => $b) { foreach ($b as $exchange => $c) { foreach ($c as $account_id => $d) { foreach ($d as $currency => $summary) { $q = db_master()->prepare("INSERT INTO graph_data_balances SET\n user_id=:user_id, exchange=:exchange, account_id=:account_id, currency=:currency, data_date=:data_date, samples=:samples,\n balance_min=:min, balance_opening=:open, balance_closing=:close, balance_max=:max, balance_stdev=:stdev"); $q->execute(array('user_id' => $user_id, 'exchange' => $exchange, 'account_id' => $account_id, 'currency' => $currency, 'data_date' => $date, 'samples' => $summary['samples'], 'min' => $summary['min'], 'open' => $summary['open'], 'close' => $summary['close'], 'max' => $summary['max'], 'stdev' => stdev($summary['values']))); $insert_count++; } } } } } crypto_log("Inserted " . number_format($insert_count) . " balances entries into graph_data_balances"); // finally, delete all the old data // we've exhausted over everything so this should be safe $q = db_master()->prepare("DELETE FROM balances WHERE created_at <= :date ORDER BY created_at ASC"); $q->execute(array("date" => $cutoff_date)); crypto_log("Deleted " . number_format($count) . " summary entries"); batch_footer();
function get_apa_stats() { $p = apa_round($this->get_p()); $pvalue = $p < 0.001 ? '< .001' : "= {$p}"; $m1 = apa_round(mean($this->get_var1())); $m2 = apa_round(mean($this->get_var2())); $sd1 = apa_round(stdev($this->get_var1())); $sd2 = apa_round(stdev($this->get_var2())); $n1 = count($this->get_var1()); $n2 = count($this->get_var2()); $stats = '<i>M</i><sub>' . $this->get_vname1() . '</sub> = ' . $m1 . ', '; $stats .= '<i>SD</i><sub>' . $this->get_vname1() . '</sub> = ' . $sd1 . ', '; $stats .= '<i>N</i><sub>' . $this->get_vname1() . '</sub> = ' . $n1 . '; '; $stats .= '<i>M</i><sub>' . $this->get_vname2() . '</sub> = ' . $m2 . ', '; $stats .= '<i>SD</i><sub>' . $this->get_vname2() . '</sub> = ' . $sd2 . ', '; $stats .= '<i>N</i><sub>' . $this->get_vname2() . '</sub> = ' . $n2 . '<br />'; $stats .= '<i>t</i><sub>' . $this->get_df() . '</sub> = ' . apa_round($this->get_t()) . ', '; $stats .= '<i>p</i> ' . $pvalue . ', '; $stats .= '<i>d</i> = ' . apa_round($this->get_d()); return $stats; }
<?php include '../vendor/autoload.php'; use NlpTools\Random\Distributions\Normal as NormalDistribution; function mean($samples) { return array_sum($samples) / count($samples); } function stdev($samples, $m = null) { if ($m == null) { $m = mean($samples); } return sqrt(array_sum(array_map(function ($x) use($m) { $t = $x - $m; return $t * $t; }, $samples)) / count($samples) - 1); } $normal = new NormalDistribution(10, 5); $samples = array(); for ($i = 0; $i < 10000; $i++) { $samples[] = $normal->sample(); } var_dump(mean($samples), stdev($samples));
function getTopDeviations($account = null, $count = null) { $pcount = $count; $qcount = tryGET('count'); $vcount = 10; if (isset($pcount)) { $vcount = $pcount; } else { if (isset($qcount)) { $vcount = $qcount; } } if ($vcount > 100) { $vcount = 100; } $settings = getSettings(); if (!isset($account)) { $account = $settings["Account"]; } $start = tryGET('start'); $end = tryGET('end'); if (!isset($start) || !isset($end)) { return null; } $ndays = getDays($start, $end); $start = GoogleDate($start); $end = GoogleDate($end); $analytics = getAnalytics(); $filter = ""; //More than 1 pageview an hour to cut down on outliers and processing $dims = "ga:hostname,ga:pagePath,ga:date"; $metric = "ga:pageviews"; $sort = "-ga:pageviews"; $count = 10000; //max $data = runQuery($analytics, $account, $start, $end, $metric, $dims, $sort, $count, $filter); if (isset($data->ga_error)) { return DoNotCache(); } $data = $data->getRows(); $values = array(); $path = ''; $tvals = array(); foreach ($data as $key => $row) { if (!isset($values[$row[0] . $row[1]])) { $values[$row[0] . $row[1]] = array(); } array_push($values[$row[0] . $row[1]], floatval($row[3])); } foreach ($values as $key => $val) { $rem = $ndays - count($val); for ($i = 0; $i < $rem; $i++) { array_push($val, 0); } $mean = mean($val); if ($mean < 1) { continue; } //Aviod super low page averages $sd = stdev($mean, $val); if ($sd == 0) { continue; } //Let's not deal with how this is even possible for right now $stdevs[$key] = array('mean' => $mean, 'stdev' => $sd, 'values' => $val); } $count = 100 * $vcount; $filter = ""; $dims = "ga:date,ga:hour,ga:hostname,ga:pagePath,ga:pageTitle"; $metric = "ga:pageviews"; $sort = "-ga:pageviews"; $data = runQuery($analytics, $account, $start, $end, $metric, $dims, $sort, $count, $filter); if (isset($data->ga_error)) { return DoNotCache(); } $data = $data->getRows(); $result = array(); foreach ($data as $key => $row) { $path = $row[2] . $row[3]; if (!isset($stdevs[$path])) { continue; } //if($sd['mean'] <= 0) continue; $sd = $stdevs[$path]; $z = zscore($sd['stdev'], $sd['mean'], $row[5]); $y = substr($row[0], 0, 4); $m = substr($row[0], 4, 2); $d = substr($row[0], 6, 2); $time = "{$y}-{$m}-{$d} " . $row[1] . ":00"; $ts = strtotime($time); $result[] = array('path' => $path, 'title' => $row[4], 'mean' => $sd['mean'], 'stdev' => $sd['stdev'], 'pageviews' => $row[5], 'z' => $z, 'timestamp' => $ts, "time" => $time, "values" => $sd['values']); } usort($result, "zsort"); $ret = array_splice($result, 0, $vcount); return $ret; }
$stored[$date][$user_id][$type] = array(); } if (!isset($stored[$date][$user_id][$type]['open'])) { $stored[$date][$user_id][$type] = array('min' => $ticker['balance'], 'max' => $ticker['balance'], 'open' => $ticker['balance'], 'close' => $ticker['balance'], 'samples' => 0, 'values' => array()); } // update as necessary $stored[$date][$user_id][$type]['min'] = min($ticker['balance'], $stored[$date][$user_id][$type]['min']); $stored[$date][$user_id][$type]['max'] = max($ticker['balance'], $stored[$date][$user_id][$type]['max']); $stored[$date][$user_id][$type]['close'] = $ticker['balance']; $stored[$date][$user_id][$type]['samples']++; $stored[$date][$user_id][$type]['values'][] = $ticker['balance']; } crypto_log("Processed " . number_format($count) . " summary entries"); // we now have lots of data; insert it $insert_count = 0; foreach ($stored as $date => $a) { foreach ($a as $user_id => $b) { foreach ($b as $type => $summary) { $q = db_master()->prepare("INSERT INTO graph_data_summary SET\n user_id=:user_id, summary_type=:summary_type, data_date=:data_date, samples=:samples,\n balance_min=:min, balance_opening=:open, balance_closing=:close, balance_max=:max, balance_stdev=:stdev"); $q->execute(array('user_id' => $user_id, 'summary_type' => $type, 'data_date' => $date, 'samples' => $summary['samples'], 'min' => $summary['min'], 'open' => $summary['open'], 'close' => $summary['close'], 'max' => $summary['max'], 'stdev' => stdev($summary['values']))); $insert_count++; } } } crypto_log("Inserted " . number_format($insert_count) . " summary entries into graph_data_summary"); // finally, delete all the old data // we've exhausted over everything so this should be safe $q = db_master()->prepare("DELETE FROM summary_instances WHERE created_at <= :date ORDER BY created_at ASC"); $q->execute(array("date" => $cutoff_date)); crypto_log("Deleted " . number_format($count) . " summary entries"); batch_footer();
$stored[$date][$exchange][$cur1][$cur2]['min'] = min($ticker['last_trade'], $stored[$date][$exchange][$cur1][$cur2]['min']); $stored[$date][$exchange][$cur1][$cur2]['max'] = max($ticker['last_trade'], $stored[$date][$exchange][$cur1][$cur2]['max']); $stored[$date][$exchange][$cur1][$cur2]['volume'] = max($ticker['volume'], $stored[$date][$exchange][$cur1][$cur2]['volume']); $stored[$date][$exchange][$cur1][$cur2]['close'] = $ticker['last_trade']; $stored[$date][$exchange][$cur1][$cur2]['samples']++; $stored[$date][$exchange][$cur1][$cur2]['bid'] = $ticker['bid']; // bid, ask are the last values for the day $stored[$date][$exchange][$cur1][$cur2]['ask'] = $ticker['ask']; $stored[$date][$exchange][$cur1][$cur2]['values'][] = $ticker['last_trade']; } crypto_log("Processed " . number_format($count) . " ticker entries"); // we now have lots of data; insert it $insert_count = 0; foreach ($stored as $date => $a) { foreach ($a as $exchange => $b) { foreach ($b as $cur1 => $c) { foreach ($c as $cur2 => $summary) { $q = db_master()->prepare("INSERT INTO graph_data_ticker SET\n exchange=:exchange, currency1=:currency1, currency2=:currency2, data_date=:data_date, samples=:samples, data_date_day=TO_DAYS(:data_date),\n volume=:volume, last_trade_min=:min, last_trade_opening=:open, last_trade_closing=:close, last_trade_max=:max, bid=:bid, ask=:ask, last_trade_stdev=:stdev"); $q->execute(array('exchange' => $exchange, 'currency1' => $cur1, 'currency2' => $cur2, 'data_date' => $date, 'samples' => $summary['samples'], 'volume' => $summary['volume'], 'min' => $summary['min'], 'open' => $summary['open'], 'close' => $summary['close'], 'max' => $summary['max'], 'bid' => $summary['bid'], 'ask' => $summary['ask'], 'stdev' => stdev($summary['values']))); $insert_count++; } } } } crypto_log("Inserted " . number_format($insert_count) . " summarised entries into graph_data_ticker"); // finally, delete all the old data // we've exhausted over everything so this should be safe $q = db_master()->prepare("DELETE FROM ticker WHERE created_at <= :date ORDER BY created_at ASC"); $q->execute(array("date" => $cutoff_date)); crypto_log("Deleted " . number_format($count) . " ticker entries"); batch_footer();