Skip to content

Instantly share code, notes, and snippets.

@imos
Created March 27, 2016 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imos/342bb7f3b85230c29e1e to your computer and use it in GitHub Desktop.
Save imos/342bb7f3b85230c29e1e to your computer and use it in GitHub Desktop.
日経平均相関計算
<?php
date_default_timezone_set('UTC');
function Parse($file, $shift = 0) {
$data = explode("\n", trim(str_replace(
["\r\n", "\r"], "\n", file_get_contents($file))));
array_shift($data);
$output = [];
foreach ($data as $row) {
list($date, $value) = explode("\t", $row, 2);
$time = strtotime($date);
$output[date('Y-m-d', $time + $shift * 24 * 3600)] = floatval($value);
}
return $output;
}
$nikkei = Parse('nikkei.txt', -1);
$sp = Parse('sp.txt');
$usdjpy = Parse('usdjpy.txt');
$output = [];
foreach ($nikkei as $date => $value) {
if (!isset($nikkei[$date]) ||
!isset($sp[$date]) ||
!isset($usdjpy[$date])) {
continue;
}
$output[] = [
'date' => $date,
'nikkei' => $nikkei[$date],
'sp' => $sp[$date],
'usdjpy' => $usdjpy[$date]];
}
$names = ['nikkei', 'sp', 'usdjpy'];
$avgs = [];
for ($i = 0; $i < count($output); $i++) {
$sum = [];
for ($j = 3; $j < 10 && isset($output[$i + $j]); $j++) {
$result = [];
$ratio = sqrt($j);
foreach ($names as $name) {
$result[$name] =
log($output[$i][$name] / $output[$i + $j][$name]) / $ratio;
}
$sum[''] += 1;
foreach ($names as $name1) {
$sum[$name1] += $result[$name1];
foreach ($names as $name2) {
$sum["$name1.$name2"] += $result[$name1] * $result[$name2];
}
}
}
$output[$i]['sum'] = $sum;
}
$output = array_values(array_reverse($output));
for ($i = 0; $i < count($output); $i++) {
$last = $output[$i > 0 ? ($i - 1) : 0];
$sum = [];
for ($j = 0; $j < 60 && isset($output[$i + $j]); $j++) {
foreach ($output[$i + $j]['sum'] as $key => $value) {
$sum[$key] += $value;
}
}
foreach ($sum as $key => $value) {
if ($key === '') { continue; }
$sum[$key] /= $sum[''];
}
echo "{$output[$i]['date']}";
echo "\t";
echo $output[$i]['nikkei'];
echo "\t";
echo $output[$i]['usdjpy'];
echo "\t";
echo $output[$i]['sp'];
echo "\t";
echo ($sum['nikkei.usdjpy'] - $sum['nikkei'] * $sum['usdjpy']) /
sqrt(($sum['nikkei.nikkei'] - $sum['nikkei'] * $sum['nikkei']) *
($sum['usdjpy.usdjpy'] - $sum['usdjpy'] * $sum['usdjpy']));
echo "\t";
echo ($sum['nikkei.sp'] - $sum['nikkei'] * $sum['sp']) /
sqrt(($sum['nikkei.nikkei'] - $sum['nikkei'] * $sum['nikkei']) *
($sum['sp.sp'] - $sum['sp'] * $sum['sp']));
echo "\t";
echo ($sum['nikkei.usdjpy'] - $sum['nikkei'] * $sum['usdjpy']) /
($sum['usdjpy.usdjpy'] - $sum['usdjpy'] * $sum['usdjpy']);
echo "\t";
echo ($sum['nikkei.sp'] - $sum['nikkei'] * $sum['sp']) /
($sum['sp.sp'] - $sum['sp'] * $sum['sp']);
$output[$i]['nikkei/usdjpy'] =
($sum['nikkei.usdjpy'] - $sum['nikkei'] * $sum['usdjpy']) /
($sum['usdjpy.usdjpy'] - $sum['usdjpy'] * $sum['usdjpy']);
$output[$i]['usdjpy~nikkei'] =
$last['usdjpy~nikkei'] +
floatval($last['nikkei/usdjpy']) *
log($output[$i]['usdjpy'] / $last['usdjpy']);
echo "\t";
echo log($output[$i]['nikkei']) - $output[$i]['usdjpy~nikkei'];
$output[$i]['nikkei/sp'] =
($sum['nikkei.sp'] - $sum['nikkei'] * $sum['sp']) /
($sum['sp.sp'] - $sum['sp'] * $sum['sp']);
$output[$i]['sp~nikkei'] =
$last['sp~nikkei'] +
floatval($last['nikkei/sp']) *
log($output[$i]['sp'] / $last['sp']);
echo "\t";
echo log($output[$i]['nikkei']) - $output[$i]['sp~nikkei'];
$output[$i]['sp/nikkei'] =
($sum['nikkei.sp'] - $sum['nikkei'] * $sum['sp']) /
($sum['nikkei.nikkei'] - $sum['nikkei'] * $sum['nikkei']);
$output[$i]['nikkei~sp'] =
$last['nikkei~sp'] +
floatval($last['sp/nikkei']) *
log($output[$i]['nikkei'] / $last['nikkei']);
echo "\t";
echo log($output[$i]['sp']) - $output[$i]['nikkei~sp'];
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment