<jupyter_start><jupyter_text>Discussion: Week 5  Exercise: More Business Cycle DataThe file `business_cycle_data_actual_trend.csv`, available at https://github.com/letsgoexploring/econ126/raw/master/Data/Csv/business_cycle_data_actual_trend.csv, contains actual and trend data for real GDP per capita, real consumption per capita, real investment per capita, real physical capital per capita, TFP,  hours per capita, real M2 per capita, the rate of PCE (personal consumption expenditures) inflation, the three month T-bill rate, and the unemployment rate at quarterly frequency. The GDP, consumption, investment, capital, and M2 data are in terms of 2012 dollars. Hours is measured as an index with the value in October 2012 set to 100. PCE inflation is measured at the percent change from one year prior. PCE inflation, unemployment, and the T-bill rate are expressed in units of percent.In this Notebook, you will replicate the work from class 9 but for the following quantities:* Real GDP per capita* 3-month T-Bill rate* PCE inflation rate* Unemployment rateRefer to the Notebook from Class 9 for reference: https://github.com/letsgoexploring/econ126/blob/master/Lecture%20Notebooks/Econ126_Class_09.ipynb  Part (a): Preliminaries<jupyter_code># Read business_cycle_data_actual_trend.csv into a Pandas DataFrame with the first column set as the index and parse_dates=True
df = pd.read_csv('https://github.com/letsgoexploring/econ126/raw/master/Data/Csv/business_cycle_data_actual_trend.csv',index_col=0,parse_dates=True)

# Print the last five rows of the data
df.tail()
# CELL PROVIDED 
# Construct a 2x2 grid of plots of GDP, consumption, t_bill_3mo, unemployment, and pce_inflation. Multiply
# t_bill_3mo, unemployment, and pce_inflation by 100 inside the plot command to plot in units of percent.
base_year = '2012'
fig = plt.figure(figsize=(12,8))

ax1 = fig.add_subplot(2,2,1)
ax1.plot(df.gdp,'-',lw=3,alpha = 0.7)
ax1.grid()
ax1.set_title('GDP per capita')
ax1.set_ylabel('Thousands of '+base_year+' dollars')

ax2 = fig.add_subplot(2,2,2)
ax2.plot(df.t_bill_3mo*100,'-',lw=3,alpha = 0.7)
ax2.grid()
ax2.set_title('3-month T-Bill mate')
ax2.set_ylabel('Thousands of '+base_year+' dollars')

ax3 = fig.add_subplot(2,2,3)
ax3.plot(df.unemployment*100,'-',lw=3,alpha = 0.7)
ax3.grid()
ax3.set_title('Unemployment rate')
ax3.set_ylabel('Thousands of '+base_year+' dollars')

ax4 = fig.add_subplot(2,2,4)
ax4.plot(df.pce_inflation*100,'-',lw=3,alpha = 0.7)
ax4.grid()
ax4.set_title('PCE inflation')
ax4.set_ylabel('Percent')

fig.tight_layout()<jupyter_output><empty_output><jupyter_text>Part (b): Cycles and TrendsRecalle: A time series $X_t$ can be decomposed into a trend component $X_t^{trend}$ and a cyclical component $X_t^{cycle}$ such that:\begin{align}X_t & = X_t^{trend} + X_t^{cycle}. \tag{1}\end{align}In equation (1), $X_t^{trend}$ is the long-run value about which $X_t$ fluctuates. $X^{cycle}_t$ is the amount by which $X_t$ excedes its trend.Often times, it's useful to express the cyclical component of a time series as the difference between the (natural) log of the series and the log of the trend:\begin{align}\hat{x}_t & = \log\left(X_t\right) - \log\left(X_t^{trend}\right) \approx\frac{X_t-X_t^{trend}}{X_t^{trend}}\end{align} The log-deviation from trend is approximately equal to the percent deviation of the series from trend (divided by 100).  GDP<jupyter_code># CELL PROVIDED
# Construct a plot of real GDP with its trend with:
#    1. Actual line: blue with lw=1, alpha=0.7, label = 'actual'
#    2. Trend line:  red with lw=3, alpha=0.7, label = 'trend'
#    3. A legend in an appropriate place
plt.plot(df.gdp,'-',lw=1,alpha = 0.7,label='actual')
plt.plot(df.gdp_trend,'r-',lw=3,alpha = 0.7,label='trend')
plt.grid()
plt.ylabel('Thousands of '+base_year+' dollars per person')
plt.title('GDP per capita')
plt.legend(loc='lower right',ncol=2)
# CELL PROVIDED
# Create a new column called gdp_cycle equal to the log difference between actual GDP and trend GDP:
df['gdp_cycle'] = np.log(df['gdp']) - np.log(df['gdp_trend'])

