DistanceSimilarity¶
yohou.interval.similarity.DistanceSimilarity
¶
Bases: BaseSimilarity
Distance-based similarity using scipy metrics for weighting observations.
Computes observation weights by measuring the distance between new predictions and historical predictions in feature space. Closer historical observations receive higher weights, which are then used by interval forecasters to weight conformity scores when constructing prediction intervals.
The weight for the i-th historical observation given prediction j is computed as:
where d is the chosen distance metric.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
metric
|
str
|
Distance metric to use (e.g., |
"euclidean"
|
metric_params
|
dict or None
|
Additional keyword arguments forwarded to the distance metric function. |
None
|
Notes¶
The distance-to-weight conversion uses the softmax of negative distances, so distant observations contribute exponentially less than nearby ones. The weights are further normalised so that each prediction row sums to a value in (0, 1).
References¶
[1] Lei, J., G'Sell, M., Rinaldo, A., Tibshirani, R.J., & Wasserman, L. (2018). "Distribution-free predictive inference for regression." Journal of the American Statistical Association, 113(523), 1094-1111. https://doi.org/10.1080/01621459.2017.1307116 [2] Barber, R.F., Candes, E.J., Ramdas, A., & Tibshirani, R.J. (2023). "Conformal prediction beyond exchangeability." Annals of Statistics, 51(2), 816-845. https://doi.org/10.1214/23-AOS2276
See Also¶
BaseSimilarity: Abstract similarity base class.BaseIntervalForecaster: Interval forecaster that can consume similarity weights.
Examples¶
>>> from datetime import datetime
>>> import polars as pl
>>> import numpy as np
>>> from yohou.interval.similarity import DistanceSimilarity
>>>
>>> # Create training data
>>> time_train = pl.datetime_range(
... start=datetime(2021, 12, 16), end=datetime(2021, 12, 16, 0, 0, 7), interval="1s", eager=True
... )
>>> y_train = pl.DataFrame({"time": time_train, "value": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]})
>>> y_pred_train = pl.DataFrame({"time": time_train, "value": [1.1, 2.1, 2.9, 4.2, 4.8, 6.1, 7.0, 8.1]})
>>>
>>> # Fit similarity model
>>> similarity = DistanceSimilarity(metric="euclidean")
>>> _ = similarity.fit(y_train, y_pred_train)
>>>
>>> # Create new predictions to compute similarities for
>>> time_test = pl.datetime_range(
... start=datetime(2021, 12, 16, 0, 0, 8),
... end=datetime(2021, 12, 16, 0, 0, 9),
... interval="1s",
... eager=True,
... )
>>> y_pred_test = pl.DataFrame({"time": time_test, "value": [8.5, 9.2]})
>>>
>>> # Compute similarity weights
>>> weights = similarity.predict(y_pred_test)
>>> weights.shape
(2, 8)
>>> isinstance(weights, np.ndarray)
True
Source Code¶
Show/Hide source
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 | |
Methods¶
fit(y, y_pred, X_actual=None)
¶
Fits the similarity model.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
y
|
DataFrame
|
Target time series. |
required |
y_pred
|
DataFrame
|
Point forecasts time series. |
required |
X_actual
|
DataFrame or None
|
Exogenous feature time series. |
None
|
Returns¶
| Type | Description |
|---|---|
self
|
|
Source Code¶
Show/Hide source
observe(y, y_pred, X_actual=None)
¶
Observe new data and update similarity model.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
y
|
DataFrame
|
New target values. |
required |
y_pred
|
DataFrame
|
New predictions. |
required |
X_actual
|
DataFrame or None
|
New exogenous features. |
None
|
Returns¶
| Type | Description |
|---|---|
self
|
|
Source Code¶
Show/Hide source
rewind(y, y_pred, X_actual=None)
¶
Rewind the most recently observed data.
Removes the last len(y) rows from the internal reference
matrix, reversing the effect of the corresponding observe()
call.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
y
|
DataFrame
|
Target observations to rewind (used only for row count). |
required |
y_pred
|
DataFrame
|
Predictions to rewind (used only for row count). |
required |
X_actual
|
DataFrame or None
|
Exogenous features to rewind (unused). |
None
|
Returns¶
| Type | Description |
|---|---|
self
|
|
Source Code¶
Show/Hide source
predict(y_pred, X_actual=None)
¶
Compute similarity weights for new predictions.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
y_pred
|
DataFrame
|
New predictions to compute similarities for. |
required |
X_actual
|
DataFrame or None
|
Exogenous features. |
None
|
Returns¶
| Type | Description |
|---|---|
ndarray
|
Similarity weight matrix. |
Source Code¶
Show/Hide source
Tutorials¶
The following example notebooks use this component:
-
How to Use Conformity Scorers
Evaluation-Search
Compare Residual, AbsoluteResidual, GammaResidual, and AbsoluteGammaResidual conformity scorers with coverage/width analysis and DistanceSimilarity interaction.
-
How to Use Distance-Based Similarity for Intervals
Forecasting-Models
Adaptive prediction intervals via similarity-weighted conformal prediction using DistanceSimilarity with configurable distance metrics and bandwidths.