QuantumLearner commited on
Commit
dacced0
·
verified ·
1 Parent(s): 47a7c53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -9
app.py CHANGED
@@ -13,17 +13,17 @@ import plotly.colors as pc
13
  warnings.filterwarnings("ignore")
14
 
15
  # Set wide layout
16
- st.set_page_config(layout="wide", page_title="Yield Curve Analysis")
17
 
18
  # Sidebar menu
19
- st.sidebar.title("Yield Curve Analysis App")
20
- st.sidebar.write("""
21
- ### Overview
22
- This application analyzes U.S. Treasury yield curves, yield spreads, and trends to provide insights into economic conditions.
23
  ### How to Use
24
- 1. Select the start and end dates for the analysis.
25
- 2. Click on 'Run Analysis' to generate the plots and interpretations.
26
- """)
27
 
28
  # Input fields for start and end dates
29
  start_date = st.sidebar.date_input("Start Date", datetime(2023, 1, 1))
@@ -45,11 +45,21 @@ labels = {
45
  'DGS30': '30 Years'
46
  }
47
 
 
 
 
 
 
 
 
 
 
48
  if st.sidebar.button("Run Analysis"):
49
  yield_data = get_data_fred(tickers, start=start_date, end=end_date).dropna()
50
  yield_data.index = yield_data.index.date
51
 
52
  # Plot the time series for each term using Plotly
 
53
  fig = go.Figure()
54
  for ticker in tickers:
55
  fig.add_trace(go.Scatter(x=yield_data.index, y=yield_data[ticker], mode='lines', name=labels[ticker]))
@@ -75,6 +85,12 @@ if st.sidebar.button("Run Analysis"):
75
  slope_indicator = yield_data['DGS30'] - yield_data['DGS1MO']
76
  average_slope = slope_indicator.mean()
77
 
 
 
 
 
 
 
78
  if average_slope > 0:
79
  st.write("On average, the yield curve shows an upward slope, which often indicates positive economic growth expectations.")
80
  else:
@@ -85,6 +101,8 @@ if st.sidebar.button("Run Analysis"):
85
  positive_trend_count = (trend > 0).sum()
86
  negative_trend_count = (trend < 0).sum()
87
 
 
 
88
  if positive_trend_count > negative_trend_count:
89
  st.write("Overall, yields are increasing over time, indicating rising interest rates.")
90
  else:
@@ -101,6 +119,7 @@ if st.sidebar.button("Run Analysis"):
101
  })
102
 
103
  # Plot the spreads over time using Plotly
 
104
  fig2 = go.Figure()
105
  for spread in spreads.columns:
106
  fig2.add_trace(go.Scatter(x=spreads.index, y=spreads[spread], mode='lines', name=spread))
@@ -114,8 +133,11 @@ if st.sidebar.button("Run Analysis"):
114
  st.plotly_chart(fig2)
115
 
116
  # Dynamic interpretation for spreads
 
 
 
 
117
  spread_stats = spreads.describe()
118
- st.write("Spread Statistics:")
119
  st.write(spread_stats)
120
 
121
  for spread in spreads.columns:
@@ -146,6 +168,7 @@ if st.sidebar.button("Run Analysis"):
146
  st.write("Negative spreads here indicate that yields decrease with maturity, reflecting investor pessimism about future economic conditions. This situation is also referred to as a yield inversion.")
147
 
148
  # Correlation matrix for the yield data
 
149
  correlation_matrix = yield_data.corr()
150
 
151
  # Plot the heatmap of the correlation matrix using Plotly
@@ -171,6 +194,7 @@ if st.sidebar.button("Run Analysis"):
171
  yield_data_long = yield_data.melt(id_vars='DATE', var_name='Maturity', value_name='Yield')
172
 
173
  # Create the initial figure
 
174
  fig4 = go.Figure()
175
 
176
  # Add traces, one for each date
@@ -266,6 +290,13 @@ if st.sidebar.button("Run Analysis"):
266
  yield_data['10Y-2Y Spread'] = yield_data['DGS10'] - yield_data['DGS2']
267
 
268
  # Fit a Markov Switching Model
 
 
 
 
 
 
 
269
  mod = sm.tsa.MarkovRegression(yield_data['10Y-2Y Spread'], k_regimes=2, trend='c')
270
  res = mod.fit()
271
 
@@ -323,6 +354,7 @@ if st.sidebar.button("Run Analysis"):
323
  st.write("Regime 2 tends to last longer, indicating longer periods of economic stress or recession.")
324
 
325
 
 
326
  hide_streamlit_style = """
327
  <style>
328
  #MainMenu {visibility: hidden;}
 
13
  warnings.filterwarnings("ignore")
14
 
15
  # Set wide layout
16
+ st.set_page_config(layout="wide", page_title="U.S Treasury Yield Curve Analysis")
17
 
18
  # Sidebar menu
19
+ st.sidebar.title("Input Parameters")
20
+ #st.sidebar.write("""
21
+ #### Overview
22
+ #This application analyzes U.S. Treasury yield curves, yield spreads, and trends to provide insights into economic conditions.
23
  ### How to Use
