Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,15 +8,20 @@ import seaborn as sns
|
|
8 |
import matplotlib.pyplot as plt
|
9 |
from datetime import date
|
10 |
|
11 |
-
#
|
12 |
-
#
|
13 |
-
#
|
14 |
-
DATA_PATH = "price_data.csv" #
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
@st.cache_data(show_spinner=False)
|
17 |
def load_data(path: str) -> pd.DataFrame:
|
18 |
-
"""Load & preprocess price data.
|
19 |
-
Expects columns: date, item, price."""
|
20 |
df = pd.read_csv(path, parse_dates=["date"])
|
21 |
df.sort_values("date", inplace=True)
|
22 |
return df
|
@@ -25,86 +30,83 @@ def load_data(path: str) -> pd.DataFrame:
|
|
25 |
def get_items(df: pd.DataFrame):
|
26 |
return sorted(df["item"].unique())
|
27 |
|
28 |
-
|
29 |
-
|
30 |
def fit_prophet(df: pd.DataFrame, horizon_end: str):
|
31 |
-
"""Fit Prophet on df(date, price) and forecast till horizon_end (YYYY-MM-DD)."""
|
32 |
m = Prophet(yearly_seasonality=True, weekly_seasonality=False, daily_seasonality=False)
|
33 |
m.fit(df.rename(columns={"date": "ds", "price": "y"}))
|
34 |
future = m.make_future_dataframe(periods=(pd.Timestamp(horizon_end) - df["date"].max()).days, freq="D")
|
35 |
forecast = m.predict(future)
|
36 |
return m, forecast
|
37 |
|
38 |
-
#
|
39 |
-
#
|
40 |
-
#
|
41 |
-
st.title("๐ ํ๋ชฉ๋ณ ๊ฐ๊ฒฉ ์์ธก ๋์๋ณด๋")
|
42 |
-
|
43 |
raw_df = load_data(DATA_PATH)
|
44 |
-
|
45 |
-
st.sidebar.header("๐ ํ๋ชฉ ์ ํ")
|
46 |
-
selected_item = st.sidebar.selectbox("ํ๋ชฉ", get_items(raw_df))
|
47 |
-
|
48 |
current_date = date.today()
|
49 |
-
st.sidebar.
|
50 |
|
51 |
item_df = raw_df[raw_df["item"] == selected_item].copy()
|
52 |
-
|
53 |
if item_df.empty:
|
54 |
-
st.
|
55 |
st.stop()
|
56 |
|
57 |
-
#
|
58 |
-
#
|
59 |
-
#
|
60 |
-
st.
|
61 |
-
|
62 |
-
macro_start = "1996-01-01"
|
63 |
-
macro_end = "2030-12-31"
|
64 |
-
macro_df = item_df[item_df["date"] >= macro_start]
|
65 |
-
|
66 |
-
m_macro, fc_macro = fit_prophet(macro_df, macro_end)
|
67 |
|
68 |
-
|
|
|
69 |
fig_macro.add_scatter(x=macro_df["date"], y=macro_df["price"], mode="lines", name="Actual")
|
70 |
st.plotly_chart(fig_macro, use_container_width=True)
|
71 |
|
72 |
-
#
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
80 |
|
81 |
-
|
82 |
|
83 |
-
|
|
|
84 |
fig_micro.add_scatter(x=micro_df["date"], y=micro_df["price"], mode="lines", name="Actual")
|
85 |
st.plotly_chart(fig_micro, use_container_width=True)
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
st.
|
91 |
|
92 |
-
|
|
|
|
|
|
|
93 |
comp_fig = m_micro.plot_components(fc_micro)
|
94 |
st.pyplot(comp_fig)
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
105 |
st.subheader("๐งฎ ํ๋ชฉ ๊ฐ ์๊ด๊ด๊ณ ํํธ๋งต")
|
106 |
|
107 |
-
# ํผ๋ฒ: ์๊ฐ ํ๊ท ๊ฐ๊ฒฉ์ผ๋ก ๋จ์ ๋ง์ถ๊ธฐ
|
108 |
corr_df = (raw_df.assign(month=lambda d: d["date"].dt.to_period("M"))
|
109 |
.groupby(["month", "item"], as_index=False)["price"].mean()
|
110 |
.pivot(index="month", columns="item", values="price"))
|
@@ -118,25 +120,26 @@ st.pyplot(fig)
|
|
118 |
|
119 |
st.markdown("""
|
120 |
**ํด์ ๊ฐ์ด๋**
|
121 |
-
-
|
122 |
-
-
|
123 |
-
- ์ ๋๊ฐ โฅ 0.7
|
124 |
""")
|
125 |
|
126 |
-
#
|
127 |
-
#
|
128 |
-
#
|
129 |
-
st.subheader("๐ 30์ผ ์ด๋
|
130 |
|
131 |
-
vol_df = (item_df.set_index("date")["price"]
|
132 |
-
|
133 |
-
.std().reset_index(name="rolling_std"))
|
134 |
-
fig_vol = px.area(vol_df, x="date", y="rolling_std", title="30D Rolling Std Dev")
|
135 |
st.plotly_chart(fig_vol, use_container_width=True)
|
136 |
|
137 |
st.markdown("""
|
138 |
-
-
|
139 |
-
-
|
140 |
""")
|
141 |
|
142 |
-
|
|
|
|
|
|
|
|
8 |
import matplotlib.pyplot as plt
|
9 |
from datetime import date
|
10 |
|
11 |
+
# ---------------------------------------------
|
12 |
+
# CONFIG --------------------------------------
|
13 |
+
# ---------------------------------------------
|
14 |
+
DATA_PATH = "price_data.csv" # CSV: date, item, price
|
15 |
+
MACRO_START, MACRO_END = "1996-01-01", "2030-12-31"
|
16 |
+
MICRO_START, MICRO_END = "2020-01-01", "2026-12-31"
|
17 |
+
|
18 |
+
st.set_page_config(page_title="ํ๋ชฉ๋ณ ๊ฐ๊ฒฉ ์์ธก", page_icon="๐", layout="wide")
|
19 |
+
|
20 |
+
# ---------------------------------------------
|
21 |
+
# UTILITIES -----------------------------------
|
22 |
+
# ---------------------------------------------
|
23 |
@st.cache_data(show_spinner=False)
|
24 |
def load_data(path: str) -> pd.DataFrame:
|
|
|
|
|
25 |
df = pd.read_csv(path, parse_dates=["date"])
|
26 |
df.sort_values("date", inplace=True)
|
27 |
return df
|
|
|
30 |
def get_items(df: pd.DataFrame):
|
31 |
return sorted(df["item"].unique())
|
32 |
|
33 |
+
@st.cache_data(show_spinner=False)
|
|
|
34 |
def fit_prophet(df: pd.DataFrame, horizon_end: str):
|
|
|
35 |
m = Prophet(yearly_seasonality=True, weekly_seasonality=False, daily_seasonality=False)
|
36 |
m.fit(df.rename(columns={"date": "ds", "price": "y"}))
|
37 |
future = m.make_future_dataframe(periods=(pd.Timestamp(horizon_end) - df["date"].max()).days, freq="D")
|
38 |
forecast = m.predict(future)
|
39 |
return m, forecast
|
40 |
|
41 |
+
# ---------------------------------------------
|
42 |
+
# LOAD ----------------------------------------
|
43 |
+
# ---------------------------------------------
|
|
|
|
|
44 |
raw_df = load_data(DATA_PATH)
|
45 |
+
selected_item = st.sidebar.selectbox("๐ ํ๋ชฉ ์ ํ", get_items(raw_df))
|
|
|
|
|
|
|
46 |
current_date = date.today()
|
47 |
+
st.sidebar.write(f"**์ค๋:** {current_date}")
|
48 |
|
49 |
item_df = raw_df[raw_df["item"] == selected_item].copy()
|
|
|
50 |
if item_df.empty:
|
51 |
+
st.error("๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.")
|
52 |
st.stop()
|
53 |
|
54 |
+
# ---------------------------------------------
|
55 |
+
# MACRO FORECAST ------------------------------
|
56 |
+
# ---------------------------------------------
|
57 |
+
st.header(f"๐ {selected_item} ๊ฐ๊ฒฉ ์ ๋ง")
|
58 |
+
macro_df = item_df[item_df["date"] >= MACRO_START]
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
m_macro, fc_macro = fit_prophet(macro_df, MACRO_END)
|
61 |
+
fig_macro = px.line(fc_macro, x="ds", y="yhat", title="Macro Forecast 1996โ2030")
|
62 |
fig_macro.add_scatter(x=macro_df["date"], y=macro_df["price"], mode="lines", name="Actual")
|
63 |
st.plotly_chart(fig_macro, use_container_width=True)
|
64 |
|
65 |
+
# --- Metrics โ
|
66 |
+
latest_price = macro_df.iloc[-1]["price"]
|
67 |
+
macro_last = fc_macro[fc_macro["ds"] == MACRO_END]["yhat"].iloc[0]
|
68 |
+
macro_diff = macro_last - latest_price
|
69 |
+
macro_pct = macro_diff / latest_price * 100
|
70 |
+
st.metric(label="2030 ์์ธก ๊ฐ๊ฒฉ", value=f"{macro_last:,.0f}", delta=f"{macro_pct:+.1f}% vs ์ต๊ทผ")
|
71 |
|
72 |
+
# ---------------------------------------------
|
73 |
+
# MICRO FORECAST ------------------------------
|
74 |
+
# ---------------------------------------------
|
75 |
+
st.subheader("๐ ๋ฏธ์ ์์ธก 2024โ2026")
|
76 |
|
77 |
+
micro_df = item_df[item_df["date"] >= MICRO_START]
|
78 |
|
79 |
+
m_micro, fc_micro = fit_prophet(micro_df, MICRO_END)
|
80 |
+
fig_micro = px.line(fc_micro, x="ds", y="yhat", title="Micro Forecast 2024โ2026")
|
81 |
fig_micro.add_scatter(x=micro_df["date"], y=micro_df["price"], mode="lines", name="Actual")
|
82 |
st.plotly_chart(fig_micro, use_container_width=True)
|
83 |
|
84 |
+
micro_last = fc_micro[fc_micro["ds"] == MICRO_END]["yhat"].iloc[0]
|
85 |
+
micro_diff = micro_last - latest_price
|
86 |
+
micro_pct = micro_diff / latest_price * 100
|
87 |
+
st.metric(label="2026 ์์ธก ๊ฐ๊ฒฉ", value=f"{micro_last:,.0f}", delta=f"{micro_pct:+.1f}% vs ์ต๊ทผ")
|
88 |
|
89 |
+
# ---------------------------------------------
|
90 |
+
# PATTERN & SEASONALITY -----------------------
|
91 |
+
# ---------------------------------------------
|
92 |
+
with st.expander("๐ ์์ฆ๋๋ฆฌํฐ ๋ถ์ ๋ฐ ํจํด ํด์ค"):
|
93 |
comp_fig = m_micro.plot_components(fc_micro)
|
94 |
st.pyplot(comp_fig)
|
95 |
+
|
96 |
+
# ์๋ณ seasonality summary
|
97 |
+
month_season = (fc_micro[["ds", "yearly"]]
|
98 |
+
.assign(month=lambda d: d["ds"].dt.month)
|
99 |
+
.groupby("month")["yearly"].mean())
|
100 |
+
peak_month = int(month_season.idxmax())
|
101 |
+
trough_month = int(month_season.idxmin())
|
102 |
+
|
103 |
+
st.markdown(f"**ํจํด ์์ฝ** \n- __์ฐ๊ฐ ํผํฌ__: {peak_month}์ \n- __์ฐ๊ฐ ์ ์ __: {trough_month}์ \n- ํ๊ท ๋ณ๋ํญ(์ฐ): {month_season.max() - month_season.min():.1f} ๋จ๊ฐ")
|
104 |
+
|
105 |
+
# ---------------------------------------------
|
106 |
+
# CORRELATION HEATMAP -------------------------
|
107 |
+
# ---------------------------------------------
|
108 |
st.subheader("๐งฎ ํ๋ชฉ ๊ฐ ์๊ด๊ด๊ณ ํํธ๋งต")
|
109 |
|
|
|
110 |
corr_df = (raw_df.assign(month=lambda d: d["date"].dt.to_period("M"))
|
111 |
.groupby(["month", "item"], as_index=False)["price"].mean()
|
112 |
.pivot(index="month", columns="item", values="price"))
|
|
|
120 |
|
121 |
st.markdown("""
|
122 |
**ํด์ ๊ฐ์ด๋**
|
123 |
+
- **๋นจ๊ฐ์(+)**: ๋ ํ๋ชฉ ๊ฐ๊ฒฉ์ด ๋์กฐํ โ ๊ณต๊ธ๋ง/์์ ์ฐ๋ ๊ฐ๋ฅ์ฑ.
|
124 |
+
- **ํ๋์(-)**: ๋์ฒด์ฌ ๊ด๊ณ.
|
125 |
+
- ์ ๋๊ฐ โฅ 0.7 ์ ์ ์ฑ
ยท์ฌ๊ณ ์ ๋ต ์ค๊ณ ์ ์ฃผ์ ๊น๊ฒ ๋ณผ ํ์๊ฐ ์์ต๋๋ค.
|
126 |
""")
|
127 |
|
128 |
+
# ---------------------------------------------
|
129 |
+
# EXTRA CHARTS -------------------------------
|
130 |
+
# ---------------------------------------------
|
131 |
+
st.subheader("๐ ์ถ๊ฐ ์ธ์ฌ์ดํธ: 30์ผ ์ด๋ ๋ณ๋์ฑ")
|
132 |
|
133 |
+
vol_df = (item_df.set_index("date")["price"].rolling(30).std().reset_index())
|
134 |
+
fig_vol = px.area(vol_df, x="date", y="price", title="30D Rolling Std Dev")
|
|
|
|
|
135 |
st.plotly_chart(fig_vol, use_container_width=True)
|
136 |
|
137 |
st.markdown("""
|
138 |
+
- ๋ณ๋์ฑ ๊ธ๋ฑ ๊ตฌ๊ฐ์ **๊ณต๊ธ ์ถฉ๊ฒฉยท์์ ์ด๋ฒคํธ** ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค.
|
139 |
+
- ์ต๊ทผ ๋ณ๋์ฑ์ด ๋ฎ์์ง๋ฉด **๊ฐ๊ฒฉ ์์ฐฉ**์ผ๋ก ํด์ํ ์ ์์ต๋๋ค.
|
140 |
""")
|
141 |
|
142 |
+
# ---------------------------------------------
|
143 |
+
# FOOTER --------------------------------------
|
144 |
+
# ---------------------------------------------
|
145 |
+
st.caption("๋ฐ์ดํฐ ์ถ์ฒ: ๋ด๋ถ ๋์์ฐ๋ฌผ ๊ฐ๊ฒฉ DB ยท Forecast by Prophet ยท Dashboard built with Streamlit")
|