Update core/external_score.py
Browse files- core/external_score.py +99 -53
core/external_score.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
from typing import Dict, Any, List, Tuple
|
| 3 |
import pandas as pd
|
|
@@ -6,9 +7,22 @@ __all__ = [
|
|
| 6 |
"get_external_template_df",
|
| 7 |
"fill_missing_with_external",
|
| 8 |
"score_external_from_df",
|
|
|
|
| 9 |
]
|
| 10 |
|
| 11 |
_TEMPLATE_ROWS: List[Tuple[str, str]] = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
("経営者能力","予実達成率_3年平均(%)"),
|
| 13 |
("経営者能力","監査・内部統制の重大な不備 件数(過去3年)"),
|
| 14 |
("経営者能力","重大コンプライアンス件数(過去3年)"),
|
|
@@ -21,15 +35,7 @@ _TEMPLATE_ROWS: List[Tuple[str, str]] = [
|
|
| 21 |
("経営者能力","倒産からの経過年数"),
|
| 22 |
("経営者能力","重大事件・事故件数(過去10年)"),
|
| 23 |
|
| 24 |
-
|
| 25 |
-
("成長率","売上_期2"),
|
| 26 |
-
("成長率","売上_期1(最古期)"),
|
| 27 |
-
("成長率","営業利益_期3(最新期)"),
|
| 28 |
-
("成長率","営業利益_期2"),
|
| 29 |
-
("成長率","営業利益_期1(最古期)"),
|
| 30 |
-
("成長率","主力商品数"),
|
| 31 |
-
("成長率","成長中主力商品数"),
|
| 32 |
-
|
| 33 |
("安定性","自己資本比率(%)"),
|
| 34 |
("安定性","利益剰余金(円)"),
|
| 35 |
("安定性","支払遅延件数(直近12ヶ月)"),
|
|
@@ -47,6 +53,7 @@ _TEMPLATE_ROWS: List[Tuple[str, str]] = [
|
|
| 47 |
("安定性","不良債権件数(直近12ヶ月)"),
|
| 48 |
("安定性","業歴(年)"),
|
| 49 |
|
|
|
|
| 50 |
("公平性・総合世評","有価証券報告書提出企業か(TRUE/FALSE)"),
|
| 51 |
("公平性・総合世評","決算公告や官報での公開あり(TRUE/FALSE)"),
|
| 52 |
("公平性・総合世評","HP/IRサイトで財務資料公開あり(TRUE/FALSE)"),
|
|
@@ -57,16 +64,44 @@ def get_external_template_df() -> pd.DataFrame:
|
|
| 57 |
return pd.DataFrame([(c,i,"") for c,i in _TEMPLATE_ROWS], columns=["カテゴリー","入力項目","値"])
|
| 58 |
|
| 59 |
def fill_missing_with_external(df: pd.DataFrame, company: str = "", country: str = "") -> pd.DataFrame:
|
| 60 |
-
# 将来: 外部DBとのブリッジ。いまはそのまま返す
|
| 61 |
return df.copy()
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# ===== スコア計算 =====
|
| 64 |
_WEIGHTS = {
|
| 65 |
-
|
| 66 |
-
("成長率","
|
| 67 |
-
("
|
| 68 |
-
("
|
| 69 |
-
("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
}
|
| 71 |
_WEIGHT_NORM = 100.0 / float(sum(_WEIGHTS.values()))
|
| 72 |
|
|
@@ -115,6 +150,32 @@ def score_external_from_df(df: pd.DataFrame) -> Dict[str, Any]:
|
|
| 115 |
return df.loc[m, "値"].values[0] if m.any() else None
|
| 116 |
|
| 117 |
items = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
yoy3=_to_float(ref("予実達成率_3年平均(%)"))
|
| 119 |
audit_bad=_to_float(ref("監査・内部統制の重大な不備 件数(過去3年)"))
|
| 120 |
comp_bad=_to_float(ref("重大コンプライアンス件数(過去3年)"))
|
|
@@ -127,37 +188,8 @@ def score_external_from_df(df: pd.DataFrame) -> Dict[str, Any]:
|
|
| 127 |
bk_years=_to_float(ref("倒産からの経過年数"))
|
| 128 |
incidents=_to_float(ref("重大事件・事故件数(過去10年)"))
|
| 129 |
|
| 130 |
-
|
| 131 |
-
p1=_to_float(ref("営業利益_期1(最古期)")); p2=_to_float(ref("営業利益_期2")); p3=_to_float(ref("営業利益_期3(最新期)"))
|
| 132 |
|
| 133 |
-
equity=_to_float(ref("自己資本比率(%)"))
|
| 134 |
-
delay_cnt=_to_float(ref("支払遅延件数(直近12ヶ月)"))
|
| 135 |
-
boun_cnt=_to_float(ref("不渡り件数(直近12ヶ月)"))
|
| 136 |
-
delay_days=_to_float(ref("平均支払遅延日数"))
|
| 137 |
-
mainbank=_to_bool(ref("メインバンク明確か(TRUE/FALSE)"))
|
| 138 |
-
lenders=_to_float(ref("借入先数"))
|
| 139 |
-
main_share=_to_float(ref("メインバンク借入シェア(%)"))
|
| 140 |
-
has_line=_to_bool(ref("コミットメントライン等の長期与信枠あり(TRUE/FALSE)"))
|
| 141 |
-
sales_m2=_to_float(ref("月商(円)_再掲")) or sales_m
|
| 142 |
-
top1=_to_float(ref("主要顧客上位1社売上比率(%)"))
|
| 143 |
-
cust_score=_to_float(ref("主要顧客の平均信用スコア(0-100)"))
|
| 144 |
-
npl_cnt=_to_float(ref("不良債権件数(直近12ヶ月)"))
|
| 145 |
-
years=_to_float(ref("業歴(年)"))
|
| 146 |
-
has_sec=_to_bool(ref("有価証券報告書提出企業か(TRUE/FALSE)"))
|
| 147 |
-
pub_off=_to_bool(ref("決算公告や官報での公開あり(TRUE/FALSE)"))
|
| 148 |
-
pub_web=_to_bool(ref("HP/IRサイトで財務資料公開あり(TRUE/FALSE)"))
|
| 149 |
-
upd_on=_to_bool(ref("直近更新が定め通りか(TRUE/FALSE)"))
|
| 150 |
-
|
| 151 |
-
def cagr(v1, v3):
|
| 152 |
-
if v1 is None or v3 is None or v1 <= 0: return None
|
| 153 |
-
try: return (v3/v1)**(1/2) - 1.0
|
| 154 |
-
except Exception: return None
|
| 155 |
-
|
| 156 |
-
s_cagr=cagr(s1,s3); p_cagr=cagr(p1,p3)
|
| 157 |
-
cash_to_ms=_ratio(cash, sales_m2)
|
| 158 |
-
coll_to_ms=_ratio(collat, sales_m2)
|
| 159 |
-
|
| 160 |
-
# 経営者能力
|
| 161 |
mg_att = (_ramp(yoy3, 90,50)+_ramp(0 if not audit_bad else -audit_bad,0,-3)+_ramp(0 if not comp_bad else -comp_bad,0,-2)+_ramp(indep,33,0))/4
|
| 162 |
_add(items,"経営者能力","経営姿勢",mg_att,_WEIGHTS[("経営者能力","経営姿勢")],f"予実{yoy3 or '—'}%/監査{audit_bad or 0}/違反{comp_bad or 0}/社外{indep or '—'}%")
|
| 163 |
|
|
@@ -175,12 +207,16 @@ def score_external_from_df(df: pd.DataFrame) -> Dict[str, Any]:
|
|
| 175 |
pen=10.0; rs="事故/倒産なし"
|
| 176 |
_add(items,"経営者能力","減点事項",pen,_WEIGHTS[("経営者能力","減点事項")],rs)
|
| 177 |
|
| 178 |
-
#
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
-
# 安定性
|
| 184 |
_add(items,"安定性","自己資本", _ramp(equity,40,5), _WEIGHTS[("安定性","自己資本")], f"自己資本比率{equity or '—'}%")
|
| 185 |
|
| 186 |
if (delay_cnt is not None) or (boun_cnt is not None) or (delay_days is not None):
|
|
@@ -194,16 +230,26 @@ def score_external_from_df(df: pd.DataFrame) -> Dict[str, Any]:
|
|
| 194 |
sc_mb += 2.0 if mainbank else (-0.5 if mainbank is False else 0)
|
| 195 |
sc_mb += 1.0 if has_line else 0
|
| 196 |
sc_mb = _clamp(sc_mb,0,10)
|
| 197 |
-
_add(items,"安定性","金融取引", sc_mb, _WEIGHTS[("安定性","金融取引")],
|
|
|
|
| 198 |
|
| 199 |
-
_add(items,"安定性","資産担保余力", _ramp(coll_to_ms,4.0,0.0), _WEIGHTS[("安定性","資産担保余力")],
|
|
|
|
| 200 |
|
|
|
|
|
|
|
|
|
|
| 201 |
_add(items,"安定性","取引先", ( _ramp(- (top1 or 50),0,-80) + _ramp(cust_score,80,50) + _ramp(- (npl_cnt or 1),0,-3) )/3,
|
| 202 |
_WEIGHTS[("安定性","取引先")], f"上位1社{top1 or '—'}%/信用{cust_score or '—'}/不良{int(npl_cnt or 0)}")
|
| 203 |
|
|
|
|
| 204 |
_add(items,"安定性","業歴", _ramp(years,20,1), _WEIGHTS[("安定性","業歴")], f"{years or '—'}年")
|
| 205 |
|
| 206 |
-
# 公平性
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
sc_dis = 0.0
|
| 208 |
sc_dis += 10.0 if has_sec else (7.0 if (pub_off or pub_web) else 4.0)
|
| 209 |
sc_dis += 1.0 if upd_on else 0.0
|
|
|
|
| 1 |
+
# core/external_score.py
|
| 2 |
from __future__ import annotations
|
| 3 |
from typing import Dict, Any, List, Tuple
|
| 4 |
import pandas as pd
|
|
|
|
| 7 |
"get_external_template_df",
|
| 8 |
"fill_missing_with_external",
|
| 9 |
"score_external_from_df",
|
| 10 |
+
"apply_llm_signals_to_df",
|
| 11 |
]
|
| 12 |
|
| 13 |
_TEMPLATE_ROWS: List[Tuple[str, str]] = [
|
| 14 |
+
# 成長性をより定量化
|
| 15 |
+
("成長率","市場CAGR(%)"),
|
| 16 |
+
("成長率","売上_期3(最新期)"),
|
| 17 |
+
("成長率","売上_期2"),
|
| 18 |
+
("成長率","売上_期1(最古期)"),
|
| 19 |
+
("成長率","営業利益_期3(最新期)"),
|
| 20 |
+
("成長率","営業利益_期2"),
|
| 21 |
+
("成長率","営業利益_期1(最古期)"),
|
| 22 |
+
("成長率","主力商品数"),
|
| 23 |
+
("成長率","成長中主力商品数"),
|
| 24 |
+
|
| 25 |
+
# 経営者能力
|
| 26 |
("経営者能力","予実達成率_3年平均(%)"),
|
| 27 |
("経営者能力","監査・内部統制の重大な不備 件数(過去3年)"),
|
| 28 |
("経営者能力","重大コンプライアンス件数(過去3年)"),
|
|
|
|
| 35 |
("経営者能力","倒産からの経過年数"),
|
| 36 |
("経営者能力","重大事件・事故件数(過去10年)"),
|
| 37 |
|
| 38 |
+
# 安定性
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
("安定性","自己資本比率(%)"),
|
| 40 |
("安定性","利益剰余金(円)"),
|
| 41 |
("安定性","支払遅延件数(直近12ヶ月)"),
|
|
|
|
| 53 |
("安定性","不良債権件数(直近12ヶ月)"),
|
| 54 |
("安定性","業歴(年)"),
|
| 55 |
|
| 56 |
+
# 公平性
|
| 57 |
("公平性・総合世評","有価証券報告書提出企業か(TRUE/FALSE)"),
|
| 58 |
("公平性・総合世評","決算公告や官報での公開あり(TRUE/FALSE)"),
|
| 59 |
("公平性・総合世評","HP/IRサイトで財務資料公開あり(TRUE/FALSE)"),
|
|
|
|
| 64 |
return pd.DataFrame([(c,i,"") for c,i in _TEMPLATE_ROWS], columns=["カテゴリー","入力項目","値"])
|
| 65 |
|
| 66 |
def fill_missing_with_external(df: pd.DataFrame, company: str = "", country: str = "") -> pd.DataFrame:
|
|
|
|
| 67 |
return df.copy()
|
| 68 |
|
| 69 |
+
# === LLMの抽出結果をテンプレに反映 ===
|
| 70 |
+
def apply_llm_signals_to_df(df: pd.DataFrame, signals: Dict[str, Any]) -> pd.DataFrame:
|
| 71 |
+
df2 = df.copy()
|
| 72 |
+
def setv(field: str, val):
|
| 73 |
+
m = df2["入力項目"].eq(field)
|
| 74 |
+
if m.any():
|
| 75 |
+
df2.loc[m, "値"] = "" if val is None else val
|
| 76 |
+
if signals:
|
| 77 |
+
mkt = signals.get("market", {})
|
| 78 |
+
prod = signals.get("products", {})
|
| 79 |
+
setv("市場CAGR(%)", mkt.get("cagr_pct"))
|
| 80 |
+
setv("主力商品数", prod.get("count"))
|
| 81 |
+
setv("成長中主力商品数", prod.get("growing_count"))
|
| 82 |
+
return df2
|
| 83 |
+
|
| 84 |
# ===== スコア計算 =====
|
| 85 |
_WEIGHTS = {
|
| 86 |
+
# 成長の定量性を強化
|
| 87 |
+
("成長率","市場成長率"): 12,
|
| 88 |
+
("成長率","売上高伸長性"): 10,
|
| 89 |
+
("成長率","利益伸長性"): 10,
|
| 90 |
+
("成長率","商品"): 6,
|
| 91 |
+
|
| 92 |
+
("経営者能力","経営姿勢"): 8,
|
| 93 |
+
("経営者能力","事業経験"): 5,
|
| 94 |
+
("経営者能力","資産担保力"): 6,
|
| 95 |
+
("経営者能力","減点事項"): 7,
|
| 96 |
+
|
| 97 |
+
("安定性","自己資本"): 8,
|
| 98 |
+
("安定性","決済振り"): 10,
|
| 99 |
+
("安定性","金融取引"): 6,
|
| 100 |
+
("安定性","資産担保余力"): 6,
|
| 101 |
+
("安定性","取引先"): 6,
|
| 102 |
+
("安定性","業歴"): 4,
|
| 103 |
+
|
| 104 |
+
("公平性・総合世評","ディスクロージャー"): 8,
|
| 105 |
}
|
| 106 |
_WEIGHT_NORM = 100.0 / float(sum(_WEIGHTS.values()))
|
| 107 |
|
|
|
|
| 150 |
return df.loc[m, "値"].values[0] if m.any() else None
|
| 151 |
|
| 152 |
items = []
|
| 153 |
+
|
| 154 |
+
# ---- 成長率(定量) ----
|
| 155 |
+
market_cagr=_to_float(ref("市場CAGR(%)"))
|
| 156 |
+
s1=_to_float(ref("売上_期1(最古期)")); s2=_to_float(ref("売上_期2")); s3=_to_float(ref("売上_期3(最新期)"))
|
| 157 |
+
p1=_to_float(ref("営業利益_期1(最古期)")); p2=_to_float(ref("営業利益_期2")); p3=_to_float(ref("営業利益_期3(最新期)"))
|
| 158 |
+
prod_n=_to_float(ref("主力商品数")); prod_g=_to_float(ref("成長中主力商品数"))
|
| 159 |
+
|
| 160 |
+
def cagr(v1, v3):
|
| 161 |
+
if v1 is None or v3 is None or v1 <= 0: return None
|
| 162 |
+
try: return (v3/v1)**(1/2) - 1.0
|
| 163 |
+
except Exception: return None
|
| 164 |
+
|
| 165 |
+
s_cagr = cagr(s1, s3); p_cagr = cagr(p1, p3)
|
| 166 |
+
# 市場CAGR 12%で満点、-3%で0点(景気循環を意識しやや厳しめ)
|
| 167 |
+
_add(items,"成長率","市場成長率", _ramp(market_cagr,12,-3), _WEIGHTS[("成長率","市場成長率")],
|
| 168 |
+
f"市場CAGR {market_cagr if market_cagr is not None else '—'}%")
|
| 169 |
+
_add(items,"成長率","売上高伸長性", _ramp(s_cagr,0.10,-0.05), _WEIGHTS[("成長率","売上高伸長性")],
|
| 170 |
+
f"CAGR売上{round((s_cagr or 0)*100,1) if s_cagr is not None else '—'}%")
|
| 171 |
+
_add(items,"成長率","利益伸長性", _ramp(p_cagr,0.10,-0.05), _WEIGHTS[("成長率","利益伸長性")],
|
| 172 |
+
f"CAGR営業{round((p_cagr or 0)*100,1) if p_cagr is not None else '—'}%")
|
| 173 |
+
# 製品ポートフォリオの定量化(成長中比率)
|
| 174 |
+
grow_ratio = _ratio(prod_g, prod_n)
|
| 175 |
+
_add(items,"成長率","商品", _ramp(grow_ratio,0.6,0.1), _WEIGHTS[("成長率","商品")],
|
| 176 |
+
f"成長中比率{round((grow_ratio or 0)*100,1) if grow_ratio is not None else '—'}%")
|
| 177 |
+
|
| 178 |
+
# ---- 経営者能力 ----
|
| 179 |
yoy3=_to_float(ref("予実達成率_3年平均(%)"))
|
| 180 |
audit_bad=_to_float(ref("監査・内部統制の重大な不備 件数(過去3年)"))
|
| 181 |
comp_bad=_to_float(ref("重大コンプライアンス件数(過去3年)"))
|
|
|
|
| 188 |
bk_years=_to_float(ref("倒産からの経過年数"))
|
| 189 |
incidents=_to_float(ref("重大事件・事故件数(過去10年)"))
|
| 190 |
|
| 191 |
+
cash_to_ms=_ratio(cash, sales_m)
|
|
|
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
mg_att = (_ramp(yoy3, 90,50)+_ramp(0 if not audit_bad else -audit_bad,0,-3)+_ramp(0 if not comp_bad else -comp_bad,0,-2)+_ramp(indep,33,0))/4
|
| 194 |
_add(items,"経営者能力","経営姿勢",mg_att,_WEIGHTS[("経営者能力","経営姿勢")],f"予実{yoy3 or '—'}%/監査{audit_bad or 0}/違反{comp_bad or 0}/社外{indep or '—'}%")
|
| 195 |
|
|
|
|
| 207 |
pen=10.0; rs="事故/倒産なし"
|
| 208 |
_add(items,"経営者能力","減点事項",pen,_WEIGHTS[("経営者能力","減点事項")],rs)
|
| 209 |
|
| 210 |
+
# ---- 安定性 ----
|
| 211 |
+
equity=_to_float(ref("自己資本比率(%)"))
|
| 212 |
+
delay_cnt=_to_float(ref("支払遅延件数(直近12ヶ月)"))
|
| 213 |
+
boun_cnt=_to_float(ref("不渡り件数(直近12ヶ月)"))
|
| 214 |
+
delay_days=_to_float(ref("平均支払遅延日数"))
|
| 215 |
+
mainbank=_to_bool(ref("メインバンク明確か(TRUE/FALSE)"))
|
| 216 |
+
main_share=_to_float(ref("メインバンク借入シェア(%)"))
|
| 217 |
+
has_line=_to_bool(ref("コミットメントライン等の長期与信枠あり(TRUE/FALSE)"))
|
| 218 |
+
coll_to_ms=_ratio(_to_float(ref("担保余力評価額(円)")), _to_float(ref("月商(円)_再掲")) or sales_m)
|
| 219 |
|
|
|
|
| 220 |
_add(items,"安定性","自己資本", _ramp(equity,40,5), _WEIGHTS[("安定性","自己資本")], f"自己資本比率{equity or '—'}%")
|
| 221 |
|
| 222 |
if (delay_cnt is not None) or (boun_cnt is not None) or (delay_days is not None):
|
|
|
|
| 230 |
sc_mb += 2.0 if mainbank else (-0.5 if mainbank is False else 0)
|
| 231 |
sc_mb += 1.0 if has_line else 0
|
| 232 |
sc_mb = _clamp(sc_mb,0,10)
|
| 233 |
+
_add(items,"安定性","金融取引", sc_mb, _WEIGHTS[("安定性","金融取引")],
|
| 234 |
+
f"メイン{'有' if mainbank else '無' if mainbank is False else '—'}/与信枠{'有' if has_line else '無' if has_line is False else '—'}")
|
| 235 |
|
| 236 |
+
_add(items,"安定性","資産担保余力", _ramp(coll_to_ms,4.0,0.0), _WEIGHTS[("安定性","資産担保余力")],
|
| 237 |
+
f"担保/月商≈{round(coll_to_ms,2) if coll_to_ms else '—'}")
|
| 238 |
|
| 239 |
+
top1=_to_float(ref("主要顧客上位1社売上比率(%)"))
|
| 240 |
+
cust_score=_to_float(ref("主要顧客の平均信用スコア(0-100)"))
|
| 241 |
+
npl_cnt=_to_float(ref("不良債権件数(直近12ヶ月)"))
|
| 242 |
_add(items,"安定性","取引先", ( _ramp(- (top1 or 50),0,-80) + _ramp(cust_score,80,50) + _ramp(- (npl_cnt or 1),0,-3) )/3,
|
| 243 |
_WEIGHTS[("安定性","取引先")], f"上位1社{top1 or '—'}%/信用{cust_score or '—'}/不良{int(npl_cnt or 0)}")
|
| 244 |
|
| 245 |
+
years=_to_float(ref("業歴(年)"))
|
| 246 |
_add(items,"安定性","業歴", _ramp(years,20,1), _WEIGHTS[("安定性","業歴")], f"{years or '—'}年")
|
| 247 |
|
| 248 |
+
# ---- 公平性 ----
|
| 249 |
+
has_sec=_to_bool(ref("有価証券報告書提出企業か(TRUE/FALSE)"))
|
| 250 |
+
pub_off=_to_bool(ref("決算公告や官報での公開あり(TRUE/FALSE)"))
|
| 251 |
+
pub_web=_to_bool(ref("HP/IRサイトで財務資料公開あり(TRUE/FALSE)"))
|
| 252 |
+
upd_on=_to_bool(ref("直近更新が定め通りか(TRUE/FALSE)"))
|
| 253 |
sc_dis = 0.0
|
| 254 |
sc_dis += 10.0 if has_sec else (7.0 if (pub_off or pub_web) else 4.0)
|
| 255 |
sc_dis += 1.0 if upd_on else 0.0
|