24
+ #1. Select the start and end dates for the analysis.
25
+ #2. Click on 'Run Analysis' to generate the plots and interpretations.
26
+ #""")
27
 
28
  # Input fields for start and end dates
29
  start_date = st.sidebar.date_input("Start Date", datetime(2023, 1, 1))
 
45
  'DGS30': '30 Years'
46
  }
47
 
48
+ # Overview and Purpose
49
+ st.title("Yield Curve Analysis")
50
+ st.write("""
51
+ The U.S. Treasury yield curve is a graph that plots the yields of Treasury securities at fixed maturities.
52
+ Yield curves are crucial for understanding the term structure of interest rates and assessing economic conditions.
53
+ This application analyzes the yield curve, yield spreads, and trends to provide insights into the economic outlook.
54
+ """)
55
+
56
+
57
  if st.sidebar.button("Run Analysis"):
58
  yield_data = get_data_fred(tickers, start=start_date, end=end_date).dropna()
59
  yield_data.index = yield_data.index.date
60
 
61
  # Plot the time series for each term using Plotly
62
+ st.subheader("U.S. Treasury Yield Curve Time Series")
63
  fig = go.Figure()
64
  for ticker in tickers:
65
  fig.add_trace(go.Scatter(x=yield_data.index, y=yield_data[ticker], mode='lines', name=labels[ticker]))
 
85
  slope_indicator = yield_data['DGS30'] - yield_data['DGS1MO']
86
  average_slope = slope_indicator.mean()
87
 
88
+ st.subheader("Yield Curve Slope Interpretation")
89
+ st.write("The slope of the yield curve is an important indicator of economic expectations:")
90
+ st.latex(r'''
91
+ \text{Slope} = \text{Yield}_{30\text{Year}} - \text{Yield}_{1\text{Month}}
92
+ ''')
93
+
94
  if average_slope > 0:
95
  st.write("On average, the yield curve shows an upward slope, which often indicates positive economic growth expectations.")
96
  else:
 
101
  positive_trend_count = (trend > 0).sum()
102
  negative_trend_count = (trend < 0).sum()
103
 
104
+ st.subheader("Yield Curve Trend Interpretation")
105
+ st.write("Analyzing the trend of the yield curve helps in understanding the direction of interest rates:")
106
  if positive_trend_count > negative_trend_count:
107
  st.write("Overall, yields are increasing over time, indicating rising interest rates.")
108
  else:
 
119
  })
120
 
121
  # Plot the spreads over time using Plotly
122
+ st.subheader("Yield Spreads Over Time")
123
  fig2 = go.Figure()
124
  for spread in spreads.columns:
125
  fig2.add_trace(go.Scatter(x=spreads.index, y=spreads[spread], mode='lines', name=spread))
 
133
  st.plotly_chart(fig2)
134
 
135
  # Dynamic interpretation for spreads
136
+ st.subheader("Interpretation of Yield Spreads")
137
+ st.write("""
138
+ Yield spreads are the differences between yields on different maturities of bonds. They are often used to predict economic changes:
139
+ """)
140
  spread_stats = spreads.describe()
 
141
  st.write(spread_stats)
142
 
143
  for spread in spreads.columns:
 
168
  st.write("Negative spreads here indicate that yields decrease with maturity, reflecting investor pessimism about future economic conditions. This situation is also referred to as a yield inversion.")
169
 
170
  # Correlation matrix for the yield data
171
+ st.subheader("Correlation Heatmap of U.S. Treasury Yields")
172
  correlation_matrix = yield_data.corr()
173
 
174
  # Plot the heatmap of the correlation matrix using Plotly
 
194
  yield_data_long = yield_data.melt(id_vars='DATE', var_name='Maturity', value_name='Yield')
195
 
196
  # Create the initial figure
197
+ st.subheader("Interactive U.S. Treasury Yield Curve")
198
  fig4 = go.Figure()
199
 
200
  # Add traces, one for each date
 
290
  yield_data['10Y-2Y Spread'] = yield_data['DGS10'] - yield_data['DGS2']
291
 
292
  # Fit a Markov Switching Model
293
+ st.subheader("Markov Switching Model Analysis")
294
+ st.write("We use a Markov Switching Model to identify different regimes in the yield spread data:")
295
+ st.latex(r'''
296
+ y_t = \mu_{s_t} + \epsilon_t \\
297
+ \epsilon_t \sim N(0, \sigma_{s_t}^2)
298
+ ''')
299
+
300
  mod = sm.tsa.MarkovRegression(yield_data['10Y-2Y Spread'], k_regimes=2, trend='c')
301
  res = mod.fit()
302
 
 
354
  st.write("Regime 2 tends to last longer, indicating longer periods of economic stress or recession.")
355
 
356
 
357
+
358
  hide_streamlit_style = """
359
  <style>
360
  #MainMenu {visibility: hidden;}