s-ttp commited on
Commit
b428910
·
verified ·
1 Parent(s): 1feede4

Upload engine/predict_yearly.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. engine/predict_yearly.py +91 -9
engine/predict_yearly.py CHANGED
@@ -154,6 +154,11 @@ WRITING RULES (strict — these govern style, not your view):
154
  - ARITHMETIC: every product, ratio or percentage you state must be correct to the figures you cite.
155
  The market composite is a BLEND of several methods; it is never "EPS × one multiple". If you
156
  want to cite a multiple, cite the forward P/E at the current price and say so.
 
 
 
 
 
157
  - Use ONLY these canonical names for the valuation anchors, exactly:
158
  "story value" (the Damodaran story-driven intrinsic value), "mechanical DCF" (the formula-based
159
  cross-check), "market composite" (the multiples + analyst-target pricing blend), and
@@ -4838,7 +4843,7 @@ EPS Forecast Detail:
4838
  Anchor flag: {pred_fund.get('eps_anchor_flag', 'NO_ANCHOR')}
4839
  Cyclical adjustment: {pred_fund.get('eps_cyclical_adjustment') or 'None'}
4840
  EPS-FCF inconsistency: {pred_fund.get('eps_fcf_inconsistency') or 'None'}
4841
- Historical beat rate: {pred_fund.get('eps_beat_rate', 0.5)*100:.0f}%
4842
  Avg surprise: {pred_fund.get('eps_avg_surprise_pct', 0):+.1f}%
4843
  """
4844
  else:
@@ -4852,7 +4857,7 @@ EPS Forecast Detail:
4852
  Anchor flag: {pred_fund.get('eps_anchor_flag', 'NO_ANCHOR')}
4853
  Cyclical adjustment: {pred_fund.get('eps_cyclical_adjustment') or 'None'}
4854
  EPS-FCF inconsistency: {pred_fund.get('eps_fcf_inconsistency') or 'None'}
4855
- Historical beat rate: {pred_fund.get('eps_beat_rate', 0.5)*100:.0f}%
4856
  Avg surprise: {pred_fund.get('eps_avg_surprise_pct', 0):+.1f}%
4857
  """
4858
  lines.append(eps_block.strip())
@@ -4881,7 +4886,7 @@ EPS Forecast Detail:
4881
  avg_surp = pred_fund.get("eps_avg_surprise_pct")
4882
  if beat_rate is not None and avg_surp is not None:
4883
  beat_sym = "▲" if beat_rate >= 0.65 else "▼" if beat_rate <= 0.35 else "→"
4884
- lines.append(f" EPS surprise history: beat rate {beat_rate*100:.0f}% {beat_sym} "
4885
  f"avg surprise {avg_surp:+.1f}% (last 8 qtrs)")
4886
  lines.append(f" NOTE: Predictions based on quarterly trend ML — treat as directional, not precise.")
4887
  parts.append("\n".join(lines))
@@ -5041,7 +5046,7 @@ EPS Forecast Detail:
5041
  parts.append(f"""SENTIMENT & INSIDER CONTEXT (qualitative — not in blend):
5042
  News sentiment: {s.get('news_sentiment',0):+.4f} ({s.get('news_n_articles','?')} articles, trend:{mom_lbl})
5043
  Insider score: {ins_str} ({s.get('insider_detail','no detail')})
5044
- Put/Call ratio: {s.get('options_pc_ratio','N/A')} | Earnings beat rate: {s.get('earnings_score','N/A')}""")
5045
 
5046
  # Detailed insider transactions
5047
  if insider:
@@ -5205,7 +5210,12 @@ _PM_SYNTHESIS_USER_TEMPLATE_YEARLY = _STYLE_RULES + """STOCK: {ticker} | CURRENT
5205
 
5206
  CROSS-SECTIONAL QUANT RANK (12-month panel-ML — Crucible's most BACKTESTED signal; weight it
5207
  heavily as an independent prior, above either advocate's narrative): {panel_ml}
5208
- VALUE vs PRICE: the signals include a Damodaran intrinsic value (DCF / story-driven FCFF) and a market-composite price. Treat the gap between them as a first-class input — your synthesis MUST say explicitly whether you expect the gap to close toward value, toward price, or persist, and why.
 
 
 
 
 
5209
 
5210
  NEUTRAL ANALYST (objective baseline — heard before the advocates):
5211
  Return: {neutral_return:+.2f}% | Recommendation: {neutral_rec} | Confidence: {neutral_conf:.0f}%
@@ -5254,6 +5264,31 @@ Respond with ONLY this JSON:
5254
  }}"""
