security-master / tests /test_intervals.py
ZipLime's picture
Update the security master
ed5ee12 verified
Raw History Blame Contribute Delete
8.84 kB
"""Observations into spans, filers, and the CUSIP bridge."""
from __future__ import annotations
from datetime import date
import polars as pl
from recipe.intervals import (
build_cusip_map,
build_cusip_map_from_pairs,
build_identifiers,
build_issuers,
build_pit,
ftd_pairs,
)
def test_a_ticker_change_becomes_two_spans(observations):
spans = build_identifiers(observations)
ticker = spans.filter(
(pl.col("cik") == "0000000001") & (pl.col("kind") == "ticker")
).sort("valid_from")
assert ticker["value"].to_list() == ["OLDT", "NEWT"]
assert ticker["valid_from"].to_list() == [date(2012, 3, 1), date(2016, 3, 1)]
assert ticker["valid_to"].to_list() == [date(2015, 3, 1), date(2024, 3, 1)]
assert ticker["is_current"].to_list() == [False, True]
def test_a_dormant_filer_holds_no_current_ticker(observations):
"""A company that stopped filing in 2014 is not currently listed under its
2014 ticker -- and PLUM belongs to whoever files under it now."""
spans = build_identifiers(observations)
plum = spans.filter((pl.col("kind") == "ticker") & (pl.col("value") == "PLUM"))
current = plum.filter(pl.col("is_current"))
assert current.height == 1, "one live owner, not two"
assert current["cik"][0] == "0000000003"
# The dormant filer keeps its own history, flagged as its latest.
dormant = plum.filter(pl.col("cik") == "0000000002").row(0, named=True)
assert dormant["is_current"] is False
assert dormant["is_latest_for_filer"] is True
assert dormant["valid_to"] == date(2014, 5, 1)
def test_issuer_keeps_its_name(observations):
"""Regression: the changes pivot produced a column called `name` too, and
renaming after the join blanked every issuer's name."""
spans = build_identifiers(observations)
issuers = build_issuers(spans, observations)
row = issuers.filter(pl.col("cik") == "0000000001").row(0, named=True)
assert row["name"] == "New Name Inc"
assert row["primary_ticker"] == "NEWT"
assert row["name_changes"] == 2
assert row["ticker_changes"] == 2
assert row["sic"] == "3571"
def test_dormant_filers_are_marked_inactive(observations):
spans = build_identifiers(observations)
issuers = build_issuers(spans, observations)
assert issuers.filter(pl.col("cik") == "0000000002")["is_active"][0] is False
assert issuers.filter(pl.col("cik") == "0000000001")["is_active"][0] is True
def test_a_reused_ticker_resolves_by_date(observations, ftd):
"""The point of the bridge: PLUM is two companies, and the CUSIP knows when."""
spans = build_identifiers(observations)
bridge = build_cusip_map(ftd, spans)
early = bridge.filter(pl.col("cusip") == "111111111").row(0, named=True)
late = bridge.filter(pl.col("cusip") == "222222222").row(0, named=True)
assert early["cik"] == "0000000002" and early["match_confidence"] == "high"
assert late["cik"] == "0000000003" and late["match_confidence"] == "high"
def test_a_cusip_with_no_filer_is_kept_and_labelled(observations, ftd):
spans = build_identifiers(observations)
bridge = build_cusip_map(ftd, spans)
ghost = bridge.filter(pl.col("cusip") == "333333333").row(0, named=True)
assert ghost["cik"] is None
assert ghost["match_method"] == "unmatched" and ghost["match_confidence"] == "none"
assert ghost["ticker"] == "GHOST", "the ticker is still usable without a filer"
def test_cusip6_is_the_issuer_prefix(observations, ftd):
bridge = build_cusip_map(ftd, build_identifiers(observations))
assert bridge.filter(pl.col("cusip") == "111111111")["cusip6"][0] == "111111"
def test_pit_dates_every_span_and_never_leaks(observations):
spans = build_identifiers(observations)
pit = build_pit(spans)
assert pit.height == spans.height
assert pit["entity_id"].null_count() == 0
# Knowledge is the end of the day observed, so it never precedes the event.
assert (pit["knowledge_date"].dt.date() >= pit["event_date"]).all()
assert pit["knowledge_estimated"].all()
def _bridge(observations, ftd_rows):
return build_cusip_map(pl.DataFrame(ftd_rows, strict=False), build_identifiers(observations))
def _ftd(cusip, ticker, name, day):
return {"cusip": cusip, "ticker": ticker, "issuer_name": name, "observed_on": day,
"source_ref": "f"}
def test_a_share_class_matches_across_separators():
"""Settlement records write BRKB, filings BRK.B: one symbol, one filer."""
from conftest import observation
obs = pl.DataFrame([
observation("0000000010", "ticker", "BRK.B", date(2015, 1, 1)),
observation("0000000010", "ticker", "BRK.B", date(2020, 1, 1)),
observation("0000000010", "name", "Berkshire Hathaway Inc", date(2020, 1, 1)),
], strict=False)
bridge = _bridge(obs, [_ftd("084670702", "BRKB", "BERKSHIRE HATHAWAY INC CL B", date(2018, 1, 1))])
row = bridge.row(0, named=True)
assert row["cik"] == "0000000010" and row["match_confidence"] == "high"
assert row["ticker"] == "BRKB", "the published ticker is the one the record stated"
def test_a_fund_is_not_the_company_that_shares_its_ticker():
"""SPDR's MDY matched Midway Gold by ticker and date. A fund whose name shares
no word with anything the filer was called is refused, not attached."""
from conftest import observation
obs = pl.DataFrame([
observation("0000000020", "ticker", "MDY", date(2010, 1, 1)),
observation("0000000020", "ticker", "MDY", date(2012, 1, 1)),
observation("0000000020", "name", "Midway Gold Corp", date(2012, 1, 1)),
], strict=False)
bridge = _bridge(obs, [_ftd("78467Y107", "MDY", "SPDR S&P MIDCAP 400 ETF TR UTS", date(2011, 1, 1))])
row = bridge.row(0, named=True)
assert row["cik"] is None
assert row["match_method"] == "name_conflict" and row["match_confidence"] == "none"
def test_one_collision_does_not_name_a_whole_fund_family():
"""iShares' ICF once matched ICF International, and inheriting by CUSIP prefix
then gave that company's CIK to IVV, IWM, EFA and 71 more funds."""
from conftest import observation
obs = pl.DataFrame([
observation("0000000030", "ticker", "ICF", date(2010, 1, 1)),
observation("0000000030", "ticker", "ICF", date(2012, 1, 1)),
], strict=False)
bridge = _bridge(obs, [
_ftd("464287564", "ICF", "ISHARES COHEN & STEERS REIT ETF", date(2011, 1, 1)),
_ftd("464287200", "IVV", "ISHARES CORE S&P 500 ETF", date(2011, 1, 1)),
])
ivv = bridge.filter(pl.col("ticker") == "IVV").row(0, named=True)
assert ivv["cik"] is None and ivv["match_method"] == "unmatched"
def test_an_issuer_prefix_still_lends_its_filer_to_other_classes():
"""The prefix rule is right for companies: a class A share of a company whose
common stock matched belongs to the same filer."""
from conftest import observation
obs = pl.DataFrame([
observation("0000000040", "ticker", "HEI", date(2010, 1, 1)),
observation("0000000040", "ticker", "HEI", date(2020, 1, 1)),
], strict=False)
bridge = _bridge(obs, [
_ftd("422806109", "HEI", "HEICO CORP", date(2015, 1, 1)),
_ftd("422806208", "HEIXA", "HEICO CORP CL A", date(2015, 1, 1)),
])
cls = bridge.filter(pl.col("cusip") == "422806208").row(0, named=True)
assert cls["cik"] == "0000000040" and cls["match_confidence"] == "medium"
def test_clearing_placeholders_are_marked():
from conftest import observation
obs = pl.DataFrame([observation("0000000050", "ticker", "XCEM", date(2015, 1, 1))], strict=False)
bridge = _bridge(obs, [
_ftd("19762B202", "XCEMZZZZ", "COLUMBIA ETF TR II", date(2015, 1, 1)),
_ftd("19762B203", "XCEM", "COLUMBIA ETF TR II", date(2015, 1, 1)),
])
flags = dict(bridge.select("ticker", "standard_symbol").iter_rows())
assert flags == {"XCEMZZZZ": False, "XCEM": True}
def test_a_tie_between_two_filers_resolves_the_same_way_every_time():
"""Two filers that each stated the symbol once are tied on every rank the
bridge sorts by. Without a last key the winner was whichever row the join
emitted first, and the same inputs in a different order gave a different
filer."""
from conftest import observation
obs = pl.DataFrame([
observation("0000000061", "ticker", "TIE", date(2011, 1, 1)),
observation("0000000062", "ticker", "TIE", date(2011, 1, 1)),
], strict=False)
identifiers = build_identifiers(obs)
pairs = ftd_pairs(pl.DataFrame(
[_ftd("111111105", "TIE", "TIE CORP", date(2011, 1, 1))], strict=False))
forward = build_cusip_map_from_pairs(pairs, identifiers)
backward = build_cusip_map_from_pairs(pairs, identifiers.reverse())
assert forward.drop("inserted_at").equals(backward.drop("inserted_at"))