""" ساخت دو دیتاست مصنوعی به سبک پروژه‌های Alpine (هفتگی) و Bosch (ماهانه) Synthetic "forecast snapshot" datasets that mimic how customers actually send forecasts. ساختار واقعی داده در این پروژه‌ها سه جدول است: {company}_forecasts.csv : snapshot_date, customer_id, part_id, target_period, forecast_qty هر snapshot یعنی «فایلی که مشتری در آن تاریخ فرستاده» و برای چند دوره‌ی آینده عدد دارد {company}_actuals.csv : period, customer_id, part_id, actual_qty (خرید واقعی = label) {company}_parts.csv : part_id, product_line, family, unit_price, launch_period, eol_period رفتارهایی که عمداً در داده گذاشته شده (و در نوت‌بوک ۰۹ کشف می‌شوند): 1. سوگیری هر مشتری برای هر product_line متفاوت است (customer x product habit) 2. خطا با افق بیشتر می‌شود؛ بعضی مشتری‌ها «hockey stick» دارند: دور را زیاد می‌گویند و نزدیک اصلاح می‌کنند 3. قطعه‌های رو به پایان عمر (EOL): تقاضا می‌افتد ولی مشتری دیر متوجه می‌شود و زیاد پیش‌بینی می‌کند 4. قطعه‌های جدید: تاریخچه ندارند (cold start) 5. یک مشتری وسط دوره رفتارش عوض می‌شود (drift؛ مثلاً برنامه‌ریز جدید) 6. بعضی snapshot ها ارسال نشده‌اند (missing)، چند خطای ورود داده، و مرجوعی منفی Run: python make_alpine_bosch.py """ from pathlib import Path import numpy as np import pandas as pd OUT = Path(__file__).parent CONFIG = { "alpine": dict(freq="W-MON", start="2023-01-02", end="2025-12-29", horizons=12, n_customers=5, n_parts=24, parts_per_customer=(8, 13), season_period=52, seed=7, base_mu=4.6, noise_scale=1.0), "bosch": dict(freq="MS", start="2021-01-01", end="2025-12-01", horizons=6, n_customers=8, n_parts=36, parts_per_customer=(10, 16), season_period=12, seed=11, base_mu=6.4, noise_scale=0.8), } PRODUCT_LINES = ["Product", "Product Plus", "Product Pro"] FAMILIES = ["Head Unit", "Amplifier", "Camera", "Sensor", "Display", "ECU"] def make_company(name, freq, start, end, horizons, n_customers, n_parts, parts_per_customer, season_period, seed, base_mu, noise_scale): rng = np.random.default_rng(seed) periods = pd.date_range(start, end, freq=freq) T = len(periods) prefix = name[0].upper() # ---------- قطعه‌ها ---------- parts = pd.DataFrame({ "part_id": [f"{prefix}P{i:03d}" for i in range(1, n_parts + 1)], "product_line": rng.choice(PRODUCT_LINES, n_parts, p=[0.5, 0.3, 0.2]), "family": rng.choice(FAMILIES, n_parts), "unit_price": rng.uniform(5, 250, n_parts).round(2), }) launch = np.zeros(n_parts, dtype=int) new_idx = rng.choice(n_parts, max(2, n_parts // 8), replace=False) # قطعه‌های جدید launch[new_idx] = rng.integers(T // 3, int(T * 0.75), len(new_idx)) eol = np.full(n_parts, T + 999) cand = np.setdiff1d(np.arange(n_parts), new_idx) eol_idx = rng.choice(cand, max(2, n_parts // 8), replace=False) # قطعه‌های رو به پایان عمر eol[eol_idx] = rng.integers(T // 2, int(T * 0.85), len(eol_idx)) parts["launch_period"] = [periods[i].date() for i in launch] parts["eol_period"] = [periods[i].date() if i < T else None for i in eol] base = rng.lognormal(base_mu, 0.6, n_parts) trend = rng.normal(0.001, 0.002, n_parts) * (52 / season_period) # ---------- مشتری‌ها و عادت‌هایشان ---------- customers = [f"{prefix}C{i:02d}" for i in range(1, n_customers + 1)] styles = ["over", "under", "noisy", "hockey", "accurate", "over", "under", "hockey"] habit = {} for i, c in enumerate(customers): style = styles[i % len(styles)] for pl in PRODUCT_LINES: b = {"over": 1.30, "under": 0.82, "noisy": 1.0, "hockey": 1.05, "accurate": 1.0}[style] b *= rng.uniform(0.9, 1.15) # هر product_line کمی متفاوت if pl == "Product Pro" and style in ("over", "hockey"): b *= 1.15 # روی محصولات گران‌تر اغراق بیشتر noise = {"noisy": 0.28, "accurate": 0.06}.get(style, 0.12) * noise_scale habit[(c, pl)] = dict(style=style, bias=b, noise=noise) drift_customer = customers[1] # این مشتری از نیمه‌ی دوره رفتارش عوض می‌شود drift_at = int(T * 0.6) pairs = [] for c in customers: k = rng.integers(*parts_per_customer) for p in rng.choice(n_parts, k, replace=False): pairs.append((c, p)) fc_rows, act_rows = [], [] t_idx = np.arange(T) season = 1 + 0.18 * np.sin(2 * np.pi * t_idx / season_period) + 0.06 * np.cos(4 * np.pi * t_idx / season_period) for c, p in pairs: share = rng.uniform(0.3, 1.0) level = base[p] * share * (1 + trend[p]) ** t_idx * season life = np.ones(T) life[t_idx < launch[p]] = 0 ramp = (t_idx >= launch[p]) & (t_idx < launch[p] + 6) life[ramp] = (t_idx[ramp] - launch[p] + 1) / 7 if eol[p] < T: after = t_idx >= eol[p] life[after] = np.exp(-(t_idx[after] - eol[p]) / 4) # افت تقاضا بعد از EOL true_dem = level * life actual = rng.poisson(np.clip(true_dem * rng.lognormal(0, 0.08, T), 0, None)).astype(float) h_info = habit[(c, parts.product_line[p])] for t in range(T): if life[t] == 0: continue act_rows.append((periods[t], c, parts.part_id[p], actual[t])) # snapshot ها: در هر دوره s مشتری برای s+1 .. s+H عدد می‌فرستد for s in range(T - 1): if rng.random() < 0.04: # این snapshot ارسال نشده continue for h in range(1, horizons + 1): t = s + h if t >= T or t < launch[p]: continue bias = h_info["bias"] if c == drift_customer and s >= drift_at: bias = 1.25 # برنامه‌ریز جدید: اغراق‌کننده شد if h_info["style"] == "hockey": bias = bias * (1 + 0.06 * (h - 1)) # هرچه دورتر، اغراق بیشتر # مشتری از EOL دیر باخبر می‌شود: پیش‌بینی بر اساس سطح «قبل از افت» believed = level[t] * life[t] if eol[p] < T and t >= eol[p]: believed = level[t] * max(life[t], 0.9 - 0.1 * max(0, s - eol[p])) noise = rng.normal(0, h_info["noise"] * (1 + 0.08 * h)) fc = max(0.0, believed * bias * (1 + noise)) fc_rows.append((periods[s], c, parts.part_id[p], periods[t], round(fc))) forecasts = pd.DataFrame(fc_rows, columns=["snapshot_date", "customer_id", "part_id", "target_period", "forecast_qty"]) actuals = pd.DataFrame(act_rows, columns=["period", "customer_id", "part_id", "actual_qty"]) # ---------- کثیفی عمدی ---------- n = len(forecasts) idx = rng.choice(n, 30, replace=False) forecasts.loc[idx, "forecast_qty"] *= 10 # یک صفر اضافه idx = rng.choice(len(actuals), 12, replace=False) actuals.loc[idx, "actual_qty"] = -np.round(actuals.loc[idx, "actual_qty"] * 0.1) # مرجوعی (منفی) forecasts = pd.concat([forecasts, forecasts.sample(25, random_state=seed)]) # ردیف تکراری forecasts = forecasts.sort_values(["snapshot_date", "customer_id", "part_id", "target_period"]).reset_index(drop=True) hidden = pd.DataFrame([dict(customer_id=c, product_line=pl, **v) for (c, pl), v in habit.items()]) hidden["note"] = np.where(hidden.customer_id == drift_customer, f"drift to bias=1.25 from {periods[drift_at].date()}", "") return forecasts, actuals, parts, hidden def main(): for name, cfg in CONFIG.items(): fc, act, parts, hidden = make_company(name, **cfg) fc.to_csv(OUT / f"{name}_forecasts.csv", index=False) act.to_csv(OUT / f"{name}_actuals.csv", index=False) parts.to_csv(OUT / f"{name}_parts.csv", index=False) hidden.to_csv(OUT / f"{name}_ANSWER_KEY_customer_habits.csv", index=False) print(f"{name:7s} forecasts={len(fc):,} actuals={len(act):,} parts={len(parts)}") if __name__ == "__main__": main()