Skip to content

Instantly share code, notes, and snippets.

@sguzman
Created March 28, 2021 01:13
Show Gist options
  • Save sguzman/e003777a216aee6f6ec18b89cbd1db13 to your computer and use it in GitHub Desktop.
Save sguzman/e003777a216aee6f6ec18b89cbd1db13 to your computer and use it in GitHub Desktop.
Rank correlation of sp500 stocks and cache results of computations
Block[{},
items = EntityList[EntityClass["Financial", "SP500"]];
finhash[str_] := finhash[str] = FinancialData[str, "Close", All];
datePath[str_] := datePath[str] = finhash[str]["DatePath"];
shared[a_, b_] :=
shared[a, b] = Intersection[Map[First, a], Map[First, b]];
assoc[data_] :=
assoc[data] = Association@Map[First@# -> Last@# &, data];
filterFrom[as_, sh_] := filterFrom[as, sh] = Map[as, sh];
corr[a_, b_] := corr[a, b] = Correlation[a, b];
payload[a_, b_] :=
payload[a, b] =
With[{dpa = datePath[a], dpb = datePath[b]},
With[{sha = shared[dpa, dpb]},
With[{assa = assoc[dpa], assb = assoc[dpb]},
With[{fa = filterFrom[assa, sha], fb = filterFrom[assb, sha]},
corr[fa, fb]
]]]];
pairs = Subsets[items, {2}];
final = Map[{
First@#,
Last@#,
payload[First@#, Last@#]
} &, pairs];
sorted = TakeSmallestBy[final, Last, 10];
Grid@
sorted
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment