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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
"""
Copyright 2025 ServiceNow
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

# This code is an adaptation of
# https://github.com/ServiceNow/context-is-key-forecasting/blob/main/cik_benchmark/metrics/roi_metric.py
# to make it convenient to use with the Hugging Face version of the Context-is-Key benchmark.
# Please see the __main__ section for an example of how to use it.

import numpy as np
import pandas as pd
from io import StringIO
from datasets import Dataset
from fractions import Fraction


def crps(
    target: np.array,
    samples: np.array,
) -> np.array:
    """
    Compute the CRPS using the probability weighted moment form.
    See Eq ePWM from "Estimation of the Continuous Ranked Probability Score with
    Limited Information and Applications to Ensemble Weather Forecasts"
    https://link.springer.com/article/10.1007/s11004-017-9709-7

    This is a O(n log n) per variable exact implementation, without estimation bias.

    Parameters:
    -----------
    target: np.ndarray
        The target values. (variable dimensions)
    samples: np.ndarray
        The forecast values. (n_samples, variable dimensions)

    Returns:
    --------
    crps: np.ndarray
        The CRPS for each of the (variable dimensions)
    """
    assert (
        target.shape == samples.shape[1:]
    ), f"shapes mismatch between: {target.shape} and {samples.shape}"

    num_samples = samples.shape[0]
    num_dims = samples.ndim
    sorted_samples = np.sort(samples, axis=0)

    abs_diff = (
        np.abs(np.expand_dims(target, axis=0) - sorted_samples).sum(axis=0)
        / num_samples
    )

    beta0 = sorted_samples.sum(axis=0) / num_samples

    # An array from 0 to num_samples - 1, but expanded to allow broadcasting over the variable dimensions
    i_array = np.expand_dims(np.arange(num_samples), axis=tuple(range(1, num_dims)))
    beta1 = (i_array * sorted_samples).sum(axis=0) / (num_samples * (num_samples - 1))

    return abs_diff + beta0 - 2 * beta1


def _crps_ea_Xy_eb_Xy(Xa, ya, Xb, yb):
    """
    Unbiased estimate of:
    E|Xa - ya| * E|Xb' - yb|
    """
    N = len(Xa)
    result = 0.0
    product = np.abs(Xa[:, None] - ya) * np.abs(Xb[None, :] - yb)  # i, j
    i, j = np.diag_indices(N)
    product[i, j] = 0
    result = product.sum()
    return result / (N * (N - 1))


def _crps_ea_XX_eb_XX(Xa, ya, Xb, yb):
    """
    Unbiased estimate of:
    E|Xa - Xa'| * E|Xb'' - Xb'''|
    """
    N = len(Xa)

    # We want to compute:
    # sum_i≠j≠k≠l |Xa_i - Xa_j| |Xb_k - Xb_l|
    # Instead of doing a sum over i, j, k, l all differents,
    # we take the sum over all i, j, k, l (which is the product between a sum over i, j and a sum over k, l),
    # then substract the collisions, ignoring those between i and j and those between k and l, since those
    # automatically gives zero.

    sum_ea_XX = np.abs(Xa[:, None] - Xa[None, :]).sum()
    sum_eb_XX = np.abs(Xb[:, None] - Xb[None, :]).sum()

    # Single conflicts: either i=k, i=l, j=k, or j=l
    # By symmetry, we are left with: 4 sum_i≠j≠k |Xa_i - Xa_j| |Xb_i - Xb_k|
    left = np.abs(Xa[:, None, None] - Xa[None, :, None])  # i, j, k
    right = np.abs(Xb[:, None, None] - Xb[None, None, :])  # i, j, k
    product = left * right
    j, k = np.diag_indices(N)
    product[:, j, k] = 0
    sum_single_conflict = product.sum()

    # Double conflicts: either i=k and j=l, or i=l and j=k
    # By symmetry, we are left with: 2 sum_i≠j |Xa_i - Xa_j| |Xb_i - Xb_j|
    left = np.abs(Xa[:, None] - Xa[None, :])  # i, j
    right = np.abs(Xb[:, None] - Xb[None, :])  # i, j
    product = left * right
    sum_double_conflict = product.sum()

    result = sum_ea_XX * sum_eb_XX - 4 * sum_single_conflict - 2 * sum_double_conflict
    return result / (N * (N - 1) * (N - 2) * (N - 3))


def _crps_ea_Xy_eb_XX(Xa, ya, Xb, yb):
    """
    Unbiased estimate of:
    E|Xa - ya| * E|Xb' - Xb''|
    """
    N = len(Xa)

    left = np.abs(Xa[:, None, None] - ya)  # i, j, k
    right = np.abs(Xb[None, :, None] - Xb[None, None, :])  # i, j, k
    product = left * right
    i, j = np.diag_indices(N)
    product[i, j, :] = 0
    i, k = np.diag_indices(N)
    product[i, :, k] = 0
    result = product.sum()
    return result / (N * (N - 1) * (N - 2))


def _crps_f_Xy(Xa, ya, Xb, yb):
    """
    Unbiased estimate of:
    E(|Xa - ya| * |Xb - yb|)
    """
    N = len(Xa)
    product = np.abs(Xa - ya) * np.abs(Xb - yb)  # i
    result = product.sum()
    return result / N


def _crps_f_XXXy(Xa, ya, Xb, yb):
    """
    Unbiased estimate of:
    E(|Xa - Xa'| * |Xb - yb|)
    """
    N = len(Xa)
    left = np.abs(Xa[:, None] - Xa[None, :])  # i, j
    right = np.abs(Xb[:, None] - yb)  # i, j
    product = left * right
    result = product.sum()
    return result / (N * (N - 1))


def _crps_f_XX(Xa, ya, Xb, yb):
    """
    Unbiased estimate of:
    E(|Xa - Xa'| * |Xb - Xb'|)
    """
    N = len(Xa)
    left = np.abs(Xa[:, None] - Xa[None, :])  # i, j
    right = np.abs(Xb[:, None] - Xb[None, :])  # i, j
    product = left * right
    result = product.sum()
    return result / (N * (N - 1))


def _crps_f_XXXX(Xa, ya, Xb, yb):
    """
    Unbiased estimate of:
    E(|Xa - Xa'| * |Xb - Xb''|)
    """
    N = len(Xa)
    left = np.abs(Xa[:, None, None] - Xa[None, :, None])  # i, j, k
    right = np.abs(Xb[:, None, None] - Xb[None, None, :])  # i, j, k
    product = left * right
    j, k = np.diag_indices(N)
    product[:, j, k] = 0
    result = product.sum()
    return result / (N * (N - 1) * (N - 2))


def crps_covariance(
    Xa: np.array,
    ya: float,
    Xb: np.array,
    yb: float,
) -> float:
    """
    Unbiased estimate of the covariance between the CRPS of two correlated random variables.
    If Xa == Xb and ya == yb, returns the variance of the CRPS instead.

    Parameters:
    -----------
    Xa: np.ndarray
        Samples from a forecast for the first variable. (n_samples)
    ya: float
        The ground-truth value for the first variable.
    Xb: np.ndarray
        Samples from a forecast for the second variable. (n_samples)
    yb: float
        The ground-truth value for the second variable.

    Returns:
    --------
    covariance: float
        The covariance between the CRPS estimators.
    """
    N = len(Xa)

    ea_Xy_eb_Xy = _crps_ea_Xy_eb_Xy(Xa, ya, Xb, yb)
    ea_Xy_eb_XX = _crps_ea_Xy_eb_XX(Xa, ya, Xb, yb)
    ea_XX_eb_Xy = _crps_ea_Xy_eb_XX(Xb, yb, Xa, ya)
    ea_XX_eb_XX = _crps_ea_XX_eb_XX(Xa, ya, Xb, yb)

    f_Xy = _crps_f_Xy(Xa, ya, Xb, yb)
    f_XXXy = _crps_f_XXXy(Xa, ya, Xb, yb)
    f_XyXX = _crps_f_XXXy(Xb, yb, Xa, ya)
    f_XX = _crps_f_XX(Xa, ya, Xb, yb)
    f_XXXX = _crps_f_XXXX(Xa, ya, Xb, yb)

    return (
        -(1 / N) * ea_Xy_eb_Xy
        + (1 / N) * ea_Xy_eb_XX
        + (1 / N) * ea_XX_eb_Xy
        - ((2 * N - 3) / (2 * N * (N - 1))) * ea_XX_eb_XX
        + (1 / N) * f_Xy
        - (1 / N) * f_XXXy
        - (1 / N) * f_XyXX
        + (1 / (2 * N * (N - 1))) * f_XX
        + ((N - 2) / (N * (N - 1))) * f_XXXX
    )


def weighted_sum_crps_variance(
    target: np.array,
    samples: np.array,
    weights: np.array,
) -> float:
    """
    Unbiased estimator of the variance of the numerical estimate of the
    given weighted sum of CRPS values.

    This implementation assumes that the univariate is estimated using:
    CRPS(X, y) ~ (1 / n) * sum_i |x_i - y| - 1 / (2 * n * (n-1)) * sum_i,i' |x_i - x_i'|.
    This formula gives the same result as the one used in the crps() implementation above.

    Note that this is a heavy computation, being O(k^2 n^3) with k variables and n samples.
    Also, while it is unbiased, it is not guaranteed to be >= 0.

    Parameters:
    -----------
    target: np.ndarray
        The target values: y in the above formula. (k variables)
    samples: np.ndarray
        The forecast values: X in the above formula. (n samples, k variables)
    weights: np.array
        The weight given to the CRPS of each variable. (k variables)

    Returns:
    --------
    variance: float
        The variance of the weighted sum of the CRPS estimators.
    """
    assert len(target.shape) == 1
    assert len(samples.shape) == 2
    assert len(weights.shape) == 1
    assert target.shape[0] == samples.shape[1] == weights.shape[0]

    s = 0.0

    for i in range(target.shape[0]):
        for j in range(i, target.shape[0]):
            Xa = samples[:, i]
            Xb = samples[:, j]
            ya = target[i]
            yb = target[j]

            if i == j:
                s += weights[i] * weights[j] * crps_covariance(Xa, ya, Xb, yb)
            else:
                # Multiply by 2 since we would get the same results by switching i and j
                s += 2 * weights[i] * weights[j] * crps_covariance(Xa, ya, Xb, yb)

    return s


def mean_crps(target, samples):
    """
    The mean of the CRPS over all variables
    """
    if target.size > 0:
        return crps(target, samples).mean()
    else:
        raise RuntimeError(
            f"CRPS received an empty target. Shapes = {target.shape} and {samples.shape}"
        )


def compute_constraint_violation(
    entry: dict,
    samples: np.array,
    scaling: float,
) -> float:
    violation = 0.0
    scaled_samples = scaling * samples

    # Min constraint
    scaled_threshold = scaling * entry["constraint_min"]
    violation += (scaled_threshold - scaled_samples).clip(min=0).mean(axis=1)

    # Max constraint
    scaled_threshold = scaling * entry["constraint_max"]
    violation += (scaled_samples - scaled_threshold).clip(min=0).mean(axis=1)

    # Variable max constraint
    if len(entry["constraint_variable_max_index"]) > 0:
        indexed_samples = scaled_samples[:, entry["constraint_variable_max_index"]]
        scaled_thresholds = scaling * np.array(entry["constraint_variable_max_values"])
        violation += (
            (indexed_samples - scaled_thresholds[None, :]).clip(min=0).mean(axis=1)
        )

    return violation


def roi_crps(
    entry: dict,
    forecast: np.array,
) -> dict[str, float]:
    """
    Compute the Region-of-Interest CRPS for a single entry of the context-is-key Hugging Face dataset,
    for the given forecast.

    Parameters:
    ----------
    entry: dict
        A dictionary containing a single entry of the context-is-key Hugging Face dataset.
    forecast: np.array
        The forecast values. (n_samples, n_timesteps)

    Returns:
    --------
    result: dict[str, float]
        A dictionary containing the following entries:
        "metric": the final metric.
        "raw_metric": the metric before the log transformation.
        "scaling": the scaling factor applied to the CRPS and the violations.
        "crps": the weighted CRPS.
        "roi_crps": the CRPS only for the region of interest.
        "non_roi_crps": the CRPS only for the forecast not in the region of interest.
        "violation_mean": the average constraint violation over the samples.
        "violation_crps": the CRPS of the constraint violation.
        "metric_variance": an unbiased estimate of the variance of the metric.
    """
    future_time = pd.read_json(StringIO(entry["future_time"]))
    target = future_time[future_time.columns[-1]].to_numpy()

    assert (
        future_time.shape[0] == forecast.shape[1]
    ), "Incorrect number of timesteps in forecast"

    variance_target = target.to_numpy() if isinstance(target, pd.Series) else target
    variance_forecast = forecast

    if entry["region_of_interest"]:
        roi_mask = np.zeros(forecast.shape[1], dtype=bool)
        for i in entry["region_of_interest"]:
            roi_mask[i] = True

        roi_crps = mean_crps(target=target[roi_mask], samples=forecast[:, roi_mask])
        non_roi_crps = mean_crps(
            target=target[~roi_mask], samples=forecast[:, ~roi_mask]
        )
        crps_value = 0.5 * roi_crps + 0.5 * non_roi_crps
        standard_crps = mean_crps(target=target, samples=forecast)
        num_roi_timesteps = roi_mask.sum()
        num_non_roi_timesteps = (~roi_mask).sum()
        variance_weights = entry["metric_scaling"] * (
            0.5 * roi_mask / num_roi_timesteps
            + (1 - 0.5) * ~roi_mask / num_non_roi_timesteps
        )
    else:
        crps_value = mean_crps(target=target, samples=forecast)
        # Those will only be used in the reporting
        roi_crps = crps_value
        non_roi_crps = crps_value
        standard_crps = crps_value
        num_roi_timesteps = len(target)
        num_non_roi_timesteps = 0
        variance_weights = np.full(
            target.shape, fill_value=entry["metric_scaling"] / len(target)
        )

    violation_amount = compute_constraint_violation(
        entry, samples=forecast, scaling=entry["metric_scaling"]
    )
    violation_func = 10.0 * violation_amount

    # The target is set to zero, since we make sure that the ground truth always satisfy the constraints
    # The crps code assume multivariate input, so add a dummy dimension
    violation_crps = crps(target=np.zeros(1), samples=violation_func[:, None])[0]

    variance_target = np.concatenate((variance_target, np.zeros(1)), axis=0)
    variance_forecast = np.concatenate(
        (variance_forecast, violation_func[:, None]), axis=1
    )
    variance_weights = np.concatenate((variance_weights, 1.0 * np.ones(1)), axis=0)

    raw_metric = entry["metric_scaling"] * crps_value + violation_crps
    metric = raw_metric

    # Computing the variance of the RCPRS is much more expensive,
    # especially when the number of samples is large.
    # So it can be commented out if not desired.
    variance = weighted_sum_crps_variance(
        target=variance_target,
        samples=variance_forecast,
        weights=variance_weights,
    )

    return {
        "metric": metric,
        "raw_metric": raw_metric,
        "scaling": entry["metric_scaling"],
        "crps": entry["metric_scaling"] * crps_value,
        "roi_crps": entry["metric_scaling"] * roi_crps,
        "non_roi_crps": entry["metric_scaling"] * non_roi_crps,
        "standard_crps": entry["metric_scaling"] * standard_crps,
        "num_roi_timesteps": num_roi_timesteps,
        "num_non_roi_timesteps": num_non_roi_timesteps,
        "violation_mean": violation_amount.mean(),
        "violation_crps": violation_crps,
        "variance": variance,
    }


def compute_all_rcprs(
    dataset: Dataset,
    forecasts: list[dict],
) -> tuple[float, float]:
    """
    Compute the Region-of-Interest CRPS for all instances in the Context-is-Key dataset.

    Parameters:
    ----------
    dataset: Dataset
        The Context-is-Key dataset.
    forecasts: list[dict]
        A list of dictionaries, each containing the following keys:
        - "name": the name of the task for which the forecast is made.
        - "seed": the seed of the instance for which the forecast is made.
        - "forecast": the forecast values. (n_samples, n_timesteps)

    Returns:
    --------
    mean_crps: float
        The aggregated RCRPS over all instances.
    std_crps: float
        An estimate of the standard error of the aggregated RCRPS.
    """
    weighted_sum_rcprs = 0.0
    weighted_sum_variance = 0.0
    total_weight = 0.0

    for entry, forecast in zip(dataset, forecasts):
        if entry["name"] != forecast["name"]:
            raise ValueError(
                f"Forecast name {forecast['name']} does not match dataset entry name {entry['name']}"
            )
        if entry["seed"] != forecast["seed"]:
            raise ValueError(
                f"Forecast seed {forecast['seed']} does not match dataset entry seed {entry['seed']}"
            )
        metric_output = roi_crps(
            entry=entry,
            forecast=forecast["forecast"],
        )

        weight = Fraction(entry["weight"])

        # Apply the cap of RCPRS = 5 to the metric
        if metric_output["metric"] >= 5.0:
            metric_output["metric"] = 5.0
            metric_output["variance"] = 0.0

        weighted_sum_rcprs += weight * metric_output["metric"]
        weighted_sum_variance += weight * weight * metric_output["variance"]
        total_weight += weight

    mean_crps = weighted_sum_rcprs / total_weight
    std_crps = np.sqrt(weighted_sum_variance) / total_weight

    return mean_crps, std_crps


if __name__ == "__main__":
    # An example of how to use this function,
    # by using a naive forecaster which use random values from the past as its forecast.

    from datasets import load_dataset

    dataset = load_dataset("ServiceNow/context-is-key", split="test")

    # Create a random forecast for each instance in the dataset
    forecasts = []
    for entry in dataset:
        past_time = pd.read_json(StringIO(entry["past_time"]))
        future_time = pd.read_json(StringIO(entry["future_time"]))
        forecast = {
            "name": entry["name"],
            "seed": entry["seed"],
            "forecast": np.random.choice(
                past_time.to_numpy()[:, -1],
                size=(25, len(future_time)),
                replace=True,
            ),
        }
        forecasts.append(forecast)

    mean_crps, std_crps = compute_all_rcprs(dataset, forecasts)
    print(f"Mean RCRPS: {mean_crps}")
    print(f"Standard error of RCRPS: {std_crps}")