# Plot the log deviation of GDP from its trend (times 100)
plt.plot(df.gdp_cycle*100,'b-',lw=3,alpha = 0.7)
plt.ylabel('Percent')
plt.title('GDP: Percent deviation from trend')
plt.grid()<jupyter_output><empty_output><jupyter_text>3-month T-bill rate<jupyter_code># Construct a plot of the 3-month T-bill rate (times 100) with its trend with:
#    1. Actual line: blue with lw=1, alpha=0.7, label = 'actual'
#    2. Trend line:  red with lw=3, alpha=0.7, label = 'trend'
#    3. A legend in an appropriate place
plt.plot(df.t_bill_3mo*100,'-',lw=1,alpha = 0.7,label='actual')
plt.plot(df.t_bill_3mo_trend*100,'r-',lw=3,alpha = 0.7,label='trend')
plt.ylabel('Percent')
plt.title('3-month T-bill rate')
plt.legend(loc='upper right',ncol=2)
plt.grid()
# Create a new column called t_bill_3mo_cycle equal to the DIFFERENCE (not log difference!) between actual 
# 3-month T-bill rate and it's trend:
df['t_bill_3mo_cycle'] = df['t_bill_3mo'] - df['t_bill_3mo_trend']

# Plot the deviation of the 3-month T-bill rate from its trend (times 100)
plt.plot(df.t_bill_3mo_cycle*100,'b-',lw=3,alpha = 0.7,label='t-bill')
plt.plot(df.gdp_cycle*100,'r-',lw=1,alpha = 0.5,label='gdp')
plt.ylabel('Percent')
plt.title('3-month T-bill rate: Deviation from trend')
plt.legend(loc='lower right',ncol=2)
plt.grid()<jupyter_output><empty_output><jupyter_text>Unemployment rate<jupyter_code># Construct a plot of the unemployment rate (times 100) with its trend with:
#    1. Actual line: blue with lw=1, alpha=0.7, label = 'actual'
#    2. Trend line:  red with lw=3, alpha=0.7, label = 'trend'
#    3. A legend in an appropriate place
plt.plot(df.unemployment*100,'-',lw=1,alpha = 0.7,label='actual')
plt.plot(df.unemployment_trend*100,'r-',lw=3,alpha = 0.7,label='trend')
plt.ylabel('Percent')
plt.title('Unemployment rate')
plt.legend(loc='upper right',ncol=2)
plt.grid()
# Create a new column called unemployment_cycle equal to the DIFFERENCE (not log difference!) between actual 
# unemployment and it's trend:
df['unemployment_cycle'] = df['unemployment'] - df['unemployment_trend']

