-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_crra.py
68 lines (52 loc) · 2.39 KB
/
test_crra.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
# test_crra.py
# pip install pytest
# pytest -q test_crra.py
import pytest
import numpy as np
from SWRsimulation.crra_ce import crra_ce, crra_ce_deathrate
def test_zero():
testseries = np.random.uniform(1, 100, 100)
testseries[50] = 0.0
assert crra_ce(testseries, 0.0) == pytest.approx(np.sum(testseries), 0.0000001), \
"if gamma = 0 should return np.sum()"
for gamma in [1, 2, 4, 8, 16]:
ce = crra_ce(testseries, gamma)
assert ce == 0, "zero cash flow should always return 0 %d %f" % (gamma, ce)
def test_negative():
testseries = np.random.uniform(1, 100, 100)
testseries[50] = -1.0
for gamma in [0, 0.5, 1, 2, 4, 8, 16]:
assert crra_ce(testseries, gamma) == 0, "negative cash flow should always return 0"
def test_constant_stream():
# a constant series should always be sum of cash flows for any gamma
testseries = np.ones(100)
for gamma in [0, 0.5, 1, 2, 4, 8, 16]:
print(gamma, np.sum(testseries), crra_ce(testseries, gamma), )
assert crra_ce(testseries, gamma) == pytest.approx(np.sum(testseries), 0.0000001), \
"constant cash flow should return sum of cash flows for any gamma"
def general_ce(cashflows, gamma):
cashflows = np.longdouble(cashflows)
calibration_factor = np.sum(cashflows) * 10
cashflows /= calibration_factor
if gamma == 1:
u = np.mean(np.log(cashflows))
ce = np.exp(u)
else:
u = np.mean((cashflows ** (1 - gamma) - 1) / (1 - gamma))
ce = (1 + u * (1 - gamma)) ** (1 / (1 - gamma))
ce = np.float(ce)
return ce * len(cashflows) * calibration_factor
def test_equals_general():
testseries = np.random.uniform(1, 100, 100)
for gamma in [0, 0.5, 1, 2, 4, 8, 16]:
print(gamma, crra_ce(testseries, gamma), general_ce(testseries, gamma))
assert crra_ce(testseries, gamma) == pytest.approx(general_ce(testseries, gamma), 0.000001)
def test_deathrate():
# if everyone dies in last year, mortality-adjusted CE = unadjusted CE
testseries = np.random.uniform(1, 100, 100)
deathrate = np.zeros(100)
deathrate[99] = 1.0
# can't go too high without some numerical problems
for gamma in [0, 0.5, 1, 2, 4, 8, 16]:
print(gamma, crra_ce(testseries, gamma), crra_ce_deathrate(testseries, gamma, deathrate))
assert crra_ce(testseries, gamma) == pytest.approx(general_ce(testseries, gamma), 0.000001)