مواد پر جائیں
لائبریری کی تمام دستاویزات

اثاثوں کی توجہ کے لیے میکرو گروپ مجاورت ماسک

کوڈ Machine Learning for Trading

خلاصہ

یہ یوٹیلٹی اثاثہ گروپ کے لیبلز اور گروپوں کے درمیان اختیاری روابط سے اثاثوں کی باہمی مجاورت کا بولین میٹرکس بناتی ہے۔ ایک ہی میکرو گروپ میں رکھے گئے اثاثے ایک دوسرے سے منسلک ہوتے ہیں، اور فراہم کردہ ہر بین الجماعتی کنارہ دونوں گروپوں کے تمام ارکان کو دونوں سمتوں میں جوڑتا ہے۔ اختیاری خود روابط ہر اثاثے کو اپنی طرف توجہ دینے دیتے ہیں۔ جن اثاثوں کی نقشہ بندی نہیں ہوتی انہیں نامعلوم گروپ دیا جاتا ہے، اس لیے وہ دوسرے غیر نقشہ شدہ اثاثوں سے منسلک ہوتے ہیں۔

دوسرا فنکشن مجاورت میٹرکس کو توجہ ماسک میں بدلتا ہے اور غیر متصل جوڑوں کو ممنوع قرار دیتا ہے۔ تبدیلی سے پہلے کوڈ جانچتا ہے کہ ان پٹ میٹرکس مربع ہے۔ اس سے میکرو گروپ بندی کی پیشگی معلومات کو توجہ کے ماڈل میں سادہ انداز میں شامل کیا جا سکتا ہے، مگر یہ مارکیٹ ڈیٹا سے گروپ یا تعلقات کا تخمینہ نہیں لگاتا۔ اس کی افادیت فراہم کردہ نقشے اور بین الجماعتی روابط کے معیار اور تازگی پر منحصر ہے۔ دستاویز تجرباتی نتائج یا ٹریڈنگ جانچ کے بجائے نفاذ کی تفصیلات دیتی ہے۔

اہم خیالات

  • ایک ہی گروپ کے اثاثوں کو مجاورت میٹرکس میں باہمی روابط ملتے ہیں۔
  • بین الجماعتی روابط ایک گروپ کے ہر اثاثے کو دوسرے گروپ کے ہر اثاثے سے دونوں سمتوں میں جوڑتے ہیں۔
  • پیرامیٹر کے ذریعے خود روابط شامل یا خارج کیے جا سکتے ہیں۔
  • بے نقشہ اثاثے ایک نامعلوم گروپ میں آتے ہیں اور اسی لیے ایک دوسرے سے منسلک ہوتے ہیں۔
  • توجہ ماسک ہر غیر متصل اثاثہ جوڑے کو ممنوع قرار دیتا ہے۔

ٹیگز

مکمل متن
# 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 کے تحقیقی ایجنٹ نے لکھا ہے؛ یہ ماخذ کی نقل نہیں۔