Kerry Back
R = 30*12 # 30 years until retirement
T = 60*12 # 60 total years
B0 = 100000 # initial balance is $100,000
D1 = 1000 # initial savings is $1,000 (per month)
W1 = 10000 # withdraw $10,000 first month in retirement
g = 0.002 # deposit is 0.2% larger each month
h = 0 # withdrawals are constant
D = D1 * (1+g)**np.arange(R)
D = dict(zip(range(1, R+1), D))
W = W1 * (1+h)**np.arange(T-R)
W = dict(zip(range(R, T), W))
Only change is r[t].
r = np.random.normal(loc=mn, scale=sd, size=T)
D = D1 * (1+g)**np.arange(R)
W = W1 * (1+h)**np.arange(T-R)
fvFactors = np.flip(np.cumprod(np.flip(1+r)))
fvFactors = np.concatenate((fvFactors, [1]))
B0 = np.concatenate(([B0], np.zeros(T)))
D = np.concatenate(([0], D, np.zeros(T-R)))
W = np.concatenate((np.zeros(R), W, [0]))
CF = B0 + D - W
BT = np.sum(CF*fvFactors)