Spaces:
Build error
Build error
Synced repo using 'sync_with_huggingface' Github Action
Browse files- .gitattributes +1 -0
- Readme.md +65 -0
- app.py +282 -0
- data/unc_and_mai_combined.xlsx +3 -0
- data_preproc.py +97 -0
- requirements.txt +7 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
data/unc_and_mai_combined.xlsx filter=lfs diff=lfs merge=lfs -text
|
Readme.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Data for Macroeconomic Attention and Narrative Uncertainty
|
| 2 |
+
|
| 3 |
+
We provide the daily and monthly macroeconomic attention indices (MAI) and narrative uncertainty scores (UNC) for the New York Times (NYT_) and Wall Street Journal (WSJ_) used in the following papers:
|
| 4 |
+
- Fisher, Martineau, and Sheng (2021) "Macroeconomic Attention and Announcement Risk Premia" [SSRN](https://ssrn.com/abstract=2703978)
|
| 5 |
+
- Martineau, Poulos, Wu, Thompson, Haghighi, Yuan, Hull (2023) "Narrative Monetary Policy Uncertainty" [SSRN](https://ssrn.com/abstract=4573829)
|
| 6 |
+
- Benson, Hull, Nozawa, Poulos, Strela, Wu (2023) "Forecasting Using Text-Based Uncertainty Measures" [SSRN]()
|
| 7 |
+
|
| 8 |
+
The visualization of the data on daily and monthly frequency can be viewed on [this GitHub page]().
|
| 9 |
+
|
| 10 |
+
## Data
|
| 11 |
+
The MAI data is available from June 1980 to December 2020, and includes the following series:
|
| 12 |
+
- CREDIT_RATING_MAI: credit rating
|
| 13 |
+
- GDP_MAI: GDP
|
| 14 |
+
- HSNG_MAI: housing market
|
| 15 |
+
- INFL_MAI: inflation
|
| 16 |
+
- MONETARY_MAI: monetary
|
| 17 |
+
- OIL_MAI: oil
|
| 18 |
+
- UNEMP_MAI: unemployment
|
| 19 |
+
- USD_MAI: USD
|
| 20 |
+
|
| 21 |
+
The UNC data is available from January 1984 to May 2023, we include both uncertainty generated using static Loughran McDonald dictionary (_LM) and uncertainty generated using [BERT model](https://huggingface.co/jeniakim/hedgehog) (_ML). The data includes the following series:
|
| 22 |
+
- NMPU: monetary policy uncertainty
|
| 23 |
+
- INFU: inflation uncertainty
|
| 24 |
+
- INFU_weighted: the daily average of inflation uncertainty is calculated based on the weighted average of counts of the word "inflation" in the article
|
| 25 |
+
- INFU_screened: the daily average of inflation uncertainty is calculated only on articles where the word "inflation" appears once in the title or the first five sentences in the text, or when the word "inflation" appears at least twice in the article
|
| 26 |
+
- GSCI: uncertainty about commodity, referenced as GSCU in our article.
|
| 27 |
+
|
| 28 |
+
We provide the daily series, daily series detrended by 252 trading day average, and monthly trading day average series.
|
| 29 |
+
|
| 30 |
+
We also include the following existing measures of uncertainty. If you use those data, please cite the corresponding works accordingly.
|
| 31 |
+
- EPU: policy uncertainty measure proposed by Baker and Bloom. [Official website](http://www.policyuncertainty.com/)
|
| 32 |
+
- MPU-MKT: Uncertainty from short-term rates based on Eurodollar futures options by [Bauer](https://www.michaeldbauer.com/files/mpu.pdf)
|
| 33 |
+
- VIX: Chicago Board Options Exchange Volatility Index, monthly data is the last valid MOVE of each month
|
| 34 |
+
- MOVE: Merrill Lynch Option Volatility Estimate, monthly data is the last valid MOVE of each month
|
| 35 |
+
|
| 36 |
+
## Citation
|
| 37 |
+
If you use this data, please cite our studies accordingly:
|
| 38 |
+
```
|
| 39 |
+
@article{MAI,
|
| 40 |
+
author = {Fisher, Adlai and Martineau, Charles and Sheng, Jinfei},
|
| 41 |
+
title = "{Macroeconomic Attention and Announcement Risk Premia}",
|
| 42 |
+
journal = {Review of Financial Studies},
|
| 43 |
+
volume = {35},
|
| 44 |
+
number = {11},
|
| 45 |
+
pages = {5057-5093},
|
| 46 |
+
year = {2022},
|
| 47 |
+
month = {02},
|
| 48 |
+
abstract = "{We construct macroeconomic attention indexes (MAI), which are new measures of attention to different macroeconomic risks, including unemployment and monetary policy. Individual MAI tend to increase around related announcements and following changes in related fundamentals. Further, bad news raises attention more than good news. For unemployment and FOMC, attention predicts announcement risk premiums and implied volatility changes with large economic magnitudes. Our findings support theories of endogenous attention and announcement risk premiums, while demonstrating future research directions, including that announcements can raise new concerns. Macroeconomic announcements are important not only for contents and timing but also for attention.Authors have furnished an Internet Appendix, which is available on the Oxford University Press Web site next to the link to the final published paper online.}",
|
| 49 |
+
issn = {0893-9454},
|
| 50 |
+
doi = {10.1093/rfs/hhac011},
|
| 51 |
+
url = {https://doi.org/10.1093/rfs/hhac011},
|
| 52 |
+
eprint = {https://academic.oup.com/rfs/article-pdf/35/11/5057/46558642/hhac011.pdf},
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
@article{NMPU,
|
| 56 |
+
author={Martineau, Charles and Poulos, Zissis and Wu, Yuntao and Thompson, Cameron and Haghighi, Maryam and Yuan, Jun and Hull, John},
|
| 57 |
+
title={Narrative Monetary Policy Uncertainty},
|
| 58 |
+
year={2023},
|
| 59 |
+
month={9},
|
| 60 |
+
note = {Available at SSRN: \url{https://ssrn.com/abstract=4573829} or \url{http://dx.doi.org/10.2139/ssrn.4573829}}
|
| 61 |
+
}
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
## Credit
|
| 65 |
+
This project is built on [Gradio](https://github.com/gradio-app/gradio), [Plotly](https://github.com/plotly/plotly.py) and [pandas](https://pandas.pydata.org/)
|
app.py
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from data_preproc import *
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
|
| 5 |
+
BASE_CSS = """
|
| 6 |
+
a:link {
|
| 7 |
+
color: blue;
|
| 8 |
+
text-decoration:underline;
|
| 9 |
+
}
|
| 10 |
+
a:hover {
|
| 11 |
+
color: red;
|
| 12 |
+
text-decoration:underline;
|
| 13 |
+
}
|
| 14 |
+
hr {
|
| 15 |
+
display: block;
|
| 16 |
+
height: 1px;
|
| 17 |
+
border: 0;
|
| 18 |
+
border-top: 1px solid #ccc;
|
| 19 |
+
margin: 1em 0;
|
| 20 |
+
padding: 0;
|
| 21 |
+
}
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
def onDownloadButtonClick():
|
| 25 |
+
return "data/unc_and_mai_combined.xlsx"
|
| 26 |
+
|
| 27 |
+
def onCheckboxChange(selected_cols, base_df, title, y_title, smooth, smooth_window):
|
| 28 |
+
# df = base_df[["date"] + selected_cols]
|
| 29 |
+
df = base_df[["date"] + selected_cols].copy()
|
| 30 |
+
corr_df = calculate_correlation_df(df, selected_cols)
|
| 31 |
+
if smooth:
|
| 32 |
+
df = smooth_df(df, selected_cols, smooth_window)
|
| 33 |
+
# melted_df = df.melt(id_vars="date", value_vars=selected_cols, var_name="type", value_name="value")
|
| 34 |
+
fig = px.line(df, x="date", y=selected_cols, title=title, width=1000, height=500).update_layout(xaxis_title="date", yaxis_title=y_title)
|
| 35 |
+
fig.update_xaxes(rangeslider_visible=True)
|
| 36 |
+
for recession in NBER_recession_dates:
|
| 37 |
+
fig.add_vrect(recession["start"], recession["stop"], fillcolor="gray", opacity=0.2)
|
| 38 |
+
|
| 39 |
+
return gr.Plot(fig), gr.DataFrame(corr_df, label="correlation")
|
| 40 |
+
|
| 41 |
+
def main():
|
| 42 |
+
with gr.Blocks(css=BASE_CSS, title="Macroeconomic Attention and Narrative Uncertainty") as block:
|
| 43 |
+
with gr.Tab("Introduction"):
|
| 44 |
+
gr.Markdown("""# Macroeconomic Attention and Narrative Uncertainty
|
| 45 |
+
We provide the daily and monthly macroeconomic attention indices (MAI) and narrative uncertainty scores (UNC) for the New York Times (NYT_) and Wall Street Journal (WSJ_) used in the following papers:
|
| 46 |
+
- Fisher, Martineau, and Sheng (2021) "Macroeconomic Attention and Announcement Risk Premia" [SSRN](https://ssrn.com/abstract=2703978)
|
| 47 |
+
- Martineau, Poulos, Wu, Thompson, Haghighi, Yuan, Hull (2023) "Narrative Monetary Policy Uncertainty" [SSRN](https://ssrn.com/abstract=4573829)
|
| 48 |
+
|
| 49 |
+
## Data
|
| 50 |
+
The MAI data is available from June 1980 to December 2020, and includes the following series:
|
| 51 |
+
- CREDIT_RATING_MAI: credit rating
|
| 52 |
+
- GDP_MAI: GDP
|
| 53 |
+
- HSNG_MAI: housing market
|
| 54 |
+
- INFL_MAI: inflation
|
| 55 |
+
- MONETARY_MAI: monetary
|
| 56 |
+
- OIL_MAI: oil
|
| 57 |
+
- UNEMP_MAI: unemployment
|
| 58 |
+
- USD_MAI: USD
|
| 59 |
+
|
| 60 |
+
The UNC data is available from January 1984 to May 2023, we include both uncertainty generated using static Loughran McDonald dictionary (_LM) and uncertainty generated using [BERT model](https://huggingface.co/jeniakim/hedgehog) (_ML). The data includes the following series:
|
| 61 |
+
- NMPU: monetary policy uncertainty
|
| 62 |
+
- INFU: inflation uncertainty
|
| 63 |
+
- INFU_weighted: the daily average of inflation uncertainty is calculated based on the weighted average of counts of the word "inflation" in the article
|
| 64 |
+
- INFU_screened: the daily average of inflation uncertainty is calculated only on articles where the word "inflation" appears once in the title or the first five sentences in the text, or when the word "inflation" appears at least twice in the article
|
| 65 |
+
- GSCI: uncertainty about commodity, referenced as GSCU in our article.
|
| 66 |
+
|
| 67 |
+
We also include the following existing measures of uncertainty. If you use those data, please cite the corresponding works accordingly.
|
| 68 |
+
- EPU: policy uncertainty measure proposed by Baker and Bloom. [Official website](http://www.policyuncertainty.com/)
|
| 69 |
+
- MPU-MKT: Uncertainty from short-term rates based on Eurodollar futures options by [Bauer](https://www.michaeldbauer.com/files/mpu.pdf)
|
| 70 |
+
- VIX: Chicago Board Options Exchange Volatility Index, monthly data is the last valid MOVE of each month
|
| 71 |
+
- MOVE: Merrill Lynch Option Volatility Estimate, monthly data is the last valid MOVE of each month
|
| 72 |
+
|
| 73 |
+
Besides this introduction tab, there another 4 tabs you can navigate to.
|
| 74 |
+
- **Daily Series**: The raw data. Full daily series from 1980-2023
|
| 75 |
+
- **Daily Detrended Series**: Trading day data only, detrended by 252 day mean
|
| 76 |
+
- **Monthly Series**: Average of the trading day value for each month.
|
| 77 |
+
|
| 78 |
+
Inside these tabs, we show the timeseris plots and correlations.""")
|
| 79 |
+
gr.File(value="data/unc_and_mai_combined.xlsx", type="filepath", label="Download Full Data")
|
| 80 |
+
|
| 81 |
+
with gr.Tab("Daily Series"):
|
| 82 |
+
gr.Markdown("**Note:** The gray boxs are NBER recessions.")
|
| 83 |
+
with gr.Group():
|
| 84 |
+
gr.Markdown("## MAI")
|
| 85 |
+
mai_check_box = gr.CheckboxGroup(mai_cols, label="MAI indices", interactive=True)
|
| 86 |
+
mai_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 87 |
+
mai_plot = gr.Plot()
|
| 88 |
+
mai_corr = gr.DataFrame(label="correlation")
|
| 89 |
+
mai_smooth_check_box.change(onCheckboxChange,
|
| 90 |
+
inputs=[mai_check_box, gr.State(daily_mai), gr.State("Macroeconomic Attention Index"), gr.State("MAI"), mai_smooth_check_box, gr.State(252)],
|
| 91 |
+
outputs=[mai_plot, mai_corr])
|
| 92 |
+
mai_check_box.change(onCheckboxChange,
|
| 93 |
+
inputs=[mai_check_box, gr.State(daily_mai), gr.State("Macroeconomic Attention Index"), gr.State("MAI"), mai_smooth_check_box, gr.State(252)],
|
| 94 |
+
outputs=[mai_plot, mai_corr])
|
| 95 |
+
with gr.Group():
|
| 96 |
+
gr.Markdown("## UNC LM")
|
| 97 |
+
lm_check_box = gr.CheckboxGroup(unc_lm_cols, label="UNC_LM indices", interactive=True)
|
| 98 |
+
lm_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 99 |
+
lm_plot = gr.Plot()
|
| 100 |
+
lm_corr = gr.DataFrame(label="correlation")
|
| 101 |
+
lm_smooth_check_box.change(onCheckboxChange,
|
| 102 |
+
inputs=[lm_check_box, gr.State(daily_unc_lm), gr.State("Narrative uncertainty LM"), gr.State("UNC"), lm_smooth_check_box, gr.State(252)],
|
| 103 |
+
outputs=[lm_plot, lm_corr])
|
| 104 |
+
lm_check_box.change(onCheckboxChange,
|
| 105 |
+
inputs=[lm_check_box, gr.State(daily_unc_lm), gr.State("Narrative uncertainty LM"), gr.State("UNC"), lm_smooth_check_box, gr.State(252)],
|
| 106 |
+
outputs=[lm_plot, lm_corr])
|
| 107 |
+
|
| 108 |
+
with gr.Group():
|
| 109 |
+
gr.Markdown("## UNC ML")
|
| 110 |
+
ml_check_box = gr.CheckboxGroup(unc_ml_cols, label="UNC_ML indices", interactive=True)
|
| 111 |
+
ml_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 112 |
+
ml_plot = gr.Plot()
|
| 113 |
+
ml_corr = gr.DataFrame(label="correlation")
|
| 114 |
+
ml_smooth_check_box.change(onCheckboxChange,
|
| 115 |
+
inputs=[ml_check_box, gr.State(daily_unc_ml), gr.State("Narrative uncertainty ML"), gr.State("UNC"), ml_smooth_check_box, gr.State(252)],
|
| 116 |
+
outputs=[ml_plot, ml_corr])
|
| 117 |
+
ml_check_box.change(onCheckboxChange,
|
| 118 |
+
inputs=[ml_check_box, gr.State(daily_unc_ml), gr.State("Narrative uncertainty ML"), gr.State("UNC"), ml_smooth_check_box, gr.State(252)],
|
| 119 |
+
outputs=[ml_plot, ml_corr])
|
| 120 |
+
|
| 121 |
+
with gr.Group():
|
| 122 |
+
gr.Markdown("## Existing Measures")
|
| 123 |
+
other_check_box = gr.CheckboxGroup(other_cols, label="Existing indices", interactive=True)
|
| 124 |
+
other_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 125 |
+
other_plot = gr.Plot()
|
| 126 |
+
other_corr = gr.DataFrame(label="correlation")
|
| 127 |
+
other_smooth_check_box.change(onCheckboxChange,
|
| 128 |
+
inputs=[other_check_box, gr.State(daily_other), gr.State("Existing indices"), gr.State("idx"), other_smooth_check_box, gr.State(252)],
|
| 129 |
+
outputs=[other_plot, other_corr])
|
| 130 |
+
other_check_box.change(onCheckboxChange,
|
| 131 |
+
inputs=[other_check_box, gr.State(daily_other), gr.State("Existing indices"), gr.State("idx"), other_smooth_check_box, gr.State(252)],
|
| 132 |
+
outputs=[other_plot, other_corr])
|
| 133 |
+
with gr.Group():
|
| 134 |
+
gr.Markdown("""## All measures
|
| 135 |
+
All the data are normalized to [0,1] by $\\frac{x-x_{\\text{min}}}{x_{\\text{max}-x_{\\text{min}}}}$""",
|
| 136 |
+
latex_delimiters=[{ "left": "$$", "right": "$$", "display": True }, { "left": "$", "right": "$", "display": False }])
|
| 137 |
+
all_check_box = gr.CheckboxGroup(all_cols, label="all measures", interactive=True)
|
| 138 |
+
all_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 139 |
+
all_plot = gr.Plot()
|
| 140 |
+
all_corr = gr.DataFrame()
|
| 141 |
+
all_smooth_check_box.change(onCheckboxChange,
|
| 142 |
+
inputs=[all_check_box, gr.State(daily_df_all), gr.State("All indices"), gr.State("idx"), all_smooth_check_box, gr.State(252)],
|
| 143 |
+
outputs=[all_plot, all_corr])
|
| 144 |
+
all_check_box.change(onCheckboxChange,
|
| 145 |
+
inputs=[all_check_box, gr.State(daily_df_all), gr.State("All indices"), gr.State("idx"), all_smooth_check_box, gr.State(252)],
|
| 146 |
+
outputs=[all_plot, all_corr])
|
| 147 |
+
|
| 148 |
+
with gr.Tab("Daily Detrended Series"):
|
| 149 |
+
gr.Markdown("**Note:** The gray boxs are NBER recessions.")
|
| 150 |
+
with gr.Group():
|
| 151 |
+
gr.Markdown("## MAI")
|
| 152 |
+
mai_check_box = gr.CheckboxGroup(mai_cols, label="MAI indices", interactive=True)
|
| 153 |
+
mai_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 154 |
+
mai_plot = gr.Plot()
|
| 155 |
+
mai_corr = gr.Dataframe(label="correlation")
|
| 156 |
+
mai_smooth_check_box.change(onCheckboxChange,
|
| 157 |
+
inputs=[mai_check_box, gr.State(daily_detrend_mai), gr.State("Macroeconomic Attention Index"), gr.State("MAI"), mai_smooth_check_box, gr.State(252)],
|
| 158 |
+
outputs=[mai_plot, mai_corr])
|
| 159 |
+
mai_check_box.change(onCheckboxChange,
|
| 160 |
+
inputs=[mai_check_box, gr.State(daily_detrend_mai), gr.State("Macroeconomic Attention Index"), gr.State("MAI"), mai_smooth_check_box, gr.State(252)],
|
| 161 |
+
outputs=[mai_plot, mai_corr])
|
| 162 |
+
with gr.Group():
|
| 163 |
+
gr.Markdown("## UNC LM")
|
| 164 |
+
lm_check_box = gr.CheckboxGroup(unc_lm_cols, label="UNC_LM indices", interactive=True)
|
| 165 |
+
lm_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 166 |
+
lm_plot = gr.Plot()
|
| 167 |
+
lm_corr = gr.DataFrame(label="correlation")
|
| 168 |
+
lm_smooth_check_box.change(onCheckboxChange,
|
| 169 |
+
inputs=[lm_check_box, gr.State(daily_detrend_unc_lm), gr.State("Narrative uncertainty LM"), gr.State("UNC"), lm_smooth_check_box, gr.State(252)],
|
| 170 |
+
outputs=[lm_plot, lm_corr])
|
| 171 |
+
lm_check_box.change(onCheckboxChange,
|
| 172 |
+
inputs=[lm_check_box, gr.State(daily_detrend_unc_lm), gr.State("Narrative uncertainty LM"), gr.State("UNC"), lm_smooth_check_box, gr.State(252)],
|
| 173 |
+
outputs=[lm_plot, lm_corr])
|
| 174 |
+
with gr.Group():
|
| 175 |
+
gr.Markdown("## UNC ML")
|
| 176 |
+
ml_check_box = gr.CheckboxGroup(unc_ml_cols, label="UNC_ML indices", interactive=True)
|
| 177 |
+
ml_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 178 |
+
ml_plot = gr.Plot()
|
| 179 |
+
ml_corr = gr.DataFrame(label="correlation")
|
| 180 |
+
ml_smooth_check_box.change(onCheckboxChange,
|
| 181 |
+
inputs=[ml_check_box, gr.State(daily_detrend_unc_ml), gr.State("Narrative uncertainty ML"), gr.State("UNC"), ml_smooth_check_box, gr.State(252)],
|
| 182 |
+
outputs=[ml_plot, ml_corr])
|
| 183 |
+
ml_check_box.change(onCheckboxChange,
|
| 184 |
+
inputs=[ml_check_box, gr.State(daily_detrend_unc_ml), gr.State("Narrative uncertainty ML"), gr.State("UNC"), ml_smooth_check_box, gr.State(252)],
|
| 185 |
+
outputs=[ml_plot, ml_corr])
|
| 186 |
+
with gr.Group():
|
| 187 |
+
gr.Markdown("## Existing Measures")
|
| 188 |
+
other_check_box = gr.CheckboxGroup(other_cols, label="Existing indices", interactive=True)
|
| 189 |
+
other_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 190 |
+
other_plot = gr.Plot()
|
| 191 |
+
other_corr = gr.Plot()
|
| 192 |
+
other_smooth_check_box.change(onCheckboxChange,
|
| 193 |
+
inputs=[other_check_box, gr.State(daily_detrend_other), gr.State("Existing indices"), gr.State("idx"), other_smooth_check_box, gr.State(252)],
|
| 194 |
+
outputs=[other_plot, other_corr])
|
| 195 |
+
other_check_box.change(onCheckboxChange,
|
| 196 |
+
inputs=[other_check_box, gr.State(daily_detrend_other), gr.State("Existing indices"), gr.State("idx"), other_smooth_check_box, gr.State(252)],
|
| 197 |
+
outputs=[other_plot, other_corr])
|
| 198 |
+
with gr.Group():
|
| 199 |
+
gr.Markdown("""## All measures
|
| 200 |
+
All the data are normalized to [0,1] by $\\frac{x-x_{\\text{min}}}{x_{\\text{max}-x_{\\text{min}}}}$""",
|
| 201 |
+
latex_delimiters=[{ "left": "$$", "right": "$$", "display": True }, { "left": "$", "right": "$", "display": False }])
|
| 202 |
+
all_check_box = gr.CheckboxGroup(all_cols, label="all measures", interactive=True)
|
| 203 |
+
all_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 204 |
+
all_plot = gr.Plot()
|
| 205 |
+
all_corr = gr.DataFrame()
|
| 206 |
+
all_smooth_check_box.change(onCheckboxChange,
|
| 207 |
+
inputs=[all_check_box, gr.State(daily_detrend_df_all), gr.State("All indices"), gr.State("idx"), all_smooth_check_box, gr.State(252)],
|
| 208 |
+
outputs=[all_plot, all_corr])
|
| 209 |
+
all_check_box.change(onCheckboxChange,
|
| 210 |
+
inputs=[all_check_box, gr.State(daily_detrend_df_all), gr.State("All indices"), gr.State("idx"), all_smooth_check_box, gr.State(252)],
|
| 211 |
+
outputs=[all_plot, all_corr])
|
| 212 |
+
|
| 213 |
+
with gr.Tab("Monthly Series"):
|
| 214 |
+
gr.Markdown("**Note:** The gray boxs are NBER recessions.")
|
| 215 |
+
with gr.Group():
|
| 216 |
+
gr.Markdown("## MAI")
|
| 217 |
+
mai_check_box = gr.CheckboxGroup(mai_cols, label="MAI indices", interactive=True)
|
| 218 |
+
mai_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 219 |
+
mai_plot = gr.Plot()
|
| 220 |
+
mai_corr = gr.DataFrame(label="correlation")
|
| 221 |
+
mai_smooth_check_box.change(onCheckboxChange,
|
| 222 |
+
inputs=[mai_check_box, gr.State(monthly_mai), gr.State("Macroeconomic Attention Index"), gr.State("MAI"), mai_smooth_check_box, gr.State(12)],
|
| 223 |
+
outputs=[mai_plot, mai_corr])
|
| 224 |
+
mai_check_box.change(onCheckboxChange,
|
| 225 |
+
inputs=[mai_check_box, gr.State(monthly_mai), gr.State("Macroeconomic Attention Index"), gr.State("MAI"), mai_smooth_check_box, gr.State(12)],
|
| 226 |
+
outputs=[mai_plot, mai_corr])
|
| 227 |
+
with gr.Group():
|
| 228 |
+
gr.Markdown("## UNC LM")
|
| 229 |
+
lm_check_box = gr.CheckboxGroup(unc_lm_cols, label="UNC_LM indices", interactive=True)
|
| 230 |
+
lm_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 231 |
+
lm_plot = gr.Plot()
|
| 232 |
+
lm_corr = gr.DataFrame(label="correlation")
|
| 233 |
+
lm_smooth_check_box.change(onCheckboxChange,
|
| 234 |
+
inputs=[lm_check_box, gr.State(monthly_unc_lm), gr.State("Narrative uncertainty LM"), gr.State("UNC"), lm_smooth_check_box, gr.State(12)],
|
| 235 |
+
outputs=[lm_plot, lm_corr])
|
| 236 |
+
lm_check_box.change(onCheckboxChange,
|
| 237 |
+
inputs=[lm_check_box, gr.State(monthly_unc_lm), gr.State("Narrative uncertainty LM"), gr.State("UNC"), lm_smooth_check_box, gr.State(12)],
|
| 238 |
+
outputs=[lm_plot, lm_corr])
|
| 239 |
+
with gr.Group():
|
| 240 |
+
gr.Markdown("## UNC ML")
|
| 241 |
+
ml_check_box = gr.CheckboxGroup(unc_ml_cols, label="UNC_ML indices", interactive=True)
|
| 242 |
+
ml_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 243 |
+
ml_plot = gr.Plot()
|
| 244 |
+
ml_corr = gr.DataFrame(label="correlation")
|
| 245 |
+
ml_smooth_check_box.change(onCheckboxChange,
|
| 246 |
+
inputs=[ml_check_box, gr.State(monthly_unc_ml), gr.State("Narrative uncertainty ML"), gr.State("UNC"), ml_smooth_check_box, gr.State(12)],
|
| 247 |
+
outputs=[ml_plot, ml_corr])
|
| 248 |
+
ml_check_box.change(onCheckboxChange,
|
| 249 |
+
inputs=[ml_check_box, gr.State(monthly_unc_ml), gr.State("Narrative uncertainty ML"), gr.State("UNC"), ml_smooth_check_box, gr.State(12)],
|
| 250 |
+
outputs=[ml_plot, ml_corr])
|
| 251 |
+
with gr.Group():
|
| 252 |
+
gr.Markdown("## Existing Measures")
|
| 253 |
+
other_check_box = gr.CheckboxGroup(other_cols, label="Existing indices", interactive=True)
|
| 254 |
+
other_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 255 |
+
other_plot = gr.Plot()
|
| 256 |
+
other_corr = gr.DataFrame(label="correlation")
|
| 257 |
+
other_smooth_check_box.change(onCheckboxChange,
|
| 258 |
+
inputs=[other_check_box, gr.State(monthly_other), gr.State("Existing indices"), gr.State("idx"), other_smooth_check_box, gr.State(12)],
|
| 259 |
+
outputs=[other_plot, other_corr])
|
| 260 |
+
other_check_box.change(onCheckboxChange,
|
| 261 |
+
inputs=[other_check_box, gr.State(monthly_other), gr.State("Existing indices"), gr.State("idx"), other_smooth_check_box, gr.State(12)],
|
| 262 |
+
outputs=[other_plot, other_corr])
|
| 263 |
+
with gr.Group():
|
| 264 |
+
gr.Markdown("""## All measures
|
| 265 |
+
All the data are normalized to [0,1] by $\\frac{x-x_{\\text{min}}}{x_{\\text{max}-x_{\\text{min}}}}$""",
|
| 266 |
+
latex_delimiters=[{ "left": "$$", "right": "$$", "display": True }, { "left": "$", "right": "$", "display": False }])
|
| 267 |
+
all_check_box = gr.CheckboxGroup(all_cols, label="all measures", interactive=True)
|
| 268 |
+
all_smooth_check_box = gr.Checkbox(False, label="Smooth")
|
| 269 |
+
all_plot = gr.Plot()
|
| 270 |
+
all_corr = gr.DataFrame()
|
| 271 |
+
all_smooth_check_box.change(onCheckboxChange,
|
| 272 |
+
inputs=[all_check_box, gr.State(monthly_df_all), gr.State("All indices"), gr.State("idx"), all_smooth_check_box, gr.State(12)],
|
| 273 |
+
outputs=[all_plot, all_corr])
|
| 274 |
+
all_check_box.change(onCheckboxChange,
|
| 275 |
+
inputs=[all_check_box, gr.State(monthly_df_all), gr.State("All indices"), gr.State("idx"), all_smooth_check_box, gr.State(12)],
|
| 276 |
+
outputs=[all_plot, all_corr])
|
| 277 |
+
|
| 278 |
+
block.launch(debug=True, server_port=8005)
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
if __name__ == "__main__":
|
| 282 |
+
main()
|
data/unc_and_mai_combined.xlsx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:23ac1ecc2b81a68b8a3c492cabae81df56f5176355524338d01e171edf1a2072
|
| 3 |
+
size 11202969
|
data_preproc.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
|
| 3 |
+
'''
|
| 4 |
+
Load the data from excel and categorize into MAI/UNC_LM/UNC_ML/existing for
|
| 5 |
+
daily, daily_detrended and monthly series
|
| 6 |
+
'''
|
| 7 |
+
|
| 8 |
+
daily_df = pd.read_excel("data/unc_and_mai_combined.xlsx", sheet_name="daily")
|
| 9 |
+
daily_detrend_df = pd.read_excel("data/unc_and_mai_combined.xlsx", sheet_name="daily_detrended")
|
| 10 |
+
monthly_df = pd.read_excel("data/unc_and_mai_combined.xlsx", sheet_name="monthly")
|
| 11 |
+
|
| 12 |
+
mai_cols = ["WSJ_CREDIT_RATING_MAI", "NYT_CREDIT_RATING_MAI",
|
| 13 |
+
"WSJ_GDP_MAI", "NYT_GDP_MAI",
|
| 14 |
+
"WSJ_HSNG_MAI", "NYT_HSNG_MAI",
|
| 15 |
+
"WSJ_INFL_MAI", "NYT_INFL_MAI",
|
| 16 |
+
"WSJ_MONETARY_MAI", "NYT_MONETARY_MAI",
|
| 17 |
+
"WSJ_OIL_MAI", "NYT_OIL_MAI",
|
| 18 |
+
"WSJ_UNEMP_MAI", "NYT_UNEMP_MAI",
|
| 19 |
+
"WSJ_USD_MAI", "NYT_USD_MAI"]
|
| 20 |
+
|
| 21 |
+
unc_lm_cols = ["WSJ_NMPU_LM", "NYT_NMPU_LM",
|
| 22 |
+
"WSJ_INFU_LM", "NYT_INFU_LM",
|
| 23 |
+
# "WSJ_INFU_LM_weighted", "NYT_INFU_LM_weighted",
|
| 24 |
+
"WSJ_INFU_LM_screened", "NYT_INFU_LM_screened",
|
| 25 |
+
"WSJ_GSCI_LM", "NYT_GSCI_LM"]
|
| 26 |
+
|
| 27 |
+
unc_ml_cols = ["WSJ_NMPU_ML", "NYT_NMPU_ML",
|
| 28 |
+
"WSJ_INFU_ML", "NYT_INFU_ML",
|
| 29 |
+
# "WSJ_INFU_ML_weighted", "NYT_INFU_ML_weighted",
|
| 30 |
+
"WSJ_INFU_ML_screened", "NYT_INFU_ML_screened",
|
| 31 |
+
"WSJ_GSCI_ML", "NYT_GSCI_ML"]
|
| 32 |
+
|
| 33 |
+
other_cols = ["EPU", "VIX", "MPU_MKT", "MOVE"]
|
| 34 |
+
|
| 35 |
+
all_cols = mai_cols + unc_lm_cols + unc_ml_cols + other_cols
|
| 36 |
+
|
| 37 |
+
daily_df_all = daily_df[["date"] + all_cols].copy()
|
| 38 |
+
daily_detrend_df_all = daily_detrend_df[["date"] + all_cols].copy()
|
| 39 |
+
monthly_df_all = monthly_df[["date"] + all_cols].copy()
|
| 40 |
+
|
| 41 |
+
daily_df_all[all_cols] = (daily_df_all[all_cols] - daily_df_all[all_cols].min()) / (daily_df_all[all_cols].max() - daily_df_all[all_cols].min())
|
| 42 |
+
daily_detrend_df_all[all_cols] = (daily_detrend_df_all[all_cols] - daily_detrend_df_all[all_cols].min()) / (daily_detrend_df_all[all_cols].max() - daily_detrend_df_all[all_cols].min())
|
| 43 |
+
monthly_df_all[all_cols] = (monthly_df_all[all_cols] - monthly_df_all[all_cols].min()) / (monthly_df_all[all_cols].max() - monthly_df_all[all_cols].min())
|
| 44 |
+
|
| 45 |
+
################ MAI
|
| 46 |
+
daily_mai = daily_df[["date"] + mai_cols]
|
| 47 |
+
daily_detrend_mai = daily_detrend_df[["date"] + mai_cols]
|
| 48 |
+
monthly_mai = monthly_df[["date"] + mai_cols]
|
| 49 |
+
|
| 50 |
+
################ UNC_LM
|
| 51 |
+
daily_unc_lm = daily_df[["date"] + unc_lm_cols]
|
| 52 |
+
daily_detrend_unc_lm = daily_detrend_df[["date"] + unc_lm_cols]
|
| 53 |
+
monthly_unc_lm = monthly_df[["date"] + unc_lm_cols]
|
| 54 |
+
|
| 55 |
+
################ UNC_ML
|
| 56 |
+
daily_unc_ml = daily_df[["date"] + unc_ml_cols]
|
| 57 |
+
daily_detrend_unc_ml = daily_detrend_df[["date"] + unc_ml_cols]
|
| 58 |
+
monthly_unc_ml = monthly_df[["date"] + unc_ml_cols]
|
| 59 |
+
|
| 60 |
+
################ OTHER
|
| 61 |
+
daily_other = daily_df[["date"] + other_cols]
|
| 62 |
+
daily_detrend_other = daily_detrend_df[["date"] + other_cols]
|
| 63 |
+
monthly_other = monthly_df[["date"] + other_cols]
|
| 64 |
+
|
| 65 |
+
#source: NBER based Recession Indicators | https://fred.stlouisfed.org/series/USREC
|
| 66 |
+
NBER_recession_dates = [{'start': '2020-03-01','stop': '2020-04-01'},
|
| 67 |
+
{'start': '2008-01-01','stop': '2009-06-01'},
|
| 68 |
+
{'start': '2001-04-01','stop': '2001-11-01'},
|
| 69 |
+
{'start': '1990-08-01','stop': '1991-03-01'},
|
| 70 |
+
{'start': '1981-08-01','stop': '1981-11-01'},
|
| 71 |
+
{'start': '1980-02-01','stop': '1981-07-01'},
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
def smooth_df(df: pd.DataFrame, cols: list, smooth_window: int=12):
|
| 75 |
+
'''
|
| 76 |
+
Smooth all the timeseries in the plot for a better visual
|
| 77 |
+
|
| 78 |
+
Inputs:
|
| 79 |
+
df: dataframe to smooth
|
| 80 |
+
cols: columns to apply the smooth
|
| 81 |
+
smooth_window: suggest 12 for monthly series, and 252 for daily series
|
| 82 |
+
Returns:
|
| 83 |
+
a copy of df with cols smoothed using rolling smooth_window mean
|
| 84 |
+
'''
|
| 85 |
+
df_ = df.copy()
|
| 86 |
+
df_[cols] = df_[cols].rolling(smooth_window, min_periods=1).mean(numeric_only=True)
|
| 87 |
+
return df_
|
| 88 |
+
|
| 89 |
+
def calculate_correlation_df(df: pd.DataFrame, cols: list):
|
| 90 |
+
'''
|
| 91 |
+
Calculates the correlation between the provided columns in the dataframe
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
a pandas df of correlation
|
| 95 |
+
'''
|
| 96 |
+
df_ = df[cols].copy()
|
| 97 |
+
return df_.corr().round(3).astype(str).reset_index()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
pandas
|
| 3 |
+
openpyxl
|
| 4 |
+
matplotlib
|
| 5 |
+
plotly
|
| 6 |
+
gradio
|
| 7 |
+
nbformat
|