إنشاء أقنعة تجاور المجموعات الكبرى لانتباه الأصول
الملخص
تنشئ هذه الأداة مصفوفة تجاور منطقية بين الأصول استنادًا إلى تسميات مجموعات الأصول وروابط اختيارية بين المجموعات. ترتبط الأصول المسندة إلى المجموعة الكلية نفسها بعضها ببعض، وتربط كل حافة معطاة بين مجموعتين جميع أعضائهما في الاتجاهين. وتتيح الروابط الذاتية الاختيارية لكل أصل الانتباه إلى نفسه. أما الأصول التي لا تملك تعيينًا فتُسند إلى مجموعة مجهولة، لذا ترتبط بالأصول الأخرى غير المعينة.
تحول دالة ثانية مصفوفة التجاور إلى قناع انتباه عبر تحديد الأزواج غير المتجاورة على أنها ممنوعة. ويتحقق الكود من أن المصفوفة المدخلة مربعة قبل التحويل. يوفر ذلك طريقة بسيطة لترميز افتراض مسبق عن تجميع الأصول في نموذج انتباه، لكنه لا يقدّر المجموعات أو العلاقات من بيانات السوق، وتعتمد فائدته على جودة التعيين المقدم وحداثته وعلى الحواف بين المجموعات. يقدم المستند تفاصيل تنفيذية بدلًا من نتائج تجريبية أو تقييم تداول.
الأفكار الرئيسية
- تحصل الأصول في المجموعة نفسها على روابط متبادلة في مصفوفة التجاور.
- تربط الروابط بين المجموعات كل أصل في مجموعة بكل أصل في المجموعة الأخرى بصورة متناظرة.
- يمكن تضمين الروابط الذاتية أو حذفها بواسطة معلمة.
- تشترك الأصول غير المعينة في مجموعة مجهولة، ولذلك ترتبط بعضها ببعض.
- يحدد قناع الانتباه كل زوج أصول غير متجاور على أنه ممنوع.
الوسوم
النص الكامل
# graph.py
```py
"""Macroeconomic graph prior utilities.
Builds an asset-level adjacency matrix from macro group labels and
cross-group edges for adjacency-masked attention in DeePM.
"""
from __future__ import annotations
from collections.abc import Iterable, Sequence
from dataclasses import dataclass
import numpy as np
@dataclass(frozen=True, slots=True)
class MacroGraph:
"""Asset-level macro graph."""
assets: list[str]
groups: list[str]
adjacency: np.ndarray # Boolean (N, N)
def build_macro_adjacency(
*,
assets: Sequence[str],
asset_to_group: dict[str, str],
cross_group_edges: Iterable[tuple[str, str]] = (),
include_self_loops: bool = True,
) -> MacroGraph:
"""Build a boolean adjacency matrix from group labels and group edges.
Parameters
----------
assets: Asset identifiers matching the price panel columns.
asset_to_group: Mapping from asset -> macro group label.
cross_group_edges: Undirected (group_a, group_b) edges.
include_self_loops: If True, sets A[i,i] = True.
"""
assets_list = [str(a) for a in assets]
groups = [str(asset_to_group.get(a, "UNKNOWN")) for a in assets_list]
n = len(assets_list)
adj = np.zeros((n, n), dtype=bool)
if include_self_loops:
np.fill_diagonal(adj, True)
group_to_indices: dict[str, list[int]] = {}
for i, g in enumerate(groups):
group_to_indices.setdefault(g, []).append(i)
for indices in group_to_indices.values():
idx = np.array(indices, dtype=int)
adj[np.ix_(idx, idx)] = True
for g1, g2 in cross_group_edges:
idx1 = group_to_indices.get(g1, [])
idx2 = group_to_indices.get(g2, [])
if not idx1 or not idx2:
continue
a = np.array(idx1, dtype=int)
b = np.array(idx2, dtype=int)
adj[np.ix_(a, b)] = True
adj[np.ix_(b, a)] = True
return MacroGraph(assets=assets_list, groups=groups, adjacency=adj)
def adjacency_to_attn_mask(adjacency: np.ndarray) -> np.ndarray:
"""Convert boolean adjacency to attention mask (True = disallowed)."""
if adjacency.ndim != 2 or adjacency.shape[0] != adjacency.shape[1]:
raise ValueError("adjacency must be square (N,N)")
return ~adjacency
```يُعرض النص كاملًا مع نسبه إلى مصدره وفقًا لترخيصه. الترخيص: MIT
أعدّ وكيل الأبحاث في Stratmill هذا الملخص استنادًا إلى المصدر الأصلي؛ وهو ليس نسخة منه.