# Plot the deviation of the unemployment rate from its trend (times 100)
plt.plot(df.unemployment_cycle*100,'b-',lw=3,alpha = 0.7,label='unemployment')
plt.plot(df.gdp_cycle*100,'r-',lw=1,alpha = 0.5,label='gdp')
plt.ylabel('Percent')
plt.title('Unemployment rate: Deviation from trend')
plt.legend(loc='lower right',ncol=2)
plt.grid()<jupyter_output><empty_output><jupyter_text>PCE Inflation<jupyter_code># Construct a plot of the pce_inflation rate (times 100) with its trend with:
#    1. Actual line: blue with lw=1, alpha=0.7, label = 'actual'
#    2. Trend line:  red with lw=3, alpha=0.7, label = 'trend'
#    3. A legend in an appropriate place
plt.plot(df.pce_inflation*100,'-',lw=1,alpha = 0.7,label='actual')
plt.plot(df.pce_inflation_trend*100,'r-',lw=3,alpha = 0.7,label='trend')
plt.ylabel('Percent')
plt.title('PCE inflation rate')
plt.legend(loc='upper right',ncol=2)
plt.grid()
# Create a new column called pce_inflation_cycle equal to the DIFFERENCE (not log difference!) between actual 
# pce_inflation and it's trend:
df['pce_inflation_cycle'] = df['pce_inflation'] - df['pce_inflation_trend']

# Plot the deviation of the PCE inflation rate from its trend (times 100)
plt.plot(df.pce_inflation_cycle*100,'b-',lw=3,alpha = 0.7,label='inflation')
plt.plot(df.gdp_cycle*100,'r-',lw=1,alpha = 0.5,label='gdp')
plt.ylabel('Percent')
plt.title('PCE inflation rate: Deviation from trend')
plt.legend(loc='lower left',ncol=2)
plt.grid()<jupyter_output><empty_output><jupyter_text>Part (c): Statistics<jupyter_code># Create a new variable called df_cycle that is a DataFrame with columns columns gdp_cycle, t_bill_3mo_cycle, 
# pce_inflation_cycle, unemployment_cycle from df.
df_cycle = df[['gdp_cycle', 't_bill_3mo_cycle', 'pce_inflation_cycle', 'unemployment_cycle']]

# Print the first five rows of df_cycle
print(df_cycle.head())
# Use the DataFrame method .mean() to find the average values of the gdp_cycle, t_bill_3mo_cycle, 
# pce_inflation_cycle, unemployment_cycle columns
df_cycle.mean()
# Use the DataFrame method .std() to find the standard deviations of the gdp_cycle, t_bill_3mo_cycle, 
# pce_inflation_cycle, unemployment_cycle columns
df_cycle.std()
# Use the DataFrame method .corr() to find the coeficients of correlation among the gdp_cycle, t_bill_3mo_cycle, 
# pce_inflation_cycle, unemployment_cycle columns
df_cycle.corr()<jupyter_output><empty_output><jupyter_text>**Questions**1. Of the three quantities besides GDP in the columns of `df_cycle`, which varies the most over the business cycle?2. Of the three quantities besides GDP in the columns of `df_cycle`, which varies the least over the business cycle?3. Of the three quantities besides GDP in the columns of `df_cycle`, which is most correlated (in absolute value) with GDP over the business cycle? **Answers**1. The PCE inflation rate fluctuates the most over the business cycle.2. The unemployment rate fluctuates the least over the business cycle.3. The unemployment rate is the quantity that is most correlated with GDP over the cycle. Since the capital stock changes slowly over time, large fluctuations due primarily to large fluctuations in employment.  Part (d): BonusDo the next cell for fun. You won't get extra credit, but do it if you want to and have time.<jupyter_code># Plot the cyclical components of GDP, three month T-bill rate, PCE inflation rate, and the unemployment rate
# (all times 100) on the same set of axes
fig = plt.figure(figsize=(12,4))
ax = fig.add_subplot(1,1,1)
(df_cycle*100).plot(ax = ax,legend=False,lw=3,alpha=0.7)
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_ylabel('Percent')
ax.set_title('Select US Business cycle data')
ax.grid()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12661314907021645, 'fraction_numerical': 0.030281475459810994, 'mean_word_length': 5.811072664359862, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '211064', 'license': 'MIT', 'n_tokens_mistral': 3704, 'n_tokens_neox': 3429, 'n_words': 1026, 'path': 'Discussion Notebooks/Econ126_Discussion_Week_05.ipynb', 'repo_name': 'letsgoexploring/econ126', 'size': '9841'}