TemporalSimilarity¶
yohou.interval.similarity.TemporalSimilarity
¶
Bases: BaseSimilarity
Temporal similarity using Fourier features for weighting observations.
Computes observation weights by measuring the distance between cyclic temporal features extracted from prediction timestamps. Observations at similar seasonal positions (e.g. same day of week, same month of year) receive higher weights.
Timestamps are converted to step indices relative to the first
observed timestamp, then encoded as sin/cos pairs at the specified
seasonal periods. Distances in this feature space are converted to
weights using the same softmax formula as DistanceSimilarity.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
seasonalities
|
list of float
|
Seasonal periods in time steps (e.g. |
None
|
harmonics
|
dict mapping float to list of int, or None
|
Harmonics to include per seasonality period. Keys must match
entries in |
None
|
metric
|
str
|
Distance metric for |
"euclidean"
|
metric_params
|
dict or None
|
Additional keyword arguments forwarded to the distance metric. |
None
|
Attributes¶
| Name | Type | Description |
|---|---|---|
first_time_ |
datetime
|
Reference timestamp from the first calibration prediction. |
interval_td_ |
timedelta
|
Time interval between consecutive timestamps, auto-detected from calibration data. |
Notes¶
Sin/cos encoding ensures that cyclic distances are correctly captured (e.g. December 31 is close to January 1). Multiple seasonalities combine naturally by concatenating feature vectors.
The weight normalisation matches DistanceSimilarity exactly:
followed by
This reserves probability mass for a uniform component, following the conformal prediction literature.
See Also¶
DistanceSimilarity: Value-based distance similarity.BaseSimilarity: Abstract similarity base class.
Examples¶
>>> from datetime import datetime, timedelta
>>> import polars as pl
>>> import numpy as np
>>> from yohou.interval.similarity import TemporalSimilarity
>>>
>>> # Daily data with 3 weeks of calibration
>>> dates = [datetime(2021, 1, 1) + timedelta(days=i) for i in range(21)]
>>> y = pl.DataFrame({"time": dates, "value": np.random.randn(21)})
>>> y_pred = pl.DataFrame({"time": dates, "value": np.random.randn(21)})
>>>
>>> # Fit with weekly seasonality
>>> sim = TemporalSimilarity(seasonalities=[7.0])
>>> _ = sim.fit(y, y_pred)
>>>
>>> # Predict weights for a new Monday
>>> new_date = [datetime(2021, 1, 22)]
>>> y_pred_new = pl.DataFrame({"time": new_date, "value": [0.5]})
>>> weights = sim.predict(y_pred_new)
>>> weights.shape
(1, 21)
Source Code¶
Show/Hide source
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 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | |
Methods¶
fit(y, y_pred, X_actual=None)
¶
Fit the temporal similarity from calibration predictions.
Auto-detects the time interval from consecutive timestamps in
y_pred and stores a reference timestamp and Fourier feature
matrix for later distance computation.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
y
|
DataFrame
|
Target time series (unused, accepted for API consistency). |
required |
y_pred
|
DataFrame
|
Point forecast time series with a |
required |
X_actual
|
DataFrame or None
|
Exogenous features (unused, accepted for API consistency). |
None
|
Returns¶
| Type | Description |
|---|---|
self
|
|
Source Code¶
Show/Hide source
observe(y, y_pred, X_actual=None)
¶
Observe new data and extend the reference feature matrix.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
y
|
DataFrame
|
New target observations (unused, accepted for API consistency). |
required |
y_pred
|
DataFrame
|
New predictions with a |
required |
X_actual
|
DataFrame or None
|
Exogenous features (unused, accepted for API consistency). |
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 feature
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 temporal similarity weights for new predictions.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
y_pred
|
DataFrame
|
New predictions with a |
required |
X_actual
|
DataFrame or None
|
Exogenous features (unused). |
None
|
Returns¶
| Type | Description |
|---|---|
ndarray
|
Weight matrix of shape |