-
Notifications
You must be signed in to change notification settings - Fork 1
/
forecast_tool.py
167 lines (146 loc) · 5.1 KB
/
forecast_tool.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
# Initial conditions
y_0 = 0.02
y_star_0 = 0.02
r_0 = 0.02
r_star_0 = 0.02
pi_0 = 0.02
tilde_r_star_0 = 0.02
pi_star = 0.02
M_0 = 1.0 # Initial value of the financial market
B_0 = 1.0 # Initial quantity of bank credit
# Parameters
alpha = 4.5 # Increased responsiveness to inflation overshooting
beta = 0.5
lambda_ = 0.7 # Increased learning rate for faster convergence
gamma = 1.0
kappa = 0.3 # Increased sensitivity of inflation to the output gap
delta = 0.1 # Sensitivity of the financial market to interest rate differences
S_0 = 1.0 # Base level of savings
I_0 = 1.0 # Base level of investment
epsilon = 0.2 # Sensitivity of savings to interest rate
theta = 0.4 # Sensitivity of investment to interest rate
mu = 0.3 # Expansion parameter of bank credit
nu = 0.1 # Contraction parameter of bank credit
rho = 0.3 # Speed of return to normal bank credit
epsilon_r = 0.01 # Threshold for close interest rates
# Simulation parameters
years = 40
y_star_path = [0.02] * 1 + [0.025] * (years - 1) # y* changes to 2.5% at year 1
# Initialize arrays to store results
y = np.zeros(years)
y_star = np.zeros(years)
r = np.zeros(years)
r_star = np.zeros(years)
pi = np.zeros(years)
tilde_r_star = np.zeros(years)
M = np.zeros(years)
S = np.zeros(years) # Savings
I = np.zeros(years) # Investment
B = np.zeros(years) # Bank credit
# Set initial values
y[0] = y_0
y_star[0] = y_star_0
r[0] = r_0
r_star[0] = r_star_0
pi[0] = pi_0
tilde_r_star[0] = tilde_r_star_0
M[0] = M_0
S[0] = S_0
I[0] = I_0
B[0] = B_0
# Run simulation
for t in range(1, years):
y_star[t] = y_star_path[t]
r_star[t] = gamma * y_star[t]
tilde_r_star[t] = tilde_r_star[t-1] + lambda_ * (r_star[t-1] - tilde_r_star[t-1])
r[t] = tilde_r_star[t] + alpha * (pi[t-1] - pi_star)
# Update savings and investment based on the interest rate differential
S[t] = S_0 * (1 + epsilon * (r[t]-r_star[t] )) # Decrease in savings when r < r_star
I[t] = I_0 * (1 - theta * (r[t]-r_star[t])) # Increase in investment when r < r_star
# Update bank credit based on the interest rate differential and return to normal level when rates are close
if r[t] < r_star[t]:
B[t] = B[t-1] + mu * (r_star[t] - r[t])
else:
B[t] = B[t-1] - nu * (r[t] - r_star[t])
if abs(r[t] - r_star[t]) < epsilon_r:
B[t] -= rho * (B[t-1] - B_0)
# Adjust y for savings, investment, and bank credit
y[t] = y_star[t] - beta * (r[t] - r_star[t])
pi[t] = pi[t-1] + kappa * (y[t] - y_star[t])
M[t] = I[t-1] + delta * (r_star[t] - r[t])
# Convert to DataFrame and multiply values by 100 to display percentages
df = pd.DataFrame({
'Year': np.arange(years),
'y': y * 100,
'y_star': y_star * 100,
'r': r * 100,
'r_star': r_star * 100,
'tilde_r_star': tilde_r_star * 100,
'pi': pi * 100,
'M': M,
'S': S,
'I': I,
'B': B
})
# Plot the results up to year 10
plot_years = 10
df_short = df[df['Year'] < plot_years]
mpl.rcParams['font.family'] = 'cmr10'
mpl.rcParams['axes.formatter.use_mathtext'] = True
plt.figure(figsize=(12, 10))
plt.suptitle('Macroeconomics simulation with Banking Sector') # Aggiungi un titolo generale per tutti i subplot
plt.subplot(3, 2, 1)
plt.plot(df_short['Year'], df_short['y'], label='y (Aggregate Demand)')
plt.plot(df_short['Year'], df_short['y_star'], label='y* (Potential Output)', linestyle='--')
plt.title('Aggregate Demand and Potential Output')
plt.xlabel('Year')
plt.ylabel('Growth Rate (%)')
plt.legend()
plt.subplot(3, 2, 2)
plt.plot(df_short['Year'], df_short['r'], label='r (Interest Rate)')
plt.plot(df_short['Year'], df_short['r_star'], label='r* (Natural Rate)', linestyle='--')
plt.plot(df_short['Year'], df_short['tilde_r_star'], label='~r* (Perceived Natural Rate)', linestyle='-.')
plt.title('Interest Rates')
plt.xlabel('Year')
plt.ylabel('Rate (%)')
plt.legend()
plt.subplot(3, 2, 3)
plt.plot(df_short['Year'], df_short['pi'], label='π (Inflation)')
plt.axhline(pi_star * 100, color='r', linestyle='--', label='π* (Inflation Target)')
plt.title('Inflation')
plt.xlabel('Year')
plt.ylabel('Rate (%)')
plt.legend()
plt.subplot(3, 2, 4)
plt.plot(df_short['Year'], df_short['M'], label='M (Financial Market)')
plt.title('Financial Market')
plt.xlabel('Year')
plt.ylabel('Market Value')
plt.legend()
plt.subplot(3, 2, 5)
plt.plot(df_short['Year'], df_short['S'], label='S (Savings)')
plt.axhline(S_0, color='r', linestyle='--', label='Normal level of Savings')
plt.title('Savings of Families')
plt.xlabel('Year')
plt.ylabel('Savings Level')
plt.legend()
plt.subplot(3, 2, 6)
plt.plot(df_short['Year'], df_short['I'], label='I (Investment)')
plt.axhline(I_0, color='r', linestyle='--', label='Normal level of investment')
plt.title('Investment by Firms')
plt.xlabel('Year')
plt.ylabel('Investment Level')
plt.legend()
plt.subplot(3, 2, 6)
plt.plot(df_short['Year'], df_short['B'], label='B (Bank Credit)')
plt.axhline(B_0, color='r', linestyle='--', label='Initial level of Bank Credit')
plt.title('Bank Credit')
plt.xlabel('Year')
plt.ylabel('Credit Level')
plt.legend()
plt.tight_layout()
plt.show()