Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,26 +3,36 @@ import pandas as pd
|
|
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 seaborn as sns
|
8 |
import matplotlib.pyplot as plt
|
9 |
from datetime import date
|
|
|
10 |
|
11 |
-
#
|
12 |
-
# CONFIG
|
13 |
-
#
|
14 |
-
|
|
|
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(
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
df.sort_values("date", inplace=True)
|
27 |
return df
|
28 |
|
@@ -38,108 +48,80 @@ def fit_prophet(df: pd.DataFrame, horizon_end: str):
|
|
38 |
forecast = m.predict(future)
|
39 |
return m, forecast
|
40 |
|
41 |
-
#
|
42 |
-
# LOAD
|
43 |
-
#
|
44 |
-
raw_df = load_data(
|
45 |
-
|
|
|
|
|
46 |
current_date = date.today()
|
47 |
-
st.sidebar.
|
48 |
|
49 |
-
item_df = raw_df
|
50 |
if item_df.empty:
|
51 |
-
st.error("
|
52 |
st.stop()
|
53 |
|
54 |
-
#
|
55 |
-
#
|
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 |
-
|
68 |
-
|
69 |
-
|
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 |
-
|
85 |
-
|
86 |
-
|
87 |
-
st.metric(label="2026 ์์ธก ๊ฐ๊ฒฉ", value=f"{micro_last:,.0f}", delta=f"{micro_pct:+.1f}% vs ์ต๊ทผ")
|
88 |
|
89 |
-
#
|
90 |
-
|
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
|
99 |
.groupby("month")["yearly"].mean())
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
#
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
.pivot(index="month", columns="item", values="price"))
|
113 |
-
|
114 |
-
corr = corr_df.corr()
|
115 |
-
|
116 |
-
fig, ax = plt.subplots(figsize=(12, 10))
|
117 |
mask = np.triu(np.ones_like(corr, dtype=bool))
|
|
|
118 |
sns.heatmap(corr, mask=mask, cmap="RdBu_r", center=0, linewidths=.5, ax=ax)
|
119 |
st.pyplot(fig)
|
120 |
|
121 |
-
st.
|
122 |
-
**ํด์ ๊ฐ์ด๋**
|
123 |
-
- **๋นจ๊ฐ์(+)**: ๋ ํ๋ชฉ ๊ฐ๊ฒฉ์ด ๋์กฐํ โ ๊ณต๊ธ๋ง/์์ ์ฐ๋ ๊ฐ๋ฅ์ฑ.
|
124 |
-
- **ํ๋์(-)**: ๋์ฒด์ฌ ๊ด๊ณ.
|
125 |
-
- ์ ๋๊ฐ โฅ 0.7 ์ ์ ์ฑ
ยท์ฌ๊ณ ์ ๋ต ์ค๊ณ ์ ์ฃผ์ ๊น๊ฒ ๋ณผ ํ์๊ฐ ์์ต๋๋ค.
|
126 |
-
""")
|
127 |
-
|
128 |
-
# ---------------------------------------------
|
129 |
-
# EXTRA CHARTS -------------------------------
|
130 |
-
# ---------------------------------------------
|
131 |
-
st.subheader("๐ ์ถ๊ฐ ์ธ์ฌ์ดํธ: 30์ผ ์ด๋ ๋ณ๋์ฑ")
|
132 |
|
133 |
-
|
134 |
-
|
|
|
|
|
135 |
st.plotly_chart(fig_vol, use_container_width=True)
|
136 |
|
137 |
-
st.
|
138 |
-
- ๋ณ๋์ฑ ๊ธ๋ฑ ๊ตฌ๊ฐ์ **๊ณต๊ธ ์ถฉ๊ฒฉยท์์ ์ด๋ฒคํธ** ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค.
|
139 |
-
- ์ต๊ทผ ๋ณ๋์ฑ์ด ๋ฎ์์ง๋ฉด **๊ฐ๊ฒฉ ์์ฐฉ**์ผ๋ก ํด์ํ ์ ์์ต๋๋ค.
|
140 |
-
""")
|
141 |
-
|
142 |
-
# ---------------------------------------------
|
143 |
-
# FOOTER --------------------------------------
|
144 |
-
# ---------------------------------------------
|
145 |
-
st.caption("๋ฐ์ดํฐ ์ถ์ฒ: ๋ด๋ถ ๋์์ฐ๋ฌผ ๊ฐ๊ฒฉ DB ยท Forecast by Prophet ยท Dashboard built with Streamlit")
|
|
|
3 |
import numpy as np
|
4 |
from prophet import Prophet
|
5 |
import plotly.express as px
|
|
|
6 |
import seaborn as sns
|
7 |
import matplotlib.pyplot as plt
|
8 |
from datetime import date
|
9 |
+
from pathlib import Path
|
10 |
|
11 |
+
# -------------------------------------------------
|
12 |
+
# CONFIG ------------------------------------------
|
13 |
+
# -------------------------------------------------
|
14 |
+
CSV_PATH = Path("price_data.csv")
|
15 |
+
PARQUET_PATH = Path("domae-202503.parquet") # 1996โ1993-03 ๊ฐ๊ฒฉ ๋ฐ์ดํฐ
|
16 |
MACRO_START, MACRO_END = "1996-01-01", "2030-12-31"
|
17 |
MICRO_START, MICRO_END = "2020-01-01", "2026-12-31"
|
18 |
|
19 |
st.set_page_config(page_title="ํ๋ชฉ๋ณ ๊ฐ๊ฒฉ ์์ธก", page_icon="๐", layout="wide")
|
20 |
|
21 |
+
# -------------------------------------------------
|
22 |
+
# UTILITIES ---------------------------------------
|
23 |
+
# -------------------------------------------------
|
24 |
@st.cache_data(show_spinner=False)
|
25 |
+
def load_data() -> pd.DataFrame:
|
26 |
+
"""Load price data from Parquet if available, else CSV."""
|
27 |
+
if PARQUET_PATH.exists():
|
28 |
+
df = pd.read_parquet(PARQUET_PATH)
|
29 |
+
elif CSV_PATH.exists():
|
30 |
+
df = pd.read_csv(CSV_PATH)
|
31 |
+
else:
|
32 |
+
st.error("๋ฐ์ดํฐ ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค. price_data.csv ๋๋ domae-202503.parquet" )
|
33 |
+
st.stop()
|
34 |
+
# ํ์คํ
|
35 |
+
df["date"] = pd.to_datetime(df["date"])
|
36 |
df.sort_values("date", inplace=True)
|
37 |
return df
|
38 |
|
|
|
48 |
forecast = m.predict(future)
|
49 |
return m, forecast
|
50 |
|
51 |
+
# -------------------------------------------------
|
52 |
+
# LOAD DATA ---------------------------------------
|
53 |
+
# -------------------------------------------------
|
54 |
+
raw_df = load_data()
|
55 |
+
|
56 |
+
st.sidebar.header("๐ ํ๋ชฉ ์ ํ")
|
57 |
+
selected_item = st.sidebar.selectbox("ํ๋ชฉ", get_items(raw_df))
|
58 |
current_date = date.today()
|
59 |
+
st.sidebar.caption(f"์ค๋: {current_date}")
|
60 |
|
61 |
+
item_df = raw_df.query("item == @selected_item").copy()
|
62 |
if item_df.empty:
|
63 |
+
st.error("์ ํํ ํ๋ชฉ ๋ฐ์ดํฐ ์์")
|
64 |
st.stop()
|
65 |
|
66 |
+
# -------------------------------------------------
|
67 |
+
# PLOTS -------------------------------------------
|
68 |
+
# -------------------------------------------------
|
69 |
+
st.header(f"๐ {selected_item} ๊ฐ๊ฒฉ ์์ธก ๋์๋ณด๋")
|
|
|
70 |
|
71 |
+
# Macro forecast 1996โ2030
|
72 |
+
macro_df = item_df[item_df["date"] >= MACRO_START]
|
73 |
m_macro, fc_macro = fit_prophet(macro_df, MACRO_END)
|
74 |
fig_macro = px.line(fc_macro, x="ds", y="yhat", title="Macro Forecast 1996โ2030")
|
75 |
fig_macro.add_scatter(x=macro_df["date"], y=macro_df["price"], mode="lines", name="Actual")
|
76 |
st.plotly_chart(fig_macro, use_container_width=True)
|
77 |
|
|
|
78 |
latest_price = macro_df.iloc[-1]["price"]
|
79 |
+
macro_pred = fc_macro.loc[fc_macro["ds"] == MACRO_END, "yhat"].iloc[0]
|
80 |
+
macro_pct = (macro_pred - latest_price) / latest_price * 100
|
81 |
+
st.metric("2030 ์์ธก๊ฐ", f"{macro_pred:,.0f}", f"{macro_pct:+.1f}%")
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
+
# Micro forecast 2024โ2026
|
84 |
+
st.subheader("๐ 2024โ2026 ๋จ๊ธฐ ์์ธก")
|
85 |
micro_df = item_df[item_df["date"] >= MICRO_START]
|
|
|
86 |
m_micro, fc_micro = fit_prophet(micro_df, MICRO_END)
|
87 |
fig_micro = px.line(fc_micro, x="ds", y="yhat", title="Micro Forecast 2024โ2026")
|
88 |
fig_micro.add_scatter(x=micro_df["date"], y=micro_df["price"], mode="lines", name="Actual")
|
89 |
st.plotly_chart(fig_micro, use_container_width=True)
|
90 |
|
91 |
+
micro_pred = fc_micro.loc[fc_micro["ds"] == MICRO_END, "yhat"].iloc[0]
|
92 |
+
micro_pct = (micro_pred - latest_price) / latest_price * 100
|
93 |
+
st.metric("2026 ์์ธก๊ฐ", f"{micro_pred:,.0f}", f"{micro_pct:+.1f}%")
|
|
|
94 |
|
95 |
+
# Seasonality components
|
96 |
+
with st.expander("๐ ์์ฆ๋๋ฆฌํฐ & ํจํด ์ค๋ช
"):
|
|
|
|
|
97 |
comp_fig = m_micro.plot_components(fc_micro)
|
98 |
st.pyplot(comp_fig)
|
|
|
|
|
99 |
month_season = (fc_micro[["ds", "yearly"]]
|
100 |
+
.assign(month=lambda d: d.ds.dt.month)
|
101 |
.groupby("month")["yearly"].mean())
|
102 |
+
st.markdown(
|
103 |
+
f"**์ฐ๊ฐ ํผํฌ ์:** {int(month_season.idxmax())}์\n\n"
|
104 |
+
f"**์ฐ๊ฐ ์ ์ ์:** {int(month_season.idxmin())}์\n\n"
|
105 |
+
f"**์ฐ๊ฐ ๋ณ๋ํญ:** {month_season.max() - month_season.min():.1f}")
|
106 |
+
|
107 |
+
# Correlation heatmap
|
108 |
+
st.subheader("๐งฎ ํ๋ชฉ ๊ฐ ์๊ด๊ด๊ณ")
|
109 |
+
monthly_pivot = (raw_df.assign(month=lambda d: d.date.dt.to_period("M"))
|
110 |
+
.groupby(["month", "item"], as_index=False)["price"].mean()
|
111 |
+
.pivot(index="month", columns="item", values="price"))
|
112 |
+
|
113 |
+
corr = monthly_pivot.corr()
|
|
|
|
|
|
|
|
|
|
|
114 |
mask = np.triu(np.ones_like(corr, dtype=bool))
|
115 |
+
fig, ax = plt.subplots(figsize=(12, 10))
|
116 |
sns.heatmap(corr, mask=mask, cmap="RdBu_r", center=0, linewidths=.5, ax=ax)
|
117 |
st.pyplot(fig)
|
118 |
|
119 |
+
st.info("๋นจ๊ฐ ์์ญ: ๊ฐ๊ฒฉ ๋์กฐํ / ํ๋ ์์ญ: ๋์ฒด์ฌ ๊ฐ๋ฅ์ฑ.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
+
# Volatility Chart
|
122 |
+
st.subheader("๐ 30์ผ ์ด๋ ํ์คํธ์ฐจ (๊ฐ๊ฒฉ ๋ณ๋์ฑ)")
|
123 |
+
vol = item_df.set_index("date")["price"].rolling(30).std().dropna().reset_index()
|
124 |
+
fig_vol = px.area(vol, x="date", y="price", title="Rolling 30D Std Dev")
|
125 |
st.plotly_chart(fig_vol, use_container_width=True)
|
126 |
|
127 |
+
st.caption("๋ฐ์ดํฐ: domae-202503.parquet ยท Prophet ์์ธก ยท Streamlit ๋์๋ณด๋")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|