-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse_project_with_answers.py
More file actions
130 lines (96 loc) · 2.92 KB
/
course_project_with_answers.py
File metadata and controls
130 lines (96 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
"""Course-Project-with-answers.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1dLxE7pTTKF6jA-yl4wE3ftjnIN07Zmm_
# Summer 2022 Python for Econ Students Course Project
## Student Name
## نام دانشجو
## Read data
You have to read the data from an excel file.
The excel file can be found here:
https://dl.dropboxusercontent.com/s/xsywvv5p5p8k9kq/MB_Data.xlsx
variable:
MB: Monetary Base
MB_gr: Monetary Base Growth
Expansion: Dummy variabel that is 1 in expansion periods and 0 in recession periods
Recession: Dummy variabel that is 1 in recession periods and 0 in expansion periods
NonOilGDP: Real Non-oil GDP of Iran
NonOilGDPCycle: Deviations of NonOilGDP from it's trends
"""
import pandas as pd
import numpy as np
D = pd.read_excel("https://dl.dropboxusercontent.com/s/xsywvv5p5p8k9kq/MB_Data.xlsx")
"""## Preview data
Show a part of data
"""
D
"""## Slice data
Keep only rows that include data on Expansion and Recession
"""
D2 = D[D.Expansion.notna()]
D2
"""## Plot data
Plot time series data of monetary base growth.
Add additional background bars that show recession periods.
"""
import matplotlib.pyplot as plt
tm = (D2.Year + (D2.Quarter-1)/4).to_numpy()
rc = D2.Recession.to_numpy()
fig, ax = plt.subplots()
ax2 = ax.twinx()
ax.bar(tm,rc,color='lightgrey',width=.25)
ax.get_yaxis().set_visible(False)
ax2.plot(tm,D2.MB_gr)
fig.show()
"""plot the time series data for recession and expansion period in different colors"""
import matplotlib.pyplot as plt
tm = (D2.Year + (D2.Quarter-1)/4).to_numpy()
rc = D2.Recession.to_numpy()
fig, ax = plt.subplots()
ax2 = ax.twinx()
ax.bar(tm,rc,color='lightgrey',width=.25)
ax.get_yaxis().set_visible(False)
gx = D2.MB_gr.to_numpy().copy()
gr = D2.MB_gr.to_numpy().copy()
gx[rc==1]=np.nan
gr[rc==0]=np.nan
ax2.plot(tm,gx)
ax2.plot(tm,gr)
fig.show()
"""## Average Growth of monetary base
The given growth rate is quarterly growth rate
* Calculate and report the average quarterly growth rate
* Calculate and report the average annual growth rate _(note that it's not 4*quarterly rate!)_
"""
g=D2.MB_gr.to_numpy()
mean_q_g = np.mean(g)
mean_a_g = np.mean((g+1)**4-1)
mean_a_g_2 = mean_q_g * 4
print(mean_q_g)
print(mean_a_g)
print(mean_a_g_2)
"""## Calculate means in expansions and recessions
"""
ex = D2.Expansion.to_numpy()
mean_ex = np.mean(g[ex==1])
print(mean_ex)
mean_rc = np.mean(g[ex==0])
print(mean_rc)
"""## Test the equality of means of monetary base growth in recession and expansions """
import statsmodels.stats.weightstats as sms
import scipy.stats as scs
g_ex = g[ex==1]
g_rc = g[ex==0]
t1 = sms.ttest_ind(g_ex,g_rc)
t2 = scs.ttest_ind(g_ex,g_rc)
print(t1)
print(t2)
"""##Test the equality of variances of monetary base growth in recensiions and expansion """
F = g_rc.var()/g_ex.var()
print(F)
df1 = len(g_rc)-1
df2 = len(g_ex)-1
alpha = 0.05
p_value = scs.f.cdf(F,df1,df2)*2
print(p_value)