Created
March 25, 2016 17:34
-
-
Save imos/bb9fd777377ca2dc911d to your computer and use it in GitHub Desktop.
日経平均相関計算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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