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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import datetime as dt
|
| 4 |
+
import warnings
|
| 5 |
+
|
| 6 |
+
from statsmodels.tsa.holtwinters import ExponentialSmoothing
|
| 7 |
+
import plotly.graph_objects as go
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
warnings.filterwarnings("ignore")
|
| 11 |
+
|
| 12 |
+
# -----------------------------
|
| 13 |
+
# CONFIG
|
| 14 |
+
# -----------------------------
|
| 15 |
+
DATA_FILE = "202503-domae.parquet" # ๊ฐ์ ๊ฒฝ๋ก์ ๋์ฌ ์์ด์ผ ํจ
|
| 16 |
+
FORECAST_END_YEAR = 2030 # ์์ธก ์ข
๋ฃ ์ฐ๋(12์๊น์ง)
|
| 17 |
+
SEASONAL_PERIODS = 12 # ์๋ณ seasonality
|
| 18 |
+
|
| 19 |
+
# -----------------------------
|
| 20 |
+
# 1. ๋ฐ์ดํฐ ์ ์ฌ & ์ ์ฒ๋ฆฌ
|
| 21 |
+
# -----------------------------
|
| 22 |
+
|
| 23 |
+
def load_data(path: str) -> pd.DataFrame:
|
| 24 |
+
"""Parquet โ ์๋ณ ํผ๋ฒ ํ
์ด๋ธ(DateIndex, ์ด: ํ๋ชฉ, ๊ฐ: ๊ฐ๊ฒฉ)."""
|
| 25 |
+
df = pd.read_parquet(path)
|
| 26 |
+
|
| 27 |
+
# ๋ ์ง ์ปฌ๋ผ ์์ฑ/์ ๊ทํ (๋ ๊ฐ์ง ์ผ์ด์ค ์ง์)
|
| 28 |
+
if "date" in df.columns:
|
| 29 |
+
df["date"] = pd.to_datetime(df["date"])
|
| 30 |
+
elif "PRCE_REG_MM" in df.columns:
|
| 31 |
+
df["date"] = pd.to_datetime(df["PRCE_REG_MM"].astype(str), format="%Y%m")
|
| 32 |
+
else:
|
| 33 |
+
raise ValueError("์ง์๋์ง ์๋ ๋ ์ง ์ปฌ๋ผ ํ์์
๋๋ค.")
|
| 34 |
+
|
| 35 |
+
# ๊ธฐ๋ณธ ์ปฌ๋ผ๋ช
ํต์ผ
|
| 36 |
+
item_col = "PDLT_NM" if "PDLT_NM" in df.columns else "item"
|
| 37 |
+
price_col = "AVRG_PRCE" if "AVRG_PRCE" in df.columns else "price"
|
| 38 |
+
|
| 39 |
+
monthly = (
|
| 40 |
+
df.groupby(["date", item_col])[price_col]
|
| 41 |
+
.mean()
|
| 42 |
+
.reset_index()
|
| 43 |
+
)
|
| 44 |
+
pivot = (
|
| 45 |
+
monthly
|
| 46 |
+
.pivot(index="date", columns=item_col, values=price_col)
|
| 47 |
+
.sort_index()
|
| 48 |
+
)
|
| 49 |
+
# ์ ์์์ผ MS ๋น๋๋ก ์ ๋ ฌ
|
| 50 |
+
pivot.index = pd.to_datetime(pivot.index).to_period("M").to_timestamp()
|
| 51 |
+
return pivot
|
| 52 |
+
|
| 53 |
+
pivot = load_data(DATA_FILE)
|
| 54 |
+
products = pivot.columns.tolist()
|
| 55 |
+
|
| 56 |
+
# -----------------------------
|
| 57 |
+
# 2. ๊ณ ์ ๋ชจ๋ธ ์ ์ (HoltโWinters + fallback)
|
| 58 |
+
# -----------------------------
|
| 59 |
+
|
| 60 |
+
def _fit_forecast(series: pd.Series) -> pd.Series:
|
| 61 |
+
"""์๋ณ ์๊ณ์ด โ 2025โ04 ์ดํ FORECAST_END_YEARโ12๊น์ง ์์ธก."""
|
| 62 |
+
# Ensure Monthly Start frequency
|
| 63 |
+
series = series.asfreq("MS")
|
| 64 |
+
|
| 65 |
+
# ์์ธก ๊ธฐ๊ฐ ๊ณ์ฐ
|
| 66 |
+
last_date = series.index[-1]
|
| 67 |
+
end_date = dt.datetime(FORECAST_END_YEAR, 12, 1)
|
| 68 |
+
horizon = (end_date.year - last_date.year) * 12 + (end_date.month - last_date.month)
|
| 69 |
+
if horizon <= 0:
|
| 70 |
+
return pd.Series(dtype=float)
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
model = ExponentialSmoothing(
|
| 74 |
+
series,
|
| 75 |
+
trend="add",
|
| 76 |
+
seasonal="mul",
|
| 77 |
+
seasonal_periods=SEASONAL_PERIODS,
|
| 78 |
+
initialization_method="estimated",
|
| 79 |
+
)
|
| 80 |
+
res = model.fit(optimized=True)
|
| 81 |
+
fc = res.forecast(horizon)
|
| 82 |
+
except Exception:
|
| 83 |
+
# ํํธ์ํฐ์ค ํ์ต ์คํจ ์ ๋จ์ CAGR ๊ธฐ๋ฐ ์์ธก
|
| 84 |
+
growth = series.pct_change().fillna(0).mean()
|
| 85 |
+
fc = pd.Series(
|
| 86 |
+
[series.iloc[-1] * (1 + growth) ** i for i in range(1, horizon + 1)],
|
| 87 |
+
index=pd.date_range(
|
| 88 |
+
series.index[-1] + pd.DateOffset(months=1),
|
| 89 |
+
periods=horizon,
|
| 90 |
+
freq="MS",
|
| 91 |
+
),
|
| 92 |
+
)
|
| 93 |
+
return fc
|
| 94 |
+
|
| 95 |
+
# ํ๋ชฉ๋ณ ์ ์ฒด ์๋ฆฌ์ฆ(๊ณผ๊ฑฐ+์์ธก) ์ฌ์ ๊ตฌ์ถ โ ์ฑ ๋ฐ์ ์๋ ๊ฐ์
|
| 96 |
+
FULL_SERIES = {}
|
| 97 |
+
FORECASTS = {}
|
| 98 |
+
for item in products:
|
| 99 |
+
hist = pivot[item].dropna()
|
| 100 |
+
fc = _fit_forecast(hist)
|
| 101 |
+
FULL_SERIES[item] = pd.concat([hist, fc])
|
| 102 |
+
FORECASTS[item] = fc
|
| 103 |
+
|
| 104 |
+
# -----------------------------
|
| 105 |
+
# 3. ๋ด์ผ ๊ฐ๊ฒฉ ์์ธก ํจ์
|
| 106 |
+
# -----------------------------
|
| 107 |
+
|
| 108 |
+
today = dt.date.today()
|
| 109 |
+
tomorrow = today + dt.timedelta(days=1)
|
| 110 |
+
|
| 111 |
+
def build_tomorrow_df() -> pd.DataFrame:
|
| 112 |
+
"""๋ด์ผ(์ผ ๋จ์) ์์ ๊ฐ๊ฒฉ DataFrame ๋ฐํ."""
|
| 113 |
+
preds = {}
|
| 114 |
+
for item, series in FULL_SERIES.items():
|
| 115 |
+
# ์ผ ๋จ์ ์ ํ ๋ณด๊ฐ
|
| 116 |
+
daily = series.resample("D").interpolate("linear")
|
| 117 |
+
preds[item] = round(daily.loc[tomorrow], 2) if tomorrow in daily.index else np.nan
|
| 118 |
+
return (
|
| 119 |
+
pd.DataFrame.from_dict(preds, orient="index", columns=[f"๋ด์ผ({tomorrow}) ์์๊ฐ(KRW)"])
|
| 120 |
+
.sort_index()
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
tomorrow_df = build_tomorrow_df()
|
| 124 |
+
|
| 125 |
+
# -----------------------------
|
| 126 |
+
# 4. ์๊ฐํ ํจ์
|
| 127 |
+
# -----------------------------
|
| 128 |
+
|
| 129 |
+
def plot_item(item: str):
|
| 130 |
+
hist = pivot[item].dropna().asfreq("MS")
|
| 131 |
+
fc = FORECASTS[item]
|
| 132 |
+
|
| 133 |
+
fig = go.Figure()
|
| 134 |
+
fig.add_trace(go.Scatter(x=hist.index, y=hist.values, mode="lines", name="Historical"))
|
| 135 |
+
fig.add_trace(go.Scatter(x=fc.index, y=fc.values, mode="lines", name="Forecast"))
|
| 136 |
+
fig.update_layout(
|
| 137 |
+
title=f"{item} โ Monthly Avg Price (1996โ2025) & Forecast(2025โ04โ2030โ12)",
|
| 138 |
+
xaxis_title="Date",
|
| 139 |
+
yaxis_title="Price (KRW)",
|
| 140 |
+
legend=dict(orientation="h", y=1.02, x=0.01),
|
| 141 |
+
margin=dict(l=40, r=20, t=60, b=40),
|
| 142 |
+
)
|
| 143 |
+
return fig
|
| 144 |
+
|
| 145 |
+
# -----------------------------
|
| 146 |
+
# 5. Gradio UI
|
| 147 |
+
# -----------------------------
|
| 148 |
+
with gr.Blocks(title="๋๋งค ๊ฐ๊ฒฉ ์์ธกย App") as demo:
|
| 149 |
+
gr.Markdown("## ๐ ๋๋งค ๊ฐ๊ฒฉ ์์ธก ๋์๋ณด๋ (1996โ2030)")
|
| 150 |
+
|
| 151 |
+
# ํ๋ชฉ ์ ํ โ ๊ทธ๋ํ ์
๋ฐ์ดํธ
|
| 152 |
+
item_dd = gr.Dropdown(products, value=products[0], label="ํ๋ชฉ ์ ํ")
|
| 153 |
+
chart_out = gr.Plot(label="๊ฐ๊ฒฉ ์ถ์ธ")
|
| 154 |
+
|
| 155 |
+
# ๋ด์ผ ๊ฐ๊ฒฉ ํ (์ด๊ธฐ ๊ณ ์ )
|
| 156 |
+
gr.Markdown(f"### ๋ด์ผ({tomorrow}) ๊ฐ ํ๋ชฉ ์์๊ฐ (KRW)")
|
| 157 |
+
tomorrow_table = gr.Dataframe(tomorrow_df, interactive=False, height=400)
|
| 158 |
+
|
| 159 |
+
def update_chart(product):
|
| 160 |
+
return plot_item(product)
|
| 161 |
+
|
| 162 |
+
item_dd.change(update_chart, inputs=item_dd, outputs=chart_out, queue=False)
|
| 163 |
+
|
| 164 |
+
# -----------------------------
|
| 165 |
+
# 6. ์คํ ์คํฌ๋ฆฝํธ ์ํธ๋ฆฌํฌ์ธํธ
|
| 166 |
+
# -----------------------------
|
| 167 |
+
if __name__ == "__main__":
|
| 168 |
+
demo.launch()
|