Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import pandas as pd
|
|
| 3 |
import numpy as np
|
| 4 |
from prophet import Prophet
|
| 5 |
import plotly.express as px
|
|
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
from datetime import date
|
| 8 |
from pathlib import Path
|
|
@@ -12,9 +13,9 @@ import matplotlib as mpl
|
|
| 12 |
# -------------------------------------------------
|
| 13 |
# CONFIG ------------------------------------------
|
| 14 |
# -------------------------------------------------
|
| 15 |
-
CSV_PATH = Path("2025-domae.csv")
|
| 16 |
MACRO_START, MACRO_END = "1996-01-01", "2030-12-31"
|
| 17 |
-
MICRO_START, MICRO_END = "
|
| 18 |
|
| 19 |
# ํ๊ธ ํฐํธ ์ค์
|
| 20 |
font_list = [f.name for f in fm.fontManager.ttflist if 'gothic' in f.name.lower() or
|
|
@@ -68,8 +69,12 @@ def _standardize_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
| 68 |
if "date" in df.columns and pd.api.types.is_object_dtype(df["date"]):
|
| 69 |
if len(df) > 0:
|
| 70 |
sample = str(df["date"].iloc[0])
|
| 71 |
-
if sample.isdigit() and len(sample)
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# โโ build item from pdlt_nm + spcs_nm if needed โโโโโโโโโโโโโโโโโโโโ
|
| 75 |
if "item" not in df.columns and {"pdlt_nm", "spcs_nm"}.issubset(df.columns):
|
|
@@ -115,6 +120,9 @@ def load_data() -> pd.DataFrame:
|
|
| 115 |
if before_date_convert != after_date_convert:
|
| 116 |
st.warning(f"๋ ์ง ๋ณํ ์ค {before_date_convert - after_date_convert}๊ฐ ํ์ด ์ ์ธ๋์์ต๋๋ค.")
|
| 117 |
|
|
|
|
|
|
|
|
|
|
| 118 |
# NA ๋ฐ์ดํฐ ์ฒ๋ฆฌ
|
| 119 |
before_na_drop = len(df)
|
| 120 |
df = df.dropna(subset=["date", "item", "price"])
|
|
@@ -146,13 +154,32 @@ def get_items(df: pd.DataFrame):
|
|
| 146 |
|
| 147 |
|
| 148 |
@st.cache_data(show_spinner=False, ttl=3600)
|
| 149 |
-
def fit_prophet(df: pd.DataFrame, horizon_end: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
# Make a copy and ensure we have data
|
| 151 |
df = df.copy()
|
| 152 |
df = df.dropna(subset=["date", "price"])
|
| 153 |
|
| 154 |
-
#
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
if len(df) < 2:
|
| 158 |
st.warning(f"๋ฐ์ดํฐ ํฌ์ธํธ๊ฐ ๋ถ์กฑํฉ๋๋ค. ์์ธก์ ์ํด์๋ ์ต์ 2๊ฐ ์ด์์ ์ ํจ ๋ฐ์ดํฐ๊ฐ ํ์ํฉ๋๋ค. (ํ์ฌ {len(df)}๊ฐ)")
|
|
@@ -162,21 +189,52 @@ def fit_prophet(df: pd.DataFrame, horizon_end: str):
|
|
| 162 |
prophet_df = df.rename(columns={"date": "ds", "price": "y"})
|
| 163 |
|
| 164 |
try:
|
| 165 |
-
# Fit the model
|
| 166 |
-
m = Prophet(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
m.fit(prophet_df)
|
| 168 |
|
| 169 |
# Generate future dates
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
# Make predictions
|
| 174 |
forecast = m.predict(future)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
return m, forecast
|
| 176 |
except Exception as e:
|
| 177 |
st.error(f"Prophet ๋ชจ๋ธ ์์ฑ ์ค ์ค๋ฅ: {str(e)}")
|
| 178 |
return None, None
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
# -------------------------------------------------
|
| 181 |
# LOAD DATA ---------------------------------------
|
| 182 |
# -------------------------------------------------
|
|
@@ -201,14 +259,12 @@ if item_df.empty:
|
|
| 201 |
# -------------------------------------------------
|
| 202 |
st.header(f"๐ {selected_item} ๊ฐ๊ฒฉ ์์ธก ๋์๋ณด๋")
|
| 203 |
|
| 204 |
-
# ๋ฐ์ดํฐ ํํฐ๋ง ๋ก์ง
|
| 205 |
try:
|
| 206 |
-
macro_start_dt = pd.Timestamp(
|
| 207 |
-
# ๋ฐ์ดํฐ
|
| 208 |
-
if
|
| 209 |
-
# ๊ฐ์ฅ ์ค๋๋ ๋ ์ง๋ถํฐ ์์
|
| 210 |
macro_start_dt = item_df["date"].min()
|
| 211 |
-
st.info(f"์ถฉ๋ถํ ๋ฐ์ดํฐ๊ฐ ์์ด ์์ ๋ ์ง๋ฅผ {macro_start_dt.strftime('%Y-%m-%d')}๋ก ์กฐ์ ํ์ต๋๋ค.")
|
| 212 |
|
| 213 |
macro_df = item_df[item_df["date"] >= macro_start_dt].copy()
|
| 214 |
except Exception as e:
|
|
@@ -232,20 +288,89 @@ if len(macro_df) < 2:
|
|
| 232 |
else:
|
| 233 |
try:
|
| 234 |
with st.spinner("์ฅ๊ธฐ ์์ธก ๋ชจ๋ธ ์์ฑ ์ค..."):
|
| 235 |
-
|
|
|
|
| 236 |
|
| 237 |
if m_macro is not None and fc_macro is not None:
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
else:
|
| 250 |
st.warning("์์ธก ๋ชจ๋ธ์ ์์ฑํ ์ ์์ต๋๋ค.")
|
| 251 |
fig = px.line(item_df, x="date", y="price", title=f"{selected_item} ๊ณผ๊ฑฐ ๊ฐ๊ฒฉ")
|
|
@@ -258,44 +383,156 @@ else:
|
|
| 258 |
# -------------------------------------------------
|
| 259 |
# MICRO FORECAST 2024โ2026 ------------------------
|
| 260 |
# -------------------------------------------------
|
| 261 |
-
st.subheader("๐ 2024โ2026 ๋จ๊ธฐ ์์ธก")
|
| 262 |
|
| 263 |
-
# ๋ฐ์ดํฐ ํํฐ๋ง
|
| 264 |
try:
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
micro_df = item_df.sort_values("date").tail(n).copy()
|
| 271 |
-
st.info(f"์ถฉ๋ถํ ์ต๊ทผ ๋ฐ์ดํฐ๊ฐ ์์ด ์ต๊ทผ {n}๊ฐ ๋ฐ์ดํฐ ํฌ์ธํธ๋ง ์ฌ์ฉํฉ๋๋ค.")
|
| 272 |
-
else:
|
| 273 |
-
micro_df = item_df[item_df["date"] >= micro_start_dt].copy()
|
| 274 |
except Exception as e:
|
| 275 |
st.error(f"๋จ๊ธฐ ์์ธก ๋ฐ์ดํฐ ํํฐ๋ง ์ค๋ฅ: {str(e)}")
|
| 276 |
-
# ์ต๊ทผ
|
| 277 |
-
micro_df = item_df.sort_values("date").tail(
|
| 278 |
|
| 279 |
if len(micro_df) < 2:
|
| 280 |
-
st.warning(f"
|
| 281 |
fig = px.line(item_df, x="date", y="price", title=f"{selected_item} ์ต๊ทผ ๊ฐ๊ฒฉ")
|
| 282 |
st.plotly_chart(fig, use_container_width=True)
|
| 283 |
else:
|
| 284 |
try:
|
| 285 |
with st.spinner("๋จ๊ธฐ ์์ธก ๋ชจ๋ธ ์์ฑ ์ค..."):
|
| 286 |
-
|
|
|
|
| 287 |
|
| 288 |
if m_micro is not None and fc_micro is not None:
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
else:
|
| 300 |
st.warning("๋จ๊ธฐ ์์ธก ๋ชจ๋ธ์ ์์ฑํ ์ ์์ต๋๋ค.")
|
| 301 |
except Exception as e:
|
|
@@ -317,6 +554,19 @@ with st.expander("๐ ์์ฆ๋๋ฆฌํฐ & ํจํด ์ค๋ช
"):
|
|
| 317 |
f"**์ฐ๊ฐ ํผํฌ ์:** {int(month_season.idxmax())}์ \n"
|
| 318 |
f"**์ฐ๊ฐ ์ ์ ์:** {int(month_season.idxmin())}์ \n"
|
| 319 |
f"**์ฐ๊ฐ ๋ณ๋ํญ:** {month_season.max() - month_season.min():.1f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
except Exception as e:
|
| 321 |
st.error(f"์์ฆ๋๋ฆฌํฐ ๋ถ์ ์ค๋ฅ: {str(e)}")
|
| 322 |
else:
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from prophet import Prophet
|
| 5 |
import plotly.express as px
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
from datetime import date
|
| 9 |
from pathlib import Path
|
|
|
|
| 13 |
# -------------------------------------------------
|
| 14 |
# CONFIG ------------------------------------------
|
| 15 |
# -------------------------------------------------
|
| 16 |
+
CSV_PATH = Path("2025-domae.csv")
|
| 17 |
MACRO_START, MACRO_END = "1996-01-01", "2030-12-31"
|
| 18 |
+
MICRO_START, MICRO_END = "2024-01-01", "2026-12-31"
|
| 19 |
|
| 20 |
# ํ๊ธ ํฐํธ ์ค์
|
| 21 |
font_list = [f.name for f in fm.fontManager.ttflist if 'gothic' in f.name.lower() or
|
|
|
|
| 69 |
if "date" in df.columns and pd.api.types.is_object_dtype(df["date"]):
|
| 70 |
if len(df) > 0:
|
| 71 |
sample = str(df["date"].iloc[0])
|
| 72 |
+
if sample.isdigit() and len(sample) == 6: # YYYYMM ํ์ ํ์ธ
|
| 73 |
+
# ์ ๋ง์ผ๋ก ๋ณํ (YYYYMM -> YYYY-MM-DD)
|
| 74 |
+
df["date"] = pd.to_datetime(df["date"].astype(str), format="%Y%m", errors="coerce")
|
| 75 |
+
df["date"] = df["date"] + pd.offsets.MonthEnd(0) # ํด๋น ์์ ๋ง์ง๋ง ๋ ๋ก ์ค์
|
| 76 |
+
elif sample.isdigit() and len(sample) == 8: # YYYYMMDD ํ์
|
| 77 |
+
df["date"] = pd.to_datetime(df["date"].astype(str), format="%Y%m%d", errors="coerce")
|
| 78 |
|
| 79 |
# โโ build item from pdlt_nm + spcs_nm if needed โโโโโโโโโโโโโโโโโโโโ
|
| 80 |
if "item" not in df.columns and {"pdlt_nm", "spcs_nm"}.issubset(df.columns):
|
|
|
|
| 120 |
if before_date_convert != after_date_convert:
|
| 121 |
st.warning(f"๋ ์ง ๋ณํ ์ค {before_date_convert - after_date_convert}๊ฐ ํ์ด ์ ์ธ๋์์ต๋๋ค.")
|
| 122 |
|
| 123 |
+
# ๊ฐ๊ฒฉ ๋ฐ์ดํฐ ์ ์ํ์ผ๋ก ๋ณํ (์ซ์๊ฐ ์๋ ๊ฐ ์ ๊ฑฐ)
|
| 124 |
+
df["price"] = pd.to_numeric(df["price"], errors="coerce")
|
| 125 |
+
|
| 126 |
# NA ๋ฐ์ดํฐ ์ฒ๋ฆฌ
|
| 127 |
before_na_drop = len(df)
|
| 128 |
df = df.dropna(subset=["date", "item", "price"])
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
@st.cache_data(show_spinner=False, ttl=3600)
|
| 157 |
+
def fit_prophet(df: pd.DataFrame, horizon_end: str, monthly=False, changepoint_prior_scale=0.05):
|
| 158 |
+
"""
|
| 159 |
+
Prophet ๋ชจ๋ธ์ ํ์ต์ํค๊ณ ์์ธกํฉ๋๋ค.
|
| 160 |
+
|
| 161 |
+
Args:
|
| 162 |
+
df: ํ์ต ๋ฐ์ดํฐ (date, price ์ปฌ๋ผ ํ์)
|
| 163 |
+
horizon_end: ์์ธก ์ข
๋ฃ์ผ
|
| 164 |
+
monthly: ์ ๋จ์ ์์ธก ์ฌ๋ถ
|
| 165 |
+
changepoint_prior_scale: ๋ณํ์ ๋ฏผ๊ฐ๋ (๋ฎ์์๋ก ๊ณผ์ ํฉ ๊ฐ์)
|
| 166 |
+
"""
|
| 167 |
# Make a copy and ensure we have data
|
| 168 |
df = df.copy()
|
| 169 |
df = df.dropna(subset=["date", "price"])
|
| 170 |
|
| 171 |
+
# ์ด์์น ์ ๊ฑฐ (99 ํผ์ผํ์ผ ์ด๊ณผ ๊ฐ๊ฒฉ ์ ์ธ)
|
| 172 |
+
upper_limit = df["price"].quantile(0.99)
|
| 173 |
+
df = df[df["price"] <= upper_limit]
|
| 174 |
+
|
| 175 |
+
# ์ค๋ณต ๋ ์ง ์ฒ๋ฆฌ
|
| 176 |
+
if monthly:
|
| 177 |
+
# ์ ๋จ์๋ก ์ง๊ณ
|
| 178 |
+
df["year_month"] = df["date"].dt.strftime('%Y-%m')
|
| 179 |
+
df = df.groupby("year_month").agg({"date": "first", "price": "mean"}).reset_index(drop=True)
|
| 180 |
+
else:
|
| 181 |
+
# ์ผ ๋จ์๋ก ์ง๊ณ
|
| 182 |
+
df = df.groupby("date")["price"].mean().reset_index()
|
| 183 |
|
| 184 |
if len(df) < 2:
|
| 185 |
st.warning(f"๋ฐ์ดํฐ ํฌ์ธํธ๊ฐ ๋ถ์กฑํฉ๋๋ค. ์์ธก์ ์ํด์๋ ์ต์ 2๊ฐ ์ด์์ ์ ํจ ๋ฐ์ดํฐ๊ฐ ํ์ํฉ๋๋ค. (ํ์ฌ {len(df)}๊ฐ)")
|
|
|
|
| 189 |
prophet_df = df.rename(columns={"date": "ds", "price": "y"})
|
| 190 |
|
| 191 |
try:
|
| 192 |
+
# Fit the model with tuned parameters
|
| 193 |
+
m = Prophet(
|
| 194 |
+
yearly_seasonality=True,
|
| 195 |
+
weekly_seasonality=False,
|
| 196 |
+
daily_seasonality=False,
|
| 197 |
+
changepoint_prior_scale=changepoint_prior_scale, # ๊ณผ์ ํฉ ๋ฐฉ์ง
|
| 198 |
+
seasonality_prior_scale=10.0, # ๊ณ์ ์ฑ ์กฐ์
|
| 199 |
+
seasonality_mode='multiplicative' # ๊ณฑ์
๋ชจ๋ (๊ฐ๊ฒฉ ๋ฐ์ดํฐ์ ์ ํฉ)
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
# ํ๊ตญ ๋ช
์ ํจ๊ณผ ์ถ๊ฐ (์ค๋ , ์ถ์)
|
| 203 |
+
m.add_country_holidays(country_name='South Korea')
|
| 204 |
+
|
| 205 |
m.fit(prophet_df)
|
| 206 |
|
| 207 |
# Generate future dates
|
| 208 |
+
if monthly:
|
| 209 |
+
# ์ ๋จ์ ์์ธก
|
| 210 |
+
future_periods = (pd.Timestamp(horizon_end).year - df["date"].max().year) * 12 + \
|
| 211 |
+
(pd.Timestamp(horizon_end).month - df["date"].max().month) + 1
|
| 212 |
+
future = m.make_future_dataframe(periods=future_periods, freq='MS') # ์ ์์์ผ
|
| 213 |
+
future = future.resample('MS', on='ds').first().reset_index() # ์ค๋ณต ์ ๊ฑฐ
|
| 214 |
+
else:
|
| 215 |
+
# ์ผ ๋จ์ ์์ธก
|
| 216 |
+
periods = max((pd.Timestamp(horizon_end) - df["date"].max()).days, 1)
|
| 217 |
+
future = m.make_future_dataframe(periods=periods, freq="D")
|
| 218 |
|
| 219 |
# Make predictions
|
| 220 |
forecast = m.predict(future)
|
| 221 |
+
|
| 222 |
+
# ์์ธก๊ฐ ๋ฒ์ ์กฐ์ (์์ ์์ธก ๋ฐฉ์ง ๋ฐ ์ํ๊ฐ ์ค์ )
|
| 223 |
+
forecast['yhat'] = np.maximum(forecast['yhat'], 0) # ์์ ์ ๊ฑฐ
|
| 224 |
+
max_historical = prophet_df['y'].max() * 5 # ์ต๋ ์ญ์ฌ์ ๊ฐ๊ฒฉ์ 5๋ฐฐ๋ก ์ ํ
|
| 225 |
+
forecast['yhat'] = np.minimum(forecast['yhat'], max_historical) # ์ํ๊ฐ ์ค์
|
| 226 |
+
|
| 227 |
return m, forecast
|
| 228 |
except Exception as e:
|
| 229 |
st.error(f"Prophet ๋ชจ๋ธ ์์ฑ ์ค ์ค๋ฅ: {str(e)}")
|
| 230 |
return None, None
|
| 231 |
|
| 232 |
+
|
| 233 |
+
def format_currency(value):
|
| 234 |
+
"""์ํ ํ์์ผ๋ก ์ซ์ ํฌ๋งทํ
"""
|
| 235 |
+
return f"{value:,.0f}์"
|
| 236 |
+
|
| 237 |
+
|
| 238 |
# -------------------------------------------------
|
| 239 |
# LOAD DATA ---------------------------------------
|
| 240 |
# -------------------------------------------------
|
|
|
|
| 259 |
# -------------------------------------------------
|
| 260 |
st.header(f"๐ {selected_item} ๊ฐ๊ฒฉ ์์ธก ๋์๋ณด๋")
|
| 261 |
|
| 262 |
+
# ๋ฐ์ดํฐ ํํฐ๋ง ๋ก์ง
|
| 263 |
try:
|
| 264 |
+
macro_start_dt = pd.Timestamp("1996-01-01")
|
| 265 |
+
# ๋ฐ์ดํฐ์ ์์์ผ์ด 1996๋
์ดํ์ธ์ง ํ์ธ
|
| 266 |
+
if item_df["date"].min() > macro_start_dt:
|
|
|
|
| 267 |
macro_start_dt = item_df["date"].min()
|
|
|
|
| 268 |
|
| 269 |
macro_df = item_df[item_df["date"] >= macro_start_dt].copy()
|
| 270 |
except Exception as e:
|
|
|
|
| 288 |
else:
|
| 289 |
try:
|
| 290 |
with st.spinner("์ฅ๊ธฐ ์์ธก ๋ชจ๋ธ ์์ฑ ์ค..."):
|
| 291 |
+
# ์ ๋จ์ ์์ธก์ผ๋ก ๋ณ๊ฒฝ
|
| 292 |
+
m_macro, fc_macro = fit_prophet(macro_df, MACRO_END, monthly=True, changepoint_prior_scale=0.01)
|
| 293 |
|
| 294 |
if m_macro is not None and fc_macro is not None:
|
| 295 |
+
# ์ค์ ๋ฐ์ดํฐ์ ์์ธก ๋ฐ์ดํฐ ๊ตฌ๋ถ
|
| 296 |
+
cutoff_date = pd.Timestamp("2025-01-01")
|
| 297 |
+
|
| 298 |
+
# ํ๋กฏ ์์ฑ
|
| 299 |
+
fig = go.Figure()
|
| 300 |
+
|
| 301 |
+
# ์ค์ ๋ฐ์ดํฐ ์ถ๊ฐ (1996-2024)
|
| 302 |
+
historical_data = macro_df[macro_df["date"] < cutoff_date].copy()
|
| 303 |
+
if not historical_data.empty:
|
| 304 |
+
fig.add_trace(go.Scatter(
|
| 305 |
+
x=historical_data["date"],
|
| 306 |
+
y=historical_data["price"],
|
| 307 |
+
mode="lines",
|
| 308 |
+
name="์ค์ ๊ฐ๊ฒฉ (1996-2024)",
|
| 309 |
+
line=dict(color="blue", width=2)
|
| 310 |
+
))
|
| 311 |
+
|
| 312 |
+
# ์์ธก ๋ฐ์ดํฐ ์ถ๊ฐ (2025-2030)
|
| 313 |
+
forecast_data = fc_macro[fc_macro["ds"] >= cutoff_date].copy()
|
| 314 |
+
if not forecast_data.empty:
|
| 315 |
+
fig.add_trace(go.Scatter(
|
| 316 |
+
x=forecast_data["ds"],
|
| 317 |
+
y=forecast_data["yhat"],
|
| 318 |
+
mode="lines",
|
| 319 |
+
name="์์ธก ๊ฐ๊ฒฉ (2025-2030)",
|
| 320 |
+
line=dict(color="red", width=2, dash="dash")
|
| 321 |
+
))
|
| 322 |
+
|
| 323 |
+
# ์ ๋ขฐ ๊ตฌ๊ฐ ์ถ๊ฐ
|
| 324 |
+
fig.add_trace(go.Scatter(
|
| 325 |
+
x=forecast_data["ds"],
|
| 326 |
+
y=forecast_data["yhat_upper"],
|
| 327 |
+
mode="lines",
|
| 328 |
+
line=dict(width=0),
|
| 329 |
+
showlegend=False
|
| 330 |
+
))
|
| 331 |
+
fig.add_trace(go.Scatter(
|
| 332 |
+
x=forecast_data["ds"],
|
| 333 |
+
y=forecast_data["yhat_lower"],
|
| 334 |
+
mode="lines",
|
| 335 |
+
line=dict(width=0),
|
| 336 |
+
fill="tonexty",
|
| 337 |
+
fillcolor="rgba(255, 0, 0, 0.1)",
|
| 338 |
+
name="95% ์ ๋ขฐ ๊ตฌ๊ฐ"
|
| 339 |
+
))
|
| 340 |
+
|
| 341 |
+
# ๋ ์ด์์ ์ค์
|
| 342 |
+
fig.update_layout(
|
| 343 |
+
title=f"{selected_item} ์ฅ๊ธฐ ๊ฐ๊ฒฉ ์์ธก (1996-2030)",
|
| 344 |
+
xaxis_title="์ฐ๋",
|
| 345 |
+
yaxis_title="๊ฐ๊ฒฉ (์)",
|
| 346 |
+
legend=dict(
|
| 347 |
+
orientation="h",
|
| 348 |
+
yanchor="bottom",
|
| 349 |
+
y=1.02,
|
| 350 |
+
xanchor="right",
|
| 351 |
+
x=1
|
| 352 |
+
)
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
# ์ฐจํธ ํ์
|
| 356 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 357 |
|
| 358 |
+
# 2030๋
์์ธก๊ฐ ํ์
|
| 359 |
+
try:
|
| 360 |
+
latest_price = macro_df.iloc[-1]["price"]
|
| 361 |
+
# 2030๋
๋ง์ง๋ง ์ ์ฐพ๊ธฐ
|
| 362 |
+
target_date = pd.Timestamp("2030-12-31")
|
| 363 |
+
close_dates = fc_macro.loc[(fc_macro["ds"] - target_date).abs().argsort()[:1], "ds"].values[0]
|
| 364 |
+
macro_pred = fc_macro.loc[fc_macro["ds"] == close_dates, "yhat"].iloc[0]
|
| 365 |
+
macro_pct = (macro_pred - latest_price) / latest_price * 100
|
| 366 |
+
|
| 367 |
+
col1, col2 = st.columns(2)
|
| 368 |
+
with col1:
|
| 369 |
+
st.metric("ํ์ฌ ๊ฐ๊ฒฉ", format_currency(latest_price))
|
| 370 |
+
with col2:
|
| 371 |
+
st.metric("2030๋
์์ธก๊ฐ", format_currency(macro_pred), f"{macro_pct:+.1f}%")
|
| 372 |
+
except Exception as e:
|
| 373 |
+
st.error(f"์์ธก๊ฐ ๊ณ์ฐ ์ค๋ฅ: {str(e)}")
|
| 374 |
else:
|
| 375 |
st.warning("์์ธก ๋ชจ๋ธ์ ์์ฑํ ์ ์์ต๋๋ค.")
|
| 376 |
fig = px.line(item_df, x="date", y="price", title=f"{selected_item} ๊ณผ๊ฑฐ ๊ฐ๊ฒฉ")
|
|
|
|
| 383 |
# -------------------------------------------------
|
| 384 |
# MICRO FORECAST 2024โ2026 ------------------------
|
| 385 |
# -------------------------------------------------
|
| 386 |
+
st.subheader("๐ 2024โ2026 ๋จ๊ธฐ ์์ธก (์๋ณ)")
|
| 387 |
|
| 388 |
+
# ๋ฐ์ดํฐ ํํฐ๋ง - ์ต๊ทผ 3๋
๋ฐ์ดํฐ ํ์ฉ
|
| 389 |
try:
|
| 390 |
+
three_years_ago = pd.Timestamp("2021-01-01")
|
| 391 |
+
if item_df["date"].min() > three_years_ago:
|
| 392 |
+
three_years_ago = item_df["date"].min()
|
| 393 |
+
|
| 394 |
+
micro_df = item_df[item_df["date"] >= three_years_ago].copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
except Exception as e:
|
| 396 |
st.error(f"๋จ๊ธฐ ์์ธก ๋ฐ์ดํฐ ํํฐ๋ง ์ค๋ฅ: {str(e)}")
|
| 397 |
+
# ์ต๊ทผ ๋ฐ์ดํฐ ์ฌ์ฉ
|
| 398 |
+
micro_df = item_df.sort_values("date").tail(24).copy()
|
| 399 |
|
| 400 |
if len(micro_df) < 2:
|
| 401 |
+
st.warning(f"์ต๊ทผ ๋ฐ์ดํฐ๊ฐ ์ถฉ๋ถํ์ง ์์ต๋๋ค.")
|
| 402 |
fig = px.line(item_df, x="date", y="price", title=f"{selected_item} ์ต๊ทผ ๊ฐ๊ฒฉ")
|
| 403 |
st.plotly_chart(fig, use_container_width=True)
|
| 404 |
else:
|
| 405 |
try:
|
| 406 |
with st.spinner("๋จ๊ธฐ ์์ธก ๋ชจ๋ธ ์์ฑ ์ค..."):
|
| 407 |
+
# ์ ๋จ์ ์์ธก์ผ๋ก ๋ณ๊ฒฝ
|
| 408 |
+
m_micro, fc_micro = fit_prophet(micro_df, MICRO_END, monthly=True, changepoint_prior_scale=0.05)
|
| 409 |
|
| 410 |
if m_micro is not None and fc_micro is not None:
|
| 411 |
+
# 2024-01-01๋ถํฐ 2026-12-31๊น์ง ํํฐ๋ง
|
| 412 |
+
start_date = pd.Timestamp("2024-01-01")
|
| 413 |
+
end_date = pd.Timestamp("2026-12-31")
|
| 414 |
+
|
| 415 |
+
# ์๋ณ ๋ฐ์ดํฐ ์ค๋น
|
| 416 |
+
monthly_historical = micro_df.copy()
|
| 417 |
+
monthly_historical["year_month"] = monthly_historical["date"].dt.strftime("%Y-%m")
|
| 418 |
+
monthly_historical = monthly_historical.groupby("year_month").agg({
|
| 419 |
+
"date": "first",
|
| 420 |
+
"price": "mean"
|
| 421 |
+
}).reset_index(drop=True)
|
| 422 |
+
|
| 423 |
+
monthly_historical = monthly_historical[
|
| 424 |
+
(monthly_historical["date"] >= start_date) &
|
| 425 |
+
(monthly_historical["date"] <= end_date)
|
| 426 |
+
]
|
| 427 |
+
|
| 428 |
+
monthly_forecast = fc_micro[
|
| 429 |
+
(fc_micro["ds"] >= start_date) &
|
| 430 |
+
(fc_micro["ds"] <= end_date)
|
| 431 |
+
].copy()
|
| 432 |
+
|
| 433 |
+
# ์๋ณ ์ฐจํธ ์์ฑ
|
| 434 |
+
fig = go.Figure()
|
| 435 |
+
|
| 436 |
+
# 2024๋
์ค์ ๋ฐ์ดํฐ
|
| 437 |
+
actual_2024 = monthly_historical[
|
| 438 |
+
(monthly_historical["date"] >= pd.Timestamp("2024-01-01")) &
|
| 439 |
+
(monthly_historical["date"] <= pd.Timestamp("2024-12-31"))
|
| 440 |
+
]
|
| 441 |
+
|
| 442 |
+
if not actual_2024.empty:
|
| 443 |
+
fig.add_trace(go.Scatter(
|
| 444 |
+
x=actual_2024["date"],
|
| 445 |
+
y=actual_2024["price"],
|
| 446 |
+
mode="lines+markers",
|
| 447 |
+
name="2024 ์ค์ ๊ฐ๊ฒฉ",
|
| 448 |
+
line=dict(color="blue", width=2),
|
| 449 |
+
marker=dict(size=8)
|
| 450 |
+
))
|
| 451 |
+
|
| 452 |
+
# 2024๋
์ดํ ์์ธก ๋ฐ์ดํฐ
|
| 453 |
+
cutoff = pd.Timestamp("2024-12-31")
|
| 454 |
+
future_data = monthly_forecast[monthly_forecast["ds"] > cutoff]
|
| 455 |
|
| 456 |
+
if not future_data.empty:
|
| 457 |
+
fig.add_trace(go.Scatter(
|
| 458 |
+
x=future_data["ds"],
|
| 459 |
+
y=future_data["yhat"],
|
| 460 |
+
mode="lines+markers",
|
| 461 |
+
name="2025-2026 ์์ธก ๊ฐ๊ฒฉ",
|
| 462 |
+
line=dict(color="red", width=2, dash="dash"),
|
| 463 |
+
marker=dict(size=8)
|
| 464 |
+
))
|
| 465 |
+
|
| 466 |
+
# ์ ๋ขฐ ๊ตฌ๊ฐ ์ถ๊ฐ
|
| 467 |
+
fig.add_trace(go.Scatter(
|
| 468 |
+
x=future_data["ds"],
|
| 469 |
+
y=future_data["yhat_upper"],
|
| 470 |
+
mode="lines",
|
| 471 |
+
line=dict(width=0),
|
| 472 |
+
showlegend=False
|
| 473 |
+
))
|
| 474 |
+
fig.add_trace(go.Scatter(
|
| 475 |
+
x=future_data["ds"],
|
| 476 |
+
y=future_data["yhat_lower"],
|
| 477 |
+
mode="lines",
|
| 478 |
+
line=dict(width=0),
|
| 479 |
+
fill="tonexty",
|
| 480 |
+
fillcolor="rgba(255, 0, 0, 0.1)",
|
| 481 |
+
name="95% ์ ๋ขฐ ๊ตฌ๊ฐ"
|
| 482 |
+
))
|
| 483 |
+
|
| 484 |
+
# ๋ ์ด์์ ์ค์
|
| 485 |
+
fig.update_layout(
|
| 486 |
+
title=f"{selected_item} ์๋ณ ๋จ๊ธฐ ์์ธก (2024-2026)",
|
| 487 |
+
xaxis_title="์",
|
| 488 |
+
yaxis_title="๊ฐ๊ฒฉ (์)",
|
| 489 |
+
xaxis=dict(
|
| 490 |
+
tickformat="%Y-%m",
|
| 491 |
+
dtick="M3", # 3๊ฐ์ ๊ฐ๊ฒฉ
|
| 492 |
+
tickangle=45
|
| 493 |
+
),
|
| 494 |
+
legend=dict(
|
| 495 |
+
orientation="h",
|
| 496 |
+
yanchor="bottom",
|
| 497 |
+
y=1.02,
|
| 498 |
+
xanchor="right",
|
| 499 |
+
x=1
|
| 500 |
+
)
|
| 501 |
+
)
|
| 502 |
+
|
| 503 |
+
# ์ฐจํธ ํ์
|
| 504 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 505 |
+
|
| 506 |
+
# ์๋ณ ์์ธก ๊ฐ๊ฒฉ ํ์ (2025-2026)
|
| 507 |
+
with st.expander("์๋ณ ์์ธก ๊ฐ๊ฒฉ ์์ธ๋ณด๊ธฐ"):
|
| 508 |
+
monthly_detail = monthly_forecast[monthly_forecast["ds"] > cutoff].copy()
|
| 509 |
+
monthly_detail["๋ ์ง"] = monthly_detail["ds"].dt.strftime("%Y๋
%m์")
|
| 510 |
+
monthly_detail["์์ธก๊ฐ๊ฒฉ"] = monthly_detail["yhat"].apply(format_currency)
|
| 511 |
+
monthly_detail["ํํ๊ฐ"] = monthly_detail["yhat_lower"].apply(format_currency)
|
| 512 |
+
monthly_detail["์ํ๊ฐ"] = monthly_detail["yhat_upper"].apply(format_currency)
|
| 513 |
+
|
| 514 |
+
st.dataframe(
|
| 515 |
+
monthly_detail[["๋ ์ง", "์์ธก๊ฐ๊ฒฉ", "ํํ๊ฐ", "์ํ๊ฐ"]],
|
| 516 |
+
hide_index=True
|
| 517 |
+
)
|
| 518 |
+
|
| 519 |
+
# 2026๋
์์ธก๊ฐ ํ์
|
| 520 |
+
try:
|
| 521 |
+
latest_price = monthly_historical.iloc[-1]["price"] if not monthly_historical.empty else micro_df.iloc[-1]["price"]
|
| 522 |
+
|
| 523 |
+
# 2026๋
๋ง์ง๋ง ์ ์ฐพ๊ธฐ
|
| 524 |
+
target_date = pd.Timestamp("2026-12-31")
|
| 525 |
+
close_dates = monthly_forecast.loc[(monthly_forecast["ds"] - target_date).abs().argsort()[:1], "ds"].values[0]
|
| 526 |
+
micro_pred = monthly_forecast.loc[monthly_forecast["ds"] == close_dates, "yhat"].iloc[0]
|
| 527 |
+
micro_pct = (micro_pred - latest_price) / latest_price * 100
|
| 528 |
+
|
| 529 |
+
col1, col2 = st.columns(2)
|
| 530 |
+
with col1:
|
| 531 |
+
st.metric("ํ์ฌ ๊ฐ๊ฒฉ", format_currency(latest_price))
|
| 532 |
+
with col2:
|
| 533 |
+
st.metric("2026๋
12์ ์์ธก๊ฐ", format_currency(micro_pred), f"{micro_pct:+.1f}%")
|
| 534 |
+
except Exception as e:
|
| 535 |
+
st.error(f"์์ธก๊ฐ ๊ณ์ฐ ์ค๋ฅ: {str(e)}")
|
| 536 |
else:
|
| 537 |
st.warning("๋จ๊ธฐ ์์ธก ๋ชจ๋ธ์ ์์ฑํ ์ ์์ต๋๋ค.")
|
| 538 |
except Exception as e:
|
|
|
|
| 554 |
f"**์ฐ๊ฐ ํผํฌ ์:** {int(month_season.idxmax())}์ \n"
|
| 555 |
f"**์ฐ๊ฐ ์ ์ ์:** {int(month_season.idxmin())}์ \n"
|
| 556 |
f"**์ฐ๊ฐ ๋ณ๋ํญ:** {month_season.max() - month_season.min():.1f}")
|
| 557 |
+
|
| 558 |
+
# ์๋ณ ๊ณ์ ์ฑ ์ฐจํธ
|
| 559 |
+
month_names = ["1์", "2์", "3์", "4์", "5์", "6์", "7์", "8์", "9์", "10์", "11์", "12์"]
|
| 560 |
+
month_values = month_season.values
|
| 561 |
+
|
| 562 |
+
fig = px.bar(
|
| 563 |
+
x=month_names,
|
| 564 |
+
y=month_values,
|
| 565 |
+
title=f"{selected_item} ์๋ณ ๊ฐ๊ฒฉ ๋ณ๋ ํจํด",
|
| 566 |
+
labels={"x": "์", "y": "์๋์ ๊ฐ๊ฒฉ ๋ณ๋"}
|
| 567 |
+
)
|
| 568 |
+
|
| 569 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 570 |
except Exception as e:
|
| 571 |
st.error(f"์์ฆ๋๋ฆฌํฐ ๋ถ์ ์ค๋ฅ: {str(e)}")
|
| 572 |
else:
|