5255
 
5256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5257
  def _emit_role(role, phase, entry, *, ret_key="expected_return"):
5258
  """Stream one analyst's FULL result (verdict + reasoning) to stdout the instant it
5259
  resolves, as a machine-parseable sentinel line. The SSE wrapper turns this into a
@@ -5636,6 +5671,7 @@ def analyze_llm_debate_yearly(
5636
  _PM_SYNTHESIS_USER_TEMPLATE_YEARLY.format(
5637
  ticker=ticker, current_price=current_price, months=12,
5638
  panel_ml=(_panel_ml_summary(ticker) or "not in the top-1000 universe (no rank)"),
 
5639
  neutral_return=_nr_ret, neutral_rec=_nr_rec,
5640
  neutral_conf=_nr_conf, neutral_reasoning=_nr_rsn,
5641
  bear_return=_br["expected_return"] * 100,
@@ -7646,6 +7682,37 @@ def _restatement_from_actions(actions, statements_through):
7646
  return None
7647
 
7648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7649
  def _nominate_peers(ticker, name, industry, description, call_json):
7650
  """Business-model comparables nominated by a model and later verified in the panel. Sector +
7651
  market-cap matching put banks and insurers next to a ratings/index company (SPGI)."""
@@ -8075,6 +8142,15 @@ Examples:
8075
  all_data["corp_actions"] = _corporate_actions_brief(ticker, _ovs.get("Name"), _events_call)
8076
  _stq0 = (((all_data.get("income") or {}).get("quarterlyReports") or [{}])[0] or {}).get("fiscalDateEnding")
8077
  all_data["restatement"] = _restatement_from_actions(all_data["corp_actions"], _stq0)
 
 
 
 
 
 
 
 
 
8078
  all_data["pending_deals"] = [a for a in all_data["corp_actions"] if a.get("status") == "pending"]
8079
  if all_data.get("corp_actions"):
8080
  print(" [actions] " + " | ".join(f"{a['kind']} {a['business']} ({a['status']}{', ' + a['effective'] if a.get('effective') else ''})"
@@ -8240,15 +8316,21 @@ Examples:
8240
  # don't set the consensus (GPT-first and Claude-first; Gemini as the shared fallback).
8241
  def _story_call(system, user):
8242
  out = _dp._call_frontier_json(user, system=system)
8243
- if out is None: out = _dp._call_claude_json(system, user)
8244
  if out is None: out = _dp._call_gemini_json(system, user)
8245
  return out
8246
  def _story_call2(system, user):
8247
- out = _dp._call_claude_json(system, user)
8248
  if out is None: out = _dp._call_gemini_json(system, user)
8249
  if out is None: out = _dp._call_frontier_json(user, system=system)
8250
  return out
8251
- story_val_result = _sd.story_value(_ctx, _story_call, _story_call2, verbose=args.verbose)
 
 
 
 
 
 
8252
  if not story_val_result:
8253
  print(f" Damodaran story-FCFF: n/a — {getattr(_sd, 'LAST_REASON', None) or 'unknown reason'}")
8254
  if story_val_result:
@@ -8608,7 +8690,7 @@ Examples:
8608
  "sma50": safe_float(_ov.get("50DayMovingAverage")), "sma200": safe_float(_ov.get("200DayMovingAverage"))},
8609
  "styles": (quant_ctx or {}).get("styles"), "quant_rank_pct": None,
8610
  "summary": _rd.get("ai_summary"),
8611
- "text_audit": (_rd.get("llm_debate") or {}).get("text_audit") or [],
8612
  }
8613
  try:
8614
  _rd["diagnostics"]["quant_rank_pct"] = round(100 * (1 - (quant_ctx["comp_rank"] - 1) / quant_ctx["universe_n"]))
 
154
  - ARITHMETIC: every product, ratio or percentage you state must be correct to the figures you cite.
155
  The market composite is a BLEND of several methods; it is never "EPS × one multiple". If you
156
  want to cite a multiple, cite the forward P/E at the current price and say so.
157
+ - ATTRIBUTION: the valuation is Crucible's own application of a published framework. Write "the
158
+ story value" or "the Damodaran-style value" — never "Damodaran's value/DCF/target", which implies
159
+ he produced or endorsed the number.
160
+ - The intrinsic-value gap you cite is the STORY VALUE vs price. The mechanical DCF is a cross-check:
161
+ name it as such and never present its gap as "the intrinsic gap" or "the premium to fair value".
162
  - Use ONLY these canonical names for the valuation anchors, exactly:
163
  "story value" (the Damodaran story-driven intrinsic value), "mechanical DCF" (the formula-based
164
  cross-check), "market composite" (the multiples + analyst-target pricing blend), and
 
4843
  Anchor flag: {pred_fund.get('eps_anchor_flag', 'NO_ANCHOR')}
4844
  Cyclical adjustment: {pred_fund.get('eps_cyclical_adjustment') or 'None'}
4845
  EPS-FCF inconsistency: {pred_fund.get('eps_fcf_inconsistency') or 'None'}
4846
+ Historical beat rate (last 8 quarters): {pred_fund.get('eps_beat_rate', 0.5)*100:.0f}%
4847
  Avg surprise: {pred_fund.get('eps_avg_surprise_pct', 0):+.1f}%
4848
  """
