Skip to content

Instantly share code, notes, and snippets.

@imos
Created March 25, 2016 17:34
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/bb9fd777377ca2dc911d to your computer and use it in GitHub Desktop.
Save imos/bb9fd777377ca2dc911d 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]];
}
for ($i = 0; $i < count($output); $i++) {
$sum = [];
for ($j = 3; $j < 60 && isset($output[$i + $j]); $j++) {
$result = [];
$ratio = sqrt($j);
foreach (['nikkei', 'sp', 'usdjpy'] as $name) {
$result[$name] =
log($output[$i][$name] / $output[$i + $j][$name]) / $ratio;
}
foreach (['nikkei', 'sp', 'usdjpy'] as $name) {
$sum['nikkei.' . $name] += $result['nikkei'] * $result[$name];
if ($name != 'nikkei') {
$sum["$name.$name"] += $result[$name] * $result[$name];
}
}
}
echo "{$output[$i]['date']}";
echo "\t{$sum['nikkei.nikkei']}";
echo "\t{$sum['nikkei.sp']}\t{$sum['sp.sp']}";
echo "\t{$sum['nikkei.usdjpy']}\t{$sum['usdjpy.usdjpy']}";
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment