|
| 1 | +"""레버 정제 : awaitingMeasurement 레버를 보유 데이터로 실표면화 (L2.5 simulate). |
| 2 | +
|
| 3 | +레버 원장(levers)의 "정제 대기(awaitingMeasurement)" 항목 중 실은 보유 데이터로 도출 가능한 |
| 4 | +것들을 실표면화한다 (10 §1). 진짜 미보유(Form 4 P/S 거래코드·락업 만기 정밀 파싱)만 남기고, |
| 5 | +빈도·군집·타이밍·시총 랭크로 도출되는 정제는 지금 낸다. ① 내부자 군집: 문헌 정제 핵심인 |
| 6 | +"복수내부자 군집·비정기"는 P/S 코드 없이도 공시 빈도로 도출 (실측: KR (code,week) 28%가 복수 |
| 7 | +공시). ② 락업 만료: 증권신고서 발행 주 + 표준 락업(26주) = 만료 주 회피. ③ 지수 편입: 시총 랭크가 |
| 8 | +편입 경계(KOSPI200/KOSDAQ150) 근방 = 편입 후보. 방향은 문헌 prior, 인증 깔때기가 도태. |
| 9 | +
|
| 10 | +- ``insiderClusterReadings`` : 내부자 공시 군집 강도 (빈도 가중, 정제). |
| 11 | +- ``lockupExpiryReadings`` : 증권신고서 + 표준 락업 = 만료 주 회피. |
| 12 | +- ``indexInclusionReadings`` : 시총 편입 경계 근방 후보. |
| 13 | +- ``refinedEventReadings`` : 이벤트 파생 정제 (군집·락업) 합 (opine 배선). |
| 14 | +
|
| 15 | +Layer: L2.5 simulate. reading·polars 만 의존. 신규 데이터 수집 0 (allFilings·시총 재조합). |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import polars as pl |
| 21 | + |
| 22 | +# 시장별 내부자·공모 이벤트 타입 (KR + US v2 정규화). |
| 23 | +INSIDER_TYPES = ("임원ㆍ주요주주특정증권등소유상황보고서", "insiderTransaction") |
| 24 | +OFFERING_TYPES = ("증권신고서", "securitiesOffering") |
| 25 | +_LOCKUP_WEEKS = 26 # 표준 의무보유 락업 (약 6개월) |
| 26 | +_CLUSTER_SCALE = 4 # 군집 강도 포화 (공시 5건 이상 = 최대 강도) |
| 27 | + |
| 28 | + |
| 29 | +def insiderClusterReadings(eventMatrix: pl.DataFrame, *, insiderTypes: tuple[str, ...] = INSIDER_TYPES) -> pl.DataFrame: |
| 30 | + """내부자 공시 군집 강도 → (code, week, surface, direction, score). 정제 = 복수 공시 군집. |
| 31 | +
|
| 32 | + 문헌(Cohen-Malloy-Pomorski) 정제의 "복수내부자 군집·비정기"를 P/S 코드 없이 공시 빈도로 |
| 33 | + 도출한다: 한 주 복수 공시 = 강한 신호. score = 0.5 + 0.5*min((cnt-1)/scale, 1). 방향 long prior. |
| 34 | + """ |
| 35 | + sub = eventMatrix.filter(pl.col("reportType").is_in(list(insiderTypes))) |
| 36 | + if sub.height == 0: |
| 37 | + return _empty() |
| 38 | + cw = sub.group_by(["code", "week"]).len().rename({"len": "cnt"}) |
| 39 | + return cw.with_columns( |
| 40 | + surface=pl.lit("lever.insiderCluster"), |
| 41 | + direction=pl.lit(1, dtype=pl.Int64), |
| 42 | + score=0.5 + 0.5 * ((pl.col("cnt") - 1) / _CLUSTER_SCALE).clip(0, 1), |
| 43 | + ).select("code", "week", "surface", "direction", "score") |
| 44 | + |
| 45 | + |
| 46 | +def lockupExpiryReadings( |
| 47 | + eventMatrix: pl.DataFrame, *, offeringTypes: tuple[str, ...] = OFFERING_TYPES, lockupWeeks: int = _LOCKUP_WEEKS |
| 48 | +) -> pl.DataFrame: |
| 49 | + """증권신고서 발행 주 + 표준 락업 = 만료 주 회피 → (code, week, surface, direction, score). |
| 50 | +
|
| 51 | + 락업 만료 주에 의무보유 물량 출회 = 회피(-1). 만기 주 = 발행 주 + lockupWeeks (iso 주 근사, |
| 52 | + 연 52주 기준). 정밀 만기(확약 파싱)는 미보유라 표준 기간 근사 (문헌 표준 180일 = 26주). |
| 53 | + """ |
| 54 | + sub = eventMatrix.filter(pl.col("reportType").is_in(list(offeringTypes))) |
| 55 | + if sub.height == 0: |
| 56 | + return _empty() |
| 57 | + # week = isoYear*100 + isoWeek → 총주차 + lockupWeeks → 만료 week (연 52주 근사). |
| 58 | + total = (pl.col("week") // 100) * 52 + (pl.col("week") % 100) + lockupWeeks |
| 59 | + expiryWeek = (total // 52) * 100 + (total % 52).clip(1, 53) |
| 60 | + return ( |
| 61 | + sub.with_columns(expiry=expiryWeek) |
| 62 | + .group_by(pl.col("code"), pl.col("expiry").alias("week")) |
| 63 | + .len() |
| 64 | + .with_columns(surface=pl.lit("lever.lockupExpiry"), direction=pl.lit(-1, dtype=pl.Int64), score=pl.lit(0.0)) |
| 65 | + .select("code", "week", "surface", "direction", "score") |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def indexInclusionReadings( |
| 70 | + marketCap: pl.DataFrame, weekMap: pl.DataFrame, *, lowerPct: float = 0.90, upperPct: float = 0.97 |
| 71 | +) -> pl.DataFrame: |
| 72 | + """시총 편입 경계 근방 후보 → (code, week, surface, direction, score). 지수 편입 예측(long). |
| 73 | +
|
| 74 | + 주말 시총 백분위가 편입 경계 밴드(KOSPI200/KOSDAQ150 근방)에 든 종목 = 편입 후보. 정기 |
| 75 | + 재구성(6·12월) 규칙 공개라 사전 예측 가능. 방향 long prior (레짐 부패 전제, 인증이 검증). |
| 76 | + """ |
| 77 | + if marketCap.height == 0 or weekMap.height == 0: |
| 78 | + return _empty() |
| 79 | + snap = marketCap.join(weekMap, on="date", how="inner") |
| 80 | + weekEndSnap = snap.sort("date").group_by(["code", "week"]).agg(mktcap=pl.col("mktcap").last()) |
| 81 | + ranked = weekEndSnap.with_columns(pct=(pl.col("mktcap").rank() / pl.len()).over("week")) |
| 82 | + band = ranked.filter((pl.col("pct") >= lowerPct) & (pl.col("pct") <= upperPct)) |
| 83 | + if band.height == 0: |
| 84 | + return _empty() |
| 85 | + return band.with_columns( |
| 86 | + surface=pl.lit("lever.indexInclusion"), direction=pl.lit(1, dtype=pl.Int64), score=pl.lit(0.75) |
| 87 | + ).select("code", "week", "surface", "direction", "score") |
| 88 | + |
| 89 | + |
| 90 | +def refinedEventReadings(eventMatrix: pl.DataFrame) -> pl.DataFrame: |
| 91 | + """이벤트 파생 정제 레버 합 (내부자 군집 + 락업 만료). opine 배선용.""" |
| 92 | + parts = [insiderClusterReadings(eventMatrix), lockupExpiryReadings(eventMatrix)] |
| 93 | + parts = [p for p in parts if p.height] |
| 94 | + return pl.concat(parts) if parts else _empty() |
| 95 | + |
| 96 | + |
| 97 | +def _empty() -> pl.DataFrame: |
| 98 | + return pl.DataFrame( |
| 99 | + schema={"code": pl.Utf8, "week": pl.Int64, "surface": pl.Utf8, "direction": pl.Int64, "score": pl.Float64} |
| 100 | + ) |
0 commit comments