4849
  else:
 
4857
  Anchor flag: {pred_fund.get('eps_anchor_flag', 'NO_ANCHOR')}
4858
  Cyclical adjustment: {pred_fund.get('eps_cyclical_adjustment') or 'None'}
4859
  EPS-FCF inconsistency: {pred_fund.get('eps_fcf_inconsistency') or 'None'}
4860
+ Historical beat rate (last 8 quarters): {pred_fund.get('eps_beat_rate', 0.5)*100:.0f}%
4861
  Avg surprise: {pred_fund.get('eps_avg_surprise_pct', 0):+.1f}%
4862
  """
4863
  lines.append(eps_block.strip())
 
4886
  avg_surp = pred_fund.get("eps_avg_surprise_pct")
4887
  if beat_rate is not None and avg_surp is not None:
4888
  beat_sym = "▲" if beat_rate >= 0.65 else "▼" if beat_rate <= 0.35 else "→"
4889
+ lines.append(f" EPS surprise history (last 8 quarters): beat rate {beat_rate*100:.0f}% {beat_sym} "
4890
  f"avg surprise {avg_surp:+.1f}% (last 8 qtrs)")
4891
  lines.append(f" NOTE: Predictions based on quarterly trend ML — treat as directional, not precise.")
4892
  parts.append("\n".join(lines))
 
5046
  parts.append(f"""SENTIMENT & INSIDER CONTEXT (qualitative — not in blend):
5047
  News sentiment: {s.get('news_sentiment',0):+.4f} ({s.get('news_n_articles','?')} articles, trend:{mom_lbl})
5048
  Insider score: {ins_str} ({s.get('insider_detail','no detail')})
5049
+ Put/Call ratio: {s.get('options_pc_ratio','N/A')} | Earnings-quality score (0-1 composite, NOT a beat rate): {s.get('earnings_score','N/A')}""")
5050
 
5051
  # Detailed insider transactions
5052
  if insider:
 
5210
 
5211
  CROSS-SECTIONAL QUANT RANK (12-month panel-ML — Crucible's most BACKTESTED signal; weight it
5212
  heavily as an independent prior, above either advocate's narrative): {panel_ml}
5213
+ VALUE vs PRICE — canonical anchors (use these numbers and names; the advocates may have mislabelled them):
5214
+ {anchors}
5215
+ The STORY VALUE is the intrinsic value. The mechanical DCF is only a formula cross-check of it — never quote the
5216
+ mechanical-DCF gap as "the intrinsic gap" or "the premium to fair value". The market composite is PRICING (what the
5217
+ market's own yardsticks imply), not value. Treat the story-value-vs-price gap as a first-class input — your synthesis
5218
+ MUST say explicitly whether you expect that gap to close toward value, toward price, or persist, and why.
5219
 
5220
  NEUTRAL ANALYST (objective baseline — heard before the advocates):
5221
  Return: {neutral_return:+.2f}% | Recommendation: {neutral_rec} | Confidence: {neutral_conf:.0f}%
 
5264
  }}"""
5265
 
5266
 
