Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdebacker committed Jul 6, 2024
1 parent 28d4afd commit ee8916e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
30 changes: 17 additions & 13 deletions ogcore/pensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,14 @@ def deriv_NDC(r, w, e, Y, per_rmn, p):
d_theta = np.zeros(per_rmn)
else:
d_theta_empty = np.zeros(per_rmn)
delta_ret_amount = delta_ret(r, Y)
g_ndc_amount = g_ndc(r, Y, p.g_n_SS, p.g_y)
delta_ret_amount = delta_ret(r, Y, p)
g_ndc_amount = g_ndc(r, Y, p)
d_theta = deriv_NDC_loop(
w,
e,
per_rmn,
p.S,
p.retire,
p.g_y,
p.tau_p,
g_ndc_amount,
delta_ret_amount,
Expand Down Expand Up @@ -501,7 +500,7 @@ def delta_point(r, Y, g_n, g_y, p):
return delta_point


def g_ndc(r, Y, g_n, g_y, p):
def g_ndc(r, Y, p):
"""
Compute growth rate used for contributions to NDC pension
"""
Expand All @@ -510,14 +509,14 @@ def g_ndc(r, Y, g_n, g_y, p):
elif p.ndc_growth_rate == "Curr GDP":
g_ndc = (Y[1:] - Y[:-1]) / Y[:-1]
elif p.ndc_growth_rate == "LR GDP":
g_ndc = g_y + g_n
g_ndc = p.g_y + p.g_n
else:
g_ndc = g_y + g_n
g_ndc = p.g_y + p.g_n

return g_ndc


def g_dir(r, Y, g_n, g_y, p):
def g_dir(r, Y, p):
"""
Compute growth rate used for contributions to NDC pension
"""
Expand All @@ -526,20 +525,25 @@ def g_dir(r, Y, g_n, g_y, p):
elif p.dir_growth_rate == "Curr GDP":
g_dir = (Y[1:] - Y[:-1]) / Y[:-1]
elif p.dir_growth_rate == "LR GDP":
g_dir = g_y + g_n
g_dir = p.g_y + p.g_n
else:
g_dir = g_y + g_n
g_dir = p.g_y + p.g_n

return g_dir


def delta_ret(self, r, Y, p):
def delta_ret(r, Y, p):
"""
Compute conversion coefficient for the NDC pension amount
"""
surv_rates = 1 - p.mort_rates_SS
dir_delta_s_empty = np.zeros(p.S - p.retire + 1)
g_dir_value = g_dir(r, Y, p.g_n_SS, p.g_y)
g_dir_value = g_dir(r, Y, p)
print("G dir value type = ", type(p.S))
print("G dir value type = ", type(p.retire))
print("G dir value type = ", type(surv_rates))
print("G dir value type = ", type(g_dir_value))
print("G dir value type = ", type(dir_delta_s_empty))
dir_delta = delta_ret_loop(
p.S, p.retire, surv_rates, g_dir_value, dir_delta_s_empty
)
Expand Down Expand Up @@ -588,15 +592,15 @@ def deriv_NDC_loop(w, e, per_rmn, S, S_ret, tau_p, g_ndc, delta_ret, d_theta):


@numba.jit
def delta_ret_loop(S, S_ret, surv_rates, g_dir, dir_delta_s):
def delta_ret_loop(S, S_ret, surv_rates, g_dir_value, dir_delta_s):

cumul_surv_rates = np.ones(S - S_ret + 1)
for s in range(S - S_ret + 1):
surv_rates_vec = surv_rates[S_ret : S_ret + s + 1]
surv_rates_vec[0] = 1.0
cumul_surv_rates[s] = np.prod(surv_rates_vec)
cumul_g_y = np.ones(S - S_ret + 1)
cumul_g_y[s] = (1 / (1 + g_dir)) ** s
cumul_g_y[s] = (1 / (1 + g_dir_value)) ** s
dir_delta_s[s] = cumul_surv_rates[s] * cumul_g_y[s]
dir_delta = dir_delta_s.sum()
return dir_delta
Expand Down
52 changes: 52 additions & 0 deletions tests/test_pensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,55 @@ def test_deriv_S(args, deriv_PS_expected):
deriv_PS = pensions.deriv_PS(w, e, per_rmn, factor, p)

assert np.allclose(deriv_PS, deriv_PS_expected)


#############complete lifetimes, S = 4###################
p = Specifications()
p.S = 4
p.retire = 2
per_rmn = p.S
p.g_y = 0.03
p.g_n_SS = 0.0
p.ndc_growth_rate = "LR GDP"
p.dir_growth_rate = "r"
p.tau_p = 0.3
p.k_ret = 0.4615
p.mort_rates_SS = np.array([0.01, 0.05, 0.3, 1])
w = np.array([1.2, 1.1, 1.21, 1])
e = np.array([1.1, 1.11, 0.9, 0.87])
r = 0.02
d_NDC_expected1 = np.array([0.757437326, 0.680222841, 0.0, 0.0])
args1 = (r, w, e, None, per_rmn, p)

#############Incomplete lifetimes###################
p2 = Specifications()
p2.S = 4
p2.retire = 2
per_rmn2 = 3
p2.g_y = 0.04
p2.g_n_SS = 0.0
p2.ndc_growth_rate = "LR GDP"
p2.dir_growth_rate = "LR GDP"
p2.tau_p = 0.3
p2.k_ret = 0.4615
p2.mort_rates_SS = np.array([0.1, 0.2, 0.4, 0.6, 1.0])
w2 = np.array([1.1, 1.21, 1.25])
e2 = np.array([1.11, 0.9, 1.0])
r2 = 0.04
d_NDC_expected2 = np.array([0.396808466, 0.0, 0.0])
args2 = (r2, w2, e2, None, per_rmn2, p2)

test_data = [(args1, d_NDC_expected1), (args2, d_NDC_expected2)]


@pytest.mark.parametrize(
"args,d_NDC_expected", test_data, ids=["SS/Complete", "Incomplete"]
)
def test_deriv_NDC(args, d_NDC_expected):
"""
Test of the pensions.deriv_NDC() function.
"""
r, w, e, Y, per_rmn, p = args
d_NDC = pensions.deriv_NDC(r, w, e, Y, per_rmn, p)

assert np.allclose(d_NDC, d_NDC_expected)

0 comments on commit ee8916e

Please sign in to comment.