From 83b8218a604c5cea4731df53408704b39db28d14 Mon Sep 17 00:00:00 2001 From: John Stachurski Date: Tue, 30 May 2023 10:02:17 +1000 Subject: [PATCH 1/2] misc --- lectures/business_cycle.md | 131 +++++++++++++---------------------- lectures/eigen_I.md | 12 ++-- lectures/markov_chains_I.md | 38 +++++++--- lectures/markov_chains_II.md | 19 ++--- lectures/prob_dist.md | 2 +- 5 files changed, 95 insertions(+), 107 deletions(-) diff --git a/lectures/business_cycle.md b/lectures/business_cycle.md index 896f41ac3..964e3c446 100644 --- a/lectures/business_cycle.md +++ b/lectures/business_cycle.md @@ -11,22 +11,20 @@ kernelspec: name: python3 --- -+++ {"user_expressions": []} # Business Cycles ## Overview -In this lecture we study business cycles +In this lecture we review some empirical aspects of business cycles. Business cycles are fluctuations in economic activity over time. -These fluctuations are in the form of expansions (booms), contractions (recessions), and recoveries. +The include expansions (also called booms) and contractions (also called recessions). -We will look into a series of economic indicators to visualize the expansions and contractions of economies using [World Bank](https://documents.worldbank.org/en/publication/documents-reports/api) and [FRED](https://fred.stlouisfed.org/) data. +For our study, we will use economic indicators from the [World Bank](https://documents.worldbank.org/en/publication/documents-reports/api) and [FRED](https://fred.stlouisfed.org/). In addition to those installed by Anaconda, this lecture requires -libraries to obtain World Bank and FRED data: ```{code-cell} ipython3 :tags: [hide-output] @@ -47,6 +45,8 @@ import wbgapi as wb import pandas_datareader.data as web ``` +Here's some minor code to help with colors in our plots. + ```{code-cell} ipython3 :tags: [hide-input] @@ -56,14 +56,10 @@ cycler = plt.cycler(linestyle=['-', '-.', '--', ':'], plt.rc('axes', prop_cycle=cycler) ``` -+++ {"user_expressions": []} ## Data acquisition -We will use `wbgapi` and `pandas_datareader` to retrieve data throughout this -lecture. - -Let's explore how to query data first. +We will use `wbgapi` and `pandas_datareader` to retrieve data. We can use `wb.series.info` with the argument `q` to query available data from the [World Bank](https://www.worldbank.org/en/home). @@ -74,22 +70,18 @@ For example, let's retrieve the ID to query GDP growth data. wb.series.info(q='GDP growth') ``` -+++ {"user_expressions": []} Now we use this series ID to obtain the data. ```{code-cell} ipython3 -:tags: [hide-output] - gdp_growth = wb.data.DataFrame('NY.GDP.MKTP.KD.ZG', ['USA', 'ARG', 'GBR', 'GRC', 'JPN'], labels=True) gdp_growth ``` -+++ {"user_expressions": []} -We can the metadata to learn more about the series. +We can the metadata to learn more about the series (click to expand). ```{code-cell} ipython3 :tags: [hide-output] @@ -97,14 +89,12 @@ We can the metadata to learn more about the series. wb.series.metadata.get('NY.GDP.MKTP.KD.ZG') ``` -+++ {"user_expressions": []} -Let's dive into the data with the tools we have. (gdp_growth)= ## GDP growth rate -First we look at the GDP growth rate. +First we look at GDP growth. Let's source our data from the World Bank and clean it. @@ -123,7 +113,7 @@ Here's a first look at the data gdp_growth ``` -Now we can write a function to generate plots for individual countries taking into account the recessions. +We write a function to generate plots for individual countries taking into account the recessions. ```{code-cell} ipython3 :tags: [hide-input] @@ -199,9 +189,6 @@ t_params = {'color':'grey', 'fontsize': 9, 'va':'center', 'ha':'center'} ``` -+++ {"user_expressions": []} - -Now we can plot the data as a time series. Let's start with the United States. @@ -299,7 +286,7 @@ plot_series(gdp_growth, country, plt.show() ``` -Greece had a significant drop in GDP growth around 2010-2011, during the peak +Greece experienced a very large drop in GDP growth around 2010-2011, during the peak of the Greek debt crisis. Next let's consider Argentina. @@ -321,24 +308,18 @@ plot_series(gdp_growth, country, plt.show() ``` -+++ {"user_expressions": []} - -The figure shows that Argentina has experienced more volatile cycles than -the economies mentioned above. +Notice that Argentina has experienced far more volatile cycles than +the economies examined above. At the same time, growth of Argentina did not fall during the two developed economy recessions in the 1970s and 1990s. -+++ {"user_expressions": []} ## Unemployment Another important measure of business cycles is the unemployment rate. -During a recession, it is more likely that a larger proportion of the working -population will be laid off. - -We demonstrate this using a long-run unemployment rate from FRED spanning from [1929-1942](https://fred.stlouisfed.org/series/M0892AUSM156SNBR) to [1948-2022](https://fred.stlouisfed.org/series/UNRATE) with the unemployment rate between 1942 and 1948 estimated by the [Census Bureau](https://www.census.gov/library/publications/1975/compendia/hist_stats_colonial-1970.html). +We study unemployment using rate data from FRED spanning from [1929-1942](https://fred.stlouisfed.org/series/M0892AUSM156SNBR) to [1948-2022](https://fred.stlouisfed.org/series/UNRATE), combined unemployment rate data over 1942-1948 estimated by the [Census Bureau](https://www.census.gov/library/publications/1975/compendia/hist_stats_colonial-1970.html). ```{code-cell} ipython3 :tags: [hide-input] @@ -358,7 +339,8 @@ unrate = web.DataReader('UNRATE', 'fred', start_date, end_date) ``` -Now we plot the long-run unemployment rate in the US from 1929 to 2022 with recession defined by NBER +Let's plot the unemployment rate in the US from 1929 to 2022 with recessions +defined by the NBER. ```{code-cell} ipython3 --- @@ -411,24 +393,19 @@ ax.set_ylabel('unemployment rate (%)') plt.show() ``` -+++ {"user_expressions": []} - -In the plot, we can see that the expansions and contractions of the labor -market have been highly correlated with recessions. -However, there is often a delay in the recovery of the labor market after -recessions. +The plot shows that -This trend is clearly visible in the 1930s, as well as in recessions in the -1980s. +* expansions and contractions of the labor market have been highly correlated + with recessions. +* cycles are, in general, asymmetric: sharp rises in unemployment are followed + by slow recoveries. -It also shows us how unique labor market conditions have been during the +It also shows us how unique labor market conditions were in the US during the post-pandemic recovery. -The labor market has recovered at an unprecedented rate, leading to the -tightest point in the past decades after the shock in 2020-2021. +The labor market recovered at an unprecedented rate after the shock in 2020-2021. -+++ {"user_expressions": []} (synchronization)= ## Synchronization @@ -577,9 +554,8 @@ plot_comparison(gdp_growth.loc[countries, 1962:], plt.show() ``` -+++ {"user_expressions": []} -The comparison of GDP growth rates above shows that +The comparison of GDP growth rates above suggests that business cycles are becoming more synchronized in 21st-century recessions. However, emerging and less developed economies often experience more volatile @@ -618,15 +594,13 @@ plot_comparison(unempl_rate, countries, plt.show() ``` -France, with its strong labor unions, has a prolonged labor market recovery -compared to the US and UK. +We see that France, with its strong labor unions, typically experiences +relatively slow labor market recoveries after negative shocks. -However, Japan has a history of very low and stable unemployment rates due to -a constellation of social, demographic, and cultural factors. +We also notice that, Japan has a history of very low and stable unemployment rates. -+++ {"user_expressions": []} -## Leading indicators and correlated factors for business cycles +## Leading indicators and correlated factors Examining leading indicators and correlated factors helps policymakers to understand the causes and results of business cycles. @@ -634,9 +608,8 @@ understand the causes and results of business cycles. We will discuss potential leading indicators and correlated factors from three perspectives: consumption, production, and credit level. -### Consumption -+++ {"user_expressions": []} +### Consumption Consumption depends on consumers' confidence towards their income and the overall performance of the economy in the future. @@ -644,16 +617,10 @@ income and the overall performance of the economy in the future. One widely cited indicator for consumer confidence is the [consumer sentiment index](https://fred.stlouisfed.org/series/UMCSENT) published by the University of Michigan. -Consumer sentiment remains high during during expansion, but there are significant drops before recession hits. - -There is also a clear negative correlation between consumer sentiment and [core consumer price index](https://fred.stlouisfed.org/series/CPILFESL). - -This trend is more significant in the during [stagflation](https://en.wikipedia.org/wiki/Stagflation). - -When the price of consumer commodities rises, consumer confidence diminishes. - -We plot the University of Michigan Consumer Sentiment Index and -Year-over-year Consumer Price Index Change from 1978-2022 in the US to show this trend +Here we plot the University of Michigan Consumer Sentiment Index and +year-on-year +[core consumer price index](https://fred.stlouisfed.org/series/CPILFESL) +(CPI) change from 1978-2022 in the US. ```{code-cell} ipython3 --- @@ -715,23 +682,28 @@ ax_t.set_ylabel('CPI YoY change (%)') plt.show() ``` -+++ {"user_expressions": []} +We see that +* consumer sentiment often remains high during during expansion and +drops before a recession. +* there is a clear negative correlation between consumer sentiment and the CPI. -### Production +When the price of consumer commodities rises, consumer confidence diminishes. -Consumers' confidence often influences their consumption pattern. +This trend is more significant in the during [stagflation](https://en.wikipedia.org/wiki/Stagflation). + + + +### Production -This often manifests on the production side. -The real industrial output is highly correlated with -recessions in the economy. +Real industrial output is highly correlated with recessions in the economy. However, it is not a leading indicator, as the peak of contraction in production -delays compared to consumer confidence and inflation. +is delayed relative to consumer confidence and inflation. We plot the real industrial output change from the previous year -from 1919 to 2022 in the US to show this trend +from 1919 to 2022 in the US to show this trend. ```{code-cell} ipython3 --- @@ -767,20 +739,19 @@ plt.show() We observe the delayed contraction in the plot across recessions. -+++ {"user_expressions": []} ### Credit level Credit contractions often occur during recessions, as lenders become more cautious and borrowers become more hesitant to take on additional debt. -This can be due to several factors such as a decrease in overall economic -activity, rising unemployment, and gloomy expectations for the future. +This is due to factors such as a decrease in overall economic +activity and gloomy expectations for the future. One example is domestic credit to the private sector by banks in the UK. -The following graph shows the domestic credit to the private sector -as a percentage of GDP by banks from 1970 to 2022 in the UK +The following graph shows the domestic credit to the private sector as a +percentage of GDP by banks from 1970 to 2022 in the UK ```{code-cell} ipython3 --- @@ -806,11 +777,7 @@ ax = plot_series(private_credit, countries, plt.show() ``` -+++ {"user_expressions": []} Note that the credit rises during economic expansion and stagnates or even contracts after recessions. -```{code-cell} ipython3 - -``` diff --git a/lectures/eigen_I.md b/lectures/eigen_I.md index d53faa923..924f59a2f 100644 --- a/lectures/eigen_I.md +++ b/lectures/eigen_I.md @@ -25,8 +25,7 @@ kernelspec: ## Overview -Eigenvalues and eigenvectors are an advanced topic in linear and -matrix algebra. +Eigenvalues and eigenvectors are a relatively advanced topic in linear algebra. At the same time, these concepts are extremely useful for @@ -38,8 +37,8 @@ At the same time, these concepts are extremely useful for In this lecture we explain the basics of eigenvalues and eigenvectors. -We assume in this lecture that students are familiar with matrices and -understand the basics of matrix algebra. +We assume in this lecture that students are familiar with matrices + and understand {doc}`the basics of matrix algebra`. We will use the following imports: @@ -54,7 +53,6 @@ from matplotlib.patches import FancyArrowPatch from mpl_toolkits.mplot3d import proj3d ``` -+++ {"user_expressions": []} (matrices_as_transformation)= ## Matrices as transformations @@ -63,10 +61,10 @@ Let's start by discussing an important concept concerning matrices. ### Mapping vectors into vectors -One way to think about a given matrix is as a rectangular collection of +One way to think about a matrix is as a rectangular collection of numbers. -Another way to think about a matrix is as a **map** (i.e., as a function) that +Another way to think about a matrix is as a *map* (i.e., as a function) that transforms vectors into new vectors. To understand the second point of view, suppose we multiply an $n \times m$ diff --git a/lectures/markov_chains_I.md b/lectures/markov_chains_I.md index f396b23c7..f62e7d6b8 100644 --- a/lectures/markov_chains_I.md +++ b/lectures/markov_chains_I.md @@ -13,7 +13,7 @@ kernelspec: -# Markov Chains: Basic Concepts and Stationarity +# Markov Chains: Basic Concepts ```{index} single: Markov Chains: Basic Concepts and Stationarity @@ -255,15 +255,15 @@ We'll cover some of these applications below. (mc_eg3)= #### Example 3 -Imam and Temple {cite}`imampolitical` categorize political institutions into three types: democracy $\text{(D)}$, autocracy $\text{(A)}$, and an intermediate state called anocracy $\text{(N)}$. +Imam and Temple {cite}`imampolitical` categorize political institutions into +three types: democracy $\text{(D)}$, autocracy $\text{(A)}$, and an intermediate +state called anocracy $\text{(N)}$. Each institution can have two potential development regimes: collapse $\text{(C)}$ and growth $\text{(G)}$. This results in six possible states: $\text{DG, DC, NG, NC, AG}$ and $\text{AC}$. -The lower probability of transitioning from $\text{NC}$ to itself indicates that collapses in anocracies quickly evolve into changes in the political institution. +Imam and Temple {cite}`imampolitical` estimate the following transition +probabilities: -Democracies tend to have longer-lasting growth regimes compared to autocracies as indicated by the lower probability of transitioning from growth to growth in autocracies. - -We can also find a higher probability from collapse to growth in democratic regimes $$ P := @@ -287,7 +287,10 @@ P = [[0.86, 0.11, 0.03, 0.00, 0.00, 0.00], [0.00, 0.00, 0.09, 0.15, 0.26, 0.50]] ``` +Here is a visualization, with darker colors indicating higher probability. + ```{code-cell} ipython3 +:tags: [hide-input] G = nx.MultiDiGraph() edge_ls = [] label_dict = {} @@ -316,6 +319,13 @@ plt.colorbar(pc, ax=ax) plt.show() ``` +Looking at the data, we see that democracies tend to have longer-lasting growth +regimes compared to autocracies (as indicated by the lower probability of +transitioning from growth to growth in autocracies). + +We can also find a higher probability from collapse to growth in democratic regimes + + ### Defining Markov chains So far we've given examples of Markov chains but now let's define them more @@ -763,7 +773,7 @@ If $P$ is everywhere positive, then $P$ has exactly one stationary distribution. ``` -We will come back to this when we introduce irreducibility in the next lecture +We will come back to this when we introduce irreducibility in the {doc}`next lecture ` on Markov chains. @@ -967,7 +977,17 @@ The convergence to $\psi^*$ holds for different initial distributions. #### Example: Failure of convergence -In the case of our periodic chain, we find the distribution is oscillating +In the case of a periodic chain, with + +$$ +P = +\begin{bmatrix} + 0 & 1 \\ + 1 & 0 \\ +\end{bmatrix} +$$ + +we find the distribution oscillates ```{code-cell} ipython3 P = np.array([[0, 1], @@ -979,6 +999,8 @@ num_distributions = 30 plot_distribution(P, ts_length, num_distributions) ``` +Indeed, this $P$ fails our asymptotic stationarity condition, since, as you can +verify, $P^t$ is not everywhere positive for any $t$. (finite_mc_expec)= diff --git a/lectures/markov_chains_II.md b/lectures/markov_chains_II.md index d4938a2a4..979325322 100644 --- a/lectures/markov_chains_II.md +++ b/lectures/markov_chains_II.md @@ -37,21 +37,24 @@ to be installed on your computer. Installation instructions for graphviz can be [here](https://www.graphviz.org/download/) ``` -+++ {"user_expressions": []} ## Overview -This lecture continues our journey in Markov chains. +This lecture continues on from our {doc}`earlier lecture on Markov chains +`. + -Specifically, we will introduce irreducibility and ergodicity, and how they connect to stationarity. +Specifically, we will introduce the concepts of irreducibility and ergodicity, and see how they connect to stationarity. -Irreducibility is a concept that describes the ability of a Markov chain to move between any two states in the system. +Irreducibility describes the ability of a Markov chain to move between any two states in the system. Ergodicity is a sample path property that describes the behavior of the system over long periods of time. -The concepts of irreducibility and ergodicity are closely related to the idea of stationarity. +As we will see, -An irreducible Markov chain guarantees the existence of a unique stationary distribution, while an ergodic Markov chain ensures that the system eventually reaches its stationary distribution, regardless of its initial state. +* an irreducible Markov chain guarantees the existence of a unique stationary distribution, while +* an ergodic Markov chain generates time series that satisfy a version of the + law of large numbers. Together, these concepts provide a foundation for understanding the long-term behavior of Markov chains. @@ -71,9 +74,7 @@ import matplotlib as mpl ## Irreducibility -Irreducibility is a central concept of Markov chain theory. - -To explain it, let's take $P$ to be a fixed stochastic matrix. +To explain irreducibility, let's take $P$ to be a fixed stochastic matrix. Two states $x$ and $y$ are said to **communicate** with each other if there exist positive integers $j$ and $k$ such that diff --git a/lectures/prob_dist.md b/lectures/prob_dist.md index 034f5f27b..78147597c 100644 --- a/lectures/prob_dist.md +++ b/lectures/prob_dist.md @@ -11,7 +11,6 @@ kernelspec: name: python3 --- -+++ {"user_expressions": []} # Distributions and Probabilities @@ -27,6 +26,7 @@ kernelspec: In this lecture we give a quick introduction to data and probability distributions using Python ```{code-cell} ipython3 +:tags: [hide-output] !pip install --upgrade yfinance ``` From 997c3fb575fd8b89e11220455f16b48ff0fee3c0 Mon Sep 17 00:00:00 2001 From: John Stachurski Date: Tue, 30 May 2023 11:08:30 +1000 Subject: [PATCH 2/2] misc --- lectures/markov_chains_II.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lectures/markov_chains_II.md b/lectures/markov_chains_II.md index 979325322..337d1e467 100644 --- a/lectures/markov_chains_II.md +++ b/lectures/markov_chains_II.md @@ -276,11 +276,13 @@ This is one aspect of the concept of ergodicity. (ergo)= ### Example 2 -Another example is Hamilton {cite}`Hamilton2005` dynamics {ref}`discussed before `. +Another example is the Hamilton dynamics we {ref}`discussed before `. -The diagram of the Markov chain shows that it is **irreducible**. +The {ref}`graph ` of the Markov chain shows it is irreducible -Therefore, we can see the sample path averages for each state (the fraction of time spent in each state) converges to the stationary distribution regardless of the starting state +Therefore, we can see the sample path averages for each state (the fraction of +time spent in each state) converges to the stationary distribution regardless of +the starting state ```{code-cell} ipython3 P = np.array([[0.971, 0.029, 0.000], @@ -328,9 +330,11 @@ P := $$ -The graph for the chain shows states are densely connected indicating that it is **irreducible**. +The {ref}`graph ` for the chain shows all states are reachable, +indicating that this chain is irreducible. -Similar to previous examples, the sample path averages for each state converge to the stationary distribution +Similar to previous examples, the sample path averages for each state converge +to the stationary distribution. ```{code-cell} ipython3 P = [[0.86, 0.11, 0.03, 0.00, 0.00, 0.00], @@ -391,9 +395,8 @@ dot.edge("1", "0", label="1.0", color='red') dot ``` -+++ {"user_expressions": []} -Unlike other Markov chains we have seen before, it has a periodic cycle --- the state cycles between the two states in a regular way. +In fact it has a periodic cycle --- the state cycles between the two states in a regular way. This is called [periodicity](https://www.randomservices.org/random/markov/Periodicity.html).