5267
+ def _pm_anchor_lines(current_price, fair_val, pred_fund, data):
5268
+ """Canonical valuation anchors for the PM (and the text audit): the PM previously took the
5269
+ bear's framing ('45% above mechanical DCF') as the intrinsic gap when the story value sat 8%
5270
+ below price (SPGI)."""
5271
+ fv = fair_val or {}
5272
+ story = fv.get("story") or globals().get("_CRUCIBLE_STORY") or {}
5273
+ out = [f" • current price ${current_price:,.2f}"]
5274
+ if story.get("value"):
5275
+ out.append(f" • story value (INTRINSIC, Damodaran-style story-driven FCFF): ${story['value']:,.2f} ({(story['value']/current_price-1)*100:+.1f}% vs price)")
5276
+ _dcf = ((fv.get("methods") or {}).get("DCF") or {}).get("price")
5277
+ if _dcf:
5278
+ out.append(f" • mechanical DCF (formula cross-check only): ${_dcf:,.2f} ({(_dcf/current_price-1)*100:+.1f}%)")
5279
+ if fv.get("fair_price"):
5280
+ out.append(f" • market composite (pricing: multiples + analyst targets): ${fv['fair_price']:,.2f} ({(fv['fair_price']/current_price-1)*100:+.1f}%)")
5281
+ _eps = (pred_fund or {}).get("eps_annual")
5282
+ if _eps:
5283
+ out.append(f" • engine forward EPS ${_eps:,.2f} → forward P/E {current_price/_eps:.1f}x at the current price")
5284
+ _rst = (data or {}).get("restatement")
5285
+ if _rst:
5286
+ out.append(f" • RESTATEMENT: {_rst['note']}")
5287
+ for a in (data or {}).get("pending_deals") or []:
5288
+ out.append(f" • PENDING (not closed): {a['kind'].replace('_', ' ')} of {a['business']}")
5289
+ return "\n".join(out)
5290
+
5291
+
5292
  def _emit_role(role, phase, entry, *, ret_key="expected_return"):
5293
  """Stream one analyst's FULL result (verdict + reasoning) to stdout the instant it
5294
  resolves, as a machine-parseable sentinel line. The SSE wrapper turns this into a
 
5671
  _PM_SYNTHESIS_USER_TEMPLATE_YEARLY.format(
5672
  ticker=ticker, current_price=current_price, months=12,
5673
  panel_ml=(_panel_ml_summary(ticker) or "not in the top-1000 universe (no rank)"),
5674
+ anchors=_pm_anchor_lines(current_price, fair_val, pred_fund, data),
5675
  neutral_return=_nr_ret, neutral_rec=_nr_rec,
5676
  neutral_conf=_nr_conf, neutral_reasoning=_nr_rsn,
5677
  bear_return=_br["expected_return"] * 100,
 
7682
  return None
7683
 
7684
 
7685
+ def _restatement_from_consensus(eec, rev_ttm, rev_growth_yoy, actions, statements_through):
7686
+ """Consensus-vs-TTM restatement signal (see call site). Requires >=8 analysts, current-FY
7687
+ consensus revenue <95% of reported TTM, a next-FY consensus that is ALSO below TTM, and
7688
+ positive reported growth (a shrinking company legitimately has consensus below TTM)."""
7689
+ try:
7690
+ cur, nxt = eec.get("current_year_revenue"), eec.get("next_year_revenue")
7691
+ n = int(eec.get("analyst_count") or 0)
7692
+ g = rev_growth_yoy if rev_growth_yoy is not None else None
7693
+ if g is not None and abs(g) > 1.0: g = g / 100.0
7694
+ if not (cur and rev_ttm and rev_ttm > 0 and n >= 8):
7695
+ return None
7696
+ if not (cur < 0.95 * rev_ttm and (nxt is None or nxt < rev_ttm) and (g is None or g > 0.02)):
7697
+ return None
7698
+ unit = None
7699
+ for a in sorted(actions or [], key=lambda x: -(x.get("revenue_share") or 0)):
7700
+ if a.get("kind") in ("spin_off", "divestiture"):
7701
+ unit = a; break
7702
+ biz = unit["business"] if unit else "a divested business"
7703
+ gap = 1.0 - cur / rev_ttm
7704
+ return {"kind": (unit or {}).get("kind") or "divestiture", "business": biz, "effective": (unit or {}).get("effective"),
7705
+ "revenue_share": (unit or {}).get("revenue_share") or round(gap, 2), "statements_through": str(statements_through)[:10],
7706
+ "basis": "consensus", "consensus_current_fy_usd_b": round(cur / 1e9, 2), "reported_ttm_usd_b": round(rev_ttm / 1e9, 2),
7707
+ "note": (f"Sell-side consensus revenue for the current fiscal year (${cur/1e9:,.1f}B, {n} analysts) is "
7708
+ f"{gap*100:.0f}% below reported trailing-twelve-month revenue (${rev_ttm/1e9:,.1f}B) although reported revenue "
7709
+ f"grew {g*100:.0f}% year over year — analysts are modelling a smaller continuing company, consistent with "
7710
+ f"the {(unit or {}).get('kind','divestiture').replace('_',' ')} of {biz} having completed; reported TTM revenue, "
7711
+ f"margins and multiples still include it.")}
7712
+ except Exception: # noqa: BLE001
7713
+ return None
7714
+
7715
+
7716
  def _nominate_peers(ticker, name, industry, description, call_json):
7717
  """Business-model comparables nominated by a model and later verified in the panel. Sector +
