Skip to content

Instantly share code, notes, and snippets.

@maple-nishiyama
Created August 10, 2014 02:42
Show Gist options
  • Save maple-nishiyama/4d0691e1c6ba7e28a89a to your computer and use it in GitHub Desktop.
Save maple-nishiyama/4d0691e1c6ba7e28a89a to your computer and use it in GitHub Desktop.
<?php
$dsn = 'mysql:dbname=singularity;host=localhost';
$user = 'ユーザー名';
$pass = 'パスワード';
$pdo = new PDO($dsn, $user, $pass);
// 1年のうちの日ごとに処理をする
for ($i = 0; $i < 365; $i++) {
$today = date('Y-m-d', strtotime("2014-01-01 +$i day"));
$inArray = [];
// 前後10日間の日付を求める
for ($j = -10; $j <= 10; $j++) {
$sign = ($j >= 0) ? '+' : '';
$date = date('Y-n-j', strtotime("{$today} {$sign}{$j} day"));
list(, $month, $day) = explode('-', $date);
$inArray[] = "($month, $day)";
}
$in = implode(',', $inArray);
// それらの日で平均、標準偏差を求める
$rate = "sunny_days / total_days * 100";
$select ="select AVG(sunny_rate) as avg, STD(sunny_rate) as std from summaries where (month, day) in ($in)";
$stmt = $pdo->query($select);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$avg = $result['avg']; // 平均
$std = $result['std']; // 標準偏差
list(, $today_month, $today_day) = explode('-', $today);
// z統計量を求めるために必要な、この日のデータを取得
$z_test = "select * from summaries where month = $today_month and day = $today_day";
$stmt = $pdo->query($z_test);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$rate = $result['sunny_rate']; // この日の晴れの割合
// z 統計量の計算
$z = ($rate - $avg) / $std;
// データを更新する
$update = "update summaries set avg = $avg, std = $std, z = $z where month = $today_month and day = $today_day";
$pdo->query($update);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment