-
Notifications
You must be signed in to change notification settings - Fork 70
/
utils.py
140 lines (125 loc) 路 4.47 KB
/
utils.py
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
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/utils.ipynb.
# %% auto 0
__all__ = ['generate_daily_series', 'generate_prices_for_series', 'PredictionIntervals']
# %% ../nbs/utils.ipynb 3
from math import ceil, log10
import numpy as np
import pandas as pd
from utilsforecast.compat import DataFrame, pl
from utilsforecast.data import generate_series
# %% ../nbs/utils.ipynb 5
def generate_daily_series(
n_series: int,
min_length: int = 50,
max_length: int = 500,
n_static_features: int = 0,
equal_ends: bool = False,
static_as_categorical: bool = True,
with_trend: bool = False,
seed: int = 0,
engine: str = "pandas",
) -> DataFrame:
"""Generate Synthetic Panel Series.
Parameters
----------
n_series : int
Number of series for synthetic panel.
min_length : int (default=50)
Minimum length of synthetic panel's series.
max_length : int (default=500)
Maximum length of synthetic panel's series.
n_static_features : int (default=0)
Number of static exogenous variables for synthetic panel's series.
equal_ends : bool (default=False)
Series should end in the same date stamp `ds`.
static_as_categorical : bool (default=True)
Static features should have a categorical data type.
with_trend : bool (default=False)
Series should have a (positive) trend.
seed : int (default=0)
Random seed used for generating the data.
engine : str (default='pandas')
Output Dataframe type.
Returns
-------
pandas or polars DataFrame
Synthetic panel with columns [`unique_id`, `ds`, `y`] and exogenous features.
"""
series = generate_series(
n_series=n_series,
freq="D",
min_length=min_length,
max_length=max_length,
n_static_features=n_static_features,
equal_ends=equal_ends,
static_as_categorical=static_as_categorical,
with_trend=with_trend,
seed=seed,
engine=engine,
)
n_digits = ceil(log10(n_series))
if engine == "pandas":
series["unique_id"] = (
"id_" + series["unique_id"].astype(str).str.rjust(n_digits, "0")
).astype("category")
else:
series = series.with_columns(
("id_" + pl.col("unique_id").cast(pl.Utf8).str.rjust(n_digits, "0"))
.alias("unique_id")
.cast(pl.Categorical)
)
return series
# %% ../nbs/utils.ipynb 16
def generate_prices_for_series(
series: pd.DataFrame, horizon: int = 7, seed: int = 0
) -> pd.DataFrame:
rng = np.random.RandomState(seed)
unique_last_dates = series.groupby("unique_id", observed=True)["ds"].max().nunique()
if unique_last_dates > 1:
raise ValueError("series must have equal ends.")
day_offset = pd.tseries.frequencies.Day()
starts_ends = series.groupby("unique_id", observed=True)["ds"].agg(["min", "max"])
dfs = []
for idx, (start, end) in starts_ends.iterrows():
product_df = pd.DataFrame(
{
"unique_id": idx,
"price": rng.rand((end - start).days + 1 + horizon),
},
index=pd.date_range(start, end + horizon * day_offset, name="ds"),
)
dfs.append(product_df)
prices_catalog = pd.concat(dfs).reset_index()
return prices_catalog
# %% ../nbs/utils.ipynb 19
class PredictionIntervals:
"""Class for storing prediction intervals metadata information."""
def __init__(
self,
n_windows: int = 2,
h: int = 1,
method: str = "conformal_distribution",
):
if n_windows < 2:
raise ValueError(
"You need at least two windows to compute conformal intervals"
)
allowed_methods = ["conformal_error", "conformal_distribution"]
if method not in allowed_methods:
raise ValueError(f"method must be one of {allowed_methods}")
self.n_windows = n_windows
self.h = h
self.method = method
def __repr__(self):
return f"PredictionIntervals(n_windows={self.n_windows}, h={self.h}, method='{self.method}')"
# %% ../nbs/utils.ipynb 20
def _ensure_shallow_copy(df: pd.DataFrame) -> pd.DataFrame:
from packaging.version import Version
if Version(pd.__version__) < Version("1.4"):
# https://github.com/pandas-dev/pandas/pull/43406
df = df.copy()
return df
# %% ../nbs/utils.ipynb 21
class _ShortSeriesException(Exception):
def __init__(self, idxs):
self.idxs = idxs