7718
  market-cap matching put banks and insurers next to a ratings/index company (SPGI)."""
 
8142
  all_data["corp_actions"] = _corporate_actions_brief(ticker, _ovs.get("Name"), _events_call)
8143
  _stq0 = (((all_data.get("income") or {}).get("quarterlyReports") or [{}])[0] or {}).get("fiscalDateEnding")
8144
  all_data["restatement"] = _restatement_from_actions(all_data["corp_actions"], _stq0)
8145
+ if not all_data["restatement"]:
8146
+ # Data-driven fallback: model recall lags reality (it still had S&P Global's
8147
+ # July-2026 Mobility spin as "pending"). If sell-side consensus revenue for the
8148
+ # CURRENT fiscal year sits well below reported TTM revenue while the company is
8149
+ # growing, analysts are modelling a smaller continuing company — a completed
8150
+ # spin-off/divestiture is the only ordinary explanation.
8151
+ all_data["restatement"] = _restatement_from_consensus(
8152
+ all_data.get("earnings_estimates_consensus") or {}, safe_float(_ovs.get("RevenueTTM")),
8153
+ safe_float(_ovs.get("QuarterlyRevenueGrowthYOY")), all_data["corp_actions"], _stq0)
8154
  all_data["pending_deals"] = [a for a in all_data["corp_actions"] if a.get("status") == "pending"]
8155
  if all_data.get("corp_actions"):
8156
  print(" [actions] " + " | ".join(f"{a['kind']} {a['business']} ({a['status']}{', ' + a['effective'] if a.get('effective') else ''})"
 
8316
  # don't set the consensus (GPT-first and Claude-first; Gemini as the shared fallback).
8317
  def _story_call(system, user):
8318
  out = _dp._call_frontier_json(user, system=system)
8319
+ if out is None: out = _dp._call_claude_json(system, user, temperature=0.3)
8320
  if out is None: out = _dp._call_gemini_json(system, user)
8321
  return out
8322
  def _story_call2(system, user):
8323
+ out = _dp._call_claude_json(system, user, temperature=0.3)
8324
  if out is None: out = _dp._call_gemini_json(system, user)
8325
  if out is None: out = _dp._call_frontier_json(user, system=system)
8326
  return out
8327
+ # Third, Gemini-led storyteller: the consensus is the MEDIAN of three sets of
8328
+ # assumptions, so a single drifting story can't move the value (run-to-run stability).
8329
+ def _story_call3(system, user):
8330
+ out = _dp._call_gemini_json(system, user)
8331
+ if out is None: out = _dp._call_claude_json(system, user, temperature=0.3)
8332
+ return out
8333
+ story_val_result = _sd.story_value(_ctx, _story_call, _story_call2, _story_call3, verbose=args.verbose)
8334
  if not story_val_result:
8335
  print(f" Damodaran story-FCFF: n/a — {getattr(_sd, 'LAST_REASON', None) or 'unknown reason'}")
8336
  if story_val_result:
 
8690
  "sma50": safe_float(_ov.get("50DayMovingAverage")), "sma200": safe_float(_ov.get("200DayMovingAverage"))},
8691
  "styles": (quant_ctx or {}).get("styles"), "quant_rank_pct": None,
8692
  "summary": _rd.get("ai_summary"),
8693
+ "text_audit": ((llm_result or {}).get("text_audit") if isinstance(llm_result, dict) else None) or (_rd.get("llm_debate") or {}).get("text_audit") or [],
8694
  }
8695
  try:
8696
  _rd["diagnostics"]["quant_rank_pct"] = round(100 * (1 - (quant_ctx["comp_rank"] - 1) / quant_ctx["universe_n"]))