File size: 6,790 Bytes
1baf215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""History-only baselines with the exact same delta-query interface."""

from __future__ import annotations

import numpy as np
import torch


class PersistenceBaseline(torch.nn.Module):
    def __init__(self, std=0.1):
        super().__init__()
        self.std = std

    def forward(self, batch):
        mean = torch.zeros_like(batch["query_horizons"])
        return mean, torch.full_like(mean, float(np.log(self.std)))


class HistoryLinearBaseline(torch.nn.Module):
    """Fit a local affine controlled state transition separately for each window.

    Ridge is centered on persistence. Only observed historical transition pairs
    are fitted, then planned actions and exogenous inputs are rolled out. Hidden
    simulator parameters, future targets, and test-wide fitting are prohibited.
    """

    def __init__(self, role_ids, ridge=1.0, min_std=0.05, dt_minutes=15):
        super().__init__()
        self.roles = np.asarray(role_ids)
        self.ridge, self.min_std, self.dt_minutes = ridge, min_std, dt_minutes

    def forward(self, batch):
        device = batch["history"].device
        values = {k: v.detach().cpu().numpy() for k, v in batch.items() if torch.is_tensor(v)}
        outputs, uncertainty = [], []
        for index in range(len(values["history"])):
            history, mask = values["history"][index], values["history_mask"][index].astype(bool)
            active = values["field_mask"][index].astype(bool)
            state_ids = np.flatnonzero(active & (self.roles == 0))
            input_ids = np.flatnonzero(active & (self.roles != 0))
            current = values["current"][index, state_ids].astype(float)
            # Impute absent current fields only from this window's observations.
            for j, field in enumerate(state_ids):
                if not values["current_mask"][index, field]:
                    observed = np.flatnonzero(mask[:, field])
                    current[j] = history[observed[-1], field] if len(observed) else 0
            features = np.concatenate(
                [history[:-1, state_ids], history[:-1, input_ids], np.ones((len(history) - 1, 1))], axis=1
            )
            targets = history[1:, state_ids] - history[:-1, state_ids]
            valid = mask[:-1, state_ids].all(axis=1) & mask[1:, state_ids].all(axis=1)
            if len(input_ids):
                valid &= mask[:-1, input_ids].all(axis=1)
            x, y = features[valid], targets[valid]
            if len(x):
                coef = np.linalg.solve(x.T @ x + self.ridge * np.eye(x.shape[1]), x.T @ y)
                residual_std = np.maximum(np.sqrt(np.mean((y - x @ coef) ** 2, axis=0)), self.min_std)
            else:
                coef = np.zeros((features.shape[1], len(state_ids)))
                residual_std = np.full(len(state_ids), self.min_std)
            predicted, state = [], current.copy()
            for actions, exogenous in zip(values["future_actions"][index], values["future_exogenous"][index]):
                inputs = (actions + exogenous)[input_ids]
                state = state + np.concatenate([state, inputs, [1]]) @ coef
                predicted.append(state.copy())
            means, stds = [], []
            lookup = {int(field): j for j, field in enumerate(state_ids)}
            for field, horizon in zip(values["query_fields"][index], values["query_horizons"][index]):
                steps = round(float(horizon) / self.dt_minutes)
                if steps < 1 or steps > len(predicted) or field not in lookup:
                    means.append(0.0)
                    stds.append(self.min_std)
                    continue
                j = lookup[int(field)]
                means.append(predicted[steps - 1][j] - current[j])
                stds.append(residual_std[j] * np.sqrt(steps))
            outputs.append(means)
            uncertainty.append(np.log(stds))
        return (
            torch.tensor(np.asarray(outputs), dtype=torch.float32, device=device),
            torch.tensor(np.asarray(uncertainty), dtype=torch.float32, device=device),
        )


def catboost_features(batch):
    """Canonical masks and semantic query tokens; no family or system metadata."""
    keys = (
        "history",
        "history_mask",
        "history_dt",
        "current",
        "current_mask",
        "future_actions",
        "future_exogenous",
        "future_dt",
        "field_mask",
    )
    n = len(batch["history"])
    q = batch["query_fields"].shape[1]
    parts = []
    before_query = (
        np.asarray(batch["future_dt"])[:, None, :] <= np.asarray(batch["query_horizons"])[:, :, None]
    )
    for key in keys:
        expanded = np.repeat(np.asarray(batch[key])[:, None], q, axis=1)
        if key in ("future_actions", "future_exogenous"):
            expanded = expanded * before_query[:, :, :, None]
        elif key == "future_dt":
            expanded = expanded * before_query
        parts.append(expanded.reshape(n * q, -1))
    return np.concatenate(
        [
            *parts,
            np.asarray(batch["query_fields"]).reshape(-1, 1),
            np.asarray(batch["query_horizons"]).reshape(-1, 1),
        ],
        axis=1,
    )


class CatBoostBaseline:
    """Optional pooled query regression fitted on training windows only."""

    def __init__(self, iterations=200, seed=0):
        self.iterations, self.seed = iterations, seed

    def fit(self, batch, *, training_system_ids, fitted_system_ids):
        if not set(fitted_system_ids) <= set(training_system_ids) or not fitted_system_ids:
            raise ValueError("CatBoost may only be fitted on nonempty training systems")
        from catboost import CatBoostRegressor

        features, mask = catboost_features(batch), np.asarray(batch["target_mask"]).reshape(-1).astype(bool)
        targets = np.asarray(batch["target"]).reshape(-1)
        self.model = CatBoostRegressor(
            iterations=self.iterations,
            depth=6,
            learning_rate=0.05,
            random_seed=self.seed,
            verbose=False,
            allow_writing_files=False,
        )
        self.model.fit(features[mask], targets[mask])
        residual = targets[mask] - self.model.predict(features[mask])
        self.std = max(float(np.sqrt(np.mean(residual**2))), 0.05)
        self.fitted_system_ids = sorted(set(fitted_system_ids))
        return self

    def __call__(self, batch):
        numpy_batch = {k: v.detach().cpu().numpy() if torch.is_tensor(v) else v for k, v in batch.items()}
        mean = self.model.predict(catboost_features(numpy_batch)).reshape(batch["query_fields"].shape)
        device = batch["history"].device
        mean = torch.tensor(mean, dtype=torch.float32, device=device)
        return mean, torch.full_like(mean, np